aboutsummaryrefslogtreecommitdiff
path: root/rust/anagram/tests
diff options
context:
space:
mode:
authorFederico Igne <git@federicoigne.com>2021-11-05 22:58:26 +0000
committerFederico Igne <git@federicoigne.com>2021-11-05 23:00:47 +0000
commitc689e2ad8273b22e1ce8ae4ec58013d3e43010dc (patch)
tree83f27a3a0aed3728506da4af301b29ca795ddf68 /rust/anagram/tests
parenta43a779ad12b464175f95d9381499fe28510ec09 (diff)
downloadexercism-c689e2ad8273b22e1ce8ae4ec58013d3e43010dc.tar.gz
exercism-c689e2ad8273b22e1ce8ae4ec58013d3e43010dc.zip
[rust] Anagram
Diffstat (limited to 'rust/anagram/tests')
-rw-r--r--rust/anagram/tests/anagram.rs173
1 files changed, 173 insertions, 0 deletions
diff --git a/rust/anagram/tests/anagram.rs b/rust/anagram/tests/anagram.rs
new file mode 100644
index 0000000..40fc03e
--- /dev/null
+++ b/rust/anagram/tests/anagram.rs
@@ -0,0 +1,173 @@
1use std::collections::HashSet;
2
3fn process_anagram_case(word: &str, inputs: &[&str], expected: &[&str]) {
4 let result = anagram::anagrams_for(word, inputs);
5
6 let expected: HashSet<&str> = expected.iter().cloned().collect();
7
8 assert_eq!(result, expected);
9}
10
11#[test]
12fn test_no_matches() {
13 let word = "diaper";
14
15 let inputs = ["hello", "world", "zombies", "pants"];
16
17 let outputs = vec![];
18
19 process_anagram_case(word, &inputs, &outputs);
20}
21
22#[test]
23fn test_detect_simple_anagram() {
24 let word = "ant";
25
26 let inputs = ["tan", "stand", "at"];
27
28 let outputs = vec!["tan"];
29
30 process_anagram_case(word, &inputs, &outputs);
31}
32
33#[test]
34fn test_does_not_confuse_different_duplicates() {
35 let word = "galea";
36
37 let inputs = ["eagle"];
38
39 let outputs = vec![];
40
41 process_anagram_case(word, &inputs, &outputs);
42}
43
44#[test]
45fn test_eliminate_anagram_subsets() {
46 let word = "good";
47
48 let inputs = ["dog", "goody"];
49
50 let outputs = vec![];
51
52 process_anagram_case(word, &inputs, &outputs);
53}
54
55#[test]
56fn test_detect_anagram() {
57 let word = "listen";
58
59 let inputs = ["enlists", "google", "inlets", "banana"];
60
61 let outputs = vec!["inlets"];
62
63 process_anagram_case(word, &inputs, &outputs);
64}
65
66#[test]
67fn test_multiple_anagrams() {
68 let word = "allergy";
69
70 let inputs = [
71 "gallery",
72 "ballerina",
73 "regally",
74 "clergy",
75 "largely",
76 "leading",
77 ];
78
79 let outputs = vec!["gallery", "regally", "largely"];
80
81 process_anagram_case(word, &inputs, &outputs);
82}
83
84#[test]
85fn test_case_insensitive_anagrams() {
86 let word = "Orchestra";
87
88 let inputs = ["cashregister", "Carthorse", "radishes"];
89
90 let outputs = vec!["Carthorse"];
91
92 process_anagram_case(word, &inputs, &outputs);
93}
94
95#[test]
96fn test_unicode_anagrams() {
97 let word = "ΑΒΓ";
98
99 // These words don't make sense, they're just greek letters cobbled together.
100 let inputs = ["ΒΓΑ", "ΒΓΔ", "γβα"];
101
102 let outputs = vec!["ΒΓΑ", "γβα"];
103
104 process_anagram_case(word, &inputs, &outputs);
105}
106
107#[test]
108fn test_misleading_unicode_anagrams() {
109 // Despite what a human might think these words contain different letters, the input uses Greek
110 // A and B while the list of potential anagrams uses Latin A and B.
111 let word = "ΑΒΓ";
112
113 let inputs = ["ABΓ"];
114
115 let outputs = vec![];
116
117 process_anagram_case(word, &inputs, &outputs);
118}
119
120#[test]
121fn test_does_not_detect_a_word_as_its_own_anagram() {
122 let word = "banana";
123
124 let inputs = ["banana"];
125
126 let outputs = vec![];
127
128 process_anagram_case(word, &inputs, &outputs);
129}
130
131#[test]
132fn test_does_not_detect_a_differently_cased_word_as_its_own_anagram() {
133 let word = "banana";
134
135 let inputs = ["bAnana"];
136
137 let outputs = vec![];
138
139 process_anagram_case(word, &inputs, &outputs);
140}
141
142#[test]
143fn test_does_not_detect_a_differently_cased_unicode_word_as_its_own_anagram() {
144 let word = "ΑΒΓ";
145
146 let inputs = ["ΑΒγ"];
147
148 let outputs = vec![];
149
150 process_anagram_case(word, &inputs, &outputs);
151}
152
153#[test]
154fn test_same_bytes_different_chars() {
155 let word = "a⬂"; // 61 E2 AC 82
156
157 let inputs = ["€a"]; // E2 82 AC 61
158
159 let outputs = vec![];
160
161 process_anagram_case(word, &inputs, &outputs);
162}
163
164#[test]
165fn test_different_words_but_same_ascii_sum() {
166 let word = "bc";
167
168 let inputs = ["ad"];
169
170 let outputs = vec![];
171
172 process_anagram_case(word, &inputs, &outputs);
173}