|
@@ -1,2 +1,232 @@
|
|
1
|
+import org.junit.Assert;
|
|
2
|
+import org.junit.Test;
|
|
3
|
+
|
|
4
|
+import java.lang.reflect.Array;
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+
|
1
|
7
|
public class MyArrayListTest {
|
|
8
|
+
|
|
9
|
+ MyArrayList<Integer> testArray = new MyArrayList<>();
|
|
10
|
+
|
|
11
|
+ public void populateArray() {
|
|
12
|
+ testArray.add(10);
|
|
13
|
+ testArray.add(3);
|
|
14
|
+ testArray.add(9);
|
|
15
|
+ testArray.add(2);
|
|
16
|
+ }
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+ @Test
|
|
20
|
+ public void test(){
|
|
21
|
+ Integer[] i = new Integer[0];
|
|
22
|
+ System.out.println(i.getClass());
|
|
23
|
+
|
|
24
|
+ Object[] objects = new Object[0];
|
|
25
|
+ System.out.println(objects.getClass());
|
|
26
|
+
|
|
27
|
+ populateArray();
|
|
28
|
+ System.out.println(testArray.get(0).getClass());
|
|
29
|
+
|
|
30
|
+ MyArrayList<String> testStringArray = new MyArrayList<>();
|
|
31
|
+ testStringArray.add("Meow");
|
|
32
|
+ System.out.println(testStringArray.get(0).getClass());
|
|
33
|
+ // System.out.println(testStringArray.getOriginalArray()[0]);
|
|
34
|
+ }
|
|
35
|
+
|
|
36
|
+ @Test
|
|
37
|
+ public void initiateSizeOnConstruction() {
|
|
38
|
+ MyArrayList<Integer> testArray1 = new MyArrayList<>(5);
|
|
39
|
+
|
|
40
|
+ int expected = 5;
|
|
41
|
+ int actual = testArray1.size();
|
|
42
|
+
|
|
43
|
+ Assert.assertEquals(expected, actual);
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ @Test
|
|
47
|
+ public void testAdd() {
|
|
48
|
+ testArray.add(5);
|
|
49
|
+
|
|
50
|
+ int expected = 5;
|
|
51
|
+ int actual = testArray.get(0);
|
|
52
|
+
|
|
53
|
+ Assert.assertEquals(expected, actual);
|
|
54
|
+ }
|
|
55
|
+
|
|
56
|
+ @Test
|
|
57
|
+ public void testAdd_Size() {
|
|
58
|
+ testArray.add(5);
|
|
59
|
+
|
|
60
|
+ int expected = 1;
|
|
61
|
+ int actual = testArray.size();
|
|
62
|
+
|
|
63
|
+ Assert.assertEquals(expected, actual);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ @Test
|
|
67
|
+ public void testAdd_Multiple() {
|
|
68
|
+ populateArray();
|
|
69
|
+
|
|
70
|
+ int expected = 9;
|
|
71
|
+ int actual = testArray.get(2);
|
|
72
|
+
|
|
73
|
+ Assert.assertEquals(expected, actual);
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ @Test
|
|
77
|
+ public void testAdd_Multiple_String() {
|
|
78
|
+ MyArrayList<String> testStringArray = new MyArrayList<>();
|
|
79
|
+ testStringArray.add("Jenn");
|
|
80
|
+ testStringArray.add("Ned");
|
|
81
|
+
|
|
82
|
+ String expected = "Ned";
|
|
83
|
+ String actual = testStringArray.get(1);
|
|
84
|
+
|
|
85
|
+ Assert.assertEquals(expected, actual);
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ @Test
|
|
89
|
+ public void testAdd_Multiple_String_Size() {
|
|
90
|
+ MyArrayList<String> testStringArray = new MyArrayList<>();
|
|
91
|
+ testStringArray.add("Jenn");
|
|
92
|
+ testStringArray.add("Ned");
|
|
93
|
+ testStringArray.add("Thulasi");
|
|
94
|
+
|
|
95
|
+ int expected = 3;
|
|
96
|
+ int actual = testStringArray.size();
|
|
97
|
+
|
|
98
|
+ Assert.assertEquals(expected, actual);
|
|
99
|
+ }
|
|
100
|
+
|
|
101
|
+ @Test
|
|
102
|
+ public void testAdd_Multiple_Size() {
|
|
103
|
+ populateArray();
|
|
104
|
+
|
|
105
|
+ int expected = 4;
|
|
106
|
+ int actual = testArray.size();
|
|
107
|
+
|
|
108
|
+ Assert.assertEquals(expected, actual);
|
|
109
|
+ }
|
|
110
|
+
|
|
111
|
+ @Test
|
|
112
|
+ public void testAdd_AtIndex() {
|
|
113
|
+ populateArray();
|
|
114
|
+ testArray.add(5, 2);
|
|
115
|
+
|
|
116
|
+ int expected = 5;
|
|
117
|
+ int actual = testArray.get(2);
|
|
118
|
+
|
|
119
|
+ Assert.assertEquals(expected, actual);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ @Test
|
|
123
|
+ public void testAdd_AtIndex_Size() {
|
|
124
|
+ populateArray();
|
|
125
|
+ testArray.add(5, 2);
|
|
126
|
+
|
|
127
|
+ int expected = 5;
|
|
128
|
+ int actual = testArray.size();
|
|
129
|
+
|
|
130
|
+ Assert.assertEquals(expected, actual);
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ @Test
|
|
134
|
+ public void testRemove_First() {
|
|
135
|
+ populateArray();
|
|
136
|
+ testArray.remove(10);
|
|
137
|
+
|
|
138
|
+ int unexpected = 10;
|
|
139
|
+ int actual = testArray.get(0);
|
|
140
|
+
|
|
141
|
+ Assert.assertNotEquals(unexpected, actual);
|
|
142
|
+ }
|
|
143
|
+
|
|
144
|
+ @Test
|
|
145
|
+ public void testRemove_Middle() {
|
|
146
|
+ populateArray();
|
|
147
|
+ testArray.remove(9);
|
|
148
|
+
|
|
149
|
+ int expected = 2;
|
|
150
|
+ int actual = testArray.get(2);
|
|
151
|
+
|
|
152
|
+ Assert.assertEquals(expected, actual);
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ @Test
|
|
156
|
+ public void testRemove_Size() {
|
|
157
|
+ populateArray();
|
|
158
|
+ testArray.remove(2);
|
|
159
|
+
|
|
160
|
+ int expected = 3;
|
|
161
|
+ int actual = testArray.size();
|
|
162
|
+
|
|
163
|
+ Assert.assertEquals(expected, actual);
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ @Test
|
|
167
|
+ public void testIsEmpty_True() {
|
|
168
|
+ Assert.assertTrue(testArray.isEmpty());
|
|
169
|
+ }
|
|
170
|
+
|
|
171
|
+ @Test
|
|
172
|
+ public void testIsEmpty_False() {
|
|
173
|
+ populateArray();
|
|
174
|
+ Assert.assertFalse(testArray.isEmpty());
|
|
175
|
+ }
|
|
176
|
+
|
|
177
|
+ @Test
|
|
178
|
+ public void testContains_True() {
|
|
179
|
+ populateArray();
|
|
180
|
+ Assert.assertTrue(testArray.contains(9));
|
|
181
|
+ }
|
|
182
|
+
|
|
183
|
+ @Test
|
|
184
|
+ public void testContains_False() {
|
|
185
|
+ populateArray();
|
|
186
|
+ Assert.assertFalse(testArray.contains(5));
|
|
187
|
+ }
|
|
188
|
+
|
|
189
|
+ @Test
|
|
190
|
+ public void testSet() {
|
|
191
|
+ populateArray();
|
|
192
|
+ testArray.set(2, 55);
|
|
193
|
+
|
|
194
|
+ int expected = 55;
|
|
195
|
+ int actual = testArray.get(2);
|
|
196
|
+
|
|
197
|
+ Assert.assertEquals(expected, actual);
|
|
198
|
+ }
|
|
199
|
+
|
|
200
|
+ @Test
|
|
201
|
+ public void testSet_Again() {
|
|
202
|
+ populateArray();
|
|
203
|
+
|
|
204
|
+ testArray.set(0, 11);
|
|
205
|
+
|
|
206
|
+ int expected = 11;
|
|
207
|
+ int actual = testArray.get(0);
|
|
208
|
+
|
|
209
|
+ Assert.assertEquals(expected, actual);
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ @Test(expected = IndexOutOfBoundsException.class)
|
|
213
|
+ public void testClear() {
|
|
214
|
+ populateArray();
|
|
215
|
+ testArray.clear();
|
|
216
|
+
|
|
217
|
+ testArray.get(0);
|
|
218
|
+ }
|
|
219
|
+
|
|
220
|
+ @Test
|
|
221
|
+ public void testClear_Size() {
|
|
222
|
+ populateArray();
|
|
223
|
+ testArray.clear();
|
|
224
|
+
|
|
225
|
+ int expected = 0;
|
|
226
|
+ int actual = testArray.size();
|
|
227
|
+
|
|
228
|
+ Assert.assertEquals(expected, actual);
|
|
229
|
+ }
|
|
230
|
+
|
|
231
|
+
|
2
|
232
|
}
|