-
-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy pathIntListTest.java
More file actions
492 lines (443 loc) · 16.9 KB
/
IntListTest.java
File metadata and controls
492 lines (443 loc) · 16.9 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package processing.data;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.*;
/**
* IntList.java has two fields:
* 1. count - the number of elements currently stored in the list with an initial value of 0.
* 2. int[] data - An array to store elements with an initial capacity of 10.
*/
public class IntListTest {
@Test
public void testDefaultConstructor() {
IntList testedList = new IntList();
assertEquals(0, testedList.size());
assertEquals(10, testedList.data.length);
}
@Test
public void testConstructorWithLength() {
IntList testedList = new IntList(20);
assertEquals(0, testedList.size());
assertEquals(20, testedList.data.length);
}
@Test
public void testConstructorWithArray() {
int[] source = {1, 2};
IntList testedList = new IntList(source);
assertEquals(2, testedList.size());
assertEquals(2, testedList.data.length);
assertEquals(1, testedList.get(0));
assertEquals(2, testedList.get(1));
}
@Test
public void testConstructorWithIterableObject() {
List<Object> source = new ArrayList<>(Arrays.asList(1, "2", null, 4.5, -1));
IntList testedList = new IntList(source);
assertEquals(5, testedList.size());
int[] expected = {1, 2, 0, 4, -1};
assertArrayEquals(expected, testedList.values());
}
@Test
public void testConstructorWithObject() {
String eleStr = "Hello";
int eleInt = 10;
float eleFloat = 1.2f;
Object eleObj = new Object();
IntList testedList = new IntList(eleStr, eleInt, eleFloat, eleObj);
int[] expected = {0, 10, 1, 0};
assertArrayEquals(expected, testedList.values());
}
@Test
public void testFromRangeWithStopIndex() {
IntList originalList = new IntList(new int[]{5,10,15,20,25});
IntList result = originalList.fromRange(2);
assertArrayEquals(new int[]{0,1}, result.values());
}
@Test
public void testFromRangeWithStartAndStopIndex() {
IntList originalList = new IntList(new int[]{5,10,15,20,25});
IntList result = originalList.fromRange(1,3);
assertArrayEquals(new int[]{1,2}, result.values());
}
@Test
public void testClear() {
IntList testedList = new IntList(new int[]{1, 2, 3});
testedList.clear();
assertEquals(0, testedList.size());
}
@Test
public void testResize() {
IntList testedList = new IntList(new int[]{1, 2, 3});
testedList.resize(5);
assertEquals(5, testedList.size());
assertEquals(5, testedList.data.length);
}
@Test
public void testSet() {
IntList testedList = new IntList();
testedList.set(0, 20);
assertEquals(1, testedList.size());
assertEquals(20, testedList.get(0));
testedList.set(100, 2000);
assertEquals(101, testedList.size());
assertEquals(2000, testedList.get(100));
}
@Test
public void testPush() {
IntList testedList = new IntList();
testedList.push(100);
assertEquals(1, testedList.size());
assertEquals(100, testedList.get(0));
}
@Test
public void testAppendWithInt() {
IntList testedList = new IntList();
testedList.append(100);
assertEquals(1, testedList.size());
assertEquals(100, testedList.get(0));
}
@Test
public void testAppendWithIntArray() {
IntList testedList = new IntList();
int[] source = {10, 20, 30};
testedList.append(source);
assertArrayEquals(source, testedList.values());
}
@Test
public void testAppendWithIntList() {
IntList testedList = new IntList();
IntList source = new IntList(new int[]{10, 20, 30});
testedList.append(source);
assertArrayEquals(source.values(), testedList.values());
}
@Test
public void testAppendUnique() {
IntList testedList = new IntList(new int[]{10, 20, 30});
testedList.appendUnique(100);
assertArrayEquals(new int[]{10, 20, 30, 100}, testedList.values());
}
@Test
public void testPop() {
IntList testedList = new IntList(new int[]{10, 20});
assertEquals(20,testedList.pop());
assertEquals(1,testedList.size());
assertEquals(10,testedList.pop());
assertEquals(0,testedList.size());
}
@Test
public void testPopOnEmptyIntListThrowsException() {
IntList testedList = new IntList();
RuntimeException exception = assertThrows(RuntimeException.class, () -> {
testedList.pop();
});
assertEquals("Can't call pop() on an empty list", exception.getMessage());
}
@Test
public void testRemoveWithIndexGreaterThanSize() {
IntList testedList = new IntList();
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> testedList.remove(3));
}
@Test
public void testRemoveWithIndexLessThanZeroThrowsException() {
IntList testedList = new IntList();
assertThrows(ArrayIndexOutOfBoundsException.class,
() -> testedList.remove(-1));
}
@Test
public void testRemoveWithIndex() {
IntList testedList = new IntList(new int[]{1, 2, 3});
assertEquals(2,testedList.remove(1));
assertEquals(2,testedList.size());
assertArrayEquals(new int[]{1, 3},testedList.values());
}
@Test
public void testRemoveValue() {
IntList testedList = new IntList(new int[]{10, 20, 20});
assertEquals(1,testedList.removeValue(20));
assertEquals(-1,testedList.removeValue(100));
}
@Test
public void testRemoveValues() {
IntList testedList = new IntList(new int[]{10, 20, 20});
assertEquals(2,testedList.removeValues(20));
}
@Test
public void testInsertWithInvalidIndexThrowsException() {
IntList testedList = new IntList();
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
() -> testedList.insert(1, 20));
IllegalArgumentException negativeIndex = assertThrows(IllegalArgumentException.class,
() -> testedList.insert(-1, 20));
assertEquals("insert() index cannot be negative: it was -1", negativeIndex.getMessage());
}
@Test
public void testInsertWithValidIndex() {
IntList testedList = new IntList();
testedList.insert(0, 10);
assertEquals(10, testedList.get(0));
}
@Test
public void testInsertWithArray() {
IntList testedList = new IntList(new int[]{5, 5, 5});
int[] source = {100, 200};
testedList.insert(1, source);
int[] expectedList = new int[]{5, 100, 200, 5, 5};
assertArrayEquals(expectedList, testedList.values());
}
@Test
public void testInsertWithIntList() {
IntList testedList = new IntList(new int[]{5, 5, 5});
IntList source = new IntList(new int[]{100, 200});
testedList.insert(1, source);
int[] expectedList = new int[]{5, 100, 200, 5, 5};
assertArrayEquals(expectedList, testedList.values());
}
@Test
public void testIndex() {
IntList testedList = new IntList(new int[]{5, 5, 5});
assertEquals(0,testedList.index(5));
assertEquals(-1,testedList.index(10));
}
@Test
public void testHasValue() {
IntList testedList = new IntList(new int[]{1, 2, 3});
assertTrue(testedList.hasValue(3));
assertFalse(testedList.hasValue(100));
}
@Test
public void testIncrementWithIndex() {
IntList testedList = new IntList(new int[] {20});
testedList.increment(0);
assertEquals(21,testedList.get(0));
}
@Test
public void testIncrementWithIndexGreaterThanSize() {
IntList testedList = new IntList(new int[] {20});
testedList.increment(5);
assertEquals(1,testedList.get(5));
}
@Test
public void testAddWithValidIndex() {
IntList testedList = new IntList(new int[] {20});
testedList.add(0, 20);
assertEquals(40,testedList.get(0));
}
@Test
public void testAddWithInvalidIndexThrowsException() {
IntList testedList = new IntList(new int[] {20});
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.add(5, 20));
assertEquals("The list size is 1. You cannot add() to element 5.",exception.getMessage());
}
@Test
public void testSubWithValidIndex() {
IntList testedList = new IntList(new int[] {20});
testedList.sub(0, 20);
assertEquals(0,testedList.get(0));
}
@Test
public void testSubWithInvalidIndexThrowsException() {
IntList testedList = new IntList(new int[] {20});
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.sub(5, 20));
assertEquals("The list size is 1. You cannot sub() to element 5.",exception.getMessage());
}
@Test
public void testMultWithValidIndex() {
IntList testedList = new IntList(new int[] {20});
testedList.mult(0, 20);
assertEquals(400,testedList.get(0));
}
@Test
public void testMultWithInvalidIndexThrowsException() {
IntList testedList = new IntList(new int[] {20});
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.mult(5, 20));
assertEquals("The list size is 1. You cannot mult() to element 5.",exception.getMessage());
}
@Test
public void testDivWithValidIndex() {
IntList testedList = new IntList(new int[] {20});
testedList.div(0, 20);
assertEquals(1,testedList.get(0));
}
@Test
public void testDivWithInvalidIndexThrowsException() {
IntList testedList = new IntList(new int[] {20});
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.div(5, 20));
assertEquals("The list size is 1. You cannot div() to element 5.",exception.getMessage());
}
@Test
public void testMinWithValidIndex() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(-5, testedList.min());
}
@Test
public void testMinWithInvalidIndexThrowsException() {
IntList testedList = new IntList();
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.min());
assertEquals("Cannot use min() on an empty IntList.",exception.getMessage());
}
@Test
public void testMinIndexWithValidIndex() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(2, testedList.minIndex());
}
@Test
public void testMinIndexWithInvalidIndexThrowsException() {
IntList testedList = new IntList();
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.minIndex());
assertEquals("Cannot use minIndex() on an empty IntList.",exception.getMessage());
}
@Test
public void testMaxWithValidIndex() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(20, testedList.max());
}
@Test
public void testMaxWithInvalidIndexThrowsException() {
IntList testedList = new IntList();
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.max());
assertEquals("Cannot use max() on an empty IntList.",exception.getMessage());
}
@Test
public void testMaxIndexWithValidIndex() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(0, testedList.maxIndex());
}
@Test
public void testMaxIndexWithInvalidIndexThrowsException() {
IntList testedList = new IntList();
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.maxIndex());
assertEquals("Cannot use maxIndex() on an empty IntList.",exception.getMessage());
}
@Test
public void testSumLong() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(25, testedList.sumLong());
}
@Test
public void testSumWithValidIntegerValue() {
IntList testedList = new IntList(new int[] {20, 10, -5});
assertEquals(25, testedList.sum());
}
@Test
public void testSumGreaterThanMaxIntegerValueThrowsException() {
int value = Integer.MAX_VALUE;
IntList testedList = new IntList(new int[] {value, 1});
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.sum());
assertEquals("sum() exceeds 2147483647, use sumLong()",exception.getMessage());
}
@Test
public void testSumLessThanMinIntegerValueThrowsException() {
int value = Integer.MIN_VALUE;
IntList testedList = new IntList(new int[] {value, -1});
RuntimeException exception = assertThrows(RuntimeException.class,
()->testedList.sum());
assertEquals("sum() less than -2147483648, use sumLong()",exception.getMessage());
}
@Test
public void testSort() {
IntList testedList = new IntList(new int[] {20, 10, -5});
testedList.sort();
assertArrayEquals(new int[]{-5,10,20}, testedList.values());
}
@Test
public void testSortReverse() {
IntList testedList = new IntList(new int[] {20, 10, 100});
testedList.sortReverse();
assertArrayEquals(new int[]{100,20,10}, testedList.values());
}
@Test
public void testReverse() {
IntList testedList = new IntList(new int[] {20, 10, 100});
testedList.reverse();
assertArrayEquals(new int[]{100,10,20}, testedList.values());
}
@Test
public void testChoice() {
IntList testedList = new IntList(new int[] {20, 10, 100});
int num = testedList.choice();
assertTrue(testedList.hasValue(num));
}
@Test
public void testChoiceOnEmptyIntListThrowsException() {
IntList testedList = new IntList();
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.choice());
assertEquals("No entries in this IntList", exception.getMessage());
}
@Test
public void testRemoveChoice() {
IntList testedList = new IntList(new int[] {20, 10, 100});
int num = testedList.removeChoice();
assertEquals(2, testedList.size());
}
@Test
public void testRemoveChoiceOnEmptyIntListThrowsException() {
IntList testedList = new IntList();
ArrayIndexOutOfBoundsException exception = assertThrows(ArrayIndexOutOfBoundsException.class,
()->testedList.removeChoice());
assertEquals("No entries in this IntList", exception.getMessage());
}
@Test
public void testCopy() {
IntList originalList = new IntList(new int[] {20, 10, 100});
IntList copyList = originalList.copy();
assertArrayEquals(originalList.values(), copyList.values());
assertEquals(originalList.size(), copyList.size());
}
@Test
public void testToArray() {
IntList originalList = new IntList(new int[] {20, 10, 100});
int[] result = originalList.toArray();
assertArrayEquals(new int[] {20, 10, 100}, result);
}
@Test
public void testToArrayWithDestinationArray() {
IntList originalList = new IntList(new int[] {20, 10, 100});
int[] oversize = new int[100];
int[] result = originalList.toArray(oversize);
assertArrayEquals(new int[] {20, 10, 100}, result);
assertEquals(3, result.length);
}
@Test
public void testToArrayWithDestinationArrayNull() {
IntList originalList = new IntList(new int[] {20, 10, 100});
int[] initialIsNull = null;
int[] result = originalList.toArray(initialIsNull);
assertArrayEquals(new int[] {20, 10, 100}, result);
assertEquals(3, result.length);
}
@Test
public void testGetPercent() {
IntList originalList = new IntList(new int[] {5, 10, 5});
FloatList result = originalList.getPercent();
assertArrayEquals(new float[] {0.25f, 0.5f, 0.25f}, result.values(), 1e-6f);
}
@Test
public void testGetSubset() {
IntList originalList = new IntList(new int[] {5, 10, 20});
IntList result = originalList.getSubset(1);
assertArrayEquals(new int[]{10, 20}, result.values());
}
@Test
public void testJoin() {
IntList originalList = new IntList(new int[] {5, 10});
assertEquals("5&10", originalList.join("&"));
}
@Test
public void testJoinWithEmptyList() {
IntList originalList = new IntList();
assertEquals("", originalList.join("&"));
}
}