diff options
author | Michael Bien <[email protected]> | 2010-02-12 03:05:20 +0100 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-02-12 03:05:20 +0100 |
commit | 3f1bda99f1e4aec19c1c71de5324814bfc1c2eaa (patch) | |
tree | 005bfbfec0d8f69c43c45cf0a8ebd670fc5a422d /test/junit/com/sun | |
parent | 0fa706b4eef533ead671310a9a7e063a910198cb (diff) |
added test target to main build script.
added StructAccessorTest, refactored other tests.
updated project files, paths etc.
Diffstat (limited to 'test/junit/com/sun')
-rw-r--r-- | test/junit/com/sun/gluegen/AbstractTest.java | 120 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/BasicTest.java | 101 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/GlueGenTest.java | 206 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/StructAccessorTest.java | 93 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/StructValidator.java | 77 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/build.xml | 10 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/struct.cfg | 7 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/struct.h | 21 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/test.c | 4 | ||||
-rw-r--r-- | test/junit/com/sun/gluegen/test.h | 2 |
10 files changed, 430 insertions, 211 deletions
diff --git a/test/junit/com/sun/gluegen/AbstractTest.java b/test/junit/com/sun/gluegen/AbstractTest.java new file mode 100644 index 0000000..9262347 --- /dev/null +++ b/test/junit/com/sun/gluegen/AbstractTest.java @@ -0,0 +1,120 @@ +package com.sun.gluegen; + +import java.io.File; +import java.net.URISyntaxException; +import org.apache.tools.ant.DefaultLogger; +import org.apache.tools.ant.Project; +import org.apache.tools.ant.ProjectHelper; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; + +import static java.lang.System.*; + +/** + * @author Michael Bien + */ +@Ignore +public abstract class AbstractTest { + + static final Project project = new Project(); + + protected static String gluegenRoot; + protected static String path; + protected static String output; + + + @BeforeClass + public static void setUp() throws Exception { + + out.println(" - - - System info - - - "); + out.println("OS: " + System.getProperty("os.name")); + out.println("VM: " + System.getProperty("java.vm.name")); + + // setup paths + try { + File executionRoot = new File(AbstractTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()); + out.println("execution root: " + executionRoot); + gluegenRoot = executionRoot.getParentFile().getParentFile().getParentFile().getParentFile().toString(); + out.println("gluegen project root: " + gluegenRoot); + } catch (URISyntaxException ex) { + throw new RuntimeException("can not determine gluegen root", ex); + } + + path = gluegenRoot + "/test/junit/com/sun/gluegen"; + output = gluegenRoot + "/build/test"; + + out.println("path: "+path); + out.println("output: "+output); + out.println(" - - - - - - - - - - - - "); + + deleteDirectory(new File(output+"/gensrc")); + + //setup ant build file + project.setBaseDir(new File(gluegenRoot)); + + DefaultLogger logger = new DefaultLogger(); + logger.setErrorPrintStream(err); + logger.setOutputPrintStream(out); + logger.setMessageOutputLevel(Project.MSG_INFO); + project.addBuildListener(logger); + + project.init(); + + File buildFile = new File(path, "build.xml"); + ProjectHelper.configureProject(project, buildFile); + } + + @AfterClass + public static void tearDown() { +// deleteDirectory(new File(output)); + } + + /** + * fails if ant script fails (which is a good thing). + * executeTarget throws RuntimeException on failure + */ + public final void compileJava() { + project.executeTarget("compile.java"); + } + + /** + * fails if ant script fails (which is a good thing) + * executeTarget throws RuntimeException on failure + */ + public final void compileNatives() { + project.executeTarget("compile.native"); + } + + static final void generate(String config) { + out.println("generate: "+config); + GlueGen.main( + new String[] { + "-I"+path, + "-O"+output+"/gensrc", +// "-Ecom.sun.gluegen.DebugEmitter", + "-C"+path+"/"+config+".cfg", + path+"/"+config+".h" + } + ); + out.println("done"); + } + + static final void deleteDirectory(File path) { + if(path.exists()) { + + File[] files = path.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isDirectory()) { + deleteDirectory(files[i]); + } else { + files[i].delete(); + } + } + + path.delete(); + } + } + + +} diff --git a/test/junit/com/sun/gluegen/BasicTest.java b/test/junit/com/sun/gluegen/BasicTest.java new file mode 100644 index 0000000..89a4c21 --- /dev/null +++ b/test/junit/com/sun/gluegen/BasicTest.java @@ -0,0 +1,101 @@ +package com.sun.gluegen; + +import com.sun.gluegen.runtime.BufferFactory; +import com.sun.gluegen.runtime.PointerBuffer; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import org.junit.Test; +import static java.lang.System.*; + +/** + * + * @author Michael Bien + */ +public class BasicTest extends AbstractTest { + + @Test + public void generateBindingTest() { + generate("test"); + } + + /** + * fails if ant script fails (which is a good thing). + * executeTarget throws RuntimeException on failure + */ + @Test + public void compileJavaTest() { + compileJava(); + } + + /* + * fails if ant script fails (which is a good thing) + * executeTarget throws RuntimeException on failure + */ + @Test + public void compileNativesTest() { + compileNatives(); + } + +// @Test + public void bindingTest() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException { + + String nativesPath = gluegenRoot + "/build/test/build/natives"; + System.load(nativesPath + "/librofl.so"); + + Object bindingTest = Class.forName("test.BindingTest").newInstance(); + + // test values + ByteBuffer dbb = BufferFactory.newDirectByteBuffer(32); + ByteBuffer bb = ByteBuffer.allocate(32).order(ByteOrder.nativeOrder()); + + PointerBuffer dpb = PointerBuffer.allocateDirect(32); + PointerBuffer pb = PointerBuffer.allocate(32); + + long[] array = new long[] {1,2,3,4,5,6,7,8,9}; + int offset = 0; + long id = 42; + + // invoke everything public + Method[] methods = bindingTest.getClass().getDeclaredMethods(); + + for (Method method : methods) { + + // prepare method parameters + Class<?>[] paramTypes = method.getParameterTypes(); + Object[] paramInstances = new Object[paramTypes.length]; + + for (int i = 0; i < paramTypes.length; i++) { + Class<?> paramType = paramTypes[i]; + if(paramType.isInstance(dbb)) { + paramInstances[i] = dbb; + }else if(paramType.isInstance(bb)) { + paramInstances[i] = bb; + }else if(paramType.isInstance(dpb)) { + paramInstances[i] = dpb; + }else if(paramType.isInstance(pb)) { + paramInstances[i] = pb; + }else if(paramType.isPrimitive()) { // TODO primitive types + paramInstances[i] = offset; + }else if(paramType.isArray()) { // TODO array types + paramInstances[i] = array; + } + } + + out.println("invoking: "+method); + out.println("with params: "); + for (Object param : paramInstances) + out.print(param+", "); + out.println(); + + // TODO fix Exception: ...Caused by: java.lang.UnsatisfiedLinkError: test.BindingTest.arrayTest0(JLjava/lang/Object;I)I + + Object result = method.invoke(bindingTest, paramInstances); + out.println("result: "+result); + out.println("success"); + } + + } + +} diff --git a/test/junit/com/sun/gluegen/GlueGenTest.java b/test/junit/com/sun/gluegen/GlueGenTest.java deleted file mode 100644 index c21f1ba..0000000 --- a/test/junit/com/sun/gluegen/GlueGenTest.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.sun.gluegen; - -import com.sun.gluegen.runtime.BufferFactory; -import com.sun.gluegen.runtime.PointerBuffer; -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.apache.tools.ant.DefaultLogger; -import org.apache.tools.ant.Project; -import org.apache.tools.ant.ProjectHelper; -import org.junit.Assert; -import org.junit.BeforeClass; -import org.junit.Test; - -import static java.lang.System.*; - -/** - * @author Michael Bien - */ -public class GlueGenTest { - - private static final Project project = new Project(); - - private static String gluegenRoot; - private static String path; - private static String output; - - - @BeforeClass - public static void setUpTest() throws Exception { - - out.println("System info: "); - out.println("OS: " + System.getProperty("os.name")); - out.println("VM: " + System.getProperty("java.vm.name")); - - // setup paths - try { - File executionRoot = new File(GlueGenTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()); - System.out.println("execution root: " + executionRoot); - gluegenRoot = executionRoot.getParentFile().getParentFile().getParentFile().getParentFile().toString(); - System.out.println("gluegen project root: " + gluegenRoot); - } catch (URISyntaxException ex) { - Logger.getLogger(GlueGenTest.class.getName()).log(Level.SEVERE, "can not determine gluegen root", ex); - Assert.fail(); - } - - path = gluegenRoot + "/test/junit/com/sun/gluegen"; - output = gluegenRoot + "/build/test"; - - //setup ant build file - project.setBaseDir(new File(gluegenRoot)); - - DefaultLogger logger = new DefaultLogger(); - logger.setErrorPrintStream(err); - logger.setOutputPrintStream(out); - logger.setMessageOutputLevel(Project.MSG_INFO); - project.addBuildListener(logger); - - project.init(); - - File buildFile = new File(path, "build.xml"); - ProjectHelper.configureProject(project, buildFile); - } - - @Test - public void generateBindingTest() { - - out.println("path: "+path); - out.println("output: "+output); - - String name = "test"; - - GlueGen.main( - new String[] { - "-I"+path, - "-O"+output+"/gensrc", -// "-Ecom.sun.gluegen.DebugEmitter", - "-C"+path+"/"+name+".cfg", - path+"/"+name+".h" - } - ); - } - - /* yeah, java 6 has even a compiler api... - @Test - public void compileJavaTest() throws IOException { - - out.println("compiling generated files..."); - - String source = output+"/gensrc/java/test/BindingTest.java"; - - JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); - DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>(); - StandardJavaFileManager fileManager = compiler.getStandardFileManager(collector, null, null); - - Iterable<? extends JavaFileObject> fileObj = fileManager.getJavaFileObjects(source); - - compiler.getTask( new OutputStreamWriter(out), - fileManager, - collector, - Arrays.asList("-d",output+"/build/java","-verbose"), - null, - fileObj ).call(); - - List<Diagnostic<? extends JavaFileObject>> list = collector.getDiagnostics(); - if(!list.isEmpty()) { - for (Diagnostic<? extends JavaFileObject> d : list) { - out.println("Error on line "+ d.getLineNumber()); - out.println("Compiler Message:\n"+d.getMessage(Locale.ENGLISH)); - } - Assert.fail("compilation test failed"); - } - - fileManager.close(); - - out.println("done"); - - } -*/ - - /* - * fails when ant script fails (which is a good thing). - * executeTarget throws RuntimeException on failure - */ - @Test - public void compileJavaTest() { - project.executeTarget("compile.java"); - } - - /* - * fails when ant script fails (which is a good thing) - * executeTarget throws RuntimeException on failure - */ - @Test - public void compileNativesTest() { - project.executeTarget("compile.native"); - } - - @Test - public void bindingTest() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, ClassNotFoundException, InstantiationException { - - String nativesPath = output + "/build/natives"; - System.load(nativesPath + "/libgluegen-rt.so"); - System.load(nativesPath + "/librofl.so"); - - Object bindingTest = Class.forName("test.BindingTest").newInstance(); - - // test values - ByteBuffer dbb = BufferFactory.newDirectByteBuffer(32); - ByteBuffer bb = ByteBuffer.allocate(32).order(ByteOrder.nativeOrder()); - - PointerBuffer dpb = PointerBuffer.allocateDirect(32); - PointerBuffer pb = PointerBuffer.allocate(32); - - long[] array = new long[] {1,2,3,4,5,6,7,8,9}; - int offset = 0; - long id = 42; - - // invoke everything public - Method[] methods = bindingTest.getClass().getDeclaredMethods(); - - for (Method method : methods) { - - // prepare method parameters - Class<?>[] paramTypes = method.getParameterTypes(); - Object[] paramInstances = new Object[paramTypes.length]; - - for (int i = 0; i < paramTypes.length; i++) { - Class<?> paramType = paramTypes[i]; - if(paramType.isInstance(dbb)) { - paramInstances[i] = dbb; - }else if(paramType.isInstance(bb)) { - paramInstances[i] = bb; - }else if(paramType.isInstance(dpb)) { - paramInstances[i] = dpb; - }else if(paramType.isInstance(pb)) { - paramInstances[i] = pb; - }else if(paramType.isPrimitive()) { // TODO primitive types - paramInstances[i] = offset; - }else if(paramType.isArray()) { // TODO array types - paramInstances[i] = array; - } - } - - out.println("invoking: "+method); - out.println("with params: "); - for (Object param : paramInstances) - out.print(param+", "); - out.println(); - - // TODO fix Exception: ...Caused by: java.lang.UnsatisfiedLinkError: test.BindingTest.arrayTest0(JLjava/lang/Object;I)I - - Object result = method.invoke(bindingTest, paramInstances); - out.println("result: "+result); - out.println("success"); - } - - } - - -} diff --git a/test/junit/com/sun/gluegen/StructAccessorTest.java b/test/junit/com/sun/gluegen/StructAccessorTest.java new file mode 100644 index 0000000..f89eb43 --- /dev/null +++ b/test/junit/com/sun/gluegen/StructAccessorTest.java @@ -0,0 +1,93 @@ +package com.sun.gluegen; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; +import java.util.List; +import java.util.Locale; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticCollector; +import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; +import javax.tools.StandardJavaFileManager; +import javax.tools.ToolProvider; +import junit.framework.Assert; +import org.junit.Test; +import static java.lang.System.*; + +/** + * + * @author Michael Bien + */ +public class StructAccessorTest extends AbstractTest { + + @Test + public void generateStruct() { + generate("struct"); + } + + @Test + public void compileStructJava() { + super.compileJava(); + } + + @Test + public void compileStructNatives() { + // this will only copy gluegen-rt to the right place + super.compileNatives(); + } + + @Test + public void validateGeneratedStructs() throws IOException, NoSuchMethodException, ClassNotFoundException, IllegalAccessException, InvocationTargetException { + + // compile testcase + String source = gluegenRoot + "/test/junit/com/sun/gluegen/StructValidator.java"; + compile(new File(source), gluegenRoot+"/build/test/build/classes"); + + // invoke test + Class<?> test = Class.forName("com.sun.gluegen.StructValidator"); + test.getDeclaredMethod("validate").invoke(null); + } + + private void compile(File file, String dest) throws IOException { + compile(new File[] {file}, dest); + } + + // yeah, java 6 has even a compiler api... + private void compile(File[] files, String destination) throws IOException { + + out.println("compiling files:\n" + Arrays.asList(files)); + + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + DiagnosticCollector<JavaFileObject> collector = new DiagnosticCollector<JavaFileObject>(); + StandardJavaFileManager fileManager = compiler.getStandardFileManager(collector, null, null); + + Iterable<? extends JavaFileObject> fileObj = fileManager.getJavaFileObjects(files); + + compiler.getTask( new OutputStreamWriter(out), + fileManager, + collector, + Arrays.asList("-d", destination/*, "-verbose"*/), + null, + fileObj ).call(); + + List<Diagnostic<? extends JavaFileObject>> list = collector.getDiagnostics(); + if(!list.isEmpty()) { + for (Diagnostic<? extends JavaFileObject> d : list) { + out.println("Error on line "+ d.getLineNumber()); + out.println("Compiler Message:\n"+d.getMessage(Locale.ENGLISH)); + } + Assert.fail("compilation failed"); + } + + fileManager.close(); + + out.println("done"); + + } + + + +} diff --git a/test/junit/com/sun/gluegen/StructValidator.java b/test/junit/com/sun/gluegen/StructValidator.java new file mode 100644 index 0000000..1fe845c --- /dev/null +++ b/test/junit/com/sun/gluegen/StructValidator.java @@ -0,0 +1,77 @@ +package com.sun.gluegen; + +import java.lang.reflect.InvocationTargetException; +import org.junit.Ignore; + +import static org.junit.Assert.*; + +/** + * this file will not compile unless {@link com.sun.gluegen.StructAccessorTest} has been run. + * @author Michael Bien + */ +@Ignore +public class StructValidator { + + // invoked via reflection from StructAccessorTest1 + public static void validate() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, ClassNotFoundException { + + System.out.println("validating struct accessors..."); + + float[] mu = new float[] {1, 2, 3, 4}; + float[] light = new float[] {5, 6, 7}; + int fastRendering = 1; + int shadow = 42; + int iterations = 512; + int sss = 12; + float epsilon = (float) Math.PI; + int height = 640; + int width = 480; + + structtest.RenderingConfig config = structtest.RenderingConfig.create(); + + //set + config.setLight(light); + config.setMu(mu); + config.setActvateFastRendering(fastRendering); + config.setEnableShadow(shadow); + config.setMaxIterations(iterations); + config.setEpsilon(epsilon); + config.setSuperSamplingSize(sss); + config.setWidth(width); + config.setHeight(height); + + structtest.Camera camera = config.getCamera(); + camera.getOrig().setX(1001).setY(1002).setZ(1003); + camera.getDir().setX(2001).setY(2002).setZ(2003); + + //get and validate + assertArrayEquals(mu, config.getMu()); + assertArrayEquals(light, config.getLight()); + + assertEquals(fastRendering, config.getActvateFastRendering()); + assertEquals(shadow, config.getEnableShadow()); + assertEquals(iterations, config.getMaxIterations()); + assertEquals(epsilon, config.getEpsilon(), 0.01f); + assertEquals(sss, config.getSuperSamplingSize()); + assertEquals(width, config.getWidth()); + assertEquals(height, config.getHeight()); + + assertEquals(camera.getOrig().getX(), 1001, 0.001); + assertEquals(camera.getOrig().getY(), 1002, 0.001); + assertEquals(camera.getOrig().getZ(), 1003, 0.001); + + assertEquals(camera.getDir().getX(), 2001, 0.001); + assertEquals(camera.getDir().getY(), 2002, 0.001); + assertEquals(camera.getDir().getZ(), 2003, 0.001); + + System.out.println("done"); + + } + + private static final void assertArrayEquals(float[] a, float[] b) { + for (int i = 0; i < b.length; i++) { + assertEquals(a[i], b[i], 0.0001f); + } + } + +} diff --git a/test/junit/com/sun/gluegen/build.xml b/test/junit/com/sun/gluegen/build.xml index cdc11ea..58ab781 100644 --- a/test/junit/com/sun/gluegen/build.xml +++ b/test/junit/com/sun/gluegen/build.xml @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> -<project name="GlueGenTest" default="default" basedir="."> +<project name="GlueGenTest" default="default" basedir="../../../../../"> <description>Tests GlueGen</description> @@ -20,9 +20,9 @@ <mkdir dir="${build.dir.java}"/> - <javac destdir="${build.dir.java}" includes="test/**" source="1.5" debug="true" verbose="true" debuglevel="lines,vars,source"> + <javac destdir="${build.dir.java}" source="1.5" debug="true" verbose="true" debuglevel="lines,vars,source"> <src path="${src.dir}"/> - <classpath path="build/test/build/java:build/classes:build/test/gensrc/java:lib/antlr-2.7.7.jar:lib/junit-4.5.jar:${jdk.home}/lib/tools.jar:${ant.core.lib}"/> + <classpath path="${basedir}/build/test/build/java:${basedir}/build/classes:${basedir}/build/test/gensrc/java:${basedir}/lib/antlr-2.7.7.jar:${basedir}/lib/junit-4.5.jar:${jdk.home}/lib/tools.jar:${ant.core.lib}"/> </javac> <echo message=" - - - java files compiled - - - "/> @@ -38,7 +38,6 @@ <property name="output.lib.name" value="rofl"/> <property name="obj.dir" value="${obj.dir}/${output.lib.name}"/> - <property name="natives.dir" value="${build.dir}/natives/${output.lib.name}"/> <property name="c.compiler.optimise" value="none"/> <property name="c.compiler.debug" value="false"/> @@ -84,9 +83,10 @@ <echo message="configure for Linux.AMD64 build" /> <linker id="linker.cfg.linux.amd64.test"> + <linkerparam name="-m64"/> </linker> - <property name="compiler.cfg.id" value="compiler.cfg.linux" /> + <property name="compiler.cfg.id" value="compiler.cfg.linux.amd64" /> <property name="linker.cfg.id" value="linker.cfg.linux.amd64.test" /> </target> diff --git a/test/junit/com/sun/gluegen/struct.cfg b/test/junit/com/sun/gluegen/struct.cfg new file mode 100644 index 0000000..a1e1138 --- /dev/null +++ b/test/junit/com/sun/gluegen/struct.cfg @@ -0,0 +1,7 @@ +Package structtest + +JavaOutputDir java + +EmitStruct Vec +EmitStruct Camera +EmitStruct RenderingConfig diff --git a/test/junit/com/sun/gluegen/struct.h b/test/junit/com/sun/gluegen/struct.h new file mode 100644 index 0000000..2819d5c --- /dev/null +++ b/test/junit/com/sun/gluegen/struct.h @@ -0,0 +1,21 @@ + +typedef struct { + float x, y, z; +} Vec; + +typedef struct { + Vec orig, dir; +} Camera; + +typedef struct { + unsigned int width, height; + int superSamplingSize; + int actvateFastRendering; + int enableShadow; + + unsigned int maxIterations; + float epsilon; + float mu[4]; + float light[3]; + Camera camera; +} RenderingConfig; diff --git a/test/junit/com/sun/gluegen/test.c b/test/junit/com/sun/gluegen/test.c index d8c26a1..06d5508 100644 --- a/test/junit/com/sun/gluegen/test.c +++ b/test/junit/com/sun/gluegen/test.c @@ -8,6 +8,10 @@ int bufferTest(void * object) { return 42; } +int manyBuffersTest(void * object1, void * object2, void * object3, void * object4, void * object5) { + return 42; +} + int mixedTest(long context, void * object, foo * array){ return 42; } diff --git a/test/junit/com/sun/gluegen/test.h b/test/junit/com/sun/gluegen/test.h index 02a22c1..b15b7e5 100644 --- a/test/junit/com/sun/gluegen/test.h +++ b/test/junit/com/sun/gluegen/test.h @@ -5,6 +5,8 @@ int arrayTest(long context, foo * array ); int bufferTest(void * object); +int manyBuffersTest(void * object1, void * object2, void * object3, void * object4, void * object5); + int mixedTest(long context, void * object, foo * array ); int doubleTest(long context, void * object1, foo * array1, void * object2, foo * array2 ); |