forked from TheAlgorithms/Rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrot13.rs
More file actions
36 lines (31 loc) · 692 Bytes
/
rot13.rs
File metadata and controls
36 lines (31 loc) · 692 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
pub fn rot13(text: &str) -> String {
let to_enc = text.to_uppercase();
to_enc
.chars()
.map(|c| match c {
'A'..='M' => ((c as u8) + 13) as char,
'N'..='Z' => ((c as u8) - 13) as char,
_ => c,
})
.collect()
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_single_letter() {
assert_eq!("N", rot13("A"));
}
#[test]
fn test_bunch_of_letters() {
assert_eq!("NOP", rot13("ABC"));
}
#[test]
fn test_non_ascii() {
assert_eq!("😀NO", rot13("😀AB"));
}
#[test]
fn test_twice() {
assert_eq!("ABCD", rot13(&rot13("ABCD")));
}
}