-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedListTest.java
More file actions
181 lines (167 loc) · 6.2 KB
/
CircularLinkedListTest.java
File metadata and controls
181 lines (167 loc) · 6.2 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
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
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import java.util.Iterator;
import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.*;
public class CircularLinkedListTest {
public static CircularLinkedList<String> names;
@BeforeAll
public static void setUp() {
names = new CircularLinkedList<>();
}
@AfterEach
public void reset() {
names.clear();
}
@Test
public void test_constructor() {
CircularLinkedList<Player> players = new CircularLinkedList<>();
players.add(new Player("Anna"));
players.add(new Player("Bartholomew"));
players.add(new Player("Cassandra"));
assertEquals(new Player("Cassandra"), players.get(2));
assertEquals(3, players.getSize());
//System.out.println(names);
}
@Test
public void test_clear() {
names.add("Dani");
assertEquals(1, names.getSize());
names.clear();
assertEquals(0, names.getSize());
assertThrows(IndexOutOfBoundsException.class, () -> names.get(0));
}
@Test
public void test_getSize() {
assertEquals(0, names.getSize());
names.add("Anton");
assertEquals(1, names.getSize());
names.add("Beatrice");
assertEquals(2, names.getSize());
}
@Test
public void test_get() {
assertThrows(IndexOutOfBoundsException.class, () ->
names.get(0));
names.add("Timmy");
names.add("Tommy");
assertThrows(IndexOutOfBoundsException.class, () ->
names.get(-1));
assertThrows(IndexOutOfBoundsException.class, () ->
names.get(names.getSize()));
assertThrows(IndexOutOfBoundsException.class, () ->
names.get(2));
assertEquals("Timmy", names.get(0));
assertEquals("Tommy", names.get(names.getSize()-1));
}
@Test
public void test_addAtFront() {
assertEquals(0, names.getSize());
names.addAtFront("Billy");
assertEquals(1, names.getSize());
names.addAtFront("Viola");
assertEquals(2, names.getSize());
names.addAtFront(null);
names.addAtFront("Kiki");
assertEquals(4, names.getSize());
assertEquals("[Kiki, null, Viola, Billy]", names.toString());
}
@Test
public void test_add_and_toString() {
assertEquals(0, names.getSize());
assertEquals("[]", names.toString());
names.add("Gina");
assertEquals(1, names.getSize());
assertEquals("Gina", names.get(0));
assertEquals("[Gina]", names.toString());
names.add(null); //yes, null values are allowed
names.add("Violet");
assertEquals(3, names.getSize());
assertEquals("Violet", names.get(2));
assertEquals("[Gina, null, Violet]", names.toString());
}
@Test
public void test_addListToList() {
CircularLinkedList<String> empty = new CircularLinkedList<>();
names.addListToList(empty);
assertEquals("[]", names.toString());
CircularLinkedList<String> oneName = new CircularLinkedList<>();
oneName.add("Stone");
names.addListToList(oneName);
assertEquals("[Stone]", names.toString());
oneName.clear();
oneName.add("Smith");
names.addListToList(oneName);
assertEquals("[Stone, Smith]", names.toString());
CircularLinkedList<String> moreNames = new CircularLinkedList<>();
moreNames.add("Cedar");
moreNames.add("Boots");
names.addListToList(moreNames);
assertEquals("[Stone, Smith, Cedar, Boots]", names.toString());
}
@Test
public void test_indexOf() {
assertEquals(-1, names.indexOf("Avocado"));
names.add("Avocado");
names.add("Blueberry");
names.add("Rhubarb");
names.add("Rhubarb");
assertEquals(-1, names.indexOf("Tomato"));
assertEquals(1, names.indexOf("Blueberry"));
names.add(null);
assertEquals(4, names.indexOf(null));
assertEquals(2, names.indexOf("Rhubarb"));
}
@Test
public void test_remove_byValue() {
names.add("Potato");
names.add("Garlic");
names.add("Butter");
assertTrue(names.remove("Garlic"));
assertEquals(2, names.getSize());
assertEquals("Potato", names.get(0));
assertEquals("Butter", names.get(1));
assertThrows(IndexOutOfBoundsException.class, () -> names.get(2));
assertTrue(names.remove("Potato"));
assertFalse(names.remove("Jalapenos"));
assertEquals(1, names.getSize());
assertEquals("Butter", names.get(0));
}
@Test
public void test_remove_byPosition() {
assertThrows(IndexOutOfBoundsException.class, () ->
names.remove(0));
names.add("Potato");
names.add("Garlic");
names.add("Butter");
assertThrows(IndexOutOfBoundsException.class, () -> names.remove(-1));
assertThrows(IndexOutOfBoundsException.class, () -> names.remove(3));
names.remove(1);
assertEquals(2, names.getSize());
assertEquals("Potato", names.get(0));
assertEquals("Butter", names.get(1));
assertThrows(IndexOutOfBoundsException.class, () -> names.get(2));
names.remove(0);
assertEquals(1, names.getSize());
assertEquals("Butter", names.get(0));
assertThrows(IndexOutOfBoundsException.class, () -> names.get(1));
}
//TODO: make game (BASIC)
//TODO: go over project document
//TODO: add sound if still alive and on this earth
@Test
public void test_iterator() {
Iterator<String> it = names.iterator();
assertFalse(it.hasNext());
assertThrows(NoSuchElementException.class, () -> it.next());
names.add("Pat");
names.add("Ruby");
names.add("Tara");
assertTrue(it.hasNext());
assertEquals("Pat", it.next());
it.next();
assertEquals("Tara", it.next());
assertEquals("Pat", it.next());
}
}