summaryrefslogtreecommitdiffstats
path: root/test/junit
diff options
context:
space:
mode:
authorSven Gothel <[email protected]>2010-04-13 14:52:51 +0200
committerSven Gothel <[email protected]>2010-04-13 14:52:51 +0200
commit74e0505c621c4573306675e0449e6dc73996eba1 (patch)
treed953e483f75dfd45db36fcbda8978f903cbdc12e /test/junit
parentdb69db1df249c69c996a35e565595e0d8d2d7856 (diff)
parentd4b189bb12ec18e2e3893a7e83074b06b09f2605 (diff)
Merge branch 'master' of github.com:mbien/gluegen
Diffstat (limited to 'test/junit')
-rw-r--r--test/junit/com/jogamp/common/util/LongIntHashMapTest.java140
1 files changed, 140 insertions, 0 deletions
diff --git a/test/junit/com/jogamp/common/util/LongIntHashMapTest.java b/test/junit/com/jogamp/common/util/LongIntHashMapTest.java
new file mode 100644
index 0000000..67a610f
--- /dev/null
+++ b/test/junit/com/jogamp/common/util/LongIntHashMapTest.java
@@ -0,0 +1,140 @@
+/**
+ * Created on Sunday, March 28 2010 21:01
+ */
+package com.jogamp.common.util;
+
+import java.util.HashMap;
+import java.util.Map.Entry;
+import java.util.Random;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import static java.lang.System.*;
+
+/**
+ *
+ * @author Michael Bien
+ * @author Simon Goller
+ */
+public class LongIntHashMapTest {
+
+ private static int iterations;
+ private static long[] rndKeys;
+ private static int[] rndValues;
+
+ @BeforeClass
+ public static void init() {
+
+ iterations = 20000;
+ final int keySeed = 42;
+ final int valueSeed = 23;
+
+ Random keyRnd = new Random(/*keySeed*/);
+ Random valueRnd = new Random(/*valueSeed*/);
+
+ rndKeys = new long[iterations];
+ rndValues = new int[iterations];
+ for (int i = 0; i < iterations; i++) {
+ rndValues[i] = valueRnd.nextInt();
+ rndKeys[i] = keyRnd.nextLong();
+ }
+
+ }
+ /**
+ * Test of put method, of class LongIntHashMap.
+ */
+ @Test
+ public void testPutRemove() {
+
+ final LongIntHashMap intmap = new LongIntHashMap();
+ final HashMap<Long, Integer> map = new HashMap<Long, Integer>();
+
+ // put
+ for (int i = 0; i < iterations; i++) {
+ intmap.put(rndKeys[i], rndValues[i]);
+
+ assertTrue(intmap.containsValue(rndValues[i]));
+ assertTrue(intmap.containsKey(rndKeys[i]));
+ }
+
+ for (int i = 0; i < iterations; i++) {
+ map.put(rndKeys[i], rndValues[i]);
+ }
+
+ assertEquals(map.size(), intmap.size());
+
+ for (Entry<Long, Integer> entry : map.entrySet()) {
+ assertTrue(intmap.containsKey(entry.getKey()));
+ assertTrue(intmap.containsValue(entry.getValue()));
+ }
+
+ int i = 0;
+ for (Entry<Long, Integer> entry : map.entrySet()) {
+ assertEquals((int)entry.getValue(), intmap.remove(entry.getKey()));
+ assertEquals(map.size() - i - 1, intmap.size());
+ i++;
+ }
+
+ }
+
+ @Test
+ public void benchmark() {
+
+ // simple benchmark
+ final LongIntHashMap intmap = new LongIntHashMap(1024);
+ final HashMap<Long, Integer> map = new HashMap<Long, Integer>(1024);
+
+ out.println("put");
+ long time = currentTimeMillis();
+ for (int i = 0; i < iterations; i++) {
+ intmap.put(rndKeys[i], rndValues[i]);
+ }
+ long intmapTime = (currentTimeMillis() - time);
+ out.println(" iimap: " + intmapTime+"ms");
+
+
+ time = currentTimeMillis();
+ for (int i = 0; i < iterations; i++) {
+ map.put(rndKeys[i], rndValues[i]);
+ }
+ long mapTime = (currentTimeMillis() - time);
+ out.println(" map: " + mapTime+"ms");
+
+ assertTrue(intmapTime <= mapTime);
+
+
+ System.out.println();
+ System.out.println("get");
+ intmapTime = (currentTimeMillis() - time);
+ out.println(" iimap: " + intmapTime+"ms");
+ for (int i = 0; i < iterations; i++) {
+ intmap.get(rndValues[i]);
+ }
+
+ mapTime = (currentTimeMillis() - time);
+ out.println(" map: " + mapTime+"ms");
+ for (int i = 0; i < iterations; i++) {
+ map.get(rndValues[i]);
+ }
+ assertTrue(intmapTime <= mapTime);
+
+
+ out.println();
+ out.println("remove");
+ intmapTime = (currentTimeMillis() - time);
+ out.println(" iimap: " + intmapTime+"ms");
+ for (int i = 0; i < iterations; i++) {
+ intmap.remove(rndValues[i]);
+ }
+
+ mapTime = (currentTimeMillis() - time);
+ out.println(" map: " + mapTime+"ms");
+ for (int i = 0; i < iterations; i++) {
+ map.remove(rndValues[i]);
+ }
+
+ assertTrue(intmapTime <= mapTime);
+ }
+
+
+}