Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 28 additions & 11 deletions src/string/reverse.rs
Original file line number Diff line number Diff line change
@@ -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()
}
Expand All @@ -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"),
}
}