|
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
|
|
|
2
|
+import java.util.ArrayList;
|
|
|
3
|
+import java.util.Arrays;
|
|
|
4
|
+import java.util.Collections;
|
|
|
5
|
+import java.util.List;
|
|
|
6
|
+import java.util.function.BiFunction;
|
|
|
7
|
+import java.util.function.BinaryOperator;
|
|
|
8
|
+import java.util.function.Predicate;
|
|
|
9
|
+import java.util.stream.Collectors;
|
|
|
10
|
+import java.util.stream.IntStream;
|
|
|
11
|
+import static junit.framework.TestCase.assertEquals;
|
|
|
12
|
+import static junit.framework.TestCase.assertFalse;
|
|
|
13
|
+import static junit.framework.TestCase.assertNotNull;
|
|
|
14
|
+import static junit.framework.TestCase.assertTrue;
|
|
|
15
|
+import org.junit.Ignore;
|
|
|
16
|
+import org.junit.Test;
|
|
|
17
|
+
|
|
|
18
|
+public class ListOpsTest {
|
|
|
19
|
+
|
|
|
20
|
+ private static final List<Integer> EMPTY_LIST
|
|
|
21
|
+ = Collections.emptyList();
|
|
|
22
|
+
|
|
|
23
|
+ @Test
|
|
|
24
|
+ public void lengthOfAnEmptyListShouldBeZero() {
|
|
|
25
|
+ final int expected = 0;
|
|
|
26
|
+ final int actual = ListOps.length(EMPTY_LIST);
|
|
|
27
|
+
|
|
|
28
|
+ assertEquals(expected, actual);
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ @Test
|
|
|
32
|
+ @Ignore
|
|
|
33
|
+ public void shouldReturnTheCorrectLengthOfAnNonEmptyList() {
|
|
|
34
|
+ final List<Integer> list = Collections.unmodifiableList(
|
|
|
35
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
36
|
+ );
|
|
|
37
|
+ final int actual = ListOps.length(list);
|
|
|
38
|
+ final int expected = list.size();
|
|
|
39
|
+
|
|
|
40
|
+ assertEquals(expected, actual);
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ @Test
|
|
|
44
|
+ @Ignore
|
|
|
45
|
+ public void shouldReverseAnEmptyList() {
|
|
|
46
|
+ final List<Integer> actual = ListOps.reverse(EMPTY_LIST);
|
|
|
47
|
+
|
|
|
48
|
+ assertNotNull(actual);
|
|
|
49
|
+ assertTrue(actual.isEmpty());
|
|
|
50
|
+ }
|
|
|
51
|
+
|
|
|
52
|
+ @Test
|
|
|
53
|
+ @Ignore
|
|
|
54
|
+ public void shouldReverseANonEmptyList() {
|
|
|
55
|
+ final List<Integer> list = Collections.unmodifiableList(
|
|
|
56
|
+ Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8)
|
|
|
57
|
+ );
|
|
|
58
|
+ final List<Integer> actual
|
|
|
59
|
+ = ListOps.reverse(list);
|
|
|
60
|
+ final List<Integer> expected
|
|
|
61
|
+ = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1, 0);
|
|
|
62
|
+
|
|
|
63
|
+ assertNotNull(actual);
|
|
|
64
|
+ assertFalse(actual.isEmpty());
|
|
|
65
|
+ assertEquals(expected, actual);
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ @Test
|
|
|
69
|
+ @Ignore
|
|
|
70
|
+ public void shouldMapAnEmptyListAndReturnAnEmptyList() {
|
|
|
71
|
+ final List<Integer> actual = ListOps.map(EMPTY_LIST, x -> x + 1);
|
|
|
72
|
+
|
|
|
73
|
+ assertNotNull(actual);
|
|
|
74
|
+ assertTrue(actual.isEmpty());
|
|
|
75
|
+ }
|
|
|
76
|
+
|
|
|
77
|
+ @Test
|
|
|
78
|
+ @Ignore
|
|
|
79
|
+ public void shouldMapNonEmptyList() {
|
|
|
80
|
+ final List<Integer> list
|
|
|
81
|
+ = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7));
|
|
|
82
|
+ final List<Integer> actual = ListOps.map(list, x -> x + 1);
|
|
|
83
|
+
|
|
|
84
|
+ assertNotNull(actual);
|
|
|
85
|
+ assertFalse(actual.isEmpty());
|
|
|
86
|
+ assertEquals(Arrays.asList(2, 4, 6, 8), actual);
|
|
|
87
|
+ }
|
|
|
88
|
+
|
|
|
89
|
+ @Test
|
|
|
90
|
+ @Ignore
|
|
|
91
|
+ public void shouldFilterAnEmptyListanddReturnAnEmptyList() {
|
|
|
92
|
+ final List<Integer> actual = ListOps.filter(EMPTY_LIST, x -> x > 0);
|
|
|
93
|
+
|
|
|
94
|
+ assertNotNull(actual);
|
|
|
95
|
+ assertTrue(actual.isEmpty());
|
|
|
96
|
+ }
|
|
|
97
|
+
|
|
|
98
|
+ @Test
|
|
|
99
|
+ @Ignore
|
|
|
100
|
+ public void shouldFilterNonEmptyList() {
|
|
|
101
|
+ Predicate<Integer> predicate = x -> x % 2 > 0;
|
|
|
102
|
+ final List<Integer> list = Collections.unmodifiableList(
|
|
|
103
|
+ IntStream.range(0, 100).boxed().collect(Collectors.toList())
|
|
|
104
|
+ );
|
|
|
105
|
+ final List<Integer> actual = ListOps.filter(list, predicate);
|
|
|
106
|
+ final List<Integer> expected = list.stream()
|
|
|
107
|
+ .filter(predicate)
|
|
|
108
|
+ .collect(Collectors.toList());
|
|
|
109
|
+
|
|
|
110
|
+ assertNotNull(actual);
|
|
|
111
|
+ assertFalse(actual.isEmpty());
|
|
|
112
|
+ assertEquals(expected, actual);
|
|
|
113
|
+ }
|
|
|
114
|
+
|
|
|
115
|
+ @Test
|
|
|
116
|
+ @Ignore
|
|
|
117
|
+ public void shouldConcatenateZeroLists() {
|
|
|
118
|
+ List<Integer> actual = ListOps.concat();
|
|
|
119
|
+
|
|
|
120
|
+ assertNotNull(actual);
|
|
|
121
|
+ assertTrue(actual.isEmpty());
|
|
|
122
|
+ }
|
|
|
123
|
+
|
|
|
124
|
+ @Test
|
|
|
125
|
+ @Ignore
|
|
|
126
|
+ public void shouldConcatenateOneNonEmptyList() {
|
|
|
127
|
+ final List<Integer> list
|
|
|
128
|
+ = Collections.unmodifiableList(
|
|
|
129
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
130
|
+ );
|
|
|
131
|
+ final List<Integer> actual = ListOps.concat(list);
|
|
|
132
|
+ final List<Integer> expected = Arrays.asList(0, 1, 2, 3, 4);
|
|
|
133
|
+
|
|
|
134
|
+ assertNotNull(actual);
|
|
|
135
|
+ assertFalse(actual.isEmpty());
|
|
|
136
|
+ assertEquals(expected, actual);
|
|
|
137
|
+ }
|
|
|
138
|
+
|
|
|
139
|
+ @Test
|
|
|
140
|
+ @Ignore
|
|
|
141
|
+ public void shouldConcatenateOneEmptyList() {
|
|
|
142
|
+ final List<Integer> actual = ListOps.concat(EMPTY_LIST);
|
|
|
143
|
+
|
|
|
144
|
+ assertNotNull(actual);
|
|
|
145
|
+ assertTrue(actual.isEmpty());
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ @Test
|
|
|
149
|
+ @Ignore
|
|
|
150
|
+ public void shouldConcatenateTwoEmptyLists() {
|
|
|
151
|
+ final List<Integer> actual = ListOps.concat(EMPTY_LIST, EMPTY_LIST);
|
|
|
152
|
+
|
|
|
153
|
+ assertNotNull(actual);
|
|
|
154
|
+ assertTrue(actual.isEmpty());
|
|
|
155
|
+ }
|
|
|
156
|
+
|
|
|
157
|
+ @Test
|
|
|
158
|
+ @Ignore
|
|
|
159
|
+ public void shouldConcatenateOneEmptyAndOneNonEmptyLists() {
|
|
|
160
|
+ final List<Integer> list
|
|
|
161
|
+ = Collections.unmodifiableList(
|
|
|
162
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
163
|
+ );
|
|
|
164
|
+ final List<Integer> actual = ListOps.concat(list, EMPTY_LIST);
|
|
|
165
|
+ final List<Integer> expected
|
|
|
166
|
+ = Arrays.asList(0, 1, 2, 3, 4);
|
|
|
167
|
+
|
|
|
168
|
+ assertNotNull(actual);
|
|
|
169
|
+ assertFalse(actual.isEmpty());
|
|
|
170
|
+ assertEquals(expected, actual);
|
|
|
171
|
+ }
|
|
|
172
|
+
|
|
|
173
|
+ @Test
|
|
|
174
|
+ @Ignore
|
|
|
175
|
+ public void shouldConcatenateOneNonEmptyAndOneEmptyLists() {
|
|
|
176
|
+ final List<Integer> list
|
|
|
177
|
+ = Collections.unmodifiableList(
|
|
|
178
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
179
|
+ );
|
|
|
180
|
+ final List<Integer> actual = ListOps.concat(EMPTY_LIST, list);
|
|
|
181
|
+ final List<Integer> expected
|
|
|
182
|
+ = Arrays.asList(0, 1, 2, 3, 4);
|
|
|
183
|
+
|
|
|
184
|
+ assertNotNull(actual);
|
|
|
185
|
+ assertFalse(actual.isEmpty());
|
|
|
186
|
+ assertEquals(expected, actual);
|
|
|
187
|
+ }
|
|
|
188
|
+
|
|
|
189
|
+ @Test
|
|
|
190
|
+ @Ignore
|
|
|
191
|
+ public void shouldConcatenateTwoListsWithSameElements() {
|
|
|
192
|
+ final List<Integer> list1 = Collections.unmodifiableList(
|
|
|
193
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
194
|
+ );
|
|
|
195
|
+ final List<Integer> list2 = Collections.unmodifiableList(
|
|
|
196
|
+ Arrays.asList(1, 2, 3, 4, 5, 6)
|
|
|
197
|
+ );
|
|
|
198
|
+ final List<Integer> expected
|
|
|
199
|
+ = Arrays.asList(0, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6);
|
|
|
200
|
+ final List<Integer> actual = ListOps.concat(list1, list2);
|
|
|
201
|
+
|
|
|
202
|
+ assertNotNull(actual);
|
|
|
203
|
+ assertFalse(actual.isEmpty());
|
|
|
204
|
+ assertEquals(expected, actual);
|
|
|
205
|
+ }
|
|
|
206
|
+
|
|
|
207
|
+ @Test
|
|
|
208
|
+ @Ignore
|
|
|
209
|
+ public void shouldConcatenateSeveralLists() {
|
|
|
210
|
+ final List<Integer> list1 = Collections.unmodifiableList(
|
|
|
211
|
+ Arrays.asList(0, 1, 2, 3)
|
|
|
212
|
+ );
|
|
|
213
|
+ final List<Integer> list2 = Collections.unmodifiableList(
|
|
|
214
|
+ Arrays.asList(4, 5, 6, 7)
|
|
|
215
|
+ );
|
|
|
216
|
+ final List<Integer> list3 = Collections.unmodifiableList(
|
|
|
217
|
+ Arrays.asList(8, 9, 10, 11)
|
|
|
218
|
+ );
|
|
|
219
|
+ final List<Integer> list4 = Collections.unmodifiableList(
|
|
|
220
|
+ Arrays.asList(12, 13, 14, 15)
|
|
|
221
|
+ );
|
|
|
222
|
+ final List<Integer> expected
|
|
|
223
|
+ = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
|
|
|
224
|
+ 14, 15);
|
|
|
225
|
+
|
|
|
226
|
+ final List<Integer> actual
|
|
|
227
|
+ = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4);
|
|
|
228
|
+
|
|
|
229
|
+ assertNotNull(actual);
|
|
|
230
|
+ assertFalse(actual.isEmpty());
|
|
|
231
|
+ assertEquals(expected, actual);
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ @Test
|
|
|
235
|
+ @Ignore
|
|
|
236
|
+ public void shouldReturnIdentityWhenAnEmptyListIsReduced() {
|
|
|
237
|
+ final int expected = 0;
|
|
|
238
|
+ final int actual
|
|
|
239
|
+ = ListOps.reduce(EMPTY_LIST, 0, (x, y) -> x + y, Integer::sum);
|
|
|
240
|
+
|
|
|
241
|
+ assertEquals(expected, actual);
|
|
|
242
|
+ }
|
|
|
243
|
+
|
|
|
244
|
+ @Test
|
|
|
245
|
+ @Ignore
|
|
|
246
|
+ public void shouldCalculateTheSumOfANonEmptyIntegerList() {
|
|
|
247
|
+ final List<Integer> list = Collections.unmodifiableList(
|
|
|
248
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
249
|
+ );
|
|
|
250
|
+ final int actual = ListOps.reduce(list, 0,
|
|
|
251
|
+ (x, y) -> x + y,
|
|
|
252
|
+ Integer::sum);
|
|
|
253
|
+
|
|
|
254
|
+ assertEquals(10, actual);
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ /*
|
|
|
258
|
+ https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html
|
|
|
259
|
+ https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-U-java.util.function.BiFunction-java.util.function.BinaryOperator-
|
|
|
260
|
+ */
|
|
|
261
|
+ private BiFunction<List<Integer>, Integer, List<Integer>> accumulator
|
|
|
262
|
+ = (List<Integer> partial, Integer elem) -> {
|
|
|
263
|
+ List<Integer> result = new ArrayList<>(partial);
|
|
|
264
|
+ result.add(elem);
|
|
|
265
|
+ return result;
|
|
|
266
|
+ };
|
|
|
267
|
+
|
|
|
268
|
+ private BinaryOperator<List<Integer>> combiner
|
|
|
269
|
+ = (list1, list2) -> {
|
|
|
270
|
+ List<Integer> result = new ArrayList<>(list1);
|
|
|
271
|
+ result.addAll(list2);
|
|
|
272
|
+ return result;
|
|
|
273
|
+ };
|
|
|
274
|
+
|
|
|
275
|
+ @Test
|
|
|
276
|
+ @Ignore
|
|
|
277
|
+ public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() {
|
|
|
278
|
+ final List<Integer> list = Collections.unmodifiableList(
|
|
|
279
|
+ Arrays.asList(0, 1, 2, 3, 4, 5)
|
|
|
280
|
+ );
|
|
|
281
|
+ final List<Integer> actual
|
|
|
282
|
+ = ListOps.reduce(list,
|
|
|
283
|
+ new ArrayList<Integer>(),
|
|
|
284
|
+ accumulator,
|
|
|
285
|
+ combiner);
|
|
|
286
|
+ final List<Integer> expected
|
|
|
287
|
+ = Arrays.asList(0, 1, 2, 3, 4, 5);
|
|
|
288
|
+
|
|
|
289
|
+ assertNotNull(actual);
|
|
|
290
|
+ assertFalse(actual.isEmpty());
|
|
|
291
|
+ assertEquals(expected, actual);
|
|
|
292
|
+ }
|
|
|
293
|
+
|
|
|
294
|
+ @Test
|
|
|
295
|
+ @Ignore
|
|
|
296
|
+ public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() {
|
|
|
297
|
+ final List<Integer> listOne = Collections.unmodifiableList(
|
|
|
298
|
+ Arrays.asList(0, 1, 2, 3, 4)
|
|
|
299
|
+ );
|
|
|
300
|
+ final List<Integer> listTwo = Collections.unmodifiableList(
|
|
|
301
|
+ Arrays.asList(5, 6, 7, 8, 9)
|
|
|
302
|
+ );
|
|
|
303
|
+ final List<Integer> actual
|
|
|
304
|
+ = ListOps.reduce(listTwo,
|
|
|
305
|
+ listOne,
|
|
|
306
|
+ accumulator,
|
|
|
307
|
+ combiner);
|
|
|
308
|
+ final List<Integer> expected
|
|
|
309
|
+ = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
|
|
|
310
|
+
|
|
|
311
|
+ assertNotNull(actual);
|
|
|
312
|
+ assertFalse(actual.isEmpty());
|
|
|
313
|
+ assertEquals(expected, actual);
|
|
|
314
|
+ }
|
|
|
315
|
+
|
|
|
316
|
+}
|