From eb33345f2bf83f35fa9c425f9d23a5f0a7107983 Mon Sep 17 00:00:00 2001 From: Sven Gothel Date: Mon, 10 May 2010 09:25:20 +0200 Subject: Move HashMapTest to junit.run; Unify classes to only 'Test' in their name if they are a junit test. --- .../com/jogamp/common/util/IntIntHashMapTest.java | 167 ++++++++ .../com/jogamp/common/util/LongIntHashMapTest.java | 167 ++++++++ .../gluegen/test/junit/generation/BaseClass.java | 428 +++++++++++++++++++++ .../gluegen/test/junit/generation/BaseTest1.java | 428 --------------------- .../test/junit/generation/BindingJNILibLoader.java | 8 +- .../test/junit/generation/Test1p1JavaEmitter.java | 14 +- .../generation/Test1p2ProcAddressEmitter.java | 16 +- .../test/junit/generation/test1-gluegen.cfg | 2 +- .../test/junit/generation/test1p1-gluegen.cfg | 8 +- .../test/junit/generation/test1p2-gluegen.cfg | 18 +- 10 files changed, 795 insertions(+), 461 deletions(-) create mode 100644 src/junit/com/jogamp/common/util/IntIntHashMapTest.java create mode 100644 src/junit/com/jogamp/common/util/LongIntHashMapTest.java create mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java delete mode 100644 src/junit/com/jogamp/gluegen/test/junit/generation/BaseTest1.java (limited to 'src') diff --git a/src/junit/com/jogamp/common/util/IntIntHashMapTest.java b/src/junit/com/jogamp/common/util/IntIntHashMapTest.java new file mode 100644 index 0000000..bc02947 --- /dev/null +++ b/src/junit/com/jogamp/common/util/IntIntHashMapTest.java @@ -0,0 +1,167 @@ +/** + * Created on Sunday, March 28 2010 21:01 + */ +package com.jogamp.common.util; + +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.*; +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 map = new HashMap(); + + // 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 entry : map.entrySet()) { + assertTrue(intmap.containsKey(entry.getKey())); + assertTrue(intmap.containsValue(entry.getValue())); + } + + int i = 0; + for (Entry entry : map.entrySet()) { + assertEquals((int)entry.getValue(), intmap.remove(entry.getKey())); + assertEquals(map.size() - i - 1, intmap.size()); + i++; + } + + } + + @Test + public void iteratorTest() { + + final IntIntHashMap intmap = new IntIntHashMap(iterations); + + for (int i = 0; i < iterations; i++) { + intmap.put(rndKeys[i], rndValues[i]); + } + + Iterator iterator = intmap.iterator(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + + int n = 0; + while (iterator.hasNext()) { + IntIntHashMap.Entry entry = (IntIntHashMap.Entry)iterator.next(); + assertNotNull(entry); + n++; + } + assertEquals(intmap.size(), n); + +// out.println(intmap); + + } + + @Test + public void benchmark() { + + // simple benchmark + final IntIntHashMap intmap = new IntIntHashMap(1024); + final HashMap map = new HashMap(1024); + + out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()); + + out.println("put"); + long time = nanoTime(); + for (int i = 0; i < iterations; i++) { + intmap.put(rndKeys[i], rndValues[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]); + } + long mapPutTime = (nanoTime() - time); + out.println(" map: " + mapPutTime/1000000.0f+"ms"); + + + System.out.println(); + System.out.println("get"); + long intmapGetTime = (nanoTime() - time); + out.println(" iimap: " + intmapGetTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + intmap.get(rndValues[i]); + } + + long mapGetTime = (nanoTime() - time); + out.println(" map: " + mapGetTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + map.get(rndValues[i]); + } + + + out.println(); + out.println("remove"); + long intmapRemoveTime = (nanoTime() - time); + out.println(" iimap: " + intmapRemoveTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + intmap.remove(rndValues[i]); + } + + long mapRemoveTime = (nanoTime() - time); + out.println(" map: " + mapRemoveTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + map.remove(rndValues[i]); + } + + assertTrue("'put' to slow", intmapPutTime <= mapPutTime); + assertTrue("'get' to slow", intmapGetTime <= mapGetTime); + assertTrue("'remove' to slow", intmapRemoveTime <= mapRemoveTime); + } + + +} diff --git a/src/junit/com/jogamp/common/util/LongIntHashMapTest.java b/src/junit/com/jogamp/common/util/LongIntHashMapTest.java new file mode 100644 index 0000000..b51211c --- /dev/null +++ b/src/junit/com/jogamp/common/util/LongIntHashMapTest.java @@ -0,0 +1,167 @@ +/** + * Created on Sunday, March 28 2010 21:01 + */ +package com.jogamp.common.util; + +import java.util.Iterator; +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 map = new HashMap(); + + // 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 entry : map.entrySet()) { + assertTrue(intmap.containsKey(entry.getKey())); + assertTrue(intmap.containsValue(entry.getValue())); + } + + int i = 0; + for (Entry entry : map.entrySet()) { + assertEquals((int)entry.getValue(), intmap.remove(entry.getKey())); + assertEquals(map.size() - i - 1, intmap.size()); + i++; + } + + } + + @Test + public void iteratorTest() { + + final LongIntHashMap map = new LongIntHashMap(iterations); + + for (int i = 0; i < iterations; i++) { + map.put(rndKeys[i], rndValues[i]); + } + + Iterator iterator = map.iterator(); + assertNotNull(iterator); + assertTrue(iterator.hasNext()); + + int n = 0; + while (iterator.hasNext()) { + LongIntHashMap.Entry entry = (LongIntHashMap.Entry)iterator.next(); + assertNotNull(entry); + n++; + } + assertEquals(map.size(), n); + +// out.println(intmap); + + } + + @Test + public void benchmark() { + + // simple benchmark + final LongIntHashMap intmap = new LongIntHashMap(1024); + final HashMap map = new HashMap(1024); + + out.println(intmap.getClass().getName()+" vs "+map.getClass().getName()); + + out.println("put"); + long time = nanoTime(); + for (int i = 0; i < iterations; i++) { + intmap.put(rndKeys[i], rndValues[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]); + } + long mapPutTime = (nanoTime() - time); + out.println(" map: " + mapPutTime/1000000.0f+"ms"); + + + System.out.println(); + System.out.println("get"); + long intmapGetTime = (nanoTime() - time); + out.println(" iimap: " + intmapGetTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + intmap.get(rndValues[i]); + } + + long mapGetTime = (nanoTime() - time); + out.println(" map: " + mapGetTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + map.get(rndValues[i]); + } + + + out.println(); + out.println("remove"); + long intmapRemoveTime = (nanoTime() - time); + out.println(" iimap: " + intmapRemoveTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + intmap.remove(rndValues[i]); + } + + long mapRemoveTime = (nanoTime() - time); + out.println(" map: " + mapRemoveTime/1000000.0f+"ms"); + for (int i = 0; i < iterations; i++) { + map.remove(rndValues[i]); + } + + assertTrue("'put' to slow", intmapPutTime <= mapPutTime); + assertTrue("'get' to slow", intmapGetTime <= mapGetTime); + assertTrue("'remove' to slow", intmapRemoveTime <= mapRemoveTime); + } + + +} diff --git a/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java new file mode 100644 index 0000000..fec8e6d --- /dev/null +++ b/src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java @@ -0,0 +1,428 @@ +/* + * Copyright (c) 2010 Sven Gothel. All Rights Reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * - Redistribution of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistribution 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. + * + * Neither the name Sven Gothel or the names of + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * + * This software is provided "AS IS," without a warranty of any kind. ALL + * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, + * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A + * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN + * MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL NOT BE LIABLE FOR + * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR + * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR + * ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR + * DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE + * DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, + * ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, EVEN IF + * SVEN GOTHEL HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + */ + +package com.jogamp.gluegen.test.junit.generation; + +import com.jogamp.common.nio.Buffers; +import com.jogamp.common.nio.PointerBuffer; +import com.jogamp.common.nio.Int64Buffer; +import com.jogamp.common.os.Platform; +import java.nio.*; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.net.URISyntaxException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.util.logging.Level; +import java.util.logging.Logger; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static java.lang.System.*; +import static com.jogamp.gluegen.test.junit.generation.BuildEnvironment.*; + +/** + * @author Michael Bien + * @author Sven Gothel + */ +public class BaseClass { + + /** + * Verifies the existence and creation of the generated class. + */ + public void testClassExist(String name) throws Exception { + String ifName = "com.jogamp.gluegen.test.junit.generation.Binding"+name; + String implName = "com.jogamp.gluegen.test.junit.generation.impl.Binding"+name+"Impl"; + + Class clazzIf = Class.forName(ifName); + Class clazzImpl = Class.forName(implName); + + Assert.assertNotNull(ifName+" does not exist", clazzIf); + Assert.assertNotNull(implName+" does not exist", clazzImpl); + + Assert.assertEquals((int)1, clazzIf.getDeclaredField("CONSTANT_ONE").get(null)); + + Object obj = clazzImpl.newInstance(); + Assert.assertTrue("Not of type "+ifName, clazzIf.isAssignableFrom(obj.getClass())); + Assert.assertTrue("Not of type com.jogamp.gluegen.test.junit.generation.Bindingtest1", (obj instanceof com.jogamp.gluegen.test.junit.generation.Bindingtest1)); + } + + /** + * Verifies if all generated method signatures are completed, + * ie a compilation only coverage test without functional tests. + */ + public void chapter__TestCoverageSignature(Bindingtest1 binding) throws Exception { + int i; + long result; + long context = 0; + ByteBuffer bb=null; + Int64Buffer lb=null; + PointerBuffer pb=null; + IntBuffer ib=null; + long[] larray = null; + int larray_offset = 0; + String str=null; + String[] strings = null; + int[] iarray = null; + int iarray_offset = 0; + + result = binding.arrayTestInt32(context, ib); + result = binding.arrayTestInt32(context, iarray, iarray_offset); + + result = binding.arrayTestInt64(context, lb); + result = binding.arrayTestInt64(context, larray, larray_offset); + + result = binding.arrayTestFoo1(context, lb); + result = binding.arrayTestFoo1(context, larray, larray_offset); + result = binding.arrayTestFooNioOnly(context, lb); + + lb = binding.arrayTestFoo2(lb); + lb = binding.arrayTestFoo2(larray, larray_offset); + + pb = binding.arrayTestFoo3ArrayToPtrPtr(lb); + pb = binding.arrayTestFoo3PtrPtr(pb); + + result = binding.bufferTest(bb); + result = binding.bufferTestNioOnly(bb); + + result = binding.doubleTest(context, bb, lb, bb, lb); + result = binding.doubleTest(context, bb, larray, larray_offset, bb, larray, larray_offset); + result = binding.doubleTestNioOnly(context, bb, lb, bb, lb); + + result = binding.mixedTest(context, bb, lb); + result = binding.mixedTest(context, bb, larray, larray_offset); + result = binding.mixedTestNioOnly(context, bb, lb); + + result = binding.nopTest(); + + i = binding.strToInt(str); + str = binding.intToStr(i); + + i = binding.stringArrayRead(strings, i); + + i = binding.intArrayRead(ib, i); + i = binding.intArrayRead(iarray, iarray_offset, i); + + long cfg=0; + cfg = binding.typeTestAnonSingle(cfg); + pb = binding.typeTestAnonPointer(pb); + } + + /** + * Verifies if all methods / signatures are properly generated, + * can be invoked and functions. + * This is a compilation (coverage) and runtime time (semantic) test. + * This covers indirect primitive arrays and direct NIO buffers. + */ + public void chapter03TestCoverageFunctionalityDirectNIOAndPrimitiveArray(Bindingtest1 binding) throws Exception { + int i; + long result; + + long context = 1; + Int64Buffer lb = Int64Buffer.allocateDirect(1); + lb.put(0, 10); + + ByteBuffer bb2 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG); + Int64Buffer bb2L = Int64Buffer.wrap(bb2); + bb2L.put(0, 100); + + IntBuffer ib1 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT * Bindingtest1.ARRAY_SIZE).asIntBuffer(); + for(i=0; i clazzIf = Class.forName(ifName); - Class clazzImpl = Class.forName(implName); - - Assert.assertNotNull(ifName+" does not exist", clazzIf); - Assert.assertNotNull(implName+" does not exist", clazzImpl); - - Assert.assertEquals((int)1, clazzIf.getDeclaredField("CONSTANT_ONE").get(null)); - - Object obj = clazzImpl.newInstance(); - Assert.assertTrue("Not of type "+ifName, clazzIf.isAssignableFrom(obj.getClass())); - Assert.assertTrue("Not of type com.jogamp.gluegen.test.junit.generation.BindingTest1", (obj instanceof com.jogamp.gluegen.test.junit.generation.BindingTest1)); - } - - /** - * Verifies if all generated method signatures are completed, - * ie a compilation only coverage test without functional tests. - */ - public void chapter__TestCoverageSignature(BindingTest1 binding) throws Exception { - int i; - long result; - long context = 0; - ByteBuffer bb=null; - Int64Buffer lb=null; - PointerBuffer pb=null; - IntBuffer ib=null; - long[] larray = null; - int larray_offset = 0; - String str=null; - String[] strings = null; - int[] iarray = null; - int iarray_offset = 0; - - result = binding.arrayTestInt32(context, ib); - result = binding.arrayTestInt32(context, iarray, iarray_offset); - - result = binding.arrayTestInt64(context, lb); - result = binding.arrayTestInt64(context, larray, larray_offset); - - result = binding.arrayTestFoo1(context, lb); - result = binding.arrayTestFoo1(context, larray, larray_offset); - result = binding.arrayTestFooNioOnly(context, lb); - - lb = binding.arrayTestFoo2(lb); - lb = binding.arrayTestFoo2(larray, larray_offset); - - pb = binding.arrayTestFoo3ArrayToPtrPtr(lb); - pb = binding.arrayTestFoo3PtrPtr(pb); - - result = binding.bufferTest(bb); - result = binding.bufferTestNioOnly(bb); - - result = binding.doubleTest(context, bb, lb, bb, lb); - result = binding.doubleTest(context, bb, larray, larray_offset, bb, larray, larray_offset); - result = binding.doubleTestNioOnly(context, bb, lb, bb, lb); - - result = binding.mixedTest(context, bb, lb); - result = binding.mixedTest(context, bb, larray, larray_offset); - result = binding.mixedTestNioOnly(context, bb, lb); - - result = binding.nopTest(); - - i = binding.strToInt(str); - str = binding.intToStr(i); - - i = binding.stringArrayRead(strings, i); - - i = binding.intArrayRead(ib, i); - i = binding.intArrayRead(iarray, iarray_offset, i); - - long cfg=0; - cfg = binding.typeTestAnonSingle(cfg); - pb = binding.typeTestAnonPointer(pb); - } - - /** - * Verifies if all methods / signatures are properly generated, - * can be invoked and functions. - * This is a compilation (coverage) and runtime time (semantic) test. - * This covers indirect primitive arrays and direct NIO buffers. - */ - public void chapter03TestCoverageFunctionalityDirectNIOAndPrimitiveArray(BindingTest1 binding) throws Exception { - int i; - long result; - - long context = 1; - Int64Buffer lb = Int64Buffer.allocateDirect(1); - lb.put(0, 10); - - ByteBuffer bb2 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_LONG); - Int64Buffer bb2L = Int64Buffer.wrap(bb2); - bb2L.put(0, 100); - - IntBuffer ib1 = Buffers.newDirectByteBuffer(Buffers.SIZEOF_INT * BindingTest1.ARRAY_SIZE).asIntBuffer(); - for(i=0; i