-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoublyLinkedListTest.java
234 lines (211 loc) · 6.25 KB
/
DoublyLinkedListTest.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
import student.TestCase;
/**
* Tests for the {@link DoublyLinkedList} class.
*
* @author karl88
* @author sfbahr
* @version Sep 17, 2014
*/
public class DoublyLinkedListTest
extends TestCase
{
//~ Instance/static variables .............................................
private DoublyLinkedList<String> list;
//~ Constructor ...........................................................
// ----------------------------------------------------------
/**
* Create a new test class
*/
public DoublyLinkedListTest()
{
// The constructor is usually empty in unit tests, since it runs
// once for the whole class, not once for each test method.
// Per-test initialization should be placed in setUp() instead.
}
//~ Public methods ........................................................
// ----------------------------------------------------------
/**
* Creates two brand new, empty sets for each test method.
*/
public void setUp()
{
list = new DoublyLinkedList<String>();
}
// ----------------------------------------------------------
/**
* Tests the getSize method.
*/
public void testGetSize()
{
assertEquals(list.getSize(), 0);
list.insertAfterCurrent("test1");
list.insertAfterCurrent("test3");
list.insertAfterCurrent("test2");
assertEquals(list.getSize(), 3);
}
/**
* Tests the insertAfterCurrent method.
*/
public void testInsertAfterCurrent()
{
assertEquals(list.getSize(), 0);
list.insertAfterCurrent("test1");
list.insertAfterCurrent("test3");
list.insertAfterCurrent("test2");
assertEquals(list.getSize(), 3);
}
/**
* Tests the insertBeforeCurrent method.
*/
public void testInsertBeforeCurrent()
{
assertEquals(list.getSize(), 0);
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
assertEquals(list.getSize(), 3);
}
/**
* Tests the getCurrent method.
*/
public void testGetCurrent()
{
assertNull(list.getCurrent());
list.insertAfterCurrent("test");
assertEquals("test", list.getCurrent());
}
/**
* Tests the moveToFront method.
*/
public void testMoveToFront()
{
list.moveToFront();
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
list.moveToFront();
assertEquals("test1", list.getCurrent());
list.moveToFront();
assertEquals("test1", list.getCurrent());
}
/**
* Tests the moveToBack method.
*/
public void testMoveToBack()
{
list.moveToBack();
list.insertAfterCurrent("test1");
list.insertAfterCurrent("test3");
list.insertAfterCurrent("test2");
list.moveToBack();
assertEquals("test3", list.getCurrent());
list.moveToBack();
assertEquals("test3", list.getCurrent());
}
/**
* Tests the moveToValue method.
*/
public void testMoveToValue()
{
assertFalse(list.moveToValue("test"));
list.insertAfterCurrent("test1");
list.insertAfterCurrent("test4");
list.insertAfterCurrent("test3");
list.insertAfterCurrent("test2");
list.moveToValue("test2");
assertEquals("test2", list.getCurrent());
list.moveToValue("invalid");
assertEquals("test4", list.getCurrent());
}
/**
* Tests the next method.
*/
public void testNext()
{
assertFalse(list.next());
list.insertAfterCurrent("test1");
list.insertAfterCurrent("test3");
list.insertAfterCurrent("test2");
assertTrue(list.next());
assertTrue(list.next());
assertFalse(list.next());
}
/**
* Tests the previous method.
*/
public void testPrevious()
{
assertFalse(list.previous());
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
assertTrue(list.previous());
assertTrue(list.previous());
assertFalse(list.previous());
}
/**
* Tests the removeAndNext method.
*/
public void testRemoveAndNext()
{
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
list.previous();
list.removeAndNext();
assertEquals("test3", list.getCurrent());
list.removeAndNext();
assertEquals("test1", list.getCurrent());
list.removeAndNext();
assertEquals(0, list.getSize());
assertNull(list.getCurrent());
list.removeAndNext();
assertEquals(0, list.getSize());
assertNull(list.getCurrent());
}
/**
* Tests the removeAndPrevious method.
*/
public void testRemoveAndPrevious()
{
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
list.previous();
list.removeAndPrevious();
assertEquals("test1", list.getCurrent());
list.removeAndPrevious();
assertEquals("test3", list.getCurrent());
list.removeAndPrevious();
assertEquals(0, list.getSize());
assertNull(list.getCurrent());
list.removeAndPrevious();
assertEquals(0, list.getSize());
assertNull(list.getCurrent());
}
/**
* Tests that clear() properly clears the list or does nothing if the list
* is clear.
*/
public void testClear()
{
assertEquals(0, list.getSize());
list.clear();
assertEquals(0, list.getSize());
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
list.insertBeforeCurrent("test3");
list.clear();
assertEquals(0, list.getSize());
}
/**
* Tests the toString method.
*/
public void testToString()
{
list.insertBeforeCurrent("test3");
list.insertBeforeCurrent("test1");
list.insertBeforeCurrent("test2");
assertEquals(list.toString(), "test1 -> test2 -> test3");
}
}