|
|
@@ -1,12 +1,17 @@
|
|
1
|
1
|
import org.junit.Before;
|
|
2
|
2
|
import org.junit.Ignore;
|
|
3
|
3
|
import org.junit.Test;
|
|
|
4
|
+import org.junit.Rule;
|
|
|
5
|
+import org.junit.rules.ExpectedException;
|
|
4
|
6
|
|
|
5
|
7
|
import java.util.Arrays;
|
|
6
|
8
|
import java.util.Collections;
|
|
7
|
9
|
import java.util.List;
|
|
8
|
10
|
import java.util.SortedMap;
|
|
9
|
11
|
import java.util.stream.Collectors;
|
|
|
12
|
+import java.util.NoSuchElementException;
|
|
|
13
|
+
|
|
|
14
|
+import java.lang.IllegalArgumentException;
|
|
10
|
15
|
|
|
11
|
16
|
import static org.junit.Assert.assertEquals;
|
|
12
|
17
|
import static org.junit.Assert.assertFalse;
|
|
|
@@ -14,12 +19,30 @@ import static org.junit.Assert.assertNotNull;
|
|
14
|
19
|
|
|
15
|
20
|
public class PalindromeCalculatorTest {
|
|
16
|
21
|
private PalindromeCalculator palindromeCalculator;
|
|
|
22
|
+
|
|
|
23
|
+ @Rule
|
|
|
24
|
+ public ExpectedException expectedException = ExpectedException.none();
|
|
17
|
25
|
|
|
18
|
26
|
@Before
|
|
19
|
27
|
public void setup() {
|
|
20
|
28
|
palindromeCalculator = new PalindromeCalculator();
|
|
21
|
29
|
}
|
|
|
30
|
+
|
|
|
31
|
+ @Test
|
|
|
32
|
+ public void smallestPalindromeFromSingleDigitFactors() {
|
|
|
33
|
+ final List<List<Integer>> expected = Collections.unmodifiableList(
|
|
|
34
|
+ Arrays.asList(
|
|
|
35
|
+ Arrays.asList(1, 1)
|
|
|
36
|
+ )
|
|
|
37
|
+ );
|
|
|
38
|
+ final long expectedValue = 1l;
|
|
|
39
|
+
|
|
|
40
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1, 9);
|
|
|
41
|
+
|
|
|
42
|
+ checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
|
|
|
43
|
+ }
|
|
22
|
44
|
|
|
|
45
|
+ @Ignore("Remove to run test")
|
|
23
|
46
|
@Test
|
|
24
|
47
|
public void largestPalindromeFromSingleDigitFactors() {
|
|
25
|
48
|
final List<List<Integer>> expected = Collections.unmodifiableList(
|
|
|
@@ -94,6 +117,78 @@ public class PalindromeCalculatorTest {
|
|
94
|
117
|
|
|
95
|
118
|
checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
|
|
96
|
119
|
}
|
|
|
120
|
+
|
|
|
121
|
+ @Ignore("Remove to run test")
|
|
|
122
|
+ @Test
|
|
|
123
|
+ public void smallestPalindromeFromQuadDigitFactors() {
|
|
|
124
|
+ final List<List<Integer>> expected = Collections.unmodifiableList(
|
|
|
125
|
+ Arrays.asList(
|
|
|
126
|
+ Arrays.asList(1001, 1001)
|
|
|
127
|
+ )
|
|
|
128
|
+ );
|
|
|
129
|
+ final long expectedValue = 1002001l;
|
|
|
130
|
+
|
|
|
131
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1000, 9999);
|
|
|
132
|
+
|
|
|
133
|
+ checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.firstKey());
|
|
|
134
|
+ }
|
|
|
135
|
+
|
|
|
136
|
+ @Ignore("Remove to run test")
|
|
|
137
|
+ @Test
|
|
|
138
|
+ public void largestPalindromeFromQuadDigitFactors() {
|
|
|
139
|
+ final List<List<Integer>> expected = Collections.unmodifiableList(
|
|
|
140
|
+ Arrays.asList(
|
|
|
141
|
+ Arrays.asList(9901, 9999)
|
|
|
142
|
+ )
|
|
|
143
|
+ );
|
|
|
144
|
+ final long expectedValue = 99000099l;
|
|
|
145
|
+
|
|
|
146
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1000, 9999);
|
|
|
147
|
+
|
|
|
148
|
+ checkPalindromeWithFactorsMatchesExpected(expected, expectedValue, palindromes, palindromes.lastKey());
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ @Ignore("Remove to run test")
|
|
|
152
|
+ @Test
|
|
|
153
|
+ public void emtpyResultSmallestNoPalindromeInRange() {
|
|
|
154
|
+
|
|
|
155
|
+ expectedException.expect(NoSuchElementException.class);
|
|
|
156
|
+ expectedException.expectMessage("no palindrome with factors in the range 1002 to 1003");
|
|
|
157
|
+
|
|
|
158
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(1002, 1003);
|
|
|
159
|
+ }
|
|
|
160
|
+
|
|
|
161
|
+ @Ignore("Remove to run test")
|
|
|
162
|
+ @Test
|
|
|
163
|
+ public void emtpyResultLargestNoPalindromeInRange() {
|
|
|
164
|
+
|
|
|
165
|
+ expectedException.expect(NoSuchElementException.class);
|
|
|
166
|
+ expectedException.expectMessage("no palindrome with factors in the range 15 to 15");
|
|
|
167
|
+
|
|
|
168
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(15, 15);
|
|
|
169
|
+ }
|
|
|
170
|
+
|
|
|
171
|
+ @Ignore("Remove to run test")
|
|
|
172
|
+ @Test
|
|
|
173
|
+ public void errorSmallestMinIsMoreThanMax() {
|
|
|
174
|
+
|
|
|
175
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
176
|
+ expectedException.expectMessage("invalid input: min is 10000 and max is 1");
|
|
|
177
|
+
|
|
|
178
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(10000, 1);
|
|
|
179
|
+ }
|
|
|
180
|
+
|
|
|
181
|
+ @Ignore("Remove to run test")
|
|
|
182
|
+ @Test
|
|
|
183
|
+ public void errorLargestMinIsMoreThanMax() {
|
|
|
184
|
+
|
|
|
185
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
186
|
+ expectedException.expectMessage("invalid input: min is 2 and max is 1");
|
|
|
187
|
+
|
|
|
188
|
+ final SortedMap<Long, List<List<Integer>>> palindromes = palindromeCalculator.getPalindromeProductsWithFactors(2, 1);
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+
|
|
97
|
192
|
|
|
98
|
193
|
private void checkPalindromeWithFactorsMatchesExpected(List<List<Integer>> expectedPalindromeFactors,
|
|
99
|
194
|
long expectedValueOfPalindrome,
|