|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+import org.junit.Ignore;
|
|
|
2
|
+import org.junit.Rule;
|
|
|
3
|
+import org.junit.Test;
|
|
|
4
|
+import org.junit.rules.ExpectedException;
|
|
|
5
|
+
|
|
|
6
|
+import static org.junit.Assert.assertEquals;
|
|
|
7
|
+
|
|
|
8
|
+public final class LargestSeriesProductCalculatorTest {
|
|
|
9
|
+
|
|
|
10
|
+ @Rule
|
|
|
11
|
+ public ExpectedException expectedException = ExpectedException.none();
|
|
|
12
|
+
|
|
|
13
|
+ @Test
|
|
|
14
|
+ public void testCorrectlyCalculatesLargestProductOfLengthTwoWithNumbersInOrder() {
|
|
|
15
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
|
|
|
16
|
+ final long expectedProduct = 72;
|
|
|
17
|
+
|
|
|
18
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);
|
|
|
19
|
+
|
|
|
20
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
21
|
+ }
|
|
|
22
|
+
|
|
|
23
|
+ @Ignore
|
|
|
24
|
+ @Test
|
|
|
25
|
+ public void testCorrectlyCalculatesLargestProductOfLengthTwoWithNumbersNotInOrder() {
|
|
|
26
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("576802143");
|
|
|
27
|
+ final long expectedProduct = 48;
|
|
|
28
|
+
|
|
|
29
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);
|
|
|
30
|
+
|
|
|
31
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ @Ignore
|
|
|
35
|
+ @Test
|
|
|
36
|
+ public void testCorrectlyCalculatesLargestProductWhenSeriesLengthEqualsStringToSearchLength() {
|
|
|
37
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("29");
|
|
|
38
|
+ final long expectedProduct = 18;
|
|
|
39
|
+
|
|
|
40
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);
|
|
|
41
|
+
|
|
|
42
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
43
|
+ }
|
|
|
44
|
+
|
|
|
45
|
+ @Ignore
|
|
|
46
|
+ @Test
|
|
|
47
|
+ public void testCorrectlyCalculatesLargestProductOfLengthThreeWithNumbersInOrder() {
|
|
|
48
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
|
|
|
49
|
+ final long expectedProduct = 504;
|
|
|
50
|
+
|
|
|
51
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);
|
|
|
52
|
+
|
|
|
53
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ @Ignore
|
|
|
57
|
+ @Test
|
|
|
58
|
+ public void testCorrectlyCalculatesLargestProductOfLengthThreeWithNumbersNotInOrder() {
|
|
|
59
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("1027839564");
|
|
|
60
|
+ final long expectedProduct = 270;
|
|
|
61
|
+
|
|
|
62
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);
|
|
|
63
|
+
|
|
|
64
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ @Ignore
|
|
|
68
|
+ @Test
|
|
|
69
|
+ public void testCorrectlyCalculatesLargestProductOfLengthFiveWithNumbersInOrder() {
|
|
|
70
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0123456789");
|
|
|
71
|
+ final long expectedProduct = 15120;
|
|
|
72
|
+
|
|
|
73
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(5);
|
|
|
74
|
+
|
|
|
75
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
76
|
+ }
|
|
|
77
|
+
|
|
|
78
|
+ @Ignore
|
|
|
79
|
+ @Test
|
|
|
80
|
+ public void testCorrectlyCalculatesLargestProductInLongStringToSearchV1() {
|
|
|
81
|
+ final LargestSeriesProductCalculator calculator
|
|
|
82
|
+ = new LargestSeriesProductCalculator("73167176531330624919225119674426574742355349194934");
|
|
|
83
|
+
|
|
|
84
|
+ final long expectedProduct = 23520;
|
|
|
85
|
+
|
|
|
86
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(6);
|
|
|
87
|
+
|
|
|
88
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ @Ignore
|
|
|
92
|
+ @Test
|
|
|
93
|
+ public void testCorrectlyCalculatesLargestProductInLongStringToSearchV2() {
|
|
|
94
|
+ final LargestSeriesProductCalculator calculator
|
|
|
95
|
+ = new LargestSeriesProductCalculator("52677741234314237566414902593461595376319419139427");
|
|
|
96
|
+
|
|
|
97
|
+ final long expectedProduct = 28350;
|
|
|
98
|
+
|
|
|
99
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(6);
|
|
|
100
|
+
|
|
|
101
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ @Ignore
|
|
|
105
|
+ @Test
|
|
|
106
|
+ public void testCorrectlyCalculatesLargestProductInLongStringToSearchFromProjectEuler() {
|
|
|
107
|
+ final LargestSeriesProductCalculator calculator
|
|
|
108
|
+ = new LargestSeriesProductCalculator("7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450");
|
|
|
109
|
+
|
|
|
110
|
+ final long expectedProduct = 23514624000L;
|
|
|
111
|
+
|
|
|
112
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(13);
|
|
|
113
|
+
|
|
|
114
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
115
|
+ }
|
|
|
116
|
+
|
|
|
117
|
+ @Ignore
|
|
|
118
|
+ @Test
|
|
|
119
|
+ public void testCorrectlyCalculatesLargestProductOfZeroIfAllDigitsAreZeroes() {
|
|
|
120
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("0000");
|
|
|
121
|
+ final long expectedProduct = 0;
|
|
|
122
|
+
|
|
|
123
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(2);
|
|
|
124
|
+
|
|
|
125
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
126
|
+ }
|
|
|
127
|
+
|
|
|
128
|
+ @Ignore
|
|
|
129
|
+ @Test
|
|
|
130
|
+ public void testCorrectlyCalculatesLargestProductOfZeroIfAllSeriesOfGivenLengthContainZero() {
|
|
|
131
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("99099");
|
|
|
132
|
+ final long expectedProduct = 0;
|
|
|
133
|
+
|
|
|
134
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(3);
|
|
|
135
|
+
|
|
|
136
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ @Ignore
|
|
|
140
|
+ @Test
|
|
|
141
|
+ public void testSeriesLengthLongerThanLengthOfStringToTestIsRejected() {
|
|
|
142
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123");
|
|
|
143
|
+
|
|
|
144
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
145
|
+ expectedException.expectMessage(
|
|
|
146
|
+ "Series length must be less than or equal to the length of the string to search.");
|
|
|
147
|
+
|
|
|
148
|
+ calculator.calculateLargestProductForSeriesLength(4);
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ @Ignore
|
|
|
152
|
+ @Test
|
|
|
153
|
+ public void testCorrectlyCalculatesLargestProductOfLength0ForEmptyStringToSearch() {
|
|
|
154
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("");
|
|
|
155
|
+ final long expectedProduct = 1;
|
|
|
156
|
+
|
|
|
157
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(0);
|
|
|
158
|
+
|
|
|
159
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
160
|
+ }
|
|
|
161
|
+
|
|
|
162
|
+ @Ignore
|
|
|
163
|
+ @Test
|
|
|
164
|
+ public void testCorrectlyCalculatesLargestProductOfLength0ForNonEmptyStringToSearch() {
|
|
|
165
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("123");
|
|
|
166
|
+ final long expectedProduct = 1;
|
|
|
167
|
+
|
|
|
168
|
+ final long actualProduct = calculator.calculateLargestProductForSeriesLength(0);
|
|
|
169
|
+
|
|
|
170
|
+ assertEquals(expectedProduct, actualProduct);
|
|
|
171
|
+ }
|
|
|
172
|
+
|
|
|
173
|
+ @Ignore
|
|
|
174
|
+ @Test
|
|
|
175
|
+ public void testEmptyStringToSearchAndSeriesOfNonZeroLengthIsRejected() {
|
|
|
176
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("");
|
|
|
177
|
+
|
|
|
178
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
179
|
+ expectedException.expectMessage(
|
|
|
180
|
+ "Series length must be less than or equal to the length of the string to search.");
|
|
|
181
|
+
|
|
|
182
|
+ calculator.calculateLargestProductForSeriesLength(1);
|
|
|
183
|
+ }
|
|
|
184
|
+
|
|
|
185
|
+ @Ignore
|
|
|
186
|
+ @Test
|
|
|
187
|
+ public void testStringToSearchContainingNonDigitCharacterIsRejected() {
|
|
|
188
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
189
|
+ expectedException.expectMessage("String to search may only contains digits.");
|
|
|
190
|
+
|
|
|
191
|
+ new LargestSeriesProductCalculator("1234a5");
|
|
|
192
|
+ }
|
|
|
193
|
+
|
|
|
194
|
+ @Ignore
|
|
|
195
|
+ @Test
|
|
|
196
|
+ public void testNegativeSeriesLengthIsRejected() {
|
|
|
197
|
+ final LargestSeriesProductCalculator calculator = new LargestSeriesProductCalculator("12345");
|
|
|
198
|
+
|
|
|
199
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
200
|
+ expectedException.expectMessage("Series length must be non-negative.");
|
|
|
201
|
+
|
|
|
202
|
+ calculator.calculateLargestProductForSeriesLength(-1);
|
|
|
203
|
+ }
|
|
|
204
|
+
|
|
|
205
|
+ @Ignore
|
|
|
206
|
+ @Test
|
|
|
207
|
+ public void testNullStringToSearchIsRejected() {
|
|
|
208
|
+ expectedException.expect(IllegalArgumentException.class);
|
|
|
209
|
+ expectedException.expectMessage("String to search must be non-null.");
|
|
|
210
|
+
|
|
|
211
|
+ new LargestSeriesProductCalculator(null);
|
|
|
212
|
+ }
|
|
|
213
|
+
|
|
|
214
|
+}
|