forked from MinecraftTAS/BigArrayList
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBigArrayListTest.java
More file actions
249 lines (195 loc) · 7.1 KB
/
BigArrayListTest.java
File metadata and controls
249 lines (195 loc) · 7.1 KB
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
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import com.dselent.bigarraylist.BigArrayList;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
public class BigArrayListTest
{
private static final int NUMBER_OF_OPERATIONS = 4;
private static int testRuns;
private static int minBlockSize;
private static int maxBlockSize;
private static int minCacheBlocks;
private static int maxCacheBlocks;
private static int minActions;
private static int maxActions;
private static Random random;
private BigArrayList<Integer> bigArrayList;
@BeforeAll
static void setUp() throws Exception
{
//modify number of test runs as desired
testRuns = 10;
minBlockSize = 5;
maxBlockSize = 1000;
minCacheBlocks = 2;
maxCacheBlocks = 20;
minActions = 2;
maxActions = 100000;
random = new Random(0);
}
@AfterEach
protected void tearDown() throws Exception
{
if(bigArrayList != null)
{
bigArrayList.clearMemory();
}
}
/**
* Monte-carlo test case. Tests random operations on BigArrayLists with parameters randomized within ranges.
*/
@Test
@Disabled
public void testBigArrayList()
{
for(int i=0; i<testRuns; i++)
{
System.out.println("Iteration " + i);
int blockSize = random.nextInt(maxBlockSize-minBlockSize) + minBlockSize;
int cacheBlocks = random.nextInt(maxCacheBlocks- minCacheBlocks) + minCacheBlocks;
int actions = random.nextInt(maxActions-minActions) + minActions;
bigArrayList = new BigArrayList<Integer>(blockSize, cacheBlocks);
List<Integer> arrayList = new ArrayList<>();
for(int j=0; j<actions; j++)
{
int action = random.nextInt(NUMBER_OF_OPERATIONS);
if(action == 0 || arrayList.size() == 0)
{
//add elements to end
int num1 = random.nextInt();
arrayList.add(num1);
bigArrayList.add(num1);
//add another if it is early
if(j < actions/2)
{
int num2 = random.nextInt();
arrayList.add(num2);
bigArrayList.add(num2);
}
String errorMessage = "(ADD) Sizes not equal: test run iteration = " + i + ", action number = " + j;
assertEquals((long)arrayList.size(), bigArrayList.size(), errorMessage);
}
else if(action == 1)
{
//get an element
int listSize = arrayList.size();
int getIndex = random.nextInt(listSize);
long number1 = arrayList.get(getIndex);
long number2 = bigArrayList.get(getIndex);
String errorMessage = "(GET) Elements not equal: test run iteration = " + i + ", action number = " + j +
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + getIndex;
assertEquals(number1, number2, errorMessage);
String errorMessage2 = "(GET) Sizes not equal: test run iteration = " + i + ", action number = " + j;
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
}
else if(action == 2)
{
//set an element
int listSize = arrayList.size();
int setIndex = random.nextInt(listSize);
int randomNumber = random.nextInt();
arrayList.set(setIndex, randomNumber);
bigArrayList.set(setIndex, randomNumber);
String errorMessage2 = "(SET) Sizes not equal: test run iteration = " + i + ", action number = " + j + ", index = " + setIndex;
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
}
else if(action == 3)
{
//remove an element
int listSize = arrayList.size();
int removeIndex = random.nextInt(listSize);
int number1 = arrayList.remove(removeIndex);
int number2 = bigArrayList.remove(removeIndex);
String errorMessage = "(REMOVE) Elements not equal: test run iteration = " + i + ", action number = " + j +
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + removeIndex;
assertEquals(number1, number2, errorMessage);
String errorMessage2 = "(REMOVE) Sizes not equal: test run iteration = " + i + ", action number = " + j;
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage2);
if(j > actions/2 && arrayList.size() > 0)
{
int listSize2 = arrayList.size();
int removeIndex2 = random.nextInt(listSize2);
int number1_2 = arrayList.remove(removeIndex2);
int number2_2 = bigArrayList.remove(removeIndex2);
String errorMessage3 = "(REMOVE) Elements not equal: test run iteration = " + i + ", action number = " + j +
", ArrayList element = " + number1_2 + ", BigArrayList element = " + number2_2 + ", index = " + removeIndex;
assertEquals(number1, number2, errorMessage3);
String errorMessage4 = "(REMOVE) Sizes not equal: test run iteration = " + i + ", action number = " + j;
assertEquals(arrayList.size(), bigArrayList.size(), errorMessage4);
}
}
//remove for testing performance
/*
for(int k=0; k<arrayList.size(); k++)
{
int number1 = arrayList.get(k);
int number2 = bigArrayList.get(k);
String errorMessage = "(Elements not equal: test run iteration = " + i +
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + k;
assertEquals(errorMessage, number1, number2);
}*/
}
for(int j=0; j<arrayList.size(); j++)
{
int number1 = arrayList.get(j);
int number2 = bigArrayList.get(j);
String errorMessage = "(Elements not equal: test run iteration = " + i +
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + j;
assertEquals(number1, number2, errorMessage);
}
Collections.sort(arrayList);
try
{
bigArrayList = BigArrayList.sort(bigArrayList);
}
catch (IOException e)
{
fail("Iteration " + i + ": " + e.getCause().toString());
}
try
{
bigArrayList.clearMemory();
}
catch(IOException e)
{
fail("Iteration " + i + ": " + e.getCause().toString());
}
for(int j=0; j<arrayList.size(); j++)
{
int number1 = arrayList.get(j);
int number2 = bigArrayList.get(j);
String errorMessage = "(Elements not equal after sorting: test run iteration = " + i +
", ArrayList element = " + number1 + ", BigArrayList element = " + number2 + ", index = " + j;
assertEquals(number1, number2, errorMessage);
}
}
}
/**
* Tests iterator implementation
*/
@Test
public void testIterator() {
bigArrayList = new BigArrayList<Integer>();
for (int i = 0; i < 10000; i++) {
bigArrayList.add(random.nextInt());
}
List<Integer> expected = new ArrayList<>();
for (int i = 0; i < bigArrayList.size(); i++) {
expected.add(bigArrayList.get(i));
}
List<Integer> actual = new ArrayList<>();
for (int i : bigArrayList) {
actual.add(i);
}
assertIterableEquals(expected, actual);
}
}