Add a couple of tests.
authorNot Zed <notzed@gmail.com>
Sat, 25 Jan 2020 21:36:28 +0000 (08:06 +1030)
committerNot Zed <notzed@gmail.com>
Sat, 25 Jan 2020 21:36:28 +0000 (08:06 +1030)
src/notzed.zcl/tests/au/notzed/zcl/CLPlatformTest.java [new file with mode: 0644]
src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java [new file with mode: 0644]

diff --git a/src/notzed.zcl/tests/au/notzed/zcl/CLPlatformTest.java b/src/notzed.zcl/tests/au/notzed/zcl/CLPlatformTest.java
new file mode 100644 (file)
index 0000000..a30b16a
--- /dev/null
@@ -0,0 +1,59 @@
+
+package au.notzed.zcl;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import static au.notzed.zcl.CL.*;
+
+public class CLPlatformTest {
+
+       boolean haveCL() {
+               CLPlatform[] list = CLPlatform.getPlatforms();
+               return list != null && list.length > 0;
+       }
+
+       //@Before
+       //public void setup() {
+       //      org.junit.Assume.assumeTrue(haveCL());
+       //}
+
+       @Test
+       public void testGetPlatforms() {
+               System.out.println("getPlatforms");
+               CLPlatform [] list = CLPlatform.getPlatforms();
+
+               for (CLPlatform p: list) {
+                       assertNotNull(p);
+                       assertNotNull(p.getProfile());
+                       assertNotNull(p.getVersion());
+                       assertNotNull(p.getName());
+                       assertNotNull(p.getVendor());
+                       assertNotNull(p.getExtensions());
+               }
+       }
+
+       @Test
+       public void testGetDevices() {
+               System.out.println("getDevices");
+               CLPlatform [] list = CLPlatform.getPlatforms();
+
+               for (CLPlatform p: list) {
+                       CLDevice none[] = p.getDevices(0);
+
+                       assertNotNull(none);
+                       assertEquals(none.length, 0);
+
+                       CLDevice all[] = p.getDevices(CL_DEVICE_TYPE_ALL);
+                       CLDevice gpu[] = p.getDevices(CL_DEVICE_TYPE_GPU);
+                       CLDevice cpu[] = p.getDevices(CL_DEVICE_TYPE_CPU);
+
+                       assertTrue(all.length >= gpu.length);
+                       assertTrue(all.length >= cpu.length);
+
+                       for (CLDevice d: gpu)
+                               assertEquals(d.getType(), CL_DEVICE_TYPE_GPU);
+                       for (CLDevice d: cpu)
+                               assertEquals(d.getType(), CL_DEVICE_TYPE_CPU);
+               }
+       }
+}
diff --git a/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java b/src/notzed.zcl/tests/au/notzed/zcl/CLProgramTest.java
new file mode 100644 (file)
index 0000000..d23b5c1
--- /dev/null
@@ -0,0 +1,169 @@
+
+package au.notzed.zcl;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import static au.notzed.zcl.CL.*;
+
+public class CLProgramTest {
+       String srca = "kernel void test(global float *x) { x[get_global_id(0)] = 0; }";
+       String srcb = "kernel void thing(global float *x) { x[get_global_id(0)] = 0; }";
+
+       boolean haveCL() {
+               CLPlatform[] list = CLPlatform.getPlatforms();
+               return list != null && list.length > 0;
+       }
+
+       CLPlatform plat;
+       CLDevice devs[];
+       CLContext cl;
+
+       @Before
+       public void setup() {
+               org.junit.Assume.assumeTrue(haveCL());
+
+               plat = CLPlatform.getPlatforms()[0];
+               devs = new CLDevice[] { plat.getDevices(CL_DEVICE_TYPE_ALL)[0] };
+               cl = CLContext.createContext(null, devs);
+       }
+       @After
+       public void shutdown() {
+               cl.release();
+       }
+
+       @Test
+       public void testCreate1() {
+               System.out.println("createProgramWithSource - null");
+
+               try {
+                       cl.createProgramWithSource((String[])null);
+               } catch (Throwable t) {
+                       assertEquals(t.getClass(), NullPointerException.class);
+               }
+               //assertThrows(NullPointerException.class,
+               //      () -> cl.createProgramWithSource(null));
+       }
+
+       @Test
+       public void testCreate2() throws Exception {
+               System.out.println("createProgramWithSource - not null");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               assertNotNull(prog);
+       }
+
+       @Test
+       public void testFields() throws Exception{
+               System.out.println("createProgramWithSource - fields");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               CLContext  pcl = prog.getContext();
+
+               assertEquals(pcl, cl);
+
+               CLDevice[] pdevs = prog.getDevices();
+
+               assertEquals(pdevs.length, prog.getNumDevices());
+               assertEquals(pdevs.length, 1);
+               assertEquals(pdevs[0], devs[0]);
+
+               String psrc = prog.getSource();
+
+               assertEquals(psrc, srca);
+       }
+
+       @Test
+       public void testNotCompile() throws Exception{
+               System.out.println("createProgramWithSource - not compile");
+               CLProgram prog = cl.createProgramWithSource(srca);
+               Exception x = null;
+
+               try {
+                       assertEquals(0, prog.getNumKernels());
+               } catch (Exception t) {
+                       x = t;
+               }
+               assertNotNull(x);
+               assertEquals(x.getClass(), CLRuntimeException.class);
+       }
+
+       @Test
+       public void testCompile0() throws Exception{
+               System.out.println("createProgramWithSource - state");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               assertEquals(prog.getBuildStatus(devs[0]), CL_BUILD_NONE);
+               assertEquals(prog.getBinaryType(devs[0]), CL_PROGRAM_BINARY_TYPE_NONE);
+
+               prog.buildProgram(devs, null);
+
+               assertEquals(prog.getBuildStatus(devs[0]), CL_BUILD_SUCCESS);
+               // platform dependent?
+               assertEquals(prog.getBinaryType(devs[0]), CL_PROGRAM_BINARY_TYPE_EXECUTABLE);
+
+               String log = prog.getBuildLog(devs[0]);
+               assertNotNull(log);
+       }
+
+       @Test
+       public void testCompile1() throws Exception{
+               System.out.println("createProgramWithSource - compile");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               prog.buildProgram(devs, null);
+
+               assertEquals(1, prog.getNumKernels());
+               assertEquals("test", prog.getKernelNames());
+
+               assertEquals("", prog.getBuildOptions(devs[0]).trim());
+       }
+
+       @Test
+       public void testCompile2() throws Exception{
+               System.out.println("createProgramWithSource - compile opts");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               prog.buildProgram(devs, "-Dtest=bob");
+
+               assertEquals(1, prog.getNumKernels());
+               assertEquals("bob", prog.getKernelNames());
+
+               assertEquals("-Dtest=bob", prog.getBuildOptions(devs[0]).trim());
+       }
+
+       @Test
+       public void testCompile3() throws Exception{
+               System.out.println("createProgramWithSource - compile 2x");
+               CLProgram prog = cl.createProgramWithSource(srca, srcb);
+
+               prog.buildProgram(devs, "");
+
+               assertEquals(2, prog.getNumKernels());
+
+               // actually order might be different?
+               assertEquals("test;thing", prog.getKernelNames());
+               assertEquals("", prog.getBuildOptions(devs[0]).trim());
+       }
+
+       @Test
+       public void testCompile4() throws Exception{
+               System.out.println("createProgramWithSource - compile binaries");
+               CLProgram prog = cl.createProgramWithSource(srca);
+
+               prog.buildProgram(devs, "");
+
+               byte[][]binaries = prog.getBinaries();
+
+               assertNotNull(binaries);
+               assertEquals(binaries.length, 1);
+               assertNotNull(binaries[0]);
+
+               int[] status = new int[1];
+               CLProgram bprog = cl.createProgramWithBinary(devs, binaries, status);
+
+               assertEquals(status[0], CL_BUILD_SUCCESS);
+               assertEquals(1, bprog.getNumKernels());
+               assertEquals("test", bprog.getKernelNames());
+
+       }
+}