forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayListTest.java
More file actions
64 lines (53 loc) · 1.69 KB
/
ArrayListTest.java
File metadata and controls
64 lines (53 loc) · 1.69 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
package com.examplehub.datastructures.array;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
class ArrayListTest {
@Test
void testArrayList() {
ArrayList<String> arrayList = new ArrayList<>(10);
assertTrue(arrayList.isEmpty());
assertEquals(0, arrayList.size());
assertEquals("[]", arrayList.toString());
try {
arrayList.remove();
fail(); /* this should will not happen */
} catch (IndexOutOfBoundsException e) {
assertTrue(true); /* this should will happen */
}
for (int i = 1; i <= 5; ++i) {
assertTrue(arrayList.add(i + ""));
}
assertEquals("[1, 2, 3, 4, 5]", arrayList.toString());
arrayList.add(0, "0");
arrayList.add(4, "444");
assertEquals("[0, 1, 2, 3, 444, 4, 5]", arrayList.toString());
assertEquals("444", arrayList.remove(4));
assertEquals("0", arrayList.remove(0));
assertEquals(5, arrayList.size());
assertEquals("[1, 2, 3, 4, 5]", arrayList.toString());
arrayList.clear();
assertEquals(0, arrayList.size());
assertTrue(arrayList.isEmpty());
}
@Test
void testGrow() {
ArrayList<String> arrayList = new ArrayList<>(5);
for (int i = 1; i <= 7; i++) {
arrayList.add(i + "");
}
assertEquals(7, arrayList.size());
assertEquals("[1, 2, 3, 4, 5, 6, 7]", arrayList.toString());
}
@Test
void testRemove() {
ArrayList<String> arrayList = new ArrayList<>();
for (int i = 1; i <= 5; i++) {
arrayList.add("" + i);
}
arrayList.add("3");
arrayList.add(2, "3");
assertEquals("[1, 2, 3, 3, 4, 5, 3]", arrayList.toString());
arrayList.remove("3");
assertEquals("[1, 2, 4, 5]", arrayList.toString());
}
}