--- /dev/null
+
+package au.notzed.zcl;
+
+import org.junit.*;
+import static org.junit.Assert.*;
+import static au.notzed.zcl.CL.*;
+import jdk.incubator.foreign.*;
+import api.Native;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/*
+ TODO: USE_HOST_PTR and what it does
+
+ Most of these tests are buffer related interfaces on clcontext or
+ clcommandqueue. Maybe some should be there or in an IT. Does it
+ really matter?
+
+ */
+public class CLBufferTest {
+ boolean haveCL() {
+ CLPlatform[] list = CLPlatform.getPlatforms();
+ return list != null && list.length > 0;
+ }
+
+ CLPlatform plat;
+ CLDevice devs[];
+ CLContext cl;
+ CLCommandQueue q;
+
+ @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);
+ q = cl.createCommandQueue(devs[0], 0);
+ }
+ @After
+ public void shutdown() {
+ CLObject.release(q, cl);
+ }
+
+ @Test
+ public void testCreate1() {
+ System.out.println("createBuffer");
+
+ CLBuffer b = cl.createBuffer(0, 1024);
+
+ assertNotNull(b);
+
+ b.release();
+ }
+
+ @Test
+ public void testCreate2() {
+ System.out.println("createBuffer fields");
+
+ CLBuffer b = cl.createBuffer(0, 1024);
+
+ assertNotNull(b);
+
+ assertEquals(1024L, b.getSize());
+ assertEquals(CL_MEM_READ_WRITE, b.getFlags());
+ assertEquals(CL_MEM_OBJECT_BUFFER, b.getType());
+
+ assertEquals(cl, b.getContext());
+ assertNull(b.getAssociatedMemObject());
+ assertEquals(0, b.getMemOffset());
+
+ b.release();
+ }
+
+ @Test
+ public void testCreate3() {
+ System.out.println("createBuffer flags");
+
+ long[] flags = {
+ CL_MEM_READ_WRITE,
+ CL_MEM_WRITE_ONLY,
+ CL_MEM_READ_ONLY,
+ };
+
+ for (long flag: flags) {
+ CLBuffer b = cl.createBuffer(flag, 1024);
+
+ assertNotNull(b);
+
+ assertEquals(flag, b.getFlags());
+
+ b.release();
+ }
+ }
+
+ byte[] filla = "opencl ftw!".getBytes();
+
+ static void fillSegment(MemorySegment seg, byte[] seq) {
+ MemoryAddress add = seg.baseAddress();
+ for (long i=0;i<seg.byteSize();i++) {
+ Native.setByte(add, i, seq[(int)(i % seq.length)]);
+ }
+ }
+
+ static boolean equalSegment(MemorySegment a, MemorySegment b) {
+ boolean same = true;
+ MemoryAddress c = a.baseAddress();
+ MemoryAddress d = b.baseAddress();
+
+ for (long i=0;same && i<a.byteSize();i++) {
+ same &= Native.getByte(c, i) == Native.getByte(d, i);
+ }
+ return same;
+ }
+
+ static boolean equal(ByteBuffer a, MemorySegment b) {
+ boolean same = true;
+ MemoryAddress d = b.baseAddress();
+
+ for (long i=0;same && i<a.capacity();i++) {
+ same &= a.get((int)i) == Native.getByte(d, i);
+ }
+ return same;
+ }
+
+ @Test
+ public void testCreate4() throws Exception {
+ System.out.println("createBuffer COPY_HOST_PTR");
+
+ try (MemorySegment sega = MemorySegment.allocateNative(1024, 8);
+ MemorySegment segb = MemorySegment.allocateNative(1024, 8)) {
+ fillSegment(sega, filla);
+
+ CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+
+ q.enqueueReadBuffer(b, true, 0, segb.byteSize(), segb, null, null);
+
+ assertTrue(equalSegment(sega, segb));
+ b.release();
+ }
+ }
+
+ @Test
+ public void testCreate5() throws Exception {
+ Throwable x;
+ System.out.println("createBuffer invalid flags");
+
+ try {
+ x = null;
+ cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, (MemorySegment)null);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+ try {
+ x = null;
+ cl.createBuffer(CL_MEM_USE_HOST_PTR, 1024, (MemorySegment)null);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+ try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+ x = null;
+ cl.createBuffer(0, 8, seg);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+ try (MemorySegment seg = MemorySegment.allocateNative(8, 8)) {
+ x = null;
+ cl.createBuffer(0, seg);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+ try {
+ ByteBuffer bb = ByteBuffer.allocateDirect(8);
+ x = null;
+ cl.createBuffer(0, 8, bb);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+
+ try {
+ ByteBuffer bb = ByteBuffer.allocateDirect(8);
+ x = null;
+ cl.createBuffer(0, bb);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(CLRuntimeException.class, x.getClass());
+ assertEquals(CL_INVALID_HOST_PTR, ((CLRuntimeException)x).err);
+ }
+
+ @Test
+ public void testCreate6() throws Exception {
+ Throwable x;
+ System.out.println("createBuffer sized segment");
+
+ try (MemorySegment seg = MemorySegment.allocateNative(720, 8)) {
+ CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, seg);
+
+ assertEquals(seg.byteSize(), b.getSize());
+
+ b.release();
+ }
+ }
+
+ @Test
+ public void testCreate7() throws Exception {
+ Throwable x;
+ System.out.println("createBuffer sized buffer");
+
+ ByteBuffer bb = ByteBuffer.allocateDirect(768);
+ CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, bb);
+
+ assertEquals(bb.capacity(), b.getSize());
+
+ b.release();
+ }
+
+ @Test
+ public void testMap1() throws Exception {
+ System.out.println("mapBuffer");
+ try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+ fillSegment(sega, filla);
+
+ CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+ ByteBuffer bb = q.enqueueMapBuffer(b, true, CL_MAP_READ, 0, sega.byteSize(), null, null);
+
+ assertNotNull(bb);
+ assertEquals(sega.byteSize(), bb.capacity());
+
+ q.enqueueUnmapMemObject(b, bb, null, null);
+
+ b.release();
+ }
+ }
+
+ @Test
+ public void testMap2() throws Exception {
+ System.out.println("mapBuffer double unmap");
+ try (MemorySegment sega = MemorySegment.allocateNative(1024, 8)) {
+ fillSegment(sega, filla);
+
+ CLBuffer b = cl.createBuffer(CL_MEM_COPY_HOST_PTR, 1024, sega);
+ ByteBuffer bb = q.enqueueMapBuffer(b, true, CL_MAP_READ, 0, sega.byteSize(), null, null);
+
+ assertNotNull(bb);
+ assertEquals(sega.byteSize(), bb.capacity());
+
+ q.enqueueUnmapMemObject(b, bb, null, null);
+
+ Throwable x;
+ try {
+ bb.limit(1); // ensure hash changes
+
+ x = null;
+ q.enqueueUnmapMemObject(b, bb, null, null);
+ } catch (Throwable t) {
+ x = t;
+ }
+ assertEquals(IllegalArgumentException.class, x.getClass());
+
+ b.release();
+ }
+ }
+
+ @Test
+ public void testFill1() throws Exception {
+ org.junit.Assume.assumeTrue(plat.getAPIVersion() >= CLPlatform.VERSION_1_2);
+
+ byte[] data = new byte[] { 1, 2, 3, 4 };
+
+ try (MemorySegment seg = MemorySegment.allocateNative(1024, 8)) {
+ CLBuffer b = cl.createBuffer(0, 1024);
+
+ q.enqueueFillBuffer(b, data, 0, 256, null, null);
+ q.enqueueReadBuffer(b, true, 0, seg.byteSize(), seg, null, null);
+
+ boolean same = true;
+ MemoryAddress add = seg.baseAddress();
+ for (int i=0;same && i<1024;i++) {
+ same = Native.getByte(add, i) == data[i % data.length];
+ }
+
+ assertTrue(same);
+ }
+ }
+}