-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathDynamicArrayTest.java
258 lines (210 loc) · 6.93 KB
/
DynamicArrayTest.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
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
package com.thealgorithms.datastructures.dynamicarray;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class DynamicArrayTest {
private DynamicArray<String> array;
@BeforeEach
public void setUp() {
array = new DynamicArray<>();
}
@Test
public void testGetElement() {
array.add("Alice");
array.add("Bob");
array.add("Charlie");
array.add("David");
assertEquals("Bob", array.get(1));
}
@Test
public void testGetInvalidIndex() {
assertThrows(IndexOutOfBoundsException.class, () -> array.get(-1));
assertThrows(IndexOutOfBoundsException.class, () -> array.get(10));
assertThrows(IndexOutOfBoundsException.class, () -> array.get(100));
}
@Test
public void testAddElement() {
array.add("Alice");
array.add("Bob");
assertEquals(2, array.getSize());
assertEquals("Alice", array.get(0));
assertEquals("Bob", array.get(1));
}
@Test
public void testAddAndGet() {
array.add("Alice");
array.add("Bob");
assertEquals("Alice", array.get(0));
assertEquals("Bob", array.get(1));
assertThrows(IndexOutOfBoundsException.class, () -> array.get(2));
}
@Test
public void testAddBeyondCapacity() {
for (int i = 0; i < 20; i++) {
array.add("Element " + i);
}
assertEquals(20, array.getSize());
assertEquals("Element 19", array.get(19));
}
@Test
public void testPutElement() {
array.put(5, "Placeholder");
assertEquals(6, array.getSize());
assertEquals("Placeholder", array.get(5));
}
@Test
public void testPutElementBeyondCapacity() {
array.put(20, "FarAway");
assertEquals(21, array.getSize());
assertEquals("FarAway", array.get(20));
}
@Test
public void testPutAndDynamicCapacity() {
array.put(0, "Alice");
array.put(2, "Bob"); // Tests capacity expansion
assertEquals("Alice", array.get(0));
assertEquals("Bob", array.get(2));
assertEquals(3, array.getSize()); // Size should be 3 due to index 2
}
@Test
public void testRemoveElement() {
array.add("Alice");
array.add("Bob");
String removed = array.remove(0);
assertEquals("Alice", removed);
assertEquals(1, array.getSize());
assertEquals("Bob", array.get(0));
}
@Test
public void testRemoveInvalidIndex() {
assertThrows(IndexOutOfBoundsException.class, () -> array.remove(-1));
assertThrows(IndexOutOfBoundsException.class, () -> array.remove(10));
}
@Test
public void testRemoveComplex() {
array.add("Alice");
array.add("Bob");
array.add("Charlie");
assertEquals("Bob", array.remove(1));
assertEquals("Alice", array.get(0));
assertEquals("Charlie", array.get(1));
assertThrows(IndexOutOfBoundsException.class, () -> array.remove(2));
}
@Test
public void testRemoveEdgeCases() {
array.add("Alice");
array.add("Bob");
assertEquals("Alice", array.remove(0));
assertEquals(1, array.getSize());
assertEquals("Bob", array.get(0));
assertEquals("Bob", array.remove(0));
assertTrue(array.isEmpty());
assertThrows(IndexOutOfBoundsException.class, () -> array.get(0));
}
@Test
public void testIsEmpty() {
assertTrue(array.isEmpty());
array.add("Alice");
assertFalse(array.isEmpty());
array.remove(0);
assertTrue(array.isEmpty());
}
@Test
public void testSize() {
DynamicArray<String> array = new DynamicArray<>();
assertEquals(0, array.getSize());
array.add("Alice");
array.add("Bob");
assertEquals(2, array.getSize());
array.remove(0);
assertEquals(1, array.getSize());
}
@Test
public void testToString() {
array.add("Alice");
array.add("Bob");
assertEquals("[Alice, Bob]", array.toString());
}
@Test
public void testIterator() {
array.add("Alice");
array.add("Bob");
String result = array.stream().collect(Collectors.joining(", "));
assertEquals("Alice, Bob", result);
}
@Test
public void testStreamAsString() {
array.add("Alice");
array.add("Bob");
String result = array.stream().collect(Collectors.joining(", "));
assertEquals("Alice, Bob", result);
}
@Test
public void testStream() {
array.add("Alice");
array.add("Bob");
long count = array.stream().count();
assertEquals(2, count);
}
@Test
public void testAddToFullCapacity() {
DynamicArray<String> array = new DynamicArray<>(2);
array.add("Alice");
array.add("Bob");
array.add("Charlie"); // Triggers capacity expansion
assertEquals(3, array.getSize());
assertEquals("Charlie", array.get(2));
}
@Test
public void testPutWithNegativeIndex() {
assertThrows(IndexOutOfBoundsException.class, () -> array.put(-1, "Alice"));
}
@Test
public void testGetWithNegativeIndex() {
assertThrows(IndexOutOfBoundsException.class, () -> array.get(-1));
}
@Test
public void testIteratorConcurrentModification() {
array.add("Alice");
array.add("Bob");
Iterator<String> iterator = array.iterator();
array.add("Charlie"); // Modify during iteration
assertThrows(ConcurrentModificationException.class, iterator::next);
}
@Test
public void testIteratorRemove() {
array.add("Alice");
array.add("Bob");
Iterator<String> iterator = array.iterator();
assertEquals("Alice", iterator.next());
iterator.remove();
assertEquals(1, array.getSize());
assertEquals("Bob", array.get(0));
}
@Test
public void testRemoveBeyondCapacity() {
DynamicArray<String> array = new DynamicArray<>(2);
array.add("Alice");
array.add("Bob");
array.add("Charlie");
array.remove(1);
assertEquals(2, array.getSize());
assertEquals("Alice", array.get(0));
assertEquals("Charlie", array.get(1));
}
@Test
public void testCapacityDoubling() {
DynamicArray<String> array = new DynamicArray<>(1);
array.add("Alice");
array.add("Bob");
array.add("Charlie"); // Ensure capacity expansion is working
assertEquals(3, array.getSize());
assertEquals("Charlie", array.get(2));
}
}