1+ /// Reverses the given string.
2+ ///
3+ /// # Arguments
4+ ///
5+ /// * `text` - A string slice that holds the string to be reversed.
6+ ///
7+ /// # Returns
8+ ///
9+ /// * A new `String` that is the reverse of the input string.
110pub fn reverse ( text : & str ) -> String {
211 text. chars ( ) . rev ( ) . collect ( )
312}
@@ -6,18 +15,26 @@ pub fn reverse(text: &str) -> String {
615mod tests {
716 use super :: * ;
817
9- #[ test]
10- fn test_simple ( ) {
11- assert_eq ! ( reverse( "racecar" ) , "racecar" ) ;
18+ macro_rules! test_cases {
19+ ( $( $name: ident: $test_case: expr, ) * ) => {
20+ $(
21+ #[ test]
22+ fn $name( ) {
23+ let ( input, expected) = $test_case;
24+ assert_eq!( reverse( input) , expected) ;
25+ }
26+ ) *
27+ } ;
1228 }
1329
14- #[ test]
15- fn test_assymetric ( ) {
16- assert_eq ! ( reverse( "abcdef" ) , "fedcba" )
17- }
18-
19- #[ test]
20- fn test_sentence ( ) {
21- assert_eq ! ( reverse( "step on no pets" ) , "step on no pets" ) ;
30+ test_cases ! {
31+ test_simple_palindrome: ( "racecar" , "racecar" ) ,
32+ test_non_palindrome: ( "abcdef" , "fedcba" ) ,
33+ test_sentence_with_spaces: ( "step on no pets" , "step on no pets" ) ,
34+ test_empty_string: ( "" , "" ) ,
35+ test_single_character: ( "a" , "a" ) ,
36+ test_leading_trailing_spaces: ( " hello " , " olleh " ) ,
37+ test_unicode_characters: ( "你好" , "好你" ) ,
38+ test_mixed_content: ( "a1b2c3!" , "!3c2b1a" ) ,
2239 }
2340}
0 commit comments