32package com.jogamp.common.util;
34import java.io.IOException;
35import java.util.HashMap;
36import java.util.Iterator;
37import java.util.Map.Entry;
39import org.junit.BeforeClass;
42import com.jogamp.common.os.Clock;
43import com.jogamp.common.os.Platform;
44import com.jogamp.junit.util.SingletonJunitCase;
46import jogamp.common.os.PlatformPropsImpl;
48import static org.junit.Assert.*;
49import static java.lang.System.*;
56import org.junit.FixMethodOrder;
57import org.junit.runners.MethodSorters;
59@FixMethodOrder(MethodSorters.NAME_ASCENDING)
62 private static int iterations;
66 public static void init() {
78 final HashMap<Integer, Integer> map =
new HashMap<Integer, Integer>();
81 for (
int i = 0; i < iterations; i++) {
88 for (
int i = 0; i < iterations; i++) {
92 assertEquals(map.size(), intmap.size());
94 for (
final Entry<Integer, Integer> entry : map.entrySet()) {
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());
113 for (
int i = 0; i < iterations; i++) {
118 assertNotNull(iterator);
119 assertTrue(iterator.hasNext());
122 while (iterator.hasNext()) {
123 final IntIntHashMap.Entry entry = iterator.next();
124 assertNotNull(entry);
127 assertEquals(intmap.size(), n);
136 final int smallSize = iterations / 4 ;
141 for (
int i = 0; i < smallSize; i++) {
144 assertEquals(intmap.size(), smallSize);
148 assertEquals(intmap.size(), intmapCopy.size());
152 assertNotNull(iterator);
153 assertTrue(iterator.hasNext());
156 assertNotNull(iteratorCopy);
157 assertTrue(iteratorCopy.hasNext());
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);
170 assertTrue(!iteratorCopy.hasNext());
172 assertEquals(intmap.size(), n);
173 assertEquals(intmapCopy.size(), n);
175 for (
int i = 0; i < smallSize; i++) {
188 final int fixedSize = 16;
189 final int capacity = 32;
194 assertEquals(intmap.capacity(), capacity);
195 for (
int i = 0; i < fixedSize; i++) {
198 assertEquals(intmap.size(), fixedSize);
199 assertEquals(intmap.capacity(), capacity);
203 assertEquals(intmap.size(), intmapCopy.size());
204 assertEquals(intmap.capacity(), intmapCopy.capacity());
208 assertNotNull(iterator);
209 assertTrue(iterator.hasNext());
212 assertNotNull(iteratorCopy);
213 assertTrue(iteratorCopy.hasNext());
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);
226 assertTrue(!iteratorCopy.hasNext());
228 assertEquals(intmap.size(), n);
229 assertEquals(intmap.capacity(), capacity);
230 assertEquals(intmapCopy.size(), n);
231 assertEquals(intmapCopy.capacity(), capacity);
233 for (
int i = 0; i < fixedSize; i++) {
251 void benchmark(
final boolean warmup) {
255 final HashMap<Integer, Integer> map =
new HashMap<Integer, Integer>(1024);
257 out.println(intmap.getClass().getName()+
" vs "+map.getClass().getName()+
258 " warmup: " + warmup);
262 for (
int i = 0; i < iterations; i++) {
265 final long intmapPutTime = (Clock.currentNanos() - time);
266 out.println(
" iimap: " + intmapPutTime/1000000.0f+
"ms");
268 time = Clock.currentNanos();
269 for (
int i = 0; i < iterations; i++) {
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);
278 System.out.println();
279 System.out.println(
"get");
280 time = Clock.currentNanos();
281 for (
int i = 0; i < iterations; i++) {
284 final long intmapGetTime = (Clock.currentNanos() - time);
285 out.println(
" iimap: " + intmapGetTime/1000000.0f+
"ms");
287 time = Clock.currentNanos();
288 for (
int i = 0; i < iterations; i++) {
289 map.get(pairs.
keys[i]);
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);
297 out.println(
"remove");
298 time = Clock.currentNanos();
299 for (
int i = 0; i < iterations; i++) {
302 assertEquals(0, intmap.size());
303 final long intmapRemoveTime = (Clock.currentNanos() - time);
304 out.println(
" iimap: " + intmapRemoveTime/1000000.0f+
"ms");
306 time = Clock.currentNanos();
307 for (
int i = 0; i < iterations; i++) {
308 map.remove(pairs.
keys[i]);
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);
319 final double ratioTolerance = PlatformPropsImpl.JAVA_21 ? 4 : 1.5;
326 assertTrue(
"'get' too slow", getRatio < ratioTolerance );
327 assertTrue(
"'remove' too slow", removeRatio < ratioTolerance );
331 public static void main(
final String args[])
throws IOException {
static native long currentNanos()
Returns current monotonic nanoseconds since start of this application.
An entry mapping a key to a value.
Fast HashMap for primitive data.
int remove(final int key)
Removes the key-value mapping from this map.
boolean containsKey(final int key)
boolean containsValue(final int value)
int getKeyNotFoundValue()
Returns the value which is returned if no value has been found for the specified key.
Iterator< Entry > iterator()
Returns a new Iterator.
int put(final int key, final int value)
Maps the key to the specified value.
int setKeyNotFoundValue(final int newKeyNotFoundValue)
Sets the new key not found value.
Object clone()
Disclaimer: If the value type doesn't implement clone(), only the reference is copied.
int get(final int key)
Returns the value to which the specified key is mapped, or getKeyNotFoundValue if this map contains n...
void testPutRemove()
Test of put method, of class IntIntHashMap.
static void main(final String args[])