-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortableListTest.java
More file actions
328 lines (283 loc) · 9.16 KB
/
SortableListTest.java
File metadata and controls
328 lines (283 loc) · 9.16 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
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package edu.ics211.h04;
import static org.junit.Assert.assertEquals;
//import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Comparator;
import org.junit.Before;
import org.junit.Test;
/**
* SortableListTest to test the SortableList class.
*
* @author Caliana Fortin
*
*/
// https://github.com/cjalvarado/Course2Solutions/blob/master/Course2/src/textgen/MyLinkedListTester.java
public class SortableListTest {
SortableList<Integer> al;
SortableList<Integer> emptyList;
SortableList<Integer> longList;
SortableList<Integer> list;
SortableList<Integer> selectionSortList;
SortableList<Integer> insertionSortList;
private final int longListLength = 10;
Comparator<Integer> compare = new IntComparator();
/**
* IntComparator compares ints.
*
* @author Caliana Fortin
*
*/
private class IntComparator implements Comparator<Integer> {
@Override
public int compare(Integer v1, Integer v2) {
return v1 < v2 ? -1 : v1 > v2 ? +1 : 0;
}
}
/**
* Sets up allt he variables that are going to be used.
*
* @throws Exception if not filled.
*/
@Before
public void setUp() throws Exception {
emptyList = new SortableList<Integer>();
al = new SortableList<Integer>();
al.add(2);
al.add(1);
longList = new SortableList<Integer>();
for (int i = 0; i < longListLength; i++) {
longList.add(i);
}
list = new SortableList<Integer>(); // create a new sortableList object called list
list.add(1);
list.add(6);
list.add(8);
selectionSortList = new SortableList<Integer>();
selectionSortList.add(2);
selectionSortList.add(1);
insertionSortList = new SortableList<Integer>();
insertionSortList.add(3);
insertionSortList.add(7);
insertionSortList.add(0);
// compare = new Comparator(); don't know how to make a comparator to use
}
/**
* Test method for sortableList.
*/
@Test
public void testSortableList() {
new SortableList<Integer>();
}
/*
*
* @Test public void testMain() { fail("Not yet implemented"); }
*/
/**
* Test method for insertionSort.
*/
@Test
public void testInsertionSort() {
insertionSortList.insertionSort(compare); // can't get this to work but this should sort the
// sorList using insertion sort
// assertEquals(3, selectionSortList.size());
assertEquals(0, (int) insertionSortList.get(0)); // makes sure the items at index 0 is 0
assertEquals(3, (int) insertionSortList.get(1)); // makes sure the items at index 1 is 3
assertEquals(7, (int) insertionSortList.get(2));
}
/**
* Test method for bubbleSort.
*/
@Test
public void testBubbleSort() {
insertionSortList.bubbleSort(compare); // can't get this to work but
// this should sort the sorList using bubble sort
// assertEquals(2, selectionSortList.size());
assertEquals(0, (int) insertionSortList.get(0)); // makes sure the items at index 0 is 1
assertEquals(3, (int) insertionSortList.get(1)); // makes sure the items at index 1 is 2
assertEquals(7, (int) insertionSortList.get(2));
}
/**
* Test method for selectionSort.
*/
@Test
public void testSelectionSort() {
selectionSortList.selectionSort(compare);// can't get this to work but
// this should sort the sorList using selection sort
assertEquals(2, selectionSortList.size());
assertEquals(1, (int) selectionSortList.get(0)); // makes sure the items at index 0 is 1
assertEquals(2, (int) selectionSortList.get(1)); // makes sure the items at index 1 is 2
}
/**
* Test method for the number of swaps.
*/
@Test
public void testGetNumberOfSwaps() {
// number of swaps for al should be 1
assertEquals(1, selectionSortList.getNumberOfSwaps());
assertEquals(1, insertionSortList.getNumberOfSwaps());
}
/**
* Test method for getNumberOfComparisions.
*/
@Test
public void testGetNumberOfComparisons() {
// number of comparisions should be 2
assertEquals(selectionSortList.size(), selectionSortList.getNumberOfComparisons());
// number of comparisions is equal to the size()
}
/**
* Test method for getsortTime.
*/
@Test
public void testGetSortTime() {
// the sort time should equal the length
al.getSortTime();
// int expectedOutput = al.size();
// assertEquals(expectedOutput, al.getSortTime());
// make sure the sortTime is the expected time
}
/**
* Test method for get.
*/
@Test
public void testGet() {
try {
emptyList.get(0); // get an item that doesn't exist
fail("Check out of bound");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
// int expectedOutput = 2;
assertEquals(Integer.valueOf(2), al.get(0));
assertEquals(Integer.valueOf(1), al.get(1));
try {
al.get(-1); // get an item that is less than size
fail("Check out of bound");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
try {
al.get(2); // get an item that is larger than size
fail("Check out of bound");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
try {
longList.get(longListLength); // get an item that is at size
fail("Check out of bound");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
}
/**
* Test method for set.
*/
@Test
public void testSet() {
assertEquals(2, al.size());// al size should be at 2
try {
al.set(-1, 3); // tries to set an item at index -1
fail("Set Check out of bounds");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
try {
al.set(3, 3);
fail("Set Check out of bounds"); // tries to set an iem at a larger index
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
al.set(0, 3); // sets valid item at valid index
assertEquals("set: check al contents ", Integer.valueOf(3), al.get(0));
assertEquals("set: check al contents ", Integer.valueOf(1), al.get(1));
assertEquals("set: check size", 2, al.size());
al.set(0, 2); // restore
}
/**
* Test method for indexOf.
*/
@Test
public void testIndexOf() {
assertEquals(2, al.size());
assertEquals(-1, list.indexOf(7)); // no 7 should return -1
assertEquals("set: check al contents ", Integer.valueOf(1), list.get(0));
// Object obj = 1;
int index = 0;
assertEquals(index, list.indexOf(Integer.valueOf(1))); // should return 0 can't get this to work
}
/**
* Test method for size.
*/
@Test
public void testSize() {
assertEquals(2, al.size()); // make sure the size of al is 2
assertEquals(10, longList.size()); // make sure the size of longList is 10
assertEquals(0, emptyList.size()); // make Sure the size of emptyList is 0
}
/**
* Test method for addE.
*/
@Test
public void testAddE() {
al.add(3); // add the element
assertEquals("AddEnd: check al contents ", Integer.valueOf(2), al.get(0));
// at 0 stayed the same
assertEquals("AddEnd: check al contents ", Integer.valueOf(1), al.get(1)); // make sure the
// value at 1 stayed the same
assertEquals("AddEnd: check al contents ", Integer.valueOf(3), al.get(2)); // make sure
// the value added is at index 2 is 2
al.remove(2); // remove 3
}
/**
* Test method for addIntE.
*/
@Test
public void testAddIntE() {
al.add(1, 5);
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(2), al.get(0));
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(5), al.get(1));// makes sure
// the value is added correctly
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(1), al.get(2));
al.remove(1);// remove 5
al.add(0, 1);
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(1), al.get(0));// makes sure the
// value is added correctly
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(2), al.get(1));
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(1), al.get(2));
al.remove(0);// remove 1
// add to invalid location
try {
al.add(-1, 7);
fail("AddAtIndex Check out of bounds");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
try {
al.add(3, 8);
fail("AddAtIndex Check out of bounds");
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
try {
al.add(1, null);
fail("AddAtIndex null insertion");
} catch (NullPointerException e) {
System.out.println("Index out of bounds.");
}
}
/**
* Test method for remove.
*/
@Test
public void testRemove() {
assertEquals(3, list.size());
assertEquals("AddAtIndex: check al contents ", Integer.valueOf(1), list.get(0));
assertEquals("remove: check al contents ", Integer.valueOf(8), list.remove(2));
list.add(8); // restores 8 that was removed
try {
list.remove(3);
} catch (IndexOutOfBoundsException e) {
System.out.println("Index out of bounds.");
}
}
}