GlueGen: src/junit/com/jogamp/common/util/TestIntIntHashMap.java Source File

GlueGen v2.6.0-rc-20250721 GlueGen, Native Binding Generator for Java™ (public API).

TestIntIntHashMap.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/**
30 * Created on Sunday, March 28 2010 21:01
31 */
32package com.jogamp.common.util;
33
34import java.io.IOException;
35import java.util.HashMap;
36import java.util.Iterator;
37import java.util.Map.Entry;
38
39import org.junit.BeforeClass;
40import org.junit.Test;
41
42import com.jogamp.common.os.Clock;
43import com.jogamp.common.os.Platform;
44import com.jogamp.junit.util.SingletonJunitCase;
45
46import jogamp.common.os.PlatformPropsImpl;
47
48import static org.junit.Assert.*;
49import static java.lang.System.*;
50
51/**
52 *
53 * @author Michael Bien
54 * @author Sven Gothel
55 */
56import org.junit.FixMethodOrder;
57import org.junit.runners.MethodSorters;
58
59@FixMethodOrder(MethodSorters.NAME_ASCENDING)
60public class TestIntIntHashMap extends SingletonJunitCase {
61
62 private static int iterations;
63 private static IntIntUniqueRndValues pairs;
64
65 @BeforeClass
66 public static void init() {
67 iterations = ( Platform.getCPUType().family == Platform.CPUFamily.ARM ) ? 20 : 10000;
68 pairs = new IntIntUniqueRndValues(iterations);
69 }
70
71 /**
72 * Test of put method, of class IntIntHashMap.
73 */
74 @Test
75 public void testPutRemove() {
76
77 final IntIntHashMap intmap = new IntIntHashMap();
78 final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
79
80 // put
81 for (int i = 0; i < iterations; i++) {
82 intmap.put(pairs.keys[i], pairs.values[i]);
83
84 assertTrue(intmap.containsValue(pairs.values[i]));
85 assertTrue(intmap.containsKey(pairs.keys[i]));
86 }
87
88 for (int i = 0; i < iterations; i++) {
89 map.put(pairs.keys[i], pairs.values[i]);
90 }
91
92 assertEquals(map.size(), intmap.size());
93
94 for (final Entry<Integer, Integer> entry : map.entrySet()) {
95 assertTrue(intmap.containsKey(entry.getKey()));
96 assertTrue(intmap.containsValue(entry.getValue()));
97 }
98
99 int i = 0;
100 for (final Entry<Integer, Integer> entry : map.entrySet()) {
101 assertEquals((int)entry.getValue(), intmap.remove(entry.getKey()));
102 assertEquals(map.size() - i - 1, intmap.size());
103 i++;
104 }
105
106 }
107
108 @Test
109 public void iteratorTest() {
110
111 final IntIntHashMap intmap = new IntIntHashMap(iterations);
112
113 for (int i = 0; i < iterations; i++) {
114 intmap.put(pairs.keys[i], pairs.values[i]);
115 }
116
117 final Iterator<IntIntHashMap.Entry> iterator = intmap.iterator();
118 assertNotNull(iterator);
119 assertTrue(iterator.hasNext());
120
121 int n = 0;
122 while (iterator.hasNext()) {
123 final IntIntHashMap.Entry entry = iterator.next();
124 assertNotNull(entry);
125 n++;
126 }
127 assertEquals(intmap.size(), n);
128
129// out.println(intmap);
130
131 }
132
133 @Test
134 public void cloneTest() {
135
136 final int smallSize = iterations / 4 ;
137
138 final IntIntHashMap intmap = new IntIntHashMap( smallSize + smallSize / 4, 0.75f);
139 intmap.setKeyNotFoundValue(-1);
140
141 for (int i = 0; i < smallSize; i++) {
142 intmap.put(pairs.keys[i], pairs.values[i]);
143 }
144 assertEquals(intmap.size(), smallSize);
145
146 final IntIntHashMap intmapCopy = (IntIntHashMap) intmap.clone();
147
148 assertEquals(intmap.size(), intmapCopy.size());
149 assertEquals(intmap.getKeyNotFoundValue(), intmapCopy.getKeyNotFoundValue());
150
151 final Iterator<IntIntHashMap.Entry> iterator = intmap.iterator();
152 assertNotNull(iterator);
153 assertTrue(iterator.hasNext());
154
155 final Iterator<IntIntHashMap.Entry> iteratorCopy = intmapCopy.iterator();
156 assertNotNull(iteratorCopy);
157 assertTrue(iteratorCopy.hasNext());
158
159 int n = 0;
160 while (iterator.hasNext()) {
161 assertTrue(iteratorCopy.hasNext());
162 final IntIntHashMap.Entry entry = iterator.next();
163 final IntIntHashMap.Entry entryCopy = iteratorCopy.next();
164 assertNotNull(entry);
165 assertNotNull(entryCopy);
166 assertEquals(entry.key, entryCopy.key);
167 assertEquals(entry.value, entryCopy.value);
168 n++;
169 }
170 assertTrue(!iteratorCopy.hasNext());
171
172 assertEquals(intmap.size(), n);
173 assertEquals(intmapCopy.size(), n);
174
175 for (int i = 0; i < smallSize; i++) {
176 assertTrue(intmap.containsValue(pairs.values[i]));
177 assertTrue(intmap.containsKey(pairs.keys[i]));
178 assertTrue(intmapCopy.containsValue(pairs.values[i]));
179 assertTrue(intmapCopy.containsKey(pairs.keys[i]));
180 }
181
182// out.println(intmap);
183
184 }
185
186 @Test
187 public void capacityTest() {
188 final int fixedSize = 16;
189 final int capacity = 32;
190
191 final IntIntHashMap intmap = new IntIntHashMap( capacity, 0.75f);
192 intmap.setKeyNotFoundValue(-1);
193
194 assertEquals(intmap.capacity(), capacity);
195 for (int i = 0; i < fixedSize; i++) {
196 intmap.put(pairs.keys[i], pairs.values[i]);
197 }
198 assertEquals(intmap.size(), fixedSize);
199 assertEquals(intmap.capacity(), capacity);
200
201 final IntIntHashMap intmapCopy = (IntIntHashMap) intmap.clone();
202
203 assertEquals(intmap.size(), intmapCopy.size());
204 assertEquals(intmap.capacity(), intmapCopy.capacity());
205 assertEquals(intmap.getKeyNotFoundValue(), intmapCopy.getKeyNotFoundValue());
206
207 final Iterator<IntIntHashMap.Entry> iterator = intmap.iterator();
208 assertNotNull(iterator);
209 assertTrue(iterator.hasNext());
210
211 final Iterator<IntIntHashMap.Entry> iteratorCopy = intmapCopy.iterator();
212 assertNotNull(iteratorCopy);
213 assertTrue(iteratorCopy.hasNext());
214
215 int n = 0;
216 while (iterator.hasNext()) {
217 assertTrue(iteratorCopy.hasNext());
218 final IntIntHashMap.Entry entry = iterator.next();
219 final IntIntHashMap.Entry entryCopy = iteratorCopy.next();
220 assertNotNull(entry);
221 assertNotNull(entryCopy);
222 assertEquals(entry.key, entryCopy.key);
223 assertEquals(entry.value, entryCopy.value);
224 n++;
225 }
226 assertTrue(!iteratorCopy.hasNext());
227
228 assertEquals(intmap.size(), n);
229 assertEquals(intmap.capacity(), capacity);
230 assertEquals(intmapCopy.size(), n);
231 assertEquals(intmapCopy.capacity(), capacity);
232
233 for (int i = 0; i < fixedSize; i++) {
234 assertTrue(intmap.containsValue(pairs.values[i]));
235 assertTrue(intmap.containsKey(pairs.keys[i]));
236 assertTrue(intmapCopy.containsValue(pairs.values[i]));
237 assertTrue(intmapCopy.containsKey(pairs.keys[i]));
238 }
239
240// out.println(intmap);
241
242 }
243
244 @Test
245 public void benchmark() {
246 benchmark(true);
247 benchmark(true);
248 benchmark(false);
249 }
250
251 void benchmark(final boolean warmup) {
252
253 // simple benchmark
254 final IntIntHashMap intmap = new IntIntHashMap(1024);
255 final HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(1024);
256
257 out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()+
258 " warmup: " + warmup);
259
260 out.println("put");
261 long time = Clock.currentNanos();
262 for (int i = 0; i < iterations; i++) {
263 intmap.put(pairs.keys[i], pairs.values[i]);
264 }
265 final long intmapPutTime = (Clock.currentNanos() - time);
266 out.println(" iimap: " + intmapPutTime/1000000.0f+"ms");
267
268 time = Clock.currentNanos();
269 for (int i = 0; i < iterations; i++) {
270 map.put(pairs.keys[i], pairs.values[i]);
271 }
272 final long mapPutTime = (Clock.currentNanos() - time);
273 final double putRatio = (double)intmapPutTime/(double)mapPutTime;
274 out.println(" map: " + mapPutTime/1000000.0f+"ms");
275 out.println(" iimap/map: " + putRatio);
276
277
278 System.out.println();
279 System.out.println("get");
280 time = Clock.currentNanos();
281 for (int i = 0; i < iterations; i++) {
282 intmap.get(pairs.keys[i]);
283 }
284 final long intmapGetTime = (Clock.currentNanos() - time);
285 out.println(" iimap: " + intmapGetTime/1000000.0f+"ms");
286
287 time = Clock.currentNanos();
288 for (int i = 0; i < iterations; i++) {
289 map.get(pairs.keys[i]);
290 }
291 final long mapGetTime = (Clock.currentNanos() - time);
292 final double getRatio = (double)intmapGetTime/(double)mapGetTime;
293 out.println(" map: " + mapGetTime/1000000.0f+"ms");
294 out.println(" iimap/map: " + getRatio);
295
296 out.println();
297 out.println("remove");
298 time = Clock.currentNanos();
299 for (int i = 0; i < iterations; i++) {
300 intmap.remove(pairs.keys[i]);
301 }
302 assertEquals(0, intmap.size());
303 final long intmapRemoveTime = (Clock.currentNanos() - time);
304 out.println(" iimap: " + intmapRemoveTime/1000000.0f+"ms");
305
306 time = Clock.currentNanos();
307 for (int i = 0; i < iterations; i++) {
308 map.remove(pairs.keys[i]);
309 }
310 assertEquals(0, map.size());
311 final long mapRemoveTime = (Clock.currentNanos() - time);
312 final double removeRatio = (double)intmapRemoveTime/(double)mapRemoveTime;
313 out.println(" map: " + mapRemoveTime/1000000.0f+"ms");
314 out.println(" iimap/map: " + removeRatio);
315
316
317 // JDK-8309361: JDK-21 perf-issue (?): 1.5 ratio margin suffice for JDK < 21, but 4x for JDK 21
318 // See <https://bugs.openjdk.org/browse/JDK-8309361>
319 final double ratioTolerance = PlatformPropsImpl.JAVA_21 ? 4 : 1.5;
320
321 if(!warmup) {
322 // In case the 1st class map magically improves
323 // we add a tolerance around 50% since this would be hardly a bug.
324 // The main goal of this primitve map is memory efficiency.
325 // high and not O(1) assertTrue("'put' too slow", intmapPutTime <= mapPutTime + mapPutTime/4 );
326 assertTrue("'get' too slow", getRatio < ratioTolerance );
327 assertTrue("'remove' too slow", removeRatio < ratioTolerance );
328 }
329 }
330
331 public static void main(final String args[]) throws IOException {
332 org.junit.runner.JUnitCore.main(TestIntIntHashMap.class.getName());
333 }
334
335}
com.jogamp.common.os.Clock
Definition: Clock.java:29
com.jogamp.common.os.Clock.currentNanos
static native long currentNanos()
Returns current monotonic nanoseconds since start of this application.
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.getCPUType
static CPUType getCPUType()
Returns the CPU architecture type.
Definition: Platform.java:415
com.jogamp.common.util.IntIntHashMap.Entry
An entry mapping a key to a value.
Definition: IntIntHashMap.java:449
com.jogamp.common.util.IntIntHashMap
Fast HashMap for primitive data.
Definition: IntIntHashMap.java:62
com.jogamp.common.util.IntIntHashMap.remove
int remove(final int key)
Removes the key-value mapping from this map.
Definition: IntIntHashMap.java:298
com.jogamp.common.util.IntIntHashMap.containsKey
boolean containsKey(final int key)
Definition: IntIntHashMap.java:210
com.jogamp.common.util.IntIntHashMap.containsValue
boolean containsValue(final int value)
Definition: IntIntHashMap.java:190
com.jogamp.common.util.IntIntHashMap.getKeyNotFoundValue
int getKeyNotFoundValue()
Returns the value which is returned if no value has been found for the specified key.
Definition: IntIntHashMap.java:372
com.jogamp.common.util.IntIntHashMap.iterator
Iterator< Entry > iterator()
Returns a new Iterator.
Definition: IntIntHashMap.java:348
com.jogamp.common.util.IntIntHashMap.put
int put(final int key, final int value)
Maps the key to the specified value.
Definition: IntIntHashMap.java:242
com.jogamp.common.util.IntIntHashMap.setKeyNotFoundValue
int setKeyNotFoundValue(final int newKeyNotFoundValue)
Sets the new key not found value.
Definition: IntIntHashMap.java:361
com.jogamp.common.util.IntIntHashMap.clone
Object clone()
Disclaimer: If the value type doesn't implement clone(), only the reference is copied.
Definition: IntIntHashMap.java:158
com.jogamp.common.util.IntIntHashMap.get
int get(final int key)
Returns the value to which the specified key is mapped, or getKeyNotFoundValue if this map contains n...
Definition: IntIntHashMap.java:226
com.jogamp.common.util.IntIntUniqueRndValues
Definition: IntIntUniqueRndValues.java:37
com.jogamp.common.util.IntIntUniqueRndValues.keys
int[] keys
Definition: IntIntUniqueRndValues.java:39
com.jogamp.common.util.IntIntUniqueRndValues.values
int[] values
Definition: IntIntUniqueRndValues.java:40
com.jogamp.common.util.TestIntIntHashMap
Definition: TestIntIntHashMap.java:60
com.jogamp.common.util.TestIntIntHashMap.cloneTest
void cloneTest()
Definition: TestIntIntHashMap.java:134
com.jogamp.common.util.TestIntIntHashMap.init
static void init()
Definition: TestIntIntHashMap.java:66
com.jogamp.common.util.TestIntIntHashMap.iteratorTest
void iteratorTest()
Definition: TestIntIntHashMap.java:109
com.jogamp.common.util.TestIntIntHashMap.capacityTest
void capacityTest()
Definition: TestIntIntHashMap.java:187
com.jogamp.common.util.TestIntIntHashMap.benchmark
void benchmark()
Definition: TestIntIntHashMap.java:245
com.jogamp.common.util.TestIntIntHashMap.testPutRemove
void testPutRemove()
Test of put method, of class IntIntHashMap.
Definition: TestIntIntHashMap.java:75
com.jogamp.common.util.TestIntIntHashMap.main
static void main(final String args[])
Definition: TestIntIntHashMap.java:331
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39
com.jogamp.common.os.Platform.CPUType.family
final CPUFamily family
Definition: Platform.java:124