|
1 | | -use affine_cipher::*; |
| 1 | +use affine_cipher::AffineCipherError::NotCoprime; |
2 | 2 |
|
3 | 3 | #[test] |
4 | 4 | fn encode_yes() { |
5 | | - assert_eq!(encode("yes", 5, 7).unwrap(), "xbt") |
| 5 | + let phrase = "yes"; |
| 6 | + let (a, b) = (5, 7); |
| 7 | + let output = affine_cipher::encode(phrase, a, b); |
| 8 | + let expected = Ok("xbt".into()); |
| 9 | + assert_eq!(output, expected); |
6 | 10 | } |
7 | 11 |
|
8 | 12 | #[test] |
9 | 13 | #[ignore] |
10 | 14 | fn encode_no() { |
11 | | - assert_eq!(encode("no", 15, 18).unwrap(), "fu") |
| 15 | + let phrase = "no"; |
| 16 | + let (a, b) = (15, 18); |
| 17 | + let output = affine_cipher::encode(phrase, a, b); |
| 18 | + let expected = Ok("fu".into()); |
| 19 | + assert_eq!(output, expected); |
12 | 20 | } |
13 | 21 |
|
14 | 22 | #[test] |
15 | 23 | #[ignore] |
16 | 24 | fn encode_omg() { |
17 | | - assert_eq!(encode("OMG", 21, 3).unwrap(), "lvz") |
| 25 | + let phrase = "OMG"; |
| 26 | + let (a, b) = (21, 3); |
| 27 | + let output = affine_cipher::encode(phrase, a, b); |
| 28 | + let expected = Ok("lvz".into()); |
| 29 | + assert_eq!(output, expected); |
18 | 30 | } |
19 | 31 |
|
20 | 32 | #[test] |
21 | 33 | #[ignore] |
22 | 34 | fn encode_o_m_g() { |
23 | | - assert_eq!(encode("O M G", 25, 47).unwrap(), "hjp") |
| 35 | + let phrase = "O M G"; |
| 36 | + let (a, b) = (25, 47); |
| 37 | + let output = affine_cipher::encode(phrase, a, b); |
| 38 | + let expected = Ok("hjp".into()); |
| 39 | + assert_eq!(output, expected); |
24 | 40 | } |
25 | 41 |
|
26 | 42 | #[test] |
27 | 43 | #[ignore] |
28 | 44 | fn encode_mindblowingly() { |
29 | | - assert_eq!(encode("mindblowingly", 11, 15).unwrap(), "rzcwa gnxzc dgt") |
| 45 | + let phrase = "mindblowingly"; |
| 46 | + let (a, b) = (11, 15); |
| 47 | + let output = affine_cipher::encode(phrase, a, b); |
| 48 | + let expected = Ok("rzcwa gnxzc dgt".into()); |
| 49 | + assert_eq!(output, expected); |
30 | 50 | } |
31 | 51 |
|
32 | 52 | #[test] |
33 | 53 | #[ignore] |
34 | 54 | fn encode_numbers() { |
35 | | - assert_eq!( |
36 | | - encode("Testing,1 2 3, testing.", 3, 4).unwrap(), |
37 | | - "jqgjc rw123 jqgjc rw" |
38 | | - ) |
| 55 | + let phrase = "Testing,1 2 3, testing."; |
| 56 | + let (a, b) = (3, 4); |
| 57 | + let output = affine_cipher::encode(phrase, a, b); |
| 58 | + let expected = Ok("jqgjc rw123 jqgjc rw".into()); |
| 59 | + assert_eq!(output, expected); |
39 | 60 | } |
40 | 61 |
|
41 | 62 | #[test] |
42 | 63 | #[ignore] |
43 | 64 | fn encode_deep_thought() { |
44 | | - assert_eq!( |
45 | | - encode("Truth is fiction", 5, 17).unwrap(), |
46 | | - "iynia fdqfb ifje" |
47 | | - ) |
| 65 | + let phrase = "Truth is fiction."; |
| 66 | + let (a, b) = (5, 17); |
| 67 | + let output = affine_cipher::encode(phrase, a, b); |
| 68 | + let expected = Ok("iynia fdqfb ifje".into()); |
| 69 | + assert_eq!(output, expected); |
48 | 70 | } |
49 | 71 |
|
50 | 72 | #[test] |
51 | 73 | #[ignore] |
52 | 74 | fn encode_all_the_letters() { |
53 | | - assert_eq!( |
54 | | - encode("The quick brown fox jumps over the lazy dog.", 17, 33).unwrap(), |
55 | | - "swxtj npvyk lruol iejdc blaxk swxmh qzglf" |
56 | | - ) |
| 75 | + let phrase = "The quick brown fox jumps over the lazy dog."; |
| 76 | + let (a, b) = (17, 33); |
| 77 | + let output = affine_cipher::encode(phrase, a, b); |
| 78 | + let expected = Ok("swxtj npvyk lruol iejdc blaxk swxmh qzglf".into()); |
| 79 | + assert_eq!(output, expected); |
57 | 80 | } |
58 | 81 |
|
59 | 82 | #[test] |
60 | 83 | #[ignore] |
61 | 84 | fn encode_with_a_not_coprime_to_m() { |
62 | | - const EXPECTED_ERROR: AffineCipherError = AffineCipherError::NotCoprime(6); |
63 | | - match encode("This is a test.", 6, 17) { |
64 | | - Err(EXPECTED_ERROR) => (), |
65 | | - Err(err) => panic!("Incorrect error: expected: {EXPECTED_ERROR:?}, actual: {err:?}."), |
66 | | - Ok(r) => panic!( |
67 | | - "Cannot encode/decode when a is coprime to m: expected: {EXPECTED_ERROR:?}, actual: {r:?}." |
68 | | - ), |
69 | | - } |
| 85 | + let phrase = "This is a test."; |
| 86 | + let (a, b) = (6, 17); |
| 87 | + let output = affine_cipher::encode(phrase, a, b); |
| 88 | + let expected = Err(NotCoprime(6)); |
| 89 | + assert_eq!(output, expected); |
70 | 90 | } |
71 | 91 |
|
72 | 92 | #[test] |
73 | 93 | #[ignore] |
74 | 94 | fn decode_exercism() { |
75 | | - assert_eq!(decode("tytgn fjr", 3, 7).unwrap(), "exercism") |
| 95 | + let phrase = "tytgn fjr"; |
| 96 | + let (a, b) = (3, 7); |
| 97 | + let output = affine_cipher::decode(phrase, a, b); |
| 98 | + let expected = Ok("exercism".into()); |
| 99 | + assert_eq!(output, expected); |
76 | 100 | } |
77 | 101 |
|
78 | 102 | #[test] |
79 | 103 | #[ignore] |
80 | 104 | fn decode_a_sentence() { |
81 | | - assert_eq!( |
82 | | - encode("anobstacleisoftenasteppingstone", 19, 16).unwrap(), |
83 | | - "qdwju nqcro muwhn odqun oppmd aunwd o" |
84 | | - ); |
85 | | - assert_eq!( |
86 | | - decode("qdwju nqcro muwhn odqun oppmd aunwd o", 19, 16).unwrap(), |
87 | | - "anobstacleisoftenasteppingstone" |
88 | | - ) |
| 105 | + let phrase = "qdwju nqcro muwhn odqun oppmd aunwd o"; |
| 106 | + let (a, b) = (19, 16); |
| 107 | + let output = affine_cipher::decode(phrase, a, b); |
| 108 | + let expected = Ok("anobstacleisoftenasteppingstone".into()); |
| 109 | + assert_eq!(output, expected); |
89 | 110 | } |
90 | 111 |
|
91 | 112 | #[test] |
92 | 113 | #[ignore] |
93 | 114 | fn decode_numbers() { |
94 | | - assert_eq!( |
95 | | - decode("odpoz ub123 odpoz ub", 25, 7).unwrap(), |
96 | | - "testing123testing" |
97 | | - ) |
| 115 | + let phrase = "odpoz ub123 odpoz ub"; |
| 116 | + let (a, b) = (25, 7); |
| 117 | + let output = affine_cipher::decode(phrase, a, b); |
| 118 | + let expected = Ok("testing123testing".into()); |
| 119 | + assert_eq!(output, expected); |
98 | 120 | } |
99 | 121 |
|
100 | 122 | #[test] |
101 | 123 | #[ignore] |
102 | 124 | fn decode_all_the_letters() { |
103 | | - assert_eq!( |
104 | | - decode("swxtj npvyk lruol iejdc blaxk swxmh qzglf", 17, 33).unwrap(), |
105 | | - "thequickbrownfoxjumpsoverthelazydog" |
106 | | - ) |
| 125 | + let phrase = "swxtj npvyk lruol iejdc blaxk swxmh qzglf"; |
| 126 | + let (a, b) = (17, 33); |
| 127 | + let output = affine_cipher::decode(phrase, a, b); |
| 128 | + let expected = Ok("thequickbrownfoxjumpsoverthelazydog".into()); |
| 129 | + assert_eq!(output, expected); |
107 | 130 | } |
108 | 131 |
|
109 | 132 | #[test] |
110 | 133 | #[ignore] |
111 | 134 | fn decode_with_no_spaces_in_input() { |
112 | | - assert_eq!( |
113 | | - decode("swxtjnpvyklruoliejdcblaxkswxmhqzglf", 17, 33).unwrap(), |
114 | | - "thequickbrownfoxjumpsoverthelazydog" |
115 | | - ) |
| 135 | + let phrase = "swxtjnpvyklruoliejdcblaxkswxmhqzglf"; |
| 136 | + let (a, b) = (17, 33); |
| 137 | + let output = affine_cipher::decode(phrase, a, b); |
| 138 | + let expected = Ok("thequickbrownfoxjumpsoverthelazydog".into()); |
| 139 | + assert_eq!(output, expected); |
116 | 140 | } |
117 | 141 |
|
118 | 142 | #[test] |
119 | 143 | #[ignore] |
120 | 144 | fn decode_with_too_many_spaces() { |
121 | | - assert_eq!( |
122 | | - decode("vszzm cly yd cg qdp", 15, 16).unwrap(), |
123 | | - "jollygreengiant" |
124 | | - ) |
| 145 | + let phrase = "vszzm cly yd cg qdp"; |
| 146 | + let (a, b) = (15, 16); |
| 147 | + let output = affine_cipher::decode(phrase, a, b); |
| 148 | + let expected = Ok("jollygreengiant".into()); |
| 149 | + assert_eq!(output, expected); |
125 | 150 | } |
126 | 151 |
|
127 | 152 | #[test] |
128 | 153 | #[ignore] |
129 | 154 | fn decode_with_a_not_coprime_to_m() { |
130 | | - const EXPECTED_ERROR: AffineCipherError = AffineCipherError::NotCoprime(13); |
131 | | - match decode("Test", 13, 11) { |
132 | | - Err(EXPECTED_ERROR) => (), |
133 | | - Err(e) => panic!("Incorrect error: expected: {EXPECTED_ERROR:?}, actual: {e:?}."), |
134 | | - Ok(r) => panic!( |
135 | | - "Cannot encode/decode when a is coprime to m: expected: {EXPECTED_ERROR:?}, actual: {r:?}." |
136 | | - ), |
137 | | - } |
| 155 | + let phrase = "Test"; |
| 156 | + let (a, b) = (13, 5); |
| 157 | + let output = affine_cipher::decode(phrase, a, b); |
| 158 | + let expected = Err(NotCoprime(13)); |
| 159 | + assert_eq!(output, expected); |
138 | 160 | } |
0 commit comments