Skip to content

Commit d1d455e

Browse files
committed
Add addAll, clear methods
- Bump version
1 parent bdf948d commit d1d455e

File tree

5 files changed

+130
-30
lines changed

5 files changed

+130
-30
lines changed

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=1.5
1+
version=1.6
22
release=false

src/main/java/com/dselent/bigarraylist/BigArrayList.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.io.IOException;
2323
import java.io.Serializable;
2424
import java.util.ArrayList;
25+
import java.util.Collection;
2526
import java.util.Comparator;
2627
import java.util.Iterator;
2728
import java.util.List;
@@ -1078,4 +1079,33 @@ public E next() {
10781079
}
10791080
};
10801081
}
1082+
1083+
/**
1084+
* @param other The other BigArrayList
1085+
* @see ArrayList#addAll(Collection)
1086+
*/
1087+
public void addAll(BigArrayList<? extends E> other) {
1088+
for (E element : other) {
1089+
add(element);
1090+
}
1091+
}
1092+
1093+
/**
1094+
* @param other The other collection
1095+
* @see ArrayList#addAll(Collection)
1096+
*/
1097+
public void addAll(Collection<? extends E> other) {
1098+
for (E element : other) {
1099+
add(element);
1100+
}
1101+
}
1102+
1103+
/**
1104+
* Clears all elements without clearing the memory
1105+
*/
1106+
public void clear() {
1107+
for (long i = wholeListSize-1; i >= 0; i--) {
1108+
remove(i);
1109+
}
1110+
}
10811111
}

src/main/java/com/dselent/bigarraylist/CacheMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,4 @@ protected void clearMemory() throws IOException
499499
{
500500
fileAccessor.clearMemory();
501501
}
502-
}
502+
}

src/main/java/com/dselent/bigarraylist/FileAccessor.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,9 +368,6 @@ protected void clearMemory() throws IOException
368368
}
369369
}
370370
}
371-
372371
//don't delete the folder, other things may be using it
373-
374372
}
375-
376-
}
373+
}

src/test/java/BigArrayListTest.java

Lines changed: 97 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import static org.junit.jupiter.api.Assertions.assertEquals;
1414
import static org.junit.jupiter.api.Assertions.fail;
1515
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
1617

1718
public class BigArrayListTest
1819
{
@@ -27,7 +28,7 @@ public class BigArrayListTest
2728
private static int maxActions;
2829

2930
private static Random random;
30-
private BigArrayList<Integer> bigArrayList;
31+
private BigArrayList<Integer> actualMonte;
3132

3233
@BeforeAll
3334
static void setUp() throws Exception
@@ -47,9 +48,9 @@ static void setUp() throws Exception
4748
@AfterEach
4849
protected void tearDown() throws Exception
4950
{
50-
if(bigArrayList != null)
51+
if(actualMonte != null)
5152
{
52-
bigArrayList.clearMemory();
53+
actualMonte.clearMemory();
5354
}
5455
}
5556

@@ -68,7 +69,7 @@ public void testBigArrayList()
6869
int cacheBlocks = random.nextInt(maxCacheBlocks- minCacheBlocks) + minCacheBlocks;
6970
int actions = random.nextInt(maxActions-minActions) + minActions;
7071

71-
bigArrayList = new BigArrayList<Integer>(blockSize, cacheBlocks);
72+
actualMonte = new BigArrayList<Integer>(blockSize, cacheBlocks);
7273
List<Integer> arrayList = new ArrayList<>();
7374

7475
for(int j=0; j<actions; j++)
@@ -81,18 +82,18 @@ public void testBigArrayList()
8182

8283
int num1 = random.nextInt();
8384
arrayList.add(num1);
84-
bigArrayList.add(num1);
85+
actualMonte.add(num1);
8586

8687
//add another if it is early
8788
if(j < actions/2)
8889
{
8990
int num2 = random.nextInt();
9091
arrayList.add(num2);
91-
bigArrayList.add(num2);
92+
actualMonte.add(num2);
9293
}
9394

9495
String errorMessage = "(ADD) Sizes not equal: test run iteration = " + i + ", action number = " + j;
95-
assertEquals((long)arrayList.size(), bigArrayList.size(), errorMessage);
96+
assertEquals((long)arrayList.size(), actualMonte.size(), errorMessage);
9697
}
9798
else if(action == 1)
9899
{
@@ -103,14 +104,14 @@ else if(action == 1)
103104
int getIndex = random.nextInt(listSize);
104105

105106
long number1 = arrayList.get(getIndex);
106-
long number2 = bigArrayList.get(getIndex);
107+
long number2 = actualMonte.get(getIndex);
107108

108109
String errorMessage = "(GET) Elements not equal: test run iteration = " + i + ", action number = " + j +
109110
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + getIndex;
110111
assertEquals(number1, number2, errorMessage);
111112

112113
String errorMessage2 = "(GET) Sizes not equal: test run iteration = " + i + ", action number = " + j;
113-
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
114+
assertEquals(arrayList.size(), actualMonte.size(), errorMessage2);
114115
}
115116
else if(action == 2)
116117
{
@@ -122,11 +123,11 @@ else if(action == 2)
122123
int randomNumber = random.nextInt();
123124

124125
arrayList.set(setIndex, randomNumber);
125-
bigArrayList.set(setIndex, randomNumber);
126+
actualMonte.set(setIndex, randomNumber);
126127

127128

128129
String errorMessage2 = "(SET) Sizes not equal: test run iteration = " + i + ", action number = " + j + ", index = " + setIndex;
129-
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
130+
assertEquals(arrayList.size(), actualMonte.size(), errorMessage2);
130131
}
131132
else if(action == 3)
132133
{
@@ -137,29 +138,29 @@ else if(action == 3)
137138
int removeIndex = random.nextInt(listSize);
138139

139140
int number1 = arrayList.remove(removeIndex);
140-
int number2 = bigArrayList.remove(removeIndex);
141+
int number2 = actualMonte.remove(removeIndex);
141142

142143
String errorMessage = "(REMOVE) Elements not equal: test run iteration = " + i + ", action number = " + j +
143144
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + removeIndex;
144145
assertEquals(number1, number2, errorMessage);
145146

146147
String errorMessage2 = "(REMOVE) Sizes not equal: test run iteration = " + i + ", action number = " + j;
147-
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
148+
assertEquals(arrayList.size(), actualMonte.size(), errorMessage2);
148149

149150
if(j > actions/2 && arrayList.size() > 0)
150151
{
151152
int listSize2 = arrayList.size();
152153
int removeIndex2 = random.nextInt(listSize2);
153154

154155
int number1_2 = arrayList.remove(removeIndex2);
155-
int number2_2 = bigArrayList.remove(removeIndex2);
156+
int number2_2 = actualMonte.remove(removeIndex2);
156157

157158
String errorMessage3 = "(REMOVE) Elements not equal: test run iteration = " + i + ", action number = " + j +
158159
", ArrayList element = " + number1_2 + ", BigArrayList element = " + number2_2 + ", index = " + removeIndex;
159160
assertEquals(number1, number2, errorMessage3);
160161

161162
String errorMessage4 = "(REMOVE) Sizes not equal: test run iteration = " + i + ", action number = " + j;
162-
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage4);
163+
assertEquals(arrayList.size(), actualMonte.size(), errorMessage4);
163164
}
164165
}
165166

@@ -181,7 +182,7 @@ else if(action == 3)
181182
for(int j=0; j<arrayList.size(); j++)
182183
{
183184
int number1 = arrayList.get(j);
184-
int number2 = bigArrayList.get(j);
185+
int number2 = actualMonte.get(j);
185186

186187
String errorMessage = "(Elements not equal: test run iteration = " + i +
187188
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + j;
@@ -193,7 +194,7 @@ else if(action == 3)
193194

194195
try
195196
{
196-
bigArrayList = BigArrayList.sort(bigArrayList);
197+
actualMonte = BigArrayList.sort(actualMonte);
197198
}
198199
catch (IOException e)
199200
{
@@ -203,7 +204,7 @@ else if(action == 3)
203204

204205
try
205206
{
206-
bigArrayList.clearMemory();
207+
actualMonte.clearMemory();
207208
}
208209
catch(IOException e)
209210
{
@@ -213,7 +214,7 @@ else if(action == 3)
213214
for(int j=0; j<arrayList.size(); j++)
214215
{
215216
int number1 = arrayList.get(j);
216-
int number2 = bigArrayList.get(j);
217+
int number2 = actualMonte.get(j);
217218

218219
String errorMessage = "(Elements not equal after sorting: test run iteration = " + i +
219220
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + j;
@@ -228,22 +229,94 @@ else if(action == 3)
228229
@Test
229230
public void testIterator() {
230231

231-
bigArrayList = new BigArrayList<Integer>();
232+
BigArrayList<Integer> bal = new BigArrayList<>();
232233

233234
for (int i = 0; i < 10000; i++) {
234-
bigArrayList.add(random.nextInt());
235+
bal.add(random.nextInt());
235236
}
236237

237238
List<Integer> expected = new ArrayList<>();
238-
for (int i = 0; i < bigArrayList.size(); i++) {
239-
expected.add(bigArrayList.get(i));
239+
for (int i = 0; i < bal.size(); i++) {
240+
expected.add(bal.get(i));
240241
}
241242

242243
List<Integer> actual = new ArrayList<>();
243-
for (int i : bigArrayList) {
244+
for (int i : bal) {
244245
actual.add(i);
245246
}
246247

247248
assertIterableEquals(expected, actual);
249+
try {
250+
bal.clearMemory();
251+
} catch (IOException e) {
252+
e.printStackTrace();
253+
}
254+
}
255+
256+
@Test
257+
public void testAddAll() {
258+
BigArrayList<Integer> actual = new BigArrayList<>();
259+
List<Integer> expected = new ArrayList<>();
260+
261+
for (int i = 0; i < 1000; i++) {
262+
int randomInt = random.nextInt();
263+
actual.add(randomInt);
264+
expected.add(randomInt);
265+
}
266+
267+
List<Integer> toAdd = new ArrayList<>();
268+
269+
for (int i = 0; i < 1000; i++) {
270+
int randomInt = random.nextInt();
271+
toAdd.add(randomInt);
272+
}
273+
274+
actual.addAll(toAdd);
275+
expected.addAll(toAdd);
276+
277+
assertIterableEquals(expected, actual);
278+
try {
279+
actual.clearMemory();
280+
} catch (IOException e) {
281+
e.printStackTrace();
282+
}
283+
}
284+
285+
@Test
286+
public void testClear() {
287+
BigArrayList<Integer> actual = new BigArrayList<>();
288+
List<Integer> expected = new ArrayList<>();
289+
290+
for (int i = 0; i < 1000; i++) {
291+
int randomInt = random.nextInt();
292+
actual.add(randomInt);
293+
expected.add(randomInt);
294+
}
295+
296+
actual.clear();
297+
expected.clear();
298+
299+
assertTrue(actual.isLive());
300+
assertTrue(actual.isEmpty());
301+
}
302+
303+
@Test
304+
public void testEquals() {
305+
BigArrayList<Integer> actual = new BigArrayList<>();
306+
BigArrayList<Integer> expected = new BigArrayList<>();
307+
for (int i = 0; i < 1000; i++) {
308+
int randomInt = random.nextInt();
309+
actual.add(randomInt);
310+
expected.add(randomInt);
311+
}
312+
313+
assertEquals(expected, actual);
314+
315+
try {
316+
actual.clearMemory();
317+
expected.clearMemory();
318+
} catch (IOException e) {
319+
e.printStackTrace();
320+
}
248321
}
249322
}

0 commit comments

Comments
 (0)