|
5 | 5 | #include "test/catch.hpp" |
6 | 6 | #endif |
7 | 7 |
|
| 8 | +#include <string> |
| 9 | +#include <unordered_set> |
| 10 | + |
8 | 11 | using namespace std; |
9 | 12 |
|
10 | 13 | // Anagram exercise test case data version 1.5.0 |
11 | 14 |
|
12 | | -TEST_CASE("no_matches") |
13 | | -{ |
| 15 | +namespace { |
| 16 | +/// Converts a vector into a set to allow comparison for equality regardless of |
| 17 | +/// order. |
| 18 | +std::unordered_set<std::string> to_set( |
| 19 | + std::vector<std::string> const& container) { |
| 20 | + return {std::cbegin(container), std::cend(container)}; |
| 21 | +} |
| 22 | +} // namespace |
| 23 | + |
| 24 | +TEST_CASE("no_matches") { |
14 | 25 | // 'anagram::anagram' defines a class |
15 | 26 | anagram::anagram subject = anagram::anagram("diaper"); |
16 | 27 | auto matches = subject.matches({"hello", "world", "zombies", "pants"}); |
17 | 28 | vector<string> expected; |
18 | 29 |
|
19 | | - REQUIRE(expected == matches); |
| 30 | + REQUIRE(to_set(expected) == to_set(matches)); |
20 | 31 | } |
21 | 32 |
|
22 | 33 | #if defined(EXERCISM_RUN_ALL_TESTS) |
23 | | -TEST_CASE("detects_two_anagrams", "[findAnagrams][03eb9bbe-8906-4ea0-84fa-ffe711b52c8b]") |
24 | | -{ |
| 34 | +TEST_CASE("detects_two_anagrams", |
| 35 | + "[findAnagrams][03eb9bbe-8906-4ea0-84fa-ffe711b52c8b]") { |
25 | 36 | auto subject = anagram::anagram("solemn"); |
26 | | - auto matches = subject.matches({"lemons", "cherry", "melons"}); |
| 37 | + auto matches = subject.matches({"cherry", "melons", "lemons"}); |
27 | 38 | vector<string> expected{"lemons", "melons"}; |
28 | 39 |
|
29 | | - REQUIRE(expected == matches); |
| 40 | + REQUIRE(to_set(expected) == to_set(matches)); |
30 | 41 | } |
31 | 42 |
|
32 | | -TEST_CASE("does_not_detect_anagram_subsets", "[findAnagrams][a27558ee-9ba0-4552-96b1-ecf665b06556]") |
33 | | -{ |
| 43 | +TEST_CASE("does_not_detect_anagram_subsets", |
| 44 | + "[findAnagrams][a27558ee-9ba0-4552-96b1-ecf665b06556]") { |
34 | 45 | auto subject = anagram::anagram("good"); |
35 | 46 | auto matches = subject.matches({"dog", "goody"}); |
36 | 47 | vector<string> expected; |
37 | 48 |
|
38 | | - REQUIRE(expected == matches); |
| 49 | + REQUIRE(to_set(expected) == to_set(matches)); |
39 | 50 | } |
40 | 51 |
|
41 | | -TEST_CASE("detects_anagram", "[findAnagrams][64cd4584-fc15-4781-b633-3d814c4941a4]") |
42 | | -{ |
| 52 | +TEST_CASE("detects_anagram", |
| 53 | + "[findAnagrams][64cd4584-fc15-4781-b633-3d814c4941a4]") { |
43 | 54 | auto subject = anagram::anagram("listen"); |
44 | 55 | auto matches = subject.matches({"enlists", "google", "inlets", "banana"}); |
45 | 56 | vector<string> expected{"inlets"}; |
46 | 57 |
|
47 | | - REQUIRE(expected == matches); |
| 58 | + REQUIRE(to_set(expected) == to_set(matches)); |
48 | 59 | } |
49 | 60 |
|
50 | | -TEST_CASE("detects_three_anagrams", "[findAnagrams][99c91beb-838f-4ccd-b123-935139917283]") |
51 | | -{ |
| 61 | +TEST_CASE("detects_three_anagrams", |
| 62 | + "[findAnagrams][99c91beb-838f-4ccd-b123-935139917283]") { |
52 | 63 | auto subject = anagram::anagram("allergy"); |
53 | | - auto matches = subject.matches({ |
54 | | - "gallery", |
55 | | - "ballerina", |
56 | | - "regally", |
57 | | - "clergy", |
58 | | - "largely", |
59 | | - "leading" |
60 | | - }); |
| 64 | + auto matches = subject.matches( |
| 65 | + {"gallery", "ballerina", "regally", "clergy", "largely", "leading"}); |
61 | 66 | vector<string> expected{"gallery", "regally", "largely"}; |
62 | 67 |
|
63 | | - REQUIRE(expected == matches); |
| 68 | + REQUIRE(to_set(expected) == to_set(matches)); |
64 | 69 | } |
65 | 70 |
|
66 | | -TEST_CASE("detects_multiple_anagrams_with_different_case", "[findAnagrams][78487770-e258-4e1f-a646-8ece10950d90]") |
67 | | -{ |
| 71 | +TEST_CASE("detects_multiple_anagrams_with_different_case", |
| 72 | + "[findAnagrams][78487770-e258-4e1f-a646-8ece10950d90]") { |
68 | 73 | auto subject = anagram::anagram("nose"); |
69 | 74 | auto matches = subject.matches({"Eons", "ONES"}); |
70 | 75 | vector<string> expected{"Eons", "ONES"}; |
71 | 76 |
|
72 | | - REQUIRE(expected == matches); |
| 77 | + REQUIRE(to_set(expected) == to_set(matches)); |
73 | 78 | } |
74 | 79 |
|
75 | | -TEST_CASE("does_not_detect_non_anagrams_with_identical_checksum", "[findAnagrams][1d0ab8aa-362f-49b7-9902-3d0c668d557b]") |
76 | | -{ |
| 80 | +TEST_CASE("does_not_detect_non_anagrams_with_identical_checksum", |
| 81 | + "[findAnagrams][1d0ab8aa-362f-49b7-9902-3d0c668d557b]") { |
77 | 82 | auto subject = anagram::anagram("mass"); |
78 | 83 | auto matches = subject.matches({"last"}); |
79 | 84 | vector<string> expected; |
80 | 85 |
|
81 | | - REQUIRE(expected == matches); |
| 86 | + REQUIRE(to_set(expected) == to_set(matches)); |
82 | 87 | } |
83 | 88 |
|
84 | | -TEST_CASE("detects_anagrams_case_insensitively", "[findAnagrams][9e632c0b-c0b1-4804-8cc1-e295dea6d8a8]") |
85 | | -{ |
| 89 | +TEST_CASE("detects_anagrams_case_insensitively", |
| 90 | + "[findAnagrams][9e632c0b-c0b1-4804-8cc1-e295dea6d8a8]") { |
86 | 91 | auto subject = anagram::anagram("Orchestra"); |
87 | 92 | auto matches = subject.matches({"cashregister", "Carthorse", "radishes"}); |
88 | 93 | vector<string> expected{"Carthorse"}; |
89 | 94 |
|
90 | | - REQUIRE(expected == matches); |
| 95 | + REQUIRE(to_set(expected) == to_set(matches)); |
91 | 96 | } |
92 | 97 |
|
93 | | -TEST_CASE("detects_anagrams_using_case_insensitive_subject", "[findAnagrams][b248e49f-0905-48d2-9c8d-bd02d8c3e392]") |
94 | | -{ |
| 98 | +TEST_CASE("detects_anagrams_using_case_insensitive_subject", |
| 99 | + "[findAnagrams][b248e49f-0905-48d2-9c8d-bd02d8c3e392]") { |
95 | 100 | auto subject = anagram::anagram("Orchestra"); |
96 | 101 | auto matches = subject.matches({"cashregister", "carthorse", "radishes"}); |
97 | 102 | vector<string> expected{"carthorse"}; |
98 | 103 |
|
99 | | - REQUIRE(expected == matches); |
| 104 | + REQUIRE(to_set(expected) == to_set(matches)); |
100 | 105 | } |
101 | 106 |
|
102 | | -TEST_CASE("detects_anagrams_using_case_insensitive_possible_matches", "[findAnagrams][f367325c-78ec-411c-be76-e79047f4bd54]") |
103 | | -{ |
| 107 | +TEST_CASE("detects_anagrams_using_case_insensitive_possible_matches", |
| 108 | + "[findAnagrams][f367325c-78ec-411c-be76-e79047f4bd54]") { |
104 | 109 | auto subject = anagram::anagram("orchestra"); |
105 | 110 | auto matches = subject.matches({"cashregister", "Carthorse", "radishes"}); |
106 | 111 | vector<string> expected{"Carthorse"}; |
107 | 112 |
|
108 | | - REQUIRE(expected == matches); |
| 113 | + REQUIRE(to_set(expected) == to_set(matches)); |
109 | 114 | } |
110 | 115 |
|
111 | | -TEST_CASE("does_not_detect_an_anagram_if_the_original_word_is_repeated", "[findAnagrams][630abb71-a94e-4715-8395-179ec1df9f91]") |
112 | | -{ |
| 116 | +TEST_CASE("does_not_detect_an_anagram_if_the_original_word_is_repeated", |
| 117 | + "[findAnagrams][630abb71-a94e-4715-8395-179ec1df9f91]") { |
113 | 118 | auto subject = anagram::anagram("go"); |
114 | 119 | auto matches = subject.matches({"goGoGO"}); |
115 | 120 | vector<string> expected; |
116 | 121 |
|
117 | | - REQUIRE(expected == matches); |
| 122 | + REQUIRE(to_set(expected) == to_set(matches)); |
118 | 123 | } |
119 | 124 |
|
120 | | -TEST_CASE("anagrams_must_use_all_letters_exactly_once", "[findAnagrams][9878a1c9-d6ea-4235-ae51-3ea2befd6842]") |
121 | | -{ |
| 125 | +TEST_CASE("anagrams_must_use_all_letters_exactly_once", |
| 126 | + "[findAnagrams][9878a1c9-d6ea-4235-ae51-3ea2befd6842]") { |
122 | 127 | auto subject = anagram::anagram("tapper"); |
123 | 128 | auto matches = subject.matches({"patter"}); |
124 | 129 | vector<string> expected; |
125 | 130 |
|
126 | | - REQUIRE(expected == matches); |
| 131 | + REQUIRE(to_set(expected) == to_set(matches)); |
127 | 132 | } |
128 | 133 |
|
129 | | -TEST_CASE("words_are_not_anagrams_of_themselves", "[findAnagrams][68934ed0-010b-4ef9-857a-20c9012d1ebf]") |
130 | | -{ |
| 134 | +TEST_CASE("words_are_not_anagrams_of_themselves", |
| 135 | + "[findAnagrams][68934ed0-010b-4ef9-857a-20c9012d1ebf]") { |
131 | 136 | auto subject = anagram::anagram("BANANA"); |
132 | 137 | auto matches = subject.matches({"BANANA"}); |
133 | 138 | vector<string> expected; |
134 | 139 |
|
135 | | - REQUIRE(expected == matches); |
| 140 | + REQUIRE(to_set(expected) == to_set(matches)); |
136 | 141 | } |
137 | 142 |
|
138 | | -TEST_CASE("words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different", "[findAnagrams][589384f3-4c8a-4e7d-9edc-51c3e5f0c90e]") |
139 | | -{ |
| 143 | +TEST_CASE( |
| 144 | + // clang-format off |
| 145 | + "words_are_not_anagrams_of_themselves_even_if_letter_case_is_partially_different", |
| 146 | + // clang-format on |
| 147 | + "[findAnagrams][589384f3-4c8a-4e7d-9edc-51c3e5f0c90e]") { |
140 | 148 | auto subject = anagram::anagram("BANANA"); |
141 | 149 | auto matches = subject.matches({"Banana"}); |
142 | 150 | vector<string> expected; |
143 | 151 |
|
144 | | - REQUIRE(expected == matches); |
| 152 | + REQUIRE(to_set(expected) == to_set(matches)); |
145 | 153 | } |
146 | 154 |
|
147 | | -TEST_CASE("words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different", "[findAnagrams][ba53e423-7e02-41ee-9ae2-71f91e6d18e6]") |
148 | | -{ |
| 155 | +TEST_CASE( |
| 156 | + // clang-format off |
| 157 | + "words_are_not_anagrams_of_themselves_even_if_letter_case_is_completely_different", |
| 158 | + // clang-format on |
| 159 | + "[findAnagrams][ba53e423-7e02-41ee-9ae2-71f91e6d18e6]") { |
149 | 160 | auto subject = anagram::anagram("BANANA"); |
150 | 161 | auto matches = subject.matches({"banana"}); |
151 | 162 | vector<string> expected; |
152 | 163 |
|
153 | | - REQUIRE(expected == matches); |
| 164 | + REQUIRE(to_set(expected) == to_set(matches)); |
154 | 165 | } |
155 | 166 |
|
156 | | -TEST_CASE("words_other_than_themselves_can_be_anagrams", "[findAnagrams][33d3f67e-fbb9-49d3-a90e-0beb00861da7]") |
157 | | -{ |
| 167 | +TEST_CASE("words_other_than_themselves_can_be_anagrams", |
| 168 | + "[findAnagrams][33d3f67e-fbb9-49d3-a90e-0beb00861da7]") { |
158 | 169 | auto subject = anagram::anagram("LISTEN"); |
159 | 170 | auto matches = subject.matches({"Silent", "LISTEN"}); |
160 | 171 | vector<string> expected{"Silent"}; |
161 | 172 |
|
162 | | - REQUIRE(expected == matches); |
| 173 | + REQUIRE(to_set(expected) == to_set(matches)); |
163 | 174 | } |
164 | 175 | #endif |
0 commit comments