Skip to content

Commit

Permalink
Fix HastMapTests - Clone: Use unique random pairs: key,value
Browse files Browse the repository at this point in the history
  • Loading branch information
sgothel committed Jun 26, 2011
1 parent 334baf0 commit ad3dc39
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 119 deletions.
3 changes: 2 additions & 1 deletion make/scripts/runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,12 @@ function onetest() {
#onetest com.jogamp.common.util.locks.TestRecursiveLock01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestArrayHashSet01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.IntIntHashMapTest 2>&1 | tee -a $LOG
onetest com.jogamp.common.util.IntObjectHashMapTest 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.LongIntHashMapTest 2>&1 | tee -a $LOG
#onetest com.jogamp.common.nio.TestBuffersFloatDoubleConversion 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.PCPPTest 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.test.TestPointerBufferEndian 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.test.TestStructAccessorEndian 2>&1 | tee -a $LOG
#onetest com.jogamp.gluegen.test.junit.generation.Test1p1JavaEmitter 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestPlatform01 2>&1 | tee -a $LOG
onetest com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG
#onetest com.jogamp.common.util.TestIOUtil01 2>&1 | tee -a $LOG
51 changes: 51 additions & 0 deletions src/junit/com/jogamp/common/util/IntCloneable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2010 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/
package com.jogamp.common.util;

public class IntCloneable implements Cloneable {
private int i;

public IntCloneable(int i) { this.i = i; }

public int intValue() { return i; }

@Override
public Object clone() {
return new IntCloneable(i);
}

@Override
public boolean equals(Object obj) {
if(this == obj) { return true; }
if (obj instanceof IntCloneable) {
IntCloneable v = (IntCloneable)obj;
return i == v.i ;
}
return false;
}
}
62 changes: 24 additions & 38 deletions src/junit/com/jogamp/common/util/IntIntHashMapTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Random;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
Expand All @@ -49,25 +48,12 @@
public class IntIntHashMapTest {

private static int iterations;
private static int[] rndKeys;
private static int[] rndValues;
private static IntIntUniqueRndValues pairs;

@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();
}
iterations = 10000;
pairs = new IntIntUniqueRndValues(iterations);
}

/**
Expand All @@ -81,14 +67,14 @@ public void testPutRemove() {

// put
for (int i = 0; i < iterations; i++) {
intmap.put(rndKeys[i], rndValues[i]);
intmap.put(pairs.keys[i], pairs.values[i]);

assertTrue(intmap.containsValue(rndValues[i]));
assertTrue(intmap.containsKey(rndKeys[i]));
assertTrue(intmap.containsValue(pairs.values[i]));
assertTrue(intmap.containsKey(pairs.keys[i]));
}

for (int i = 0; i < iterations; i++) {
map.put(rndKeys[i], rndValues[i]);
map.put(pairs.keys[i], pairs.values[i]);
}

assertEquals(map.size(), intmap.size());
Expand All @@ -113,7 +99,7 @@ public void iteratorTest() {
final IntIntHashMap intmap = new IntIntHashMap(iterations);

for (int i = 0; i < iterations; i++) {
intmap.put(rndKeys[i], rndValues[i]);
intmap.put(pairs.keys[i], pairs.values[i]);
}

Iterator<IntIntHashMap.Entry> iterator = intmap.iterator();
Expand Down Expand Up @@ -141,7 +127,7 @@ public void cloneTest() {
intmap.setKeyNotFoundValue(-1);

for (int i = 0; i < smallSize; i++) {
intmap.put(rndKeys[i], rndValues[i]);
intmap.put(pairs.keys[i], pairs.values[i]);
}
assertEquals(intmap.size(), smallSize);

Expand Down Expand Up @@ -175,10 +161,10 @@ public void cloneTest() {
assertEquals(intmapCopy.size(), n);

for (int i = 0; i < smallSize; i++) {
assertTrue(intmap.containsValue(rndValues[i]));
assertTrue(intmap.containsKey(rndKeys[i]));
assertTrue(intmapCopy.containsValue(rndValues[i]));
assertTrue(intmapCopy.containsKey(rndKeys[i]));
assertTrue(intmap.containsValue(pairs.values[i]));
assertTrue(intmap.containsKey(pairs.keys[i]));
assertTrue(intmapCopy.containsValue(pairs.values[i]));
assertTrue(intmapCopy.containsKey(pairs.keys[i]));
}

// out.println(intmap);
Expand All @@ -195,7 +181,7 @@ public void capacityTest() {

assertEquals(intmap.capacity(), capacity);
for (int i = 0; i < fixedSize; i++) {
intmap.put(rndKeys[i], rndValues[i]);
intmap.put(pairs.keys[i], pairs.values[i]);
}
assertEquals(intmap.size(), fixedSize);
assertEquals(intmap.capacity(), capacity);
Expand Down Expand Up @@ -233,10 +219,10 @@ public void capacityTest() {
assertEquals(intmapCopy.capacity(), capacity);

for (int i = 0; i < fixedSize; i++) {
assertTrue(intmap.containsValue(rndValues[i]));
assertTrue(intmap.containsKey(rndKeys[i]));
assertTrue(intmapCopy.containsValue(rndValues[i]));
assertTrue(intmapCopy.containsKey(rndKeys[i]));
assertTrue(intmap.containsValue(pairs.values[i]));
assertTrue(intmap.containsKey(pairs.keys[i]));
assertTrue(intmapCopy.containsValue(pairs.values[i]));
assertTrue(intmapCopy.containsKey(pairs.keys[i]));
}

// out.println(intmap);
Expand All @@ -261,14 +247,14 @@ void benchmark(boolean warmup) {
out.println("put");
long time = nanoTime();
for (int i = 0; i < iterations; i++) {
intmap.put(rndKeys[i], rndValues[i]);
intmap.put(pairs.keys[i], pairs.values[i]);
}
long intmapPutTime = (nanoTime() - time);
out.println(" iimap: " + intmapPutTime/1000000.0f+"ms");

time = nanoTime();
for (int i = 0; i < iterations; i++) {
map.put(rndKeys[i], rndValues[i]);
map.put(pairs.keys[i], pairs.values[i]);
}
long mapPutTime = (nanoTime() - time);
out.println(" map: " + mapPutTime/1000000.0f+"ms");
Expand All @@ -278,14 +264,14 @@ void benchmark(boolean warmup) {
System.out.println("get");
time = nanoTime();
for (int i = 0; i < iterations; i++) {
intmap.get(rndKeys[i]);
intmap.get(pairs.keys[i]);
}
long intmapGetTime = (nanoTime() - time);
out.println(" iimap: " + intmapGetTime/1000000.0f+"ms");

time = nanoTime();
for (int i = 0; i < iterations; i++) {
map.get(rndKeys[i]);
map.get(pairs.keys[i]);
}
long mapGetTime = (nanoTime() - time);
out.println(" map: " + mapGetTime/1000000.0f+"ms");
Expand All @@ -295,15 +281,15 @@ void benchmark(boolean warmup) {
out.println("remove");
time = nanoTime();
for (int i = 0; i < iterations; i++) {
intmap.remove(rndKeys[i]);
intmap.remove(pairs.keys[i]);
}
assertEquals(0, intmap.size());
long intmapRemoveTime = (nanoTime() - time);
out.println(" iimap: " + intmapRemoveTime/1000000.0f+"ms");

time = nanoTime();
for (int i = 0; i < iterations; i++) {
map.remove(rndKeys[i]);
map.remove(pairs.keys[i]);
}
assertEquals(0, map.size());
long mapRemoveTime = (nanoTime() - time);
Expand Down
60 changes: 60 additions & 0 deletions src/junit/com/jogamp/common/util/IntIntObjUniqueRndValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2011 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/

/**
* Created on Sunday, March 28 2010 21:01
*/
package com.jogamp.common.util;

import java.util.HashSet;
import java.util.Random;

public class IntIntObjUniqueRndValues {

public int[] keys;
public IntCloneable[] values;

public IntIntObjUniqueRndValues(int size) {
// final int keySeed = 42;
// final int valueSeed = 23;
Random keyRnd = new Random(/*keySeed*/);
Random valueRnd = new Random(/*valueSeed*/);

HashSet<Integer> uniqueKeys = new HashSet<Integer>(size);
keys = new int[size];
values = new IntCloneable[size];
while (uniqueKeys.size() < size) {
int k = keyRnd.nextInt();
if( uniqueKeys.add( new Integer(k) ) ) {
final int i = uniqueKeys.size()-1;
keys[i] = k;
values[i] = new IntCloneable(valueRnd.nextInt());
}
}
}
}
60 changes: 60 additions & 0 deletions src/junit/com/jogamp/common/util/IntIntUniqueRndValues.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright 2011 JogAmp Community. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of JogAmp Community.
*/

/**
* Created on Sunday, March 28 2010 21:01
*/
package com.jogamp.common.util;

import java.util.HashSet;
import java.util.Random;

public class IntIntUniqueRndValues {

public int[] keys;
public int[] values;

public IntIntUniqueRndValues(int size) {
// final int keySeed = 42;
// final int valueSeed = 23;
Random keyRnd = new Random(/*keySeed*/);
Random valueRnd = new Random(/*valueSeed*/);

HashSet<Integer> uniqueKeys = new HashSet<Integer>(size);
keys = new int[size];
values = new int[size];
while (uniqueKeys.size() < size) {
int k = keyRnd.nextInt();
if( uniqueKeys.add( new Integer(k) ) ) {
final int i = uniqueKeys.size()-1;
keys[i] = k;
values[i] = valueRnd.nextInt();
}
}
}
}
Loading

0 comments on commit ad3dc39

Please sign in to comment.