anagram.t (4176B) - raw
1 #!/usr/bin/env perl 2 use Test2::V0; 3 use JSON::PP; 4 use constant JSON => JSON::PP->new; 5 6 use FindBin qw<$Bin>; 7 use lib $Bin, "$Bin/local/lib/perl5"; 8 9 use Anagram qw<match_anagrams>; 10 11 my @test_cases = do { local $/; @{ JSON->decode(<DATA>) }; }; 12 plan 15; 13 14 imported_ok qw<match_anagrams> or bail_out; 15 16 for my $case (@test_cases) { 17 is match_anagrams( $case->{input} ), $case->{expected}, 18 $case->{description}; 19 } 20 21 __DATA__ 22 [ 23 { 24 "description": "no matches", 25 "expected": [], 26 "input": { 27 "candidates": [ 28 "hello", 29 "world", 30 "zombies", 31 "pants" 32 ], 33 "subject": "diaper" 34 }, 35 "property": "findAnagrams" 36 }, 37 { 38 "description": "detects two anagrams", 39 "expected": [ 40 "stream", 41 "maters" 42 ], 43 "input": { 44 "candidates": [ 45 "stream", 46 "pigeon", 47 "maters" 48 ], 49 "subject": "master" 50 }, 51 "property": "findAnagrams" 52 }, 53 { 54 "description": "does not detect anagram subsets", 55 "expected": [], 56 "input": { 57 "candidates": [ 58 "dog", 59 "goody" 60 ], 61 "subject": "good" 62 }, 63 "property": "findAnagrams" 64 }, 65 { 66 "description": "detects anagram", 67 "expected": [ 68 "inlets" 69 ], 70 "input": { 71 "candidates": [ 72 "enlists", 73 "google", 74 "inlets", 75 "banana" 76 ], 77 "subject": "listen" 78 }, 79 "property": "findAnagrams" 80 }, 81 { 82 "description": "detects three anagrams", 83 "expected": [ 84 "gallery", 85 "regally", 86 "largely" 87 ], 88 "input": { 89 "candidates": [ 90 "gallery", 91 "ballerina", 92 "regally", 93 "clergy", 94 "largely", 95 "leading" 96 ], 97 "subject": "allergy" 98 }, 99 "property": "findAnagrams" 100 }, 101 { 102 "description": "detects multiple anagrams with different case", 103 "expected": [ 104 "Eons", 105 "ONES" 106 ], 107 "input": { 108 "candidates": [ 109 "Eons", 110 "ONES" 111 ], 112 "subject": "nose" 113 }, 114 "property": "findAnagrams" 115 }, 116 { 117 "description": "does not detect non-anagrams with identical checksum", 118 "expected": [], 119 "input": { 120 "candidates": [ 121 "last" 122 ], 123 "subject": "mass" 124 }, 125 "property": "findAnagrams" 126 }, 127 { 128 "description": "detects anagrams case-insensitively", 129 "expected": [ 130 "Carthorse" 131 ], 132 "input": { 133 "candidates": [ 134 "cashregister", 135 "Carthorse", 136 "radishes" 137 ], 138 "subject": "Orchestra" 139 }, 140 "property": "findAnagrams" 141 }, 142 { 143 "description": "detects anagrams using case-insensitive subject", 144 "expected": [ 145 "carthorse" 146 ], 147 "input": { 148 "candidates": [ 149 "cashregister", 150 "carthorse", 151 "radishes" 152 ], 153 "subject": "Orchestra" 154 }, 155 "property": "findAnagrams" 156 }, 157 { 158 "description": "detects anagrams using case-insensitive possible matches", 159 "expected": [ 160 "Carthorse" 161 ], 162 "input": { 163 "candidates": [ 164 "cashregister", 165 "Carthorse", 166 "radishes" 167 ], 168 "subject": "orchestra" 169 }, 170 "property": "findAnagrams" 171 }, 172 { 173 "description": "does not detect an anagram if the original word is repeated", 174 "expected": [], 175 "input": { 176 "candidates": [ 177 "go Go GO" 178 ], 179 "subject": "go" 180 }, 181 "property": "findAnagrams" 182 }, 183 { 184 "description": "anagrams must use all letters exactly once", 185 "expected": [], 186 "input": { 187 "candidates": [ 188 "patter" 189 ], 190 "subject": "tapper" 191 }, 192 "property": "findAnagrams" 193 }, 194 { 195 "description": "words are not anagrams of themselves (case-insensitive)", 196 "expected": [], 197 "input": { 198 "candidates": [ 199 "BANANA", 200 "Banana", 201 "banana" 202 ], 203 "subject": "BANANA" 204 }, 205 "property": "findAnagrams" 206 }, 207 { 208 "description": "words other than themselves can be anagrams", 209 "expected": [ 210 "Silent" 211 ], 212 "input": { 213 "candidates": [ 214 "Listen", 215 "Silent", 216 "LISTEN" 217 ], 218 "subject": "LISTEN" 219 }, 220 "property": "findAnagrams" 221 } 222 ]