-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedListStudentTests.java
300 lines (281 loc) · 11.6 KB
/
LinkedListStudentTests.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package com.company;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import static org.junit.Assert.*;
/**
* My JUnits test for Homework 2
*
* @version 1.0
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class LinkedListStudentTests {
private DoublyLinkedList<String> list;
private static final int TIMEOUT = 200;
private static final String FOO = "foo";
private static final String BAR = "bar";
private static final String BAZ = "baz";
private static final String QUX = "qux";
@Before
public void setup() {
list = new DoublyLinkedList<>();
}
/**
* A helper method for various tests.
*
* First, this method tests isEmpty, the nullity of head/tail "pointers," and size.
* All elements in actual are compared against the elements in expected in a for loop,
* the traversal of which also tests the validity of the head/tail and previous/next "pointers."
*
* @param method - String for assertion message, e.g. "Did addToFront update size?"
* @param expected - An array of the elements expected
* @param actual - The actual list to be tested
*/
private void assertDoublyLinkedListEquals(String method, Object[] expected, DoublyLinkedList<String> actual) {
if (expected.length == 0) {
assertTrue(actual.isEmpty());
assertNull(list.getHead());
assertNull(list.getTail());
assertEquals("Did " + method + " update size?", expected.length, actual.size());
} else {
assertFalse(actual.isEmpty());
assertNotNull(list.getHead());
assertNotNull(list.getTail());
assertEquals("Did " + method + " update size?", expected.length, actual.size());
LinkedListNode foo = list.getHead();
for (int i = 0; i < expected.length; i++) {
assertEquals(expected[i], foo.getData());
if (i == 0) {
assertEquals(list.getHead(), foo); // tests nothing
assertNull(foo.getPrevious());
} else {
assertNotNull(foo.getPrevious());
assertEquals(expected[i - 1], foo.getPrevious().getData());
}
if (i == expected.length - 1) {
assertEquals(list.getTail(), foo);
assertNull(foo.getNext());
} else {
assertNotNull(foo.getNext());
assertEquals(expected[i + 1], foo.getNext().getData());
}
foo = foo.getNext();
}
}
}
@Test(timeout = TIMEOUT)
public void test_00_constructor() {
// Nothing to see here!
}
@Test(timeout = TIMEOUT)
public void test_01_addToFront() {
// test exception
try {
list.addToFront(null);
Assert.fail();
} catch (Exception e) {
assertEquals("addToFront must throw java.lang.IllegalArgumentException if data is null.",
IllegalArgumentException.class, e.getClass());
}
// test function
// test addToFront(data) for empty list
list.addToFront(BAZ);
assertDoublyLinkedListEquals("addToFront", new Object[]{BAZ}, list);
// test addToFront(data) for non-empty, one element list
list.addToFront(BAR);
assertDoublyLinkedListEquals("addToFront", new Object[]{BAR, BAZ}, list);
// test addToFront(data) for non-empty, two element list
list.addToFront(FOO);
assertDoublyLinkedListEquals("addToFront", new Object[]{FOO, BAR, BAZ}, list);
}
@Test(timeout = TIMEOUT)
public void test_02_addToBack() {
// test exception
try {
list.addToBack(null);
Assert.fail();
} catch (Exception e) {
assertEquals("addToBack must throw java.lang.IllegalArgumentException if data is null.",
IllegalArgumentException.class, e.getClass());
}
// test function
// test addToBack(data) for empty list
list.addToBack(FOO);
assertDoublyLinkedListEquals("addToBack", new Object[]{FOO}, list);
// test addToBack(data) for non-empty, one element list
list.addToBack(BAR);
assertDoublyLinkedListEquals("addToBack", new Object[]{FOO, BAR}, list);
// test addToBack(data) for non-empty, two element list
list.addToBack(BAZ);
assertDoublyLinkedListEquals("addToBack", new Object[]{FOO, BAR, BAZ}, list);
}
@Test(timeout = TIMEOUT)
public void test_03_addAtIndex() {
// test exceptions
String message = "addAtIndex must throw java.lang.IndexOutOfBoundsException" +
" if index is negative or index > size.";
for (int i: new int[]{-1, 1}) {
try {
list.addAtIndex(i, "");
Assert.fail();
} catch (Exception e) {
assertEquals(message, IndexOutOfBoundsException.class, e.getClass());
}
}
message = "addAtIndex must throw java.lang.IllegalArgumentException if data is null.";
try {
list.addAtIndex(0, null);
Assert.fail();
} catch (Exception e) {
assertEquals(message, IllegalArgumentException.class, e.getClass());
}
// test function
// test addAtIndex(0, data) for empty list
list.addAtIndex(0, BAZ);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{BAZ}, list);
// test addAtIndex(0, data) for non-empty, one element list
list.addAtIndex(0, BAR);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{BAR, BAZ}, list);
// test addAtIndex(0, data) for non-empty, two element list
list.addAtIndex(0, FOO);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{FOO, BAR, BAZ}, list);
list = new DoublyLinkedList<>(); // reset
// test addAtIndex(size, data) for empty list // tests nothing
list.addAtIndex(0, FOO);
// assertDoublyLinkedListEquals("addAtIndex", new Object[]{FOO}, list);
// test addAtIndex(size, data) for non-empty, one element list
list.addAtIndex(1, BAZ);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{FOO, BAZ}, list);
// test addAtIndex(size, data) for non-empty, two element list
list.addAtIndex(2, QUX);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{FOO, BAZ, QUX}, list);
// test addAtIndex(size / 2, data) for non-empty list
list.addAtIndex(1, BAR);
assertDoublyLinkedListEquals("addAtIndex", new Object[]{FOO, BAR, BAZ, QUX}, list);
}
@Test(timeout = TIMEOUT)
public void test_04_removeFromFront() {
// test no exceptions
// test function
// test removeFromFront() for empty list
assertNull(list.removeFromFront());
String dat = FOO;
list.addToFront(dat);
// test removeFromFront() for non-empty, one element list
assertEquals(dat, list.removeFromFront());
assertDoublyLinkedListEquals("removeFromFront", new String[]{}, list);
list.addToFront(FOO);
list.addToFront(BAR);
// test removeFromFront() for non-empty, two element list
assertEquals(BAR, list.removeFromFront());
assertDoublyLinkedListEquals("removeFromFront", new String[]{FOO}, list);
}
@Test(timeout = TIMEOUT)
public void test_05_removeFromBack() {
// test no exceptions
// test function
// test removeFromBack() for empty list
assertNull(list.removeFromBack());
list.addToFront(FOO);
// test removeFromBack() for non-empty, one element list
assertEquals(FOO, list.removeFromBack());
assertDoublyLinkedListEquals("removeFromBack", new String[]{}, list);
list.addToFront(BAR);
list.addToFront(FOO);
// test removeFromBack() for non-empty, two element list
assertEquals(BAR, list.removeFromBack());
assertDoublyLinkedListEquals("removeFromBack", new String[]{FOO}, list);
}
@Test(timeout = TIMEOUT)
public void test_06_removeAtIndex() {
// test exceptions
String message = "removeAtIndex must throw java.lang.IndexOutOfBoundsException" +
" if index is negative or index >= size.";
for (int i: new int[]{-1, 0, 1}) {
try {
list.removeAtIndex(i);
Assert.fail();
} catch (Exception e) {
assertEquals(message, IndexOutOfBoundsException.class, e.getClass());
}
}
// test function
// test removeAtIndex(0) for non-empty, one element list
list.addToFront(FOO);
assertEquals(FOO, list.removeAtIndex(0));
assertDoublyLinkedListEquals("removeAtIndex", new String[]{}, list);
// test removeAtIndex(0) for non-empty, two element list
list.addToFront(BAR);
list.addToFront(FOO);
assertEquals(FOO, list.removeAtIndex(0));
assertDoublyLinkedListEquals("removeAtIndex", new String[]{BAR}, list);
// test removeAtIndex(size - 1) for non-empty, one element list // tests nothing
assertEquals(BAR, list.removeAtIndex(0));
// test removeAtIndex(size - 1) for non-empty, two element list
list.addToFront(BAR);
list.addToFront(FOO);
assertEquals(BAR, list.removeAtIndex(1));
assertDoublyLinkedListEquals("removeAtIndex", new String[]{FOO}, list);
list = new DoublyLinkedList<>(); // reset
// test removeAtIndex(size / 2) for non-empty list
list.addToFront(BAZ);
list.addToFront(BAR);
list.addToFront(FOO);
assertEquals(BAR, list.removeAtIndex(1));
assertDoublyLinkedListEquals("removeAtIndex", new String[]{FOO, BAZ}, list);
}
/**
* TODO - This test is incomplete
*/
@Test(timeout = TIMEOUT)
public void test_07_removeAllOccurrences() {
// test exception
try {
list.removeAllOccurrences(null);
Assert.fail();
} catch (Exception e) {
assertEquals("removeAllOccurrences must throw java.lang.IllegalArgumentException if data is null.",
IllegalArgumentException.class, e.getClass());
}
list.addToFront(FOO);
list.addToFront(new String("foo"));
assertFalse(list.removeAllOccurrences(BAR));
assertTrue(list.removeAllOccurrences(FOO));
assertDoublyLinkedListEquals("removeAllOccurrences", new String[]{}, list);
}
@Test(timeout = TIMEOUT)
public void test_08_get() {
// test exceptions
String message = "get must throw java.lang.IndexOutOfBoundsException" +
" if index is negative or index >= size.";
for (int i: new int[]{-1, 0, 1}) {
try {
list.get(i);
Assert.fail();
} catch (Exception e) {
assertEquals(message, IndexOutOfBoundsException.class, e.getClass());
}
}
// test function
list.addToFront("0");
assertEquals("0", list.get(0));
}
@Test(timeout = TIMEOUT)
public void test_09_toArray() {
// test no exceptions
// test function
list.addToFront(BAR);
list.addToFront(FOO);
assertArrayEquals(new String[]{FOO, BAR}, list.toArray());
}
@Test(timeout = TIMEOUT)
public void test_10_clear() {
// test no exceptions
// test function
list.addToFront(FOO);
list.clear();
assertDoublyLinkedListEquals("clear", new String[]{}, list);
}
}