-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCircularLinkedListTest.java
More file actions
145 lines (120 loc) · 4.23 KB
/
CircularLinkedListTest.java
File metadata and controls
145 lines (120 loc) · 4.23 KB
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
package algorithms;
import java.util.Optional;
import org.javagrader.Grade;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
@Grade
public class CircularLinkedListTest {
// BEGIN STRIP
private void assertLinkedListState(CircularLinkedList list, int[] expectedValues) {
assertEquals(expectedValues.length, list.size);
if (expectedValues.length == 0) {
assertFalse(list.getFirst().isPresent());
assertFalse(list.getLast().isPresent());
return;
}
Optional<CircularLinkedList.Node> currentOpt = list.getFirst();
for (int expectedValue : expectedValues) {
assertTrue(currentOpt.isPresent());
CircularLinkedList.Node current = currentOpt.get();
assertEquals(expectedValue, current.value);
currentOpt = current.next;
}
// Check circularity
if (list.getLast().isPresent()) {
CircularLinkedList.Node lastNode = list.getLast().get();
assertTrue(lastNode.next.isPresent());
assertEquals(list.getFirst().get(), lastNode.next.get());
}
}
// END STRIP
@Test
@Grade(value = 1)
public void testSimple() {
CircularLinkedList list = new CircularLinkedList();
assertTrue(list.isEmpty());
list.enqueue(0);
assertFalse(list.isEmpty());
Optional<CircularLinkedList.Node> first = list.getFirst();
assertTrue(first.isPresent());
assertEquals(0, first.get().value);
Optional<CircularLinkedList.Node> last = list.getLast();
// Here we compare the references since last and first must be the same object
assertTrue(last.isPresent());
assertTrue(first.get() == last.get());
list.enqueue(1);
list.enqueue(2);
first = list.getFirst();
assertTrue(first.isPresent());
assertEquals(0, first.get().value);
assertEquals(3, list.size);
int[] array = new int[]{0, 1, 2};
// STUDENT CircularLinkedList.Node current = list.first.get();
// STUDENT for (int i = 0; i < array.length; i++) {
// STUDENT assertEquals(array[i], current.value);
// STUDENT current = current.next.get();
// STUDENT }
// BEGIN STRIP
assertLinkedListState(list, array);
// END STRIP
}
// BEGIN STRIP
@Test
@Grade(value = 1)
public void testLength0() {
CircularLinkedList list = new CircularLinkedList();
assertTrue(list.isEmpty());
int[] array = new int[]{};
assertLinkedListState(list, array);
}
@Test
@Grade(value = 1)
public void testLength1() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);
assertFalse(list.isEmpty());
int[] array = new int[]{0};
assertLinkedListState(list, array);
}
@Test
@Grade(value = 1)
public void testRemove() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);
list.enqueue(1);
list.enqueue(2);
list.enqueue(3);
assertEquals(4, list.size);
int[] array = new int[]{0, 1, 2, 3};
assertLinkedListState(list, array);
int result = list.remove(2);
assertEquals(2, result);
array = new int[]{0, 1, 3};
assertLinkedListState(list, array);
result = list.remove(2);
assertEquals(3, result);
array = new int[]{0, 1};
assertLinkedListState(list, array);
}
@Test
@Grade(value = 1)
public void testRemoveToEmpty() {
CircularLinkedList list = new CircularLinkedList();
list.enqueue(0);
assertEquals(1, list.size);
int[] array = new int[]{0};
assertLinkedListState(list, array);
int result = list.remove(0);
assertEquals(0, result);
array = new int[]{};
assertLinkedListState(list, array);
}
@Test
@Grade(value = 1)
public void testRemoveOutOfBounds() {
CircularLinkedList list = new CircularLinkedList();
int result = list.remove(0); // Should not crash, should return -1
assertEquals(-1, result);
}
// END STRIP
}