|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+import org.junit.Test;
|
|
|
2
|
+
|
|
|
3
|
+import java.util.Arrays;
|
|
|
4
|
+import java.util.LinkedList;
|
|
|
5
|
+import java.util.List;
|
|
|
6
|
+
|
|
|
7
|
+import static org.junit.Assert.assertEquals;
|
|
|
8
|
+
|
|
|
9
|
+public class AccumulateTest {
|
|
|
10
|
+
|
|
|
11
|
+ @Test
|
|
|
12
|
+ public void emptyAccumulateProducesEmptyAccumulation() {
|
|
|
13
|
+ List<Integer> input = new LinkedList<>();
|
|
|
14
|
+ List<Integer> expectedOutput = new LinkedList<>();
|
|
|
15
|
+ assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
|
|
|
16
|
+ }
|
|
|
17
|
+
|
|
|
18
|
+ @Test
|
|
|
19
|
+ public void accumulateSquares() {
|
|
|
20
|
+ List<Integer> input = Arrays.asList(1, 2, 3);
|
|
|
21
|
+ List<Integer> expectedOutput = Arrays.asList(1, 4, 9);
|
|
|
22
|
+ assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ @Test
|
|
|
26
|
+ public void accumulateUpperCases() {
|
|
|
27
|
+ List<String> input = Arrays.asList("hello", "world");
|
|
|
28
|
+ List<String> expectedOutput = Arrays.asList("HELLO", "WORLD");
|
|
|
29
|
+ assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x.toUpperCase()));
|
|
|
30
|
+ }
|
|
|
31
|
+
|
|
|
32
|
+ @Test
|
|
|
33
|
+ public void accumulateReversedStrings() {
|
|
|
34
|
+ List<String> input = Arrays.asList("the quick brown fox etc".split(" "));
|
|
|
35
|
+ List<String> expectedOutput = Arrays.asList("eht kciuq nworb xof cte".split(" "));
|
|
|
36
|
+ assertEquals(expectedOutput, Accumulate.accumulate(input, this::reverse));
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ private String reverse(String input) {
|
|
|
40
|
+ return new StringBuilder(input).reverse().toString();
|
|
|
41
|
+ }
|
|
|
42
|
+
|
|
|
43
|
+ @Test
|
|
|
44
|
+ public void accumulateWithinAccumulate() {
|
|
|
45
|
+ List<String> input1 = Arrays.asList("a", "b", "c");
|
|
|
46
|
+ List<String> input2 = Arrays.asList("1", "2", "3");
|
|
|
47
|
+ List<String> expectedOutput = Arrays.asList("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
|
|
|
48
|
+ assertEquals(expectedOutput, Accumulate.accumulate(
|
|
|
49
|
+ input1, c ->
|
|
|
50
|
+ String.join(" ", Accumulate.accumulate(input2, d -> c + d))
|
|
|
51
|
+ ));
|
|
|
52
|
+ }
|
|
|
53
|
+}
|