-
Notifications
You must be signed in to change notification settings - Fork 1
/
ListTest.java
200 lines (175 loc) · 5.2 KB
/
ListTest.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
package wk4;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.*;
class ListTest {
private List<String> list;
@BeforeEach
void setUp() {
list = new wk6.LinkedList<>();
}
@AfterEach
void tearDown() {
list = null;
}
@Test
void size() {
assertEquals(0, list.size());
list.add("one");
assertEquals(1, list.size());
}
@Test
void isEmpty() {
assertTrue(list.isEmpty());
list.add("one");
assertFalse(list.isEmpty());
}
@Test
void add() {
list.add("one");
assertEquals(1, list.size());
list.add("two");
assertEquals(2, list.size());
}
@Test
void clear() {
list.clear();
assertTrue(list.isEmpty());
list.add("one");
list.clear();
assertTrue(list.isEmpty());
list.add("one");
list.add("one");
list.add("one");
list.clear();
assertTrue(list.isEmpty());
}
@Test
void addInt() {
assertThrows(IndexOutOfBoundsException.class, () -> list.add(1, null));
assertThrows(IndexOutOfBoundsException.class, () -> list.add(-1, null));
list.add(0, "one");
assertEquals(1, list.size());
list.add(0, "two");
assertEquals(1, list.indexOf("one"));
assertEquals(0, list.indexOf("two"));
list.add(2, "three");
assertEquals(2, list.indexOf("three"));
assertEquals(1, list.indexOf("one"));
assertEquals(0, list.indexOf("two"));
list.add(1, "four");
assertEquals(1, list.indexOf("four"));
assertEquals(3, list.indexOf("three"));
assertEquals(2, list.indexOf("one"));
assertEquals(0, list.indexOf("two"));
}
@Test
void contains() {
list.add("one");
list.add("two");
list.add("three");
list.add(null);
assertFalse(list.contains(""));
assertTrue(list.contains("two"));
assertTrue(list.contains(null));
assertFalse(list.contains(3));
}
@Test
void get() {
list.add("one");
list.add("two");
list.add("three");
assertEquals("one", list.get(0));
assertEquals("two", list.get(1));
assertEquals("three", list.get(2));
assertThrows(IndexOutOfBoundsException.class, () -> list.get(3));
assertThrows(IndexOutOfBoundsException.class, () -> list.get(-1));
}
@Test
void set() {
list.add("one");
list.add("two");
list.add("three");
list.set(0, "1");
assertEquals("1", list.get(0), "Set on first element failed");
list.set(2, "3");
assertEquals("3", list.get(2));
list.set(2, null);
assertNull(list.get(2));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(3, ""));
assertThrows(IndexOutOfBoundsException.class, () -> list.set(-1, ""));
}
@Test
void remove() {
assertFalse(list.remove(""));
assertFalse(list.remove("two"));
list.add("one");
list.add("two");
list.add("three");
assertTrue(list.remove("two"));
assertFalse(list.remove("two"));
}
@Test
void removeIntBad() {
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(0));
list.add("one");
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(-1));
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(1));
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(3));
}
@Test
void removeIntFirst() {
list.add("one");
assertEquals("one", list.remove(0));
list.add("one");
list.add("two");
assertEquals("two", list.remove(1));
assertFalse(list.contains("two"));
assertTrue(list.contains("one"));
assertEquals("one", list.remove(0));
assertFalse(list.contains("one"));
}
@Test
void removeIntLast() {
list.add("one");
list.add("two");
list.add("three");
assertThrows(IndexOutOfBoundsException.class, () -> list.remove(3));
assertEquals("three", list.remove(2));
assertFalse(list.contains("three"));
assertTrue(list.contains("two"));
assertEquals("two", list.remove(1));
assertTrue(list.contains("one"));
}
@Test
void removeIntNull() {
list.add(null);
assertNull(list.remove(0));
assertEquals(0, list.size());
}
@Test
void indexOf() {
assertEquals(-1, list.indexOf("not found"));
list.add("one");
list.add("two");
list.add("three");
list.add(null);
assertEquals(-1, list.indexOf(""));
assertEquals(1, list.indexOf("two"));
assertEquals(3, list.indexOf(null));
assertEquals(-1, list.indexOf(3));
}
@Test
void toArray() {
list.add("one");
list.add("two");
list.add("three");
Object[] expected = {"one", "two", "three"};
assertArrayEquals(expected, list.toArray());
}
@Test
void toArrayEmpty() {
Object[] expected = {};
assertArrayEquals(expected, list.toArray());
}
}