1
exercism fetch java list-ops

src/test/java/ListOpsTest.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import org.junit.Ignore;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class ListOpsTest {

    @Test
    public void testAppendingEmptyLists() {
        assertEquals(
                Collections.emptyList(),
                ListOps.append(Collections.emptyList(), Collections.emptyList()));
    }

    @Ignore("Remove to run test")
    @Test
    public void testAppendingNonEmptyListOnEmptyList() {
        assertEquals(
                Arrays.asList('1', '2', '3', '4'),
                ListOps.append(Collections.emptyList(), Arrays.asList('1', '2', '3', '4')));
    }

    @Ignore("Remove to run test")
    @Test
    public void testAppendingNonEmptyListOnNonEmptyList() {
        assertEquals(
                Arrays.asList("1", "2", "2", "3", "4", "5"),
                ListOps.append(Arrays.asList("1", "2"), Arrays.asList("2", "3", "4", "5")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testConcatOnEmptyListOfLists() {
        assertEquals(
                Collections.emptyList(),
                ListOps.concat(Collections.emptyList()));
    }

    @Ignore("Remove to run test")
    @Test
    public void testConcatOnNonEmptyListOfLists() {
        List<List<Character>> listOfLists = Arrays.asList(
                Arrays.asList('1', '2'),
                Collections.singletonList('3'),
                Collections.emptyList(),
                Arrays.asList('4', '5', '6'));

        assertEquals(
                Arrays.asList('1', '2', '3', '4', '5', '6'),
                ListOps.concat(listOfLists));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFilteringEmptyList() {
        assertEquals(
                Collections.emptyList(),
                ListOps.filter(Collections.<Integer>emptyList(), integer -> integer % 2 == 1));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFilteringNonEmptyList() {
        assertEquals(
                Arrays.asList(1, 3, 5),
                ListOps.filter(Arrays.asList(1, 2, 3, 5), integer -> integer % 2 == 1));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSizeOfEmptyList() {
        assertEquals(0, ListOps.size(Collections.emptyList()));
    }

    @Ignore("Remove to run test")
    @Test
    public void testSizeOfNonEmptyList() {
        assertEquals(4, ListOps.size(Arrays.asList("one", "two", "three", "four")));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTransformingEmptyList() {
        assertEquals(
                Collections.emptyList(),
                ListOps.map(Collections.<Integer>emptyList(), integer -> integer + 1 ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testTransformingNonEmptyList() {
        assertEquals(
                Arrays.asList(2, 4, 6, 8),
                ListOps.map(Arrays.asList(1, 3, 5, 7), integer -> integer + 1 ));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldLeftOnEmptyList() {
        assertEquals(
                new Double(2.0), // Boxing required for method overload disambiguation.
                ListOps.foldLeft(
                        Collections.<Double>emptyList(),
                        2.0,
                        (x, y) -> x * y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldLeftWithDirectionIndependentOperationOnNonEmptyList() {
        assertEquals(
                new Integer(15), // Boxing required for method overload disambiguation.
                ListOps.foldLeft(
                        Arrays.asList(1, 2, 3, 4),
                        5,
                        (x, y) -> x + y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldLeftWithDirectionDependentOperationOnNonEmptyList() {
        assertEquals(
                new Integer(0), // Boxing required for method overload disambiguation.
                ListOps.foldLeft(
                        Arrays.asList(2, 5),
                        5,
                        (x, y) -> x / y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldRightOnEmptyList() {
        assertEquals(
                new Double(2.0), // Boxing required for method overload disambiguation.
                ListOps.foldRight(
                        Collections.<Double>emptyList(),
                        2.0,
                        (x, y) -> x * y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldRightWithDirectionIndependentOperationOnNonEmptyList() {
        assertEquals(
                new Integer(15), // Boxing required for method overload disambiguation.
                ListOps.foldRight(
                        Arrays.asList(1, 2, 3, 4),
                        5,
                        (x, y) -> x + y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testFoldRightWithDirectionDependentOperationOnNonEmptyList() {
        assertEquals(
                new Integer(2), // Boxing required for method overload disambiguation.
                ListOps.foldRight(
                        Arrays.asList(2, 5),
                        5,
                        (x, y) -> x / y));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReversingEmptyList() {
        assertEquals(
                Collections.emptyList(),
                ListOps.reverse(Collections.emptyList()));
    }

    @Ignore("Remove to run test")
    @Test
    public void testReversingNonEmptyList() {
        assertEquals(
                Arrays.asList('7', '5', '3', '1'),
                ListOps.reverse(Arrays.asList('1', '3', '5', '7')));
    }

}