diff --git a/src/string/reverse.rs b/src/string/reverse.rs index a8e72200787..bf17745a147 100644 --- a/src/string/reverse.rs +++ b/src/string/reverse.rs @@ -1,3 +1,12 @@ +/// Reverses the given string. +/// +/// # Arguments +/// +/// * `text` - A string slice that holds the string to be reversed. +/// +/// # Returns +/// +/// * A new `String` that is the reverse of the input string. pub fn reverse(text: &str) -> String { text.chars().rev().collect() } @@ -6,18 +15,26 @@ pub fn reverse(text: &str) -> String { mod tests { use super::*; - #[test] - fn test_simple() { - assert_eq!(reverse("racecar"), "racecar"); + macro_rules! test_cases { + ($($name:ident: $test_case:expr,)*) => { + $( + #[test] + fn $name() { + let (input, expected) = $test_case; + assert_eq!(reverse(input), expected); + } + )* + }; } - #[test] - fn test_assymetric() { - assert_eq!(reverse("abcdef"), "fedcba") - } - - #[test] - fn test_sentence() { - assert_eq!(reverse("step on no pets"), "step on no pets"); + test_cases! { + test_simple_palindrome: ("racecar", "racecar"), + test_non_palindrome: ("abcdef", "fedcba"), + test_sentence_with_spaces: ("step on no pets", "step on no pets"), + test_empty_string: ("", ""), + test_single_character: ("a", "a"), + test_leading_trailing_spaces: (" hello ", " olleh "), + test_unicode_characters: ("你好", "好你"), + test_mixed_content: ("a1b2c3!", "!3c2b1a"), } }