Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
298 lines (257 loc) · 6.63 KB

File metadata and controls

298 lines (257 loc) · 6.63 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.hamcrest.CoreMatchers.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import Array.MyArrayList;
import org.junit.Before;
import org.junit.Test;
// import org.junit.jupiter.api.Test;
/**
* @author downey
*
*/
public class MyArrayListTest {
protected List<Integer> mylist;
protected List<Integer> list;
/**
* @throws Exception
*/
@Before
public void setUp() throws Exception {
list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
mylist = new MyArrayList<Integer>();
mylist.addAll(list);
}
/**
* Test method for {@link MyArrayList#MyArrayList()}.
*/
@Test
public void testMyList() {
assertThat(mylist.size(), is(3));
}
/**
* Test method for {@link MyArrayList#add(Object)}.
*/
@Test
public void testAddT() {
for (int i = 4; i < 20; i++) {
mylist.add(i);
}
//System.out.println(Arrays.toString(mal.toArray()));
assertThat(mylist.get(18), is(19));
}
/**
* Test method for {@link MyArrayList#add(int, Object)}.
*/
@Test
public void testAddIntT() {
mylist.add(1, 5);
//System.out.println(Arrays.toString(mal.toArray()));
assertThat(mylist.get(1), is(5));
assertThat(mylist.size(), is(4));
try {
mylist.set(-1, 0);
fail();
} catch (IndexOutOfBoundsException e) {} // good
try {
mylist.set(4, 0);
fail();
} catch (IndexOutOfBoundsException e) {} // good
mylist.add(0, 6);
//System.out.println(Arrays.toString(mal.toArray()));
assertThat(mylist.get(0), is(6));
mylist.add(5, 7);
//System.out.println(Arrays.toString(mal.toArray()));
assertThat(mylist.get(5), is(7));
}
/**
* Test method for {@link MyArrayList#addAll(java.util.Collection)}.
*/
@Test
public void testAddAllCollectionOfQextendsT() {
mylist.addAll(list);
mylist.addAll(list);
mylist.addAll(list);
assertThat(mylist.size(), is(12));
assertThat(mylist.get(5), is(3));
}
/**
* Test method for {@link MyArrayList#clear()}.
*/
@Test
public void testClear() {
mylist.clear();
assertThat(mylist.size(), is(0));
}
/**
* Test method for {@link MyArrayList#contains(Object)}.
*/
@Test
public void testContains() {
assertThat(mylist.contains(1), equalTo(true));
assertThat(mylist.contains(4), equalTo(false));
assertThat(mylist.contains(null), equalTo(false));
mylist.add(null);
assertThat(mylist.contains(null), equalTo(true));
}
/**
* Test method for {@link MyArrayList#containsAll(java.util.Collection)}.
*/
@Test
public void testContainsAll() {
assertThat(mylist.containsAll(list), equalTo(true));
}
/**
* Test method for {@link MyArrayList#get(int)}.
*/
@Test
public void testGet() {
assertThat(mylist.get(1), is(2));
}
/**
* Test method for {@link MyArrayList#indexOf(Object)}.
*/
@Test
public void testIndexOf() {
assertThat(mylist.indexOf(1), is(0));
assertThat(mylist.indexOf(2), is(1));
assertThat(mylist.indexOf(3), is(2));
assertThat(mylist.indexOf(4), is(-1));
}
/**
* Test method for {@link MyArrayList#indexOf(Object)}.
*/
@Test
public void testIndexOfNull() {
assertThat(mylist.indexOf(null), is(-1));
mylist.add(null);
assertThat(mylist.indexOf(null), is(3));
}
/**
* Test method for {@link MyArrayList#isEmpty()}.
*/
@Test
public void testIsEmpty() {
assertThat(mylist.isEmpty(), equalTo(false));
mylist.clear();
assertThat(mylist.isEmpty(), equalTo(true));
}
/**
* Test method for {@link MyArrayList#iterator()}.
*/
@Test
public void testIterator() {
Iterator<Integer> iter = mylist.iterator();
assertThat(iter.next(), is(new Integer(1)));
assertThat(iter.next(), is(new Integer(2)));
assertThat(iter.next(), is(new Integer(3)));
assertThat(iter.hasNext(), equalTo(false));
}
/**
* Test method for {@link MyArrayList#lastIndexOf(Object)}.
*/
@Test
public void testLastIndexOf() {
mylist.add(2);
assertThat(mylist.lastIndexOf(new Integer(2)), is(3));
}
/**
* Test method for {@link MyArrayList#remove(Object)}.
*/
@Test
public void testRemoveObject() {
boolean flag = mylist.remove(new Integer(2));
assertThat(flag, equalTo(true));
assertThat(mylist.size(), is(2));
assertThat(mylist.get(1), is(new Integer(3)));
//System.out.println(Arrays.toString(mal.toArray()));
flag = mylist.remove(new Integer(1));
assertThat(flag, equalTo(true));
assertThat(mylist.size(), is(1));
assertThat(mylist.get(0), is(new Integer(3)));
//System.out.println(Arrays.toString(mal.toArray()));
flag = mylist.remove(new Integer(5));
assertThat(flag, equalTo(false));
assertThat(mylist.size(), is(1));
assertThat(mylist.get(0), is(new Integer(3)));
//System.out.println(Arrays.toString(mal.toArray()));
flag = mylist.remove(new Integer(3));
assertThat(flag, equalTo(true));
assertThat(mylist.size(), is(0));
//System.out.println(Arrays.toString(mal.toArray()));
}
/**
* Test method for {@link MyArrayList#remove(int)}.
*/
@Test
public void testRemoveInt() {
Integer val = mylist.remove(1);
assertThat(val, is(new Integer(2)));
assertThat(mylist.size(), is(2));
assertThat(mylist.get(1), is(new Integer(3)));
}
/**
* Test method for {@link MyArrayList#removeAll(java.util.Collection)}.
*/
@Test
public void testRemoveAll() {
mylist.removeAll(list);
assertThat(mylist.size(), is(0));
}
/**
* Test method for {@link MyArrayList#set(int, Object)}.
*/
@Test
public void testSet() {
Integer val = mylist.set(1, 5);
assertThat(val, is(new Integer(2)));
val = mylist.set(0, 6);
assertThat(val, is(new Integer(1)));
val = mylist.set(2, 7);
assertThat(val, is(new Integer(3)));
// return value should be 2
// list should be [6, 5, 7]
assertThat(mylist.get(0), is(new Integer(6)));
assertThat(mylist.get(1), is(new Integer(5)));
assertThat(mylist.get(2), is(new Integer(7)));
//System.out.println(Arrays.toString(mal.toArray()));
try {
mylist.set(-1, 0);
fail();
} catch (IndexOutOfBoundsException e) {} // good
try {
mylist.set(4, 0);
fail();
} catch (IndexOutOfBoundsException e) {} // good
}
/**
* Test method for {@link MyArrayList#size()}.
*/
@Test
public void testSize() {
assertThat(mylist.size(), is(3));
}
/**
* Test method for {@link MyArrayList#subList(int, int)}.
*/
@Test
public void testSubList() {
mylist.addAll(list);
List<Integer> sub = mylist.subList(1, 4);
assertThat(sub.get(1), is(new Integer(3)));
}
/**
* Test method for {@link MyArrayList#toArray()}.
*/
@Test
public void testToArray() {
Object[] array = mylist.toArray();
assertThat((Integer)array[0], is(new Integer(1)));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.