|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+import org.junit.Before;
|
|
|
2
|
+import org.junit.Test;
|
|
|
3
|
+
|
|
|
4
|
+import static java.util.Arrays.asList;
|
|
|
5
|
+import static java.util.Collections.emptyList;
|
|
|
6
|
+import static java.util.Collections.singletonList;
|
|
|
7
|
+import static org.junit.Assert.assertEquals;
|
|
|
8
|
+
|
|
|
9
|
+public final class FlattenerTest {
|
|
|
10
|
+
|
|
|
11
|
+ private Flattener flattener;
|
|
|
12
|
+
|
|
|
13
|
+ @Before
|
|
|
14
|
+ public void setUp() {
|
|
|
15
|
+ flattener = new Flattener();
|
|
|
16
|
+ }
|
|
|
17
|
+
|
|
|
18
|
+ @Test
|
|
|
19
|
+ public void testASingleLevelOfNestingWithNoNulls() {
|
|
|
20
|
+ assertEquals(
|
|
|
21
|
+ asList(1, '2', 3, 4, 5, "six", "7", 8),
|
|
|
22
|
+ flattener.flatten(
|
|
|
23
|
+ asList(
|
|
|
24
|
+ 1,
|
|
|
25
|
+ asList(
|
|
|
26
|
+ '2',
|
|
|
27
|
+ 3,
|
|
|
28
|
+ 4,
|
|
|
29
|
+ 5,
|
|
|
30
|
+ "six",
|
|
|
31
|
+ "7"),
|
|
|
32
|
+ 8)));
|
|
|
33
|
+ }
|
|
|
34
|
+
|
|
|
35
|
+ @Test
|
|
|
36
|
+ public void testFiveLevelsOfNestingWithNoNulls() {
|
|
|
37
|
+ assertEquals(
|
|
|
38
|
+ asList(0, '2', 2, "three", '8', 100, "four", 50, "-2"),
|
|
|
39
|
+ flattener.flatten(
|
|
|
40
|
+ asList(
|
|
|
41
|
+ 0,
|
|
|
42
|
+ '2',
|
|
|
43
|
+ asList(
|
|
|
44
|
+ asList(
|
|
|
45
|
+ 2,
|
|
|
46
|
+ "three"),
|
|
|
47
|
+ '8',
|
|
|
48
|
+ 100,
|
|
|
49
|
+ "four",
|
|
|
50
|
+ singletonList(
|
|
|
51
|
+ singletonList(
|
|
|
52
|
+ singletonList(
|
|
|
53
|
+ 50)))),
|
|
|
54
|
+ "-2")));
|
|
|
55
|
+ }
|
|
|
56
|
+
|
|
|
57
|
+ @Test
|
|
|
58
|
+ public void testSixLevelsOfNestingWithNoNulls() {
|
|
|
59
|
+ assertEquals(
|
|
|
60
|
+ asList("one", '2', 3, '4', 5, "six", 7, "8"),
|
|
|
61
|
+ flattener.flatten(
|
|
|
62
|
+ asList(
|
|
|
63
|
+ "one",
|
|
|
64
|
+ asList(
|
|
|
65
|
+ '2',
|
|
|
66
|
+ singletonList(
|
|
|
67
|
+ singletonList(
|
|
|
68
|
+ 3)),
|
|
|
69
|
+ asList(
|
|
|
70
|
+ '4',
|
|
|
71
|
+ singletonList(
|
|
|
72
|
+ singletonList(
|
|
|
73
|
+ 5))),
|
|
|
74
|
+ "six",
|
|
|
75
|
+ 7),
|
|
|
76
|
+ "8")));
|
|
|
77
|
+ }
|
|
|
78
|
+
|
|
|
79
|
+ @Test
|
|
|
80
|
+ public void testSixLevelsOfNestingWithNulls() {
|
|
|
81
|
+ assertEquals(
|
|
|
82
|
+ asList("0", 2, "two", '3', "8", "one hundred", "negative two"),
|
|
|
83
|
+ flattener.flatten(
|
|
|
84
|
+ asList(
|
|
|
85
|
+ "0",
|
|
|
86
|
+ 2,
|
|
|
87
|
+ asList(
|
|
|
88
|
+ asList(
|
|
|
89
|
+ "two",
|
|
|
90
|
+ '3'),
|
|
|
91
|
+ "8",
|
|
|
92
|
+ singletonList(
|
|
|
93
|
+ singletonList(
|
|
|
94
|
+ "one hundred")),
|
|
|
95
|
+ null,
|
|
|
96
|
+ singletonList(
|
|
|
97
|
+ singletonList(
|
|
|
98
|
+ null))),
|
|
|
99
|
+ "negative two")));
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ @Test
|
|
|
103
|
+ public void testNestedListsFullOfNullsOnly() {
|
|
|
104
|
+ assertEquals(
|
|
|
105
|
+ emptyList(),
|
|
|
106
|
+ flattener.flatten(
|
|
|
107
|
+ asList(
|
|
|
108
|
+ null,
|
|
|
109
|
+ singletonList(
|
|
|
110
|
+ singletonList(
|
|
|
111
|
+ singletonList(
|
|
|
112
|
+ null))),
|
|
|
113
|
+ null,
|
|
|
114
|
+ null,
|
|
|
115
|
+ asList(
|
|
|
116
|
+ asList(
|
|
|
117
|
+ null,
|
|
|
118
|
+ null),
|
|
|
119
|
+ null),
|
|
|
120
|
+ null)));
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+}
|