1
exercism fetch java flatten-array

src/test/java/FlattenerTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import org.junit.Before;
import org.junit.Test;
import org.junit.Ignore;

import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;

public class FlattenerTest {

    private Flattener flattener;

    @Before
    public void setUp() {
        flattener = new Flattener();
    }

    @Test
    public void testFlatListIsPreserved() {
        assertEquals(
          asList(0, '1', "two"),
            flattener.flatten(
              asList(
                0,
                '1',
                "two")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testASingleLevelOfNestingWithNoNulls() {
        assertEquals(
          asList(1, '2', 3, 4, 5, "six", "7", 8),
          flattener.flatten(
            asList(
              1,
              asList(
                '2',
                3,
                4,
                5,
                "six",
                "7"),
              8)));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFiveLevelsOfNestingWithNoNulls() {
        assertEquals(
          asList(0, '2', 2, "three", '8', 100, "four", 50, "-2"),
          flattener.flatten(
            asList(
              0,
              '2',
              asList(
                asList(
                  2,
                  "three"),
                '8',
                100,
                "four",
                singletonList(
                  singletonList(
                    singletonList(
                      50)))),
              "-2")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSixLevelsOfNestingWithNoNulls() {
        assertEquals(
          asList("one", '2', 3, '4', 5, "six", 7, "8"),
          flattener.flatten(
            asList(
              "one",
              asList(
                '2',
                singletonList(
                  singletonList(
                    3)),
                asList(
                  '4',
                  singletonList(
                    singletonList(
                      5))),
                "six",
                7),
              "8")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSixLevelsOfNestingWithNulls() {
        assertEquals(
          asList("0", 2, "two", '3', "8", "one hundred", "negative two"),
          flattener.flatten(
            asList(
              "0",
              2,
              asList(
                asList(
                  "two",
                  '3'),
                "8",
                singletonList(
                  singletonList(
                    "one hundred")),
                null,
                singletonList(
                  singletonList(
                    null))),
              "negative two")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testNestedListsFullOfNullsOnly() {
        assertEquals(
          emptyList(),
          flattener.flatten(
            asList(
              null,
              singletonList(
                singletonList(
                  singletonList(
                    null))),
              null,
              null,
              asList(
                asList(
                  null,
                  null),
                null),
              null)));
    }

}