-
Notifications
You must be signed in to change notification settings - Fork 593
/
IntLongMapProbeTest.java
238 lines (201 loc) · 8.71 KB
/
IntLongMapProbeTest.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
/*
* Copyright (c) 2015 Goldman Sachs.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompany this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*/
package org.eclipse.collections.impl.map.mutable.primitive;
import java.util.Random;
import org.eclipse.collections.api.list.primitive.MutableIntList;
import org.eclipse.collections.api.map.primitive.MutableIntLongMap;
import org.eclipse.collections.api.set.primitive.MutableIntSet;
import org.eclipse.collections.impl.SpreadFunctions;
import org.eclipse.collections.impl.list.mutable.primitive.IntArrayList;
import org.eclipse.collections.impl.set.mutable.primitive.IntHashSet;
import org.junit.Assert;
import org.junit.Test;
public class IntLongMapProbeTest
{
private static final int SMALL_COLLIDING_KEY_COUNT = 500;
private static final int LARGE_COLLIDING_KEY_COUNT = 40000;
private int smallMask(int spread)
{
return spread & ((1 << 11) - 1);
}
private int largeMask(int spread)
{
return spread & ((1 << 20) - 1);
}
@Test
public void randomNumbers_get()
{
MutableIntLongMap intlongSmallNonPresized = new IntLongHashMap();
this.testRandomGet(intlongSmallNonPresized, SMALL_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongSmallPresized = new IntLongHashMap(1_000);
this.testRandomGet(intlongSmallPresized, SMALL_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongLargeNonPresized = new IntLongHashMap();
this.testRandomGet(intlongLargeNonPresized, LARGE_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongLargePresized = new IntLongHashMap(1_000_000);
this.testRandomGet(intlongLargePresized, LARGE_COLLIDING_KEY_COUNT);
}
@Test
public void nonRandomNumbers_get()
{
MutableIntLongMap intlongMapSmall = new IntLongHashMap(1_000);
int[] intKeysForSmallMap = this.getSmallCollidingNumbers().toArray();
this.testNonRandomGet(intlongMapSmall, SMALL_COLLIDING_KEY_COUNT, intKeysForSmallMap);
MutableIntLongMap intlongMapLarge = new IntLongHashMap(1_000_000);
int[] intKeysForLargeMap = this.getLargeCollidingNumbers().toArray();
this.testNonRandomGet(intlongMapLarge, LARGE_COLLIDING_KEY_COUNT, intKeysForLargeMap);
}
@Test
public void nonRandomNumbers_remove()
{
MutableIntLongMap intlongMapSmallNonPresized = new IntLongHashMap();
int[] intKeysForMap = this.getSmallCollidingNumbers().toArray();
this.testNonRandomRemove(intlongMapSmallNonPresized, SMALL_COLLIDING_KEY_COUNT, intKeysForMap);
MutableIntLongMap intlongMapSmallPresized = new IntLongHashMap(1_000);
int[] intKeysForMap2 = this.getSmallCollidingNumbers().toArray();
this.testNonRandomRemove(intlongMapSmallPresized, SMALL_COLLIDING_KEY_COUNT, intKeysForMap2);
MutableIntLongMap intlongMapLargeNonPresized = new IntLongHashMap();
int[] intKeysForMap3 = this.getLargeCollidingNumbers().toArray();
this.testNonRandomRemove(intlongMapLargeNonPresized, LARGE_COLLIDING_KEY_COUNT, intKeysForMap3);
MutableIntLongMap intlongMapLargePresized = new IntLongHashMap(1_000_000);
int[] intKeysForMap4 = this.getLargeCollidingNumbers().toArray();
this.testNonRandomRemove(intlongMapLargePresized, LARGE_COLLIDING_KEY_COUNT, intKeysForMap4);
}
@Test
public void randomNumbers_remove()
{
MutableIntLongMap intlongMapSmallNonPresized = new IntLongHashMap();
this.testRandomRemove(intlongMapSmallNonPresized, SMALL_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongMapSmallPresized = new IntLongHashMap(1_000);
this.testRandomRemove(intlongMapSmallPresized, SMALL_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongMapLargeNonPresized = new IntLongHashMap();
this.testRandomRemove(intlongMapLargeNonPresized, LARGE_COLLIDING_KEY_COUNT);
MutableIntLongMap intlongMapLargePresized = new IntLongHashMap(1_000_000);
this.testRandomRemove(intlongMapLargePresized, LARGE_COLLIDING_KEY_COUNT);
}
private void testNonRandomGet(MutableIntLongMap intlongMap, int keyCount, int[] intKeysForMap)
{
Random random = new Random(0x123456789ABCDL);
this.shuffle(intKeysForMap, random);
for (int i = 0; i < keyCount; i++)
{
intlongMap.put(intKeysForMap[i], (long) (intKeysForMap[i] * 10));
}
for (int i = 0; i < intlongMap.size(); i++)
{
Assert.assertEquals((long) (intKeysForMap[i] * 10), intlongMap.get(intKeysForMap[i]));
}
}
private void testRandomGet(MutableIntLongMap intlongMap, int keyCount)
{
Random random = new Random(0x123456789ABCDL);
MutableIntSet set = new IntHashSet(keyCount);
while (set.size() < keyCount)
{
set.add(random.nextInt());
}
int[] randomNumbersForMap = set.toArray();
this.shuffle(randomNumbersForMap, random);
for (int i = 0; i < keyCount; i++)
{
intlongMap.put(randomNumbersForMap[i], (long) (randomNumbersForMap[i] * 10));
}
for (int i = 0; i < intlongMap.size(); i++)
{
Assert.assertEquals((long) (randomNumbersForMap[i] * 10), intlongMap.get(randomNumbersForMap[i]));
}
}
private void testNonRandomRemove(MutableIntLongMap intlongMap, int keyCount, int[] intKeysForMap)
{
Random random = new Random(0x123456789ABCDL);
this.shuffle(intKeysForMap, random);
for (int i = 0; i < keyCount; i++)
{
intlongMap.put(intKeysForMap[i], (long) (intKeysForMap[i] * 10));
}
this.shuffle(intKeysForMap, random);
for (int i = 0; i < intKeysForMap.length; i++)
{
intlongMap.remove(intKeysForMap[i]);
for (int j = i + 1; j < intKeysForMap.length; j++)
{
Assert.assertEquals((long) (intKeysForMap[j] * 10), intlongMap.get(intKeysForMap[j]));
}
}
}
private void testRandomRemove(MutableIntLongMap intlongMap, int keyCount)
{
Random random = new Random(0x123456789ABCDL);
MutableIntSet set = new IntHashSet(keyCount);
while (set.size() < keyCount)
{
set.add(random.nextInt());
}
int[] intKeysForMap = set.toArray();
this.shuffle(intKeysForMap, random);
for (int i = 0; i < keyCount; i++)
{
intlongMap.put(intKeysForMap[i], (long) (intKeysForMap[i] * 10));
}
this.shuffle(intKeysForMap, random);
for (int i = 0; i < intKeysForMap.length; i++)
{
intlongMap.remove(intKeysForMap[i]);
for (int j = i + 1; j < intKeysForMap.length; j++)
{
Assert.assertEquals((long) (intKeysForMap[j] * 10), intlongMap.get(intKeysForMap[j]));
}
}
}
private MutableIntList getSmallCollidingNumbers()
{
int lower = Integer.MIN_VALUE;
int upper = Integer.MAX_VALUE;
MutableIntList collidingNumbers = new IntArrayList();
int numberOne = this.smallMask(SpreadFunctions.intSpreadOne(0xABCDEF1));
int numberTwo = this.smallMask(SpreadFunctions.intSpreadTwo(0xABCDEF1));
for (int i = lower; i < upper && collidingNumbers.size() < SMALL_COLLIDING_KEY_COUNT; i++)
{
if (this.smallMask(SpreadFunctions.intSpreadOne(i)) == numberOne && this.smallMask(SpreadFunctions.intSpreadTwo(i)) == numberTwo)
{
collidingNumbers.add(i);
}
}
return collidingNumbers;
}
private MutableIntList getLargeCollidingNumbers()
{
int lower = Integer.MIN_VALUE;
int upper = Integer.MAX_VALUE;
int number = 23;
MutableIntList collidingNumbers = new IntArrayList();
for (int i = lower; i < upper && collidingNumbers.size() < LARGE_COLLIDING_KEY_COUNT; i++)
{
int index = this.largeMask(SpreadFunctions.intSpreadOne(i));
if (index >= number && index <= number + 100)
{
collidingNumbers.add(i);
}
}
return collidingNumbers;
}
public void shuffle(int[] intArray, Random rnd)
{
for (int i = intArray.length; i > 1; i--)
{
IntLongMapProbeTest.swap(intArray, i - 1, rnd.nextInt(i));
}
}
private static void swap(int[] arr, int i, int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}