-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayListTest.java
executable file
·221 lines (196 loc) · 4.91 KB
/
ArrayListTest.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
import static org.junit.Assert.*;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.*;
import org.junit.Rule;
/**
* Test class for ArrayList.
*/
public class ArrayListTest {
@Rule
public final ExpectedException exception = ExpectedException.none();
@Test
public void testArrayList() {
ArrayList<String> list = new ArrayList<String>();
list.add("hello");
list.add("welcome");
assertTrue(list.contains("hello"));
assertTrue(list.get(0).equals("hello"));
assertTrue(list.size() == 2);
ArrayList<String> test2 = new ArrayList<String>(list);
test2.remove(0);
assertTrue(list.size() == 2 && test2.size() == 1);
}
@Test
public void testArrayListInt() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(2);
assertTrue(test.size() == 1);
assertTrue(test.get(0) == 2);
test.remove(0);
assertTrue(test.size() == 0);
}
@Ignore
@Test
public void testArrayListArrayListOfE() {
fail("Not yet implemented");
}
@Test
public void testAddE() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(100);
test.add(10);
test.add(0);
assertTrue(test.contains(0));
assertTrue(test.size() == 3);
}
@Test
public void testAddIntE() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(100);
test.add(0,10);
test.add(0,0);
assertTrue(test.get(0) == 0);
assertTrue(test.get(1) == 10);
exception.expect(IndexOutOfBoundsException.class);
test.add(100, 2);
}
@Test
public void testClear() {
ArrayList<String> test = new ArrayList<String>();
test.add("hello");
test.add("hi");
test.add("la");
assertTrue(test.size() == 3);
test.clear();
assertTrue(test.size() == 0);
ArrayList<String> test1 = new ArrayList<String>();
test1.clear();
assertTrue(test.size() == 0);
}
@Test
public void testClone() {
ArrayList<String> test = new ArrayList<String>();
test.add("hello");
test.add("hi");
test.add("la");
ArrayList<String> test2 = (ArrayList<String>) test.clone();
assertTrue(!test2.equals(test));
test2.add("w");
assertTrue(test2.size() != test.size());
}
@Test
public void testContains() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(1);
test.add(1000);
test.add(4);
assertTrue(test.contains(1));
}
@Test
public void testGet() {
ArrayList<String> test = new ArrayList<String>();
test.add("hi");
test.add("door");
test.add("cat");
test.add("zero");
assertTrue(test.get(2).equals("cat"));
}
@Test
public void testIndexOf() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(2);
test.add(10);
assertTrue(test.indexOf(2) == 1);
}
@Test
public void testIsEmpty() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(10);
test.remove(0);
assertTrue(test.size() == 0 && test.isEmpty());
}
@Test
public void testLastIndexOf() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(2);
test.add(0);
test.add(2);
assertTrue(test.lastIndexOf(2) == 3);
}
@Test
public void testRemoveIndex() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(1);
test.add(2);
test.add(4);
test.add(3);
test.remove(2);
assertTrue(test.contains(2));
}
@Test
public void testRemoveObject() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(1);
test.add(2);
test.add(4);
test.add(3);
test.remove(Integer.valueOf(3));
assertTrue(test.contains(4));
}
@Test
public void testSet() {
ArrayList<String> test = new ArrayList<String>();
test.add("hello");
test.add("hi");
test.add("what's up?");
test.add("zero");
test.set(2, "I changed this");
assertTrue(!test.contains("what's up?"));
}
@Test
public void testSize() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(2);
test.add(0);
test.add(100);
test.remove(0);
test.add(0, 10);
assertTrue(test.size() == 3);
}
@Test
public void testSubList() {
ArrayList<String> test = new ArrayList<String>();
test.add("hi");
test.add("zero");
test.add("hello");
test.add("we");
test.add("cat");
ArrayList<String> test2 = test.subList(1,3);
assertTrue(test2.contains("zero") && test2.contains("hello"));
assertTrue(test2.size() == 2);
}
@Test
public void testToArray() {
ArrayList<Integer> test = new ArrayList<Integer>();
test.add(0);
test.add(5);
test.add(6);
Object[] arr = test.toArray();
assertTrue(arr.length == 3);
}
@Ignore
@Test
public void testToString() {
fail("Not yet implemented");
}
@Test
public void testStress() {
ArrayList<Integer> test = new ArrayList<Integer>();
for(int i = 0; i < 1000000; i++) {
test.add(i);
}
}
}