-
Notifications
You must be signed in to change notification settings - Fork 3
/
ArrayIntTest.java
279 lines (260 loc) · 8.74 KB
/
ArrayIntTest.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*******************************************************************************
* Copyright (c) 2010, 2023 IBM Corporation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.mat.tests.collect;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.number.OrderingComparison.greaterThan;
import static org.hamcrest.number.OrderingComparison.lessThanOrEqualTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;
import org.eclipse.mat.collect.ArrayInt;
import org.eclipse.mat.collect.IteratorInt;
import org.junit.Test;
public class ArrayIntTest
{
/** Needs huge heap, tests arithmetic overflow */
private static final int HUGE_SIZE_BIG = Integer.MAX_VALUE / 100 * 76;
private static final int HUGE_SIZE_SMALL = Integer.MAX_VALUE / 500 * 1;
private static final boolean USE_HUGE = false;
private static final int HUGE_SIZE = USE_HUGE ? HUGE_SIZE_BIG : HUGE_SIZE_SMALL;
private static final int KEYS = 3000;
private static final int INITIAL_SIZE = 30;
private static final int COUNT = 1000;
/**
* Basic test - no unexpected ArrayIndexOutOfBoundsException
*/
@Test
public void testArrayInt0() {
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
int t = 0;
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
int v = r.nextInt(KEYS);
ss.add(v);
t += 1;
}
assertThat("At least one item should have been added", t, greaterThan(0)); //$NON-NLS-1$
assertThat("Size should equal added items", ss.size(), equalTo(t)); //$NON-NLS-1$
assertFalse("Not empty", ss.isEmpty());
}
}
/**
* Check that set contains everything it says it has
*/
@Test
public void testArrayInt1()
{
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
testOneArrayInt(r);
}
}
private void testOneArrayInt(Random r)
{
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
ss.add(r.nextInt(KEYS));
ss.set(j, r.nextInt(KEYS));
}
int i = 0;
for (IteratorInt ii = ss.iterator(); ii.hasNext(); ){
assertThat("the iterator should match each entry", ii.next(), equalTo(ss.get(i++))); //$NON-NLS-1$
}
}
/**
* Check the number of contained items is the size
*/
@Test
public void testArrayInt2()
{
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
testTwoArrayInt(r);
}
}
private void testTwoArrayInt(Random r)
{
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
ss.add(r.nextInt(KEYS) + 1);
}
int t = 0;
for (int k = 0; k < KEYS; ++k) {
t += ss.get(k) > 0 ? 1 : 0;
}
assertThat("contained items should equals the size", t, equalTo(ss.size())); //$NON-NLS-1$
}
/**
* Check that array contains everything it says it has
*/
@Test
public void testArrayInt3()
{
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
testThreeArrayInt(r);
}
}
private void testThreeArrayInt(Random r)
{
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
ss.add(r.nextInt(KEYS));
}
ArrayInt ss2 = new ArrayInt(ss);
int i = 0;
for (IteratorInt ii = ss.iterator(); ii.hasNext(); ){
assertThat("the iterator should match each entry", ii.next(), equalTo(ss2.get(i++))); //$NON-NLS-1$
}
}
/**
* Check add works as expected
*/
@Test
public void testArrayInt4()
{
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
testFourArrayInt(r);
}
}
private void testFourArrayInt(Random r)
{
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
ss.add(r.nextInt(KEYS) + 1);
}
for (int k = 0; k < KEYS; ++k) {
assertThat(ss.get(k), greaterThan(0));
}
}
/**
* Check performance is as expected.
*/
@Test
public void testArrayInt5()
{
long best = Long.MAX_VALUE;
long worst = Long.MIN_VALUE;
int worstj = 0, bestj = 0;
for (int j = 1; j <= 6; ++j) {
long time = perf(31545, j, 500);
if (time < best) {
best = time;
bestj = j;
}
if (time > worst) {
worst = time;
worstj = j;
}
}
assertThat("Worst more for group " + worstj + " than 10 times best for group " + bestj, worst, lessThanOrEqualTo(Math.max(100, 10 * best))); //$NON-NLS-1$ //$NON-NLS-2$
}
/**
* Test performance of ArrayInt
* @param n number of entries
* @param m number of clumps
* @param c number of times to repeat
* @return time in milliseconds
*/
private long perf(int n, int m, int c)
{
ArrayInt ss = new ArrayInt();
long then = System.currentTimeMillis();
for (int i = 0; i < n / m; ++i)
{
for (int j = 0; j < m; ++j)
{
int key = i + n * 2 * j;
ss.add(key);
}
}
int cc = 0;
for (int j = 0; j < c; ++j) {
for (int i = 0; i < n; ++i) {
int key = j * n + i;
if (key >= n / m * m)
break;
if (ss.get(key) >= 0)
++cc;
}
}
long now = System.currentTimeMillis();
return (now - then) + ((cc == n) ? 0 : 1);
}
/**
* Check serialize/deserialize works as expected
* @throws IOException
* @throws ClassNotFoundException
*/
@Test
public void testArrayInt6() throws ClassNotFoundException, IOException
{
Random r = new Random(1);
for (int i = 0; i < COUNT; ++i) {
testSixArrayInt(r);
}
}
private void testSixArrayInt(Random r) throws IOException, ClassNotFoundException
{
ArrayInt ss = new ArrayInt(r.nextInt(INITIAL_SIZE));
for (int j = 0; j < KEYS; ++j) {
ss.add(r.nextInt());
}
byte b[];
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);)
{
oos.writeObject(ss.toArray());
oos.flush();
b = baos.toByteArray();
}
ArrayInt ss2;
try (ByteArrayInputStream bais = new ByteArrayInputStream(b);
ObjectInputStream ois = new ObjectInputStream(bais);)
{
int a[] = (int[]) ois.readObject();
ss2 = new ArrayInt(a);
}
int i = 0;
for (IteratorInt ii = ss.iterator(); ii.hasNext(); ){
assertThat("every key should be contained in the deserialized version", ii.next(), equalTo(ss2.get(i++))); //$NON-NLS-1$
}
i = 0;
for (IteratorInt ii = ss2.iterator(); ii.hasNext(); ){
assertThat("every deserialized key should be contained", ii.next(), equalTo(ss.get(i++))); //$NON-NLS-1$
}
}
/**
* Test huge sizes for overflow problems.
*/
@Test
public void testArrayInt7() {
int s1 = HUGE_SIZE;
ArrayInt huge = new ArrayInt(s1);
assertThat(huge.size(), equalTo(0));
assertTrue(huge.isEmpty());
int s2 = s1 / 3 * 2;
for (int i = 0; i < s2; ++i) {
huge.add(i * 2);
}
assertThat(huge.size(), equalTo(s2));
}
}