-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAnagramTest.java
More file actions
121 lines (101 loc) · 3.96 KB
/
AnagramTest.java
File metadata and controls
121 lines (101 loc) · 3.96 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import org.junit.Ignore;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import static org.hamcrest.CoreMatchers.allOf;
import static org.hamcrest.CoreMatchers.hasItem;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
public class AnagramTest {
@Test
public void testNoMatches() {
Anagram detector = new Anagram("diaper");
assertTrue(detector.match(Arrays.asList("hello", "world", "zombies", "pants")).isEmpty());
}
@Test
public void testDetectSimpleAnagram() {
Anagram detector = new Anagram("ant");
List<String> anagram = detector.match(Arrays.asList("tan", "stand", "at"));
assertThat(anagram, hasItem("tan"));
assertThat(anagram.size(), is(1));
}
@Test
public void testDetectLongerAnagram() {
Anagram detector = new Anagram("listen");
List<String> anagrams = detector.match(Arrays.asList("enlists", "google", "inlets", "banana"));
assertThat(anagrams, hasItem("inlets"));
}
@Test
public void testDetectMultipleAnagrams() {
Anagram detector = new Anagram("master");
List<String> anagrams = detector.match(Arrays.asList("stream", "pigeon", "maters"));
assertThat(anagrams, allOf(hasItem("maters"), hasItem("stream")));
}
@Test
public void testDetectMultipleAnagramsForLongerWord() {
Anagram detector = new Anagram("allergy");
List<String> anagrams = detector.match(Arrays.asList("gallery", "ballerina", "regally", "clergy", "largely", "leading"));
assertThat(anagrams, allOf(hasItem("gallery"), hasItem("largely"), hasItem("regally")));
}
@Test
public void testDoesNotConfuseDifferentDuplicates() {
Anagram detector = new Anagram("galea");
assertTrue(detector.match(Arrays.asList("eagle")).isEmpty());
}
@Test
public void testIdenticalWordIsNotAnagram() {
Anagram detector = new Anagram("corn");
List<String> anagrams = detector.match(Arrays.asList("corn", "dark", "Corn", "rank", "CORN", "cron", "park"));
assertThat(anagrams, hasItem("cron"));
assertThat(anagrams.size(), is(1));
}
@Test
public void testIdenticalWordRepeatedIsNotAnagram() {
Anagram detector = new Anagram("go");
assertTrue(detector.match(Arrays.asList("go Go GO")).isEmpty());
}
@Test
public void testCapitalWordIsNotOwnAnagram() {
Anagram detector = new Anagram("BANANA");
assertTrue(detector.match(Arrays.asList("Banana")).isEmpty());
}
@Test
public void testEliminateAnagramsWithSameChecksum() {
Anagram detector = new Anagram("mass");
assertTrue(detector.match(Arrays.asList("last")).isEmpty());
}
@Test
public void testEliminateAnagramSubsets() {
Anagram detector = new Anagram("good");
assertTrue(detector.match(Arrays.asList("dog", "goody")).isEmpty());
}
@Test
public void testCaseInsensitiveWhenSubjectStartsWithUpperCaseLetter() {
Anagram detector = new Anagram("Orchestra");
List<String> anagrams = detector.match(Arrays.asList("cashregister", "carthorse", "radishes"));
assertThat(anagrams, hasItem("carthorse"));
}
@Test
public void testCaseInsensitiveWhenAnagramStartsWithUpperCaseLetter() {
Anagram detector = new Anagram("orchestra");
List<String> anagrams = detector.match(Arrays.asList("cashregister", "Carthorse", "radishes"));
assertThat(anagrams, hasItem("Carthorse"));
}
@Test
public void testCaseInsensitiveWhenBothAnagramAndSubjectStartWithUpperCaseLetter() {
Anagram detector = new Anagram("Orchestra");
List<String> anagrams = detector.match(Arrays.asList("cashregister", "Carthorse", "radishes"));
assertThat(anagrams, hasItem("Carthorse"));
}
@Test
public void testWordIsNotItsOwnAnagram() {
Anagram detector = new Anagram("banana");
assertTrue(detector.match(Arrays.asList("Banana")).isEmpty());
}
@Test
public void testAnagramMustUseAllLettersExactlyOnce() {
Anagram detector = new Anagram("tapper");
assertTrue(detector.match(Arrays.asList("patter")).isEmpty());
}
}