diff options
author | Michael Bien <[email protected]> | 2010-04-08 02:52:52 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-04-08 02:52:52 +0200 |
commit | 31504e67e5f38d07d442ee02aef456de71a2fa90 (patch) | |
tree | 68acadbdf41639edc335ee4f1ef31005998e67d8 /test | |
parent | d3ec98ea43cf0e38860e64c111c7b09ed4b363bd (diff) |
initial import of IntIntHashmap + JUnitTest including a benchmark.
Diffstat (limited to 'test')
-rw-r--r-- | test/junit/com/jogamp/common/util/IntIntHashMapTest.java | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/test/junit/com/jogamp/common/util/IntIntHashMapTest.java b/test/junit/com/jogamp/common/util/IntIntHashMapTest.java new file mode 100644 index 0000000..6ccf0e3 --- /dev/null +++ b/test/junit/com/jogamp/common/util/IntIntHashMapTest.java @@ -0,0 +1,139 @@ +/** + * 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 + */ +public class IntIntHashMapTest { + + private static int iterations; + private static int[] 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 int[iterations]; + rndValues = new int[iterations]; + for (int i = 0; i < iterations; i++) { + rndValues[i] = valueRnd.nextInt(); + rndKeys[i] = keyRnd.nextInt(); + } + + } + /** + * Test of put method, of class IntIntHashMap. + */ + @Test + public void testPutRemove() { + + final IntIntHashMap intmap = new IntIntHashMap(); + final HashMap<Integer, Integer> map = new HashMap<Integer, 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<Integer, Integer> entry : map.entrySet()) { + assertTrue(intmap.containsKey(entry.getKey())); + assertTrue(intmap.containsValue(entry.getValue())); + } + + int i = 0; + for (Entry<Integer, 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 IntIntHashMap intmap = new IntIntHashMap(1024); + final HashMap<Integer, Integer> map = new HashMap<Integer, 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); + } + + +} |