-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImmutableListTest.java
239 lines (203 loc) · 8.32 KB
/
ImmutableListTest.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import org.junit.Test;
public class ImmutableListTest {
// ---BEGIN EQUALS TESTS---
@Test
public void equalsNilNil() {
assertTrue(new Nil().equals(new Nil()));
} // equalsNilNil
@Test
public void equalsNilCons() {
assertFalse(new Nil().equals(new Cons(1, new Nil())));
} // equalsNilCons
@Test
public void equalsConsNil() {
assertFalse(new Cons(1, new Nil()).equals(new Nil()));
} // equalsConsNil
@Test
public void equalsConsConsSameElementsLength1() {
assertTrue(new Cons(1, new Nil()).equals(new Cons(1, new Nil())));
} // equalsConsConsSameElementsLength1
@Test
public void equalsConsConsDifferentElementsLength1() {
assertFalse(new Cons(1, new Nil()).equals(new Cons(2, new Nil())));
} // equalsConsConsDifferentElementsLength1
@Test
public void equalsConsConsSameElementsLength2() {
final ImmutableList first = new Cons(1, new Cons(2, new Nil()));
final ImmutableList second = new Cons(1, new Cons(2, new Nil()));
assertTrue(first.equals(second));
} // equalsConsConsSameElementsLength2
@Test
public void equalsConsConsDifferentElementsLength2_1() {
final ImmutableList first = new Cons(1, new Cons(2, new Nil()));
final ImmutableList second = new Cons(1, new Cons(3, new Nil()));
assertFalse(first.equals(second));
} // equalsConsConsDifferentElementsLength2_1
@Test
public void equalsConsConsDifferentElementsLength2_2() {
final ImmutableList first = new Cons(1, new Cons(2, new Nil()));
final ImmutableList second = new Cons(2, new Cons(1, new Nil()));
assertFalse(first.equals(second));
} // equalsConsConsDifferentElementsLength2_2
@Test
public void equalsConsConsDifferentElementsLength2_3() {
final ImmutableList first = new Cons(1, new Cons(2, new Nil()));
final ImmutableList second = new Cons(3, new Cons(4, new Nil()));
assertFalse(first.equals(second));
} // equalsConsConsDifferentElementsLength2_3
// ---END EQUALS TESTS---
// ---BEGIN LENGTH TESTS---
@Test
public void lengthLength0() {
assertEquals(0, new Nil().length());
} // lengthLength0
@Test
public void lengthLength1() {
assertEquals(1, new Cons(-80, new Nil()).length());
} // lengthLength1
@Test
public void lengthLength2() {
assertEquals(2, new Cons(-12, new Cons(5, new Nil())).length());
} // lengthLength2
@Test
public void lengthLength3() {
final ImmutableList list = new Cons(-4, new Cons(87, new Cons(0, new Nil())));
assertEquals(3, list.length());
} // lengthLength3
// ---END LENGTH TESTS---
// ---BEGIN SUM TESTS---
@Test
public void sumLength0() {
assertEquals(0, new Nil().sum());
} // sumLength0
@Test
public void sumLength1() {
assertEquals(5, new Cons(5, new Nil()).sum());
} // sumLength1
@Test
public void sumLength2() {
assertEquals(3, new Cons(-1, new Cons(4, new Nil())).sum());
} // sumLength2
@Test
public void sumLength3() {
assertEquals(12, new Cons(6, new Cons(0, new Cons(6, new Nil()))).sum());
} // sumLength3
// ---END SUM TESTS---
// ---BEGIN APPEND TESTS---
@Test
public void appendNilNil() {
final ImmutableList first = new Nil();
final ImmutableList second = new Nil();
final ImmutableList expected = new Nil();
assertEquals(expected, first.append(second));
} // appendNilNil
@Test
public void appendConsNil() {
final ImmutableList first = new Cons(0, new Nil());
final ImmutableList second = new Nil();
final ImmutableList expected = new Cons(0, new Nil());
assertEquals(expected, first.append(second));
} // appendConsNil
@Test
public void appendNilCons() {
final ImmutableList first = new Nil();
final ImmutableList second = new Cons(1, new Nil());
final ImmutableList expected = new Cons(1, new Nil());
assertEquals(expected, first.append(second));
} // appendNilCons
@Test
public void appendConsConsSameLength_1() {
final ImmutableList first = new Cons(1, new Nil());
final ImmutableList second = new Cons(2, new Nil());
final ImmutableList expected = new Cons(1, new Cons(2, new Nil()));
assertEquals(expected, first.append(second));
} // appendConsConsSameLength_1
@Test
public void appendConsConsSameLength_2() {
final ImmutableList first = new Cons(2, new Nil());
final ImmutableList second = new Cons(1, new Nil());
final ImmutableList expected = new Cons(2, new Cons(1, new Nil()));
assertEquals(expected, first.append(second));
} // appendConsConsSameLength_2
@Test
public void appendConsConsDifferentLength_1() {
final ImmutableList first = new Cons(1, new Cons(2, new Cons(1, new Nil())));
final ImmutableList second = new Cons(-1, new Cons(1, new Nil()));
final ImmutableList expected =
new Cons(1, new Cons(2, new Cons(1, new Cons(-1, new Cons(1, new Nil())))));
assertEquals(expected, first.append(second));
} // appendConsConsDifferentLength_1
@Test
public void appendConsConsDifferentLength_2() {
final ImmutableList first = new Cons(2, new Cons(1, new Nil()));
final ImmutableList second = new Cons(-1, new Cons(-2, new Cons(-3, new Nil())));
final ImmutableList expected =
new Cons(2, new Cons(1, new Cons(-1, new Cons(-2, new Cons(-3, new Nil())))));
assertEquals(expected, first.append(second));
} // appendConsConsDifferentLength_2
@Test
public void appendConsConsDifferentLength_3() {
final ImmutableList first = new Cons(0, new Nil());
final ImmutableList second = new Cons(1, new Cons(2, new Nil()));
final ImmutableList expected = new Cons(0, new Cons(1, new Cons(2, new Nil())));
assertEquals(expected, first.append(second));
} // appendConsConsDifferentLength_3
@Test
public void appendConsConsDifferentLength_4() {
final ImmutableList first = new Cons(1, new Cons(2, new Nil()));
final ImmutableList second = new Cons(3, new Nil());
final ImmutableList expected = new Cons(1, new Cons(2, new Cons(3, new Nil())));
assertEquals(expected, first.append(second));
} // appendConsConsDifferentLength_4
// ---END APPEND TESTS---
// ---BEGIN CONTAINS TESTS---
@Test
public void containsNil() {
assertFalse(new Nil().contains(5));
} // containsNil
@Test
public void containsFirstElement() {
assertTrue(new Cons(1, new Nil()).contains(1));
} // containsFirstElement
@Test
public void containsSecondElement() {
assertTrue(new Cons(1, new Cons(-3, new Nil())).contains(-3));
} // containsSecondElement
@Test
public void containsThirdElement() {
assertTrue(new Cons(0, new Cons(1, new Cons(2, new Nil()))).contains(2));
} // containsThirdElement
@Test
public void doesNotContain() {
assertFalse(new Cons(0, new Cons(-1, new Cons(1, new Nil()))).contains(3));
} // doesNotContain
// ---END CONTAINS TESTS---
// ---BEGIN TOSTRING TESTS---
@Test
public void toStringNil() {
assertEquals("Nil", new Nil().toString());
} // toStringNil
@Test
public void toStringLength1() {
assertEquals("Cons(1, Nil)", new Cons(1, new Nil()).toString());
} // toStringLength1
@Test
public void toStringLength2() {
final ImmutableList list = new Cons(1, new Cons(2, new Nil()));
assertEquals("Cons(1, Cons(2, Nil))", list.toString());
} // toStringLength2
@Test
public void toStringLength3() {
final ImmutableList list = new Cons(-1, new Cons(-2, new Cons(3, new Nil())));
assertEquals("Cons(-1, Cons(-2, Cons(3, Nil)))", list.toString());
} // toStringLength3
@Test
public void toStringLength4() {
final ImmutableList list = new Cons(0, new Cons(1, new Cons(2, new Cons(3, new Nil()))));
assertEquals("Cons(0, Cons(1, Cons(2, Cons(3, Nil))))", list.toString());
} // toStringLength4
// ---END TOSTRING TESTS
} // ImmutableListTest