GlueGen: src/junit/com/jogamp/gluegen/test/junit/generation/BaseClass.java Source File

GlueGen v2.6.0-rc-20250822 GlueGen, Native Binding Generator for Java™ (public API).

BaseClass.java
Go to the documentation of this file.
1/**
2 * Copyright 2010-2023 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29 package com.jogamp.gluegen.test.junit.generation;
30
31import com.jogamp.common.nio.AbstractBuffer;
32import com.jogamp.common.nio.Buffers;
33import com.jogamp.common.nio.PointerBuffer;
34import com.jogamp.common.os.MachineDataInfo;
35import com.jogamp.common.os.Platform;
36import com.jogamp.junit.util.SingletonJunitCase;
37
38import java.nio.ByteBuffer;
39import java.nio.ByteOrder;
40import java.nio.FloatBuffer;
41import java.nio.IntBuffer;
42import java.nio.LongBuffer;
43import java.nio.charset.Charset;
44import java.util.Arrays;
45
46import jogamp.common.os.MachineDataInfoRuntime;
47
48import org.junit.Assert;
49
50
51 public class BaseClass extends SingletonJunitCase {
52
53 static {
54 Platform.initSingleton(); // 1st initialize GlueGen itself
55 }
56
57 /**
58 * Verifies the existence and creation of the generated class.
59 */
60 public void testClassExist(final String name) throws Exception {
61 final String ifName = "com.jogamp.gluegen.test.junit.generation.Binding"+name;
62 final String implName = "com.jogamp.gluegen.test.junit.generation.impl.Binding"+name+"Impl";
63
64 final Class<?> clazzIf = Class.forName(ifName);
65 final Class<?> clazzImpl = Class.forName(implName);
66
67 Assert.assertNotNull(ifName+" does not exist", clazzIf);
68 Assert.assertNotNull(implName+" does not exist", clazzImpl);
69
70 Assert.assertNotNull(clazzImpl.getDeclaredMethod("nopTest"));
71
72 final Object obj = clazzImpl.newInstance();
73 Assert.assertTrue("Not of type "+ifName, clazzIf.isAssignableFrom(obj.getClass()));
74 Assert.assertTrue("Not of type com.jogamp.gluegen.test.junit.generation.Bindingtest1",
75 (obj instanceof com.jogamp.gluegen.test.junit.generation.Bindingtest1));
76 }
77
78 public static final float EPSILON = 1.1920929E-7f; // Float.MIN_VALUE == 1.4e-45f ; double EPSILON 2.220446049250313E-16d
79
80 /**
81 * Verifies if all generated method signatures are completed,
82 * ie a compilation only coverage test without functional tests.
83 */
84 public void chapter__TestCoverageSignature(final Bindingtest1 binding) throws Exception {
85 int i = 0;
86 final long context = 0;
87 LongBuffer lb=null;
88 ByteBuffer bb=null;
89 final IntBuffer ib=null;
90 final long[] larray = null;
91 final int larray_offset = 0;
92 String str=null;
93 final String[] strings = null;
94 final int[] iarray = null;
95 final int iarray_offset = 0;
96 long result = 0;
97 long l = result;
98 ShortBlob sb = null;
99 Int32Struct i32s = null;
100 AnonBlob ab = null;
101 PointerBuffer pb=null;
102
103 {
104 l = binding.testXID(l);
105 l = binding.testXID_2(l);
106
107 bb = binding.testAnonBuffer(bb);
108
109 sb = binding.testShortBlob(sb);
110 sb = binding.testLPShortBlob0(sb);
111 sb = binding.testLPShortBlob1(sb);
112 sb = binding.testLPShortBlob2(sb);
113 sb = binding.testLPShortBlob3(sb);
114 sb = binding.testShortBlobL1(sb);
115 sb = binding.testShortBlobL2(sb);
116
117 i32s = binding.testInt32Struct(i32s);
118
119 ab = binding.testCreateAnonBlob();
120 binding.testDestroyAnonBlob(ab);
121
122 l = binding.testCreateAnonBlob2();
123 binding.testDestroyAnonBlob2(l);
124
125 lb = binding.testFooPtr(larray, 0);
126 lb = binding.testFooPtr(lb);
127
128 i = binding.testDelegate(i);
129 }
130
131 {
132 bb = binding.createAPtrBlob();
133 pb = safeByteBuffer2PointerBuffer(bb, 1);
134 long bb2A = binding.getAPtrAddress(bb);
135 bb2A = bb2A - 0; // avoid warning
136
137 binding.arrayTestAVoidPtrTypeDim1Mutable(pb);
138 pb = PointerBuffer.wrap( binding.arrayTestAVoidPtrTypeDim1Immutable(pb) );
139 pb = PointerBuffer.wrap( binding.arrayTestAVoidPtrTypeDim0(pb.getBuffer()) );
140 binding.releaseAPtrBlob( binding.getAPtrMemory( pb.get(0) ) );
141
142 binding.arrayTestAIntPtrTypeDim1Mutable(pb);
143 result = binding.arrayTestAIntPtrTypeDim1Immutable(pb);
144 result = binding.arrayTestAIntPtrTypeDim0(pb.get(0));
145 binding.releaseAPtrBlob( binding.getAPtrMemory( pb.get(0) ) );
146
147 binding.arrayTestAPtr1TypeDim1Mutable(pb);
148 pb = PointerBuffer.wrap( binding.arrayTestAPtr1TypeDim1Immutable(pb) );
149 pb = PointerBuffer.wrap( binding.arrayTestAPtr1TypeDim0(pb.getBuffer()) );
150 binding.releaseAPtrBlob( binding.getAPtrMemory( pb.get(0) ) );
151
152 binding.arrayTestAPtr2TypeDim1Mutable(pb);
153 result = binding.arrayTestAPtr2TypeDim1Immutable(pb);
154 result = binding.arrayTestAPtr2TypeDim0(pb.get(0));
155 binding.releaseAPtrBlob( binding.getAPtrMemory( pb.get(0) ) );
156
157 binding.releaseAPtrBlob(bb);
158 }
159
160 result = binding.arrayTestInt32(context, ib);
161 result = binding.arrayTestInt32(context, iarray, iarray_offset);
162
163 result = binding.arrayTestInt64(context, lb);
164 result = binding.arrayTestInt64(context, larray, larray_offset);
165
166 result = binding.arrayTestFoo1(context, lb);
167 result = binding.arrayTestFoo1(context, larray, larray_offset);
168 result = binding.arrayTestFooNioOnly(context, lb);
169
170 lb = binding.arrayTestFoo2(lb);
171 lb = binding.arrayTestFoo2(larray, larray_offset);
172
173 pb = binding.arrayTestFoo3ArrayToPtrPtr(lb);
174 pb = binding.arrayTestFoo3PtrPtr(pb);
175
176 result = binding.bufferTest(bb);
177 result = binding.bufferTestNioOnly(bb);
178
179 result = binding.doubleTest(context, bb, lb, bb, lb);
180 result = binding.doubleTest(context, bb, larray, larray_offset, bb, larray, larray_offset);
181 result = binding.doubleTestNioOnly(context, bb, lb, bb, lb);
182
183 result = binding.mixedTest(context, bb, lb);
184 result = binding.mixedTest(context, bb, larray, larray_offset);
185 result = binding.mixedTestNioOnly(context, bb, lb);
186
187 result = binding.nopTest();
188
189 i = binding.strToInt(str);
190 str = binding.intToStr(i);
191
192 i = binding.stringArrayRead(strings, i);
193
194 i = binding.binaryArrayRead(pb, pb, 0);
195
196 i = binding.intArrayRead(ib, i);
197 i = binding.intArrayRead(iarray, iarray_offset, i);
198
199 long cfg=0;
200 cfg = binding.typeTestAnonSingle(cfg);
201 pb = binding.typeTestAnonPointer(pb);
202
203 i = binding.typeTestInt32T(i, i);
204 i = binding.typeTestUInt32T(i, i);
205 l = binding.typeTestInt64T(l, l);
206 l = binding.typeTestUInt64T(l, l);
207
208 i = binding.typeTestWCharT(i, i);
209 l = binding.typeTestSizeT(l, l);
210 l = binding.typeTestPtrDiffT(l, l);
211 l = binding.typeTestIntPtrT(l, l);
212 l = binding.typeTestUIntPtrT(l, l);
213
214 i = binding.addStrlenAndPascalStrLen("lala");
215 }
216
217 /**
218 * Verifies if all generated static constant values are completed,
219 * and whether their value is as expected!
220 * <p>
221 * Covers all enumerates and defines.
222 * </p>
223 */
224 public void chapter01TestStaticConstants(final Bindingtest1 binding) throws Exception {
225 // Plain vanilla CPP constants
226 Assert.assertEquals( 1, Bindingtest1.CONSTANT_ONE);
227 Assert.assertEquals( 8, Bindingtest1.ARRAY_SIZE);
228 Assert.assertEquals(1234, Bindingtest1.DEFINE_01);
229
230 // Enums
231 Assert.assertEquals( 1, Bindingtest1.LI);
232 Assert.assertEquals( 3, Bindingtest1.LO);
233 Assert.assertEquals( 2, Bindingtest1.LU);
234 Assert.assertEquals( 1, Bindingtest1.MI);
235 Assert.assertEquals( 3, Bindingtest1.MO);
236 Assert.assertEquals( 2, Bindingtest1.MU);
237 Assert.assertEquals( 0, Bindingtest1.ZERO);
238 Assert.assertEquals( 1, Bindingtest1.ONE);
239 Assert.assertEquals( 2, Bindingtest1.TWO);
240 Assert.assertEquals( 3, Bindingtest1.THREE);
241
242 // CPP Macro Expansion!
243 Assert.assertEquals( 1, Bindingtest1.NUMBER_ONE);
244 Assert.assertEquals( 2, Bindingtest1.NUMBER_TWO);
245 Assert.assertEquals( 4, Bindingtest1.NUMBER_FOUR);
246 Assert.assertEquals( 5, Bindingtest1.NUMBER_FIVE);
247 Assert.assertEquals( 8, Bindingtest1.NUMBER_EIGHT);
248 Assert.assertEquals( 9, Bindingtest1.NUMBER_NINE);
249 Assert.assertEquals( 10, Bindingtest1.NUMBER_TEN);
250
251 // Bitwise not Expression
252 Assert.assertEquals(~ Bindingtest1.NUMBER_ONE, Bindingtest1.BITWISE_NOT_OF_ONE);
253 Assert.assertEquals(~ Bindingtest1.NUMBER_TWO, Bindingtest1.BITWISE_NOT_OF_TWO);
254 Assert.assertEquals(~ Bindingtest1.NUMBER_FOUR, Bindingtest1.BITWISE_NOT_OF_FOUR);
255 Assert.assertEquals(~ Bindingtest1.NUMBER_FIVE, Bindingtest1.BITWISE_NOT_OF_FIVE);
256 Assert.assertEquals(~ Bindingtest1.NUMBER_EIGHT, Bindingtest1.BITWISE_NOT_OF_EIGHT);
257 Assert.assertEquals(~ Bindingtest1.NUMBER_NINE, Bindingtest1.BITWISE_NOT_OF_NINE);
258 Assert.assertEquals(~ Bindingtest1.NUMBER_TEN, Bindingtest1.BITWISE_NOT_OF_TEN);
259
260 // Enum Constant Expressions!
261 Assert.assertEquals( 1, Bindingtest1.ENUM_NUM_ONE);
262 Assert.assertEquals( 2, Bindingtest1.ENUM_NUM_TWO);
263 Assert.assertEquals( 3, Bindingtest1.ENUM_NUM_THREE);
264 Assert.assertEquals( 4, Bindingtest1.ENUM_NUM_FOUR);
265 Assert.assertEquals( 5, Bindingtest1.ENUM_NUM_FIVE);
266 Assert.assertEquals( 8, Bindingtest1.ENUM_NUM_EIGHT);
267 Assert.assertEquals( 9, Bindingtest1.ENUM_NUM_NINE);
268 Assert.assertEquals( 10, Bindingtest1.ENUM_NUM_TEN);
269
270 // Integer 32bit (int / enum)
271 final int ENUM_I0 = Bindingtest1.ENUM_I0;
272 final int ENUM_I1 = Bindingtest1.ENUM_I1;
273 final int ENUM_I2 = Bindingtest1.ENUM_I2;
274 final int ENUM_I3 = Bindingtest1.ENUM_I3;
275 final int ENUM_I4 = -Bindingtest1.ENUM_I4;
276 final int ENUM_I5 = -Bindingtest1.ENUM_I5;
277 final int ENUM_I6 = -Bindingtest1.ENUM_I6;
278 final int ENUM_I7 = Bindingtest1.ENUM_I7;
279 final int ENUM_I8 = Bindingtest1.ENUM_I8;
280 final int ENUM_I9 = Bindingtest1.ENUM_I9;
281 final int ENUM_IA = Bindingtest1.ENUM_IA;
282 final int ENUM_IB = Bindingtest1.ENUM_IB;
283 final int ENUM_IX = Bindingtest1.ENUM_IX;
284 int iexp = 10;
285 Assert.assertEquals(iexp++, ENUM_I0);
286 Assert.assertEquals(iexp++, ENUM_I1);
287 Assert.assertEquals(iexp++, ENUM_I2);
288 Assert.assertEquals(iexp++, ENUM_I3);
289 Assert.assertEquals(iexp++, ENUM_I4);
290 Assert.assertEquals(iexp++, ENUM_I5);
291 Assert.assertEquals(iexp++, ENUM_I6);
292 Assert.assertEquals(iexp++, ENUM_I7);
293 Assert.assertEquals(iexp++, ENUM_I8);
294 Assert.assertEquals(iexp++, ENUM_I9);
295 Assert.assertEquals(iexp++, ENUM_IA);
296 Assert.assertEquals(iexp++, ENUM_IB);
297 Assert.assertEquals(0xffffffff, ENUM_IX);
298
299 // Integer 32bit (int / define)
300 final int CL_INT_I0 = Bindingtest1.CL_INT_I0;
301 final int CL_INT_I1 = Bindingtest1.CL_INT_I1;
302 final int CL_INT_I2 = Bindingtest1.CL_INT_I2;
303 final int CL_INT_I3 = Bindingtest1.CL_INT_I3;
304 final int CL_INT_I4 = -Bindingtest1.CL_INT_I4;
305 final int CL_INT_I5 = -Bindingtest1.CL_INT_I5;
306 final int CL_INT_I6 = -Bindingtest1.CL_INT_I6;
307 final int CL_INT_I7 = -Bindingtest1.CL_INT_I7;
308 final int CL_INT_I8 = Bindingtest1.CL_INT_I8;
309 final int CL_INT_I9 = Bindingtest1.CL_INT_I9;
310 final int CL_INT_IA = Bindingtest1.CL_INT_IA;
311 final int CL_INT_IB = Bindingtest1.CL_INT_IB;
312 final int CL_INT_IX = Bindingtest1.CL_INT_IX;
313 iexp = 10;
314 Assert.assertEquals(iexp++, CL_INT_I0);
315 Assert.assertEquals(iexp++, CL_INT_I1);
316 Assert.assertEquals(iexp++, CL_INT_I2);
317 Assert.assertEquals(iexp++, CL_INT_I3);
318 Assert.assertEquals(iexp++, CL_INT_I4);
319 Assert.assertEquals(iexp++, CL_INT_I5);
320 Assert.assertEquals(iexp++, CL_INT_I6);
321 Assert.assertEquals(iexp++, CL_INT_I7);
322 Assert.assertEquals(iexp++, CL_INT_I8);
323 Assert.assertEquals(iexp++, CL_INT_I9);
324 Assert.assertEquals(iexp++, CL_INT_IA);
325 Assert.assertEquals(iexp++, CL_INT_IB);
326 Assert.assertEquals(0xffffffff, CL_INT_IX);
327
328 // Integer 64bit (long / define )
329 final long CL_LNG_L0 = Bindingtest1.CL_LNG_L0;
330 final long CL_LNG_L1 = Bindingtest1.CL_LNG_L1;
331 final long CL_LNG_L2 = Bindingtest1.CL_LNG_L2;
332 final long CL_LNG_L3 = Bindingtest1.CL_LNG_L3;
333 final long CL_LNG_L4 = -Bindingtest1.CL_LNG_L4;
334 final long CL_LNG_L5 = -Bindingtest1.CL_LNG_L5;
335 final long CL_LNG_L6 = -Bindingtest1.CL_LNG_L6;
336 final long CL_LNG_L7 = -Bindingtest1.CL_LNG_L7;
337 final long CL_LNG_L8 = Bindingtest1.CL_LNG_L8;
338 final long CL_LNG_L9 = Bindingtest1.CL_LNG_L9;
339 final long CL_LNG_LA = Bindingtest1.CL_LNG_LA;
340 final long CL_LNG_LB = Bindingtest1.CL_LNG_LB;
341 final long CL_LNG_LX = Bindingtest1.CL_LNG_LX;
342 long lexp = 2147483648L;
343 Assert.assertEquals(lexp++, CL_LNG_L0);
344 Assert.assertEquals(lexp++, CL_LNG_L1);
345 Assert.assertEquals(lexp++, CL_LNG_L2);
346 Assert.assertEquals(lexp++, CL_LNG_L3);
347 Assert.assertEquals(lexp++, CL_LNG_L4);
348 Assert.assertEquals(lexp++, CL_LNG_L5);
349 Assert.assertEquals(lexp++, CL_LNG_L6);
350 Assert.assertEquals(lexp++, CL_LNG_L7);
351 Assert.assertEquals(lexp++, CL_LNG_L8);
352 Assert.assertEquals(lexp++, CL_LNG_L9);
353 Assert.assertEquals(lexp++, CL_LNG_LA);
354 Assert.assertEquals(lexp++, CL_LNG_LB);
355 Assert.assertEquals(0xffffffffffffffffL, CL_LNG_LX);
356
357 // Floating point hexadecimals
358 final float CL_FLT_A0 = Bindingtest1.CL_FLT_A0;
359 final float CL_FLT_A1 = Bindingtest1.CL_FLT_A1;
360 final float CL_FLT_A2 = Bindingtest1.CL_FLT_A2;
361 final float CL_FLT_A3 = Bindingtest1.CL_FLT_A3;
362 final float CL_FLT_A4 = Bindingtest1.CL_FLT_A4;
363 final float CL_FLT_A5 = Bindingtest1.CL_FLT_A5;
364 final float CL_FLT_A6 = Bindingtest1.CL_FLT_A6;
365 final float CL_FLT_A7 = Bindingtest1.CL_FLT_A7;
366 Assert.assertEquals( 0x1.p127f, CL_FLT_A0, EPSILON);
367 Assert.assertEquals( 0x1.p+127F, CL_FLT_A1, EPSILON);
368 Assert.assertEquals( 0x1.p-127f, CL_FLT_A2, EPSILON);
369 Assert.assertEquals( -0.1f, CL_FLT_A3, EPSILON);
370 Assert.assertEquals( 0.2f, CL_FLT_A4, EPSILON);
371 Assert.assertEquals( 0.3f, CL_FLT_A5, EPSILON);
372 Assert.assertEquals( 0.4f, CL_FLT_A6, EPSILON);
373 Assert.assertEquals( 1.0f, CL_FLT_A7, EPSILON);
374
375 final float CL_FLT_EPSILON = Bindingtest1.CL_FLT_EPSILON;
376 final double CL_FLT_MAX= Bindingtest1.CL_FLT_MAX;
377 final double CL_FLT_MIN = Bindingtest1.CL_FLT_MIN;
378 Assert.assertEquals( 0x1.0p-23f, CL_FLT_EPSILON, EPSILON);
379 Assert.assertEquals( 0x1.fffffep127f, CL_FLT_MAX, EPSILON);
380 Assert.assertEquals( 0x1.0p-126f, CL_FLT_MIN, EPSILON);
381
382 final double CL_DBL_B0 = Bindingtest1.CL_DBL_B0;
383 final double CL_DBL_B1 = Bindingtest1.CL_DBL_B1;
384 final double CL_DBL_B2 = Bindingtest1.CL_DBL_B2;
385 final double CL_DBL_B3 = Bindingtest1.CL_DBL_B3;
386 final double CL_DBL_B4 = Bindingtest1.CL_DBL_B4;
387 final double CL_DBL_B5 = Bindingtest1.CL_DBL_B5;
388 final double CL_DBL_B6 = Bindingtest1.CL_DBL_B6;
389 Assert.assertEquals( 0x1.p127d, CL_DBL_B0, EPSILON);
390 Assert.assertEquals( 0x1.p+127D, CL_DBL_B1, EPSILON);
391 Assert.assertEquals( 0x1.p-127d, CL_DBL_B2, EPSILON);
392 Assert.assertEquals( -0.1, CL_DBL_B3, EPSILON);
393 Assert.assertEquals( 0.2, CL_DBL_B4, EPSILON);
394 Assert.assertEquals( 0.3, CL_DBL_B5, EPSILON);
395 Assert.assertEquals( 3.5e+38, CL_DBL_B6, EPSILON);
396
397 final float CL_DBL_EPSILON = Bindingtest1.CL_DBL_EPSILON;
398 final double CL_DBL_MAX= Bindingtest1.CL_DBL_MAX;
399 final double CL_DBL_MIN = Bindingtest1.CL_DBL_MIN;
400 Assert.assertEquals( 0x1.0p-52f, CL_DBL_EPSILON, EPSILON);
401 Assert.assertEquals( 0x1.fffffffffffffp1023, CL_DBL_MAX, EPSILON);
402 Assert.assertEquals( 0x1.0p-1022, CL_DBL_MIN, EPSILON);
403 }
404
405 ByteBuffer newByteBuffer(final int size, final boolean direct) {
406 if(direct) {
407 final ByteBuffer bb = Buffers.newDirectByteBuffer(size);
408 Assert.assertTrue(bb.isDirect());
409 return bb;
410 } else {
411 final ByteBuffer bb = ByteBuffer.wrap(new byte[size]);
412 Assert.assertTrue(bb.hasArray());
413 Assert.assertTrue(!bb.isDirect());
414 bb.order(ByteOrder.nativeOrder());
415 Assert.assertTrue(bb.hasArray());
416 Assert.assertTrue(!bb.isDirect());
417 return bb;
418 }
419 }
420
421 IntBuffer newIntBuffer(final int size, final boolean direct) {
422 if(direct) {
423 final IntBuffer ib = Buffers.newDirectIntBuffer(size);
424 Assert.assertTrue(ib.isDirect());
425 return ib;
426 } else {
427 final IntBuffer ib = IntBuffer.wrap(new int[size]);
428 Assert.assertTrue(ib.hasArray());
429 Assert.assertTrue(!ib.isDirect());
430 return ib;
431 }
432 }
433
434 LongBuffer newLongBuffer(final int size, final boolean direct) {
435 if(direct) {
436 final LongBuffer lb = Buffers.newDirectLongBuffer(size);
437 Assert.assertTrue(lb.isDirect());
438 return lb;
439 } else {
440 final LongBuffer lb = LongBuffer.wrap(new long[size]);
441 Assert.assertTrue(!lb.isDirect());
442 Assert.assertTrue(lb.hasArray());
443 return lb;
444 }
445 }
446
447 PointerBuffer newPointerBuffer(final int size, final boolean direct) {
448 if(direct) {
449 final PointerBuffer pb = PointerBuffer.allocateDirect(size);
450 Assert.assertTrue(pb.isDirect());
451 Assert.assertTrue(pb.getBuffer().isDirect());
452 return pb;
453 } else {
454 final PointerBuffer pb = PointerBuffer.allocate(size);
455 Assert.assertTrue(pb.hasArray());
456 Assert.assertTrue(!pb.isDirect());
457 return pb;
458 }
459 }
460
461 long cleanAddress(final long a) {
462 if (Platform.is32Bit()) {
463 return a & 0x00000000FFFFFFFFL;
464 } else {
465 return a;
466 }
467 }
468
469 PointerBuffer validatePointerBuffer(final PointerBuffer pb, final int elements) {
470 Assert.assertNotNull(pb);
471 Assert.assertEquals("PointerBuffer capacity not "+elements, elements, pb.capacity());
472 Assert.assertEquals("PointerBuffer remaining not "+elements, elements, pb.remaining());
473 System.err.println("Testing accessing PointerBuffer values [0.."+(elements-1)+"]");
474 for(int i=0; i<elements; i++) {
475 final long v = pb.get(i);
476 System.err.println(" "+i+"/"+elements+": 0x"+Long.toHexString(v));
477 }
478 return pb;
479 }
480 PointerBuffer safeByteBuffer2PointerBuffer(final ByteBuffer bb, final int elements) {
481 Assert.assertEquals("ByteBuffer capacity not PointerBuffer POINTER_SIZE * "+elements, elements * AbstractBuffer.POINTER_SIZE, bb.capacity());
482 Assert.assertEquals("ByteBuffer remaining not PointerBuffer POINTER_SIZE * "+elements, elements * AbstractBuffer.POINTER_SIZE, bb.remaining());
483 return validatePointerBuffer(PointerBuffer.wrap(bb), elements);
484 }
485
486 /**
487 * Verifies if all methods / signatures are properly generated,
488 * can be invoked and functions.
489 * This is a compilation (coverage) and runtime time (semantic) test.
490 * This covers indirect primitive arrays and direct NIO buffers.
491 */
492 public void chapter03TestCoverageFunctionalityNIOAndPrimitiveArray(final Bindingtest1 binding, final boolean direct) throws Exception {
493 int i;
494 long result;
495
496 final long context = 1;
497 final LongBuffer lb = newLongBuffer(1, direct);
498 lb.put(0, 10);
499
500 final ByteBuffer bb2 = newByteBuffer(Buffers.SIZEOF_LONG, direct);
501 final LongBuffer bb2L = bb2.asLongBuffer();
502 bb2L.put(0, 100);
503
504 final IntBuffer ib1 = newIntBuffer(Bindingtest1.ARRAY_SIZE, direct);
505 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
506 ib1.put(i, 1000);
507 }
508
509 final LongBuffer lb1 = newLongBuffer(Bindingtest1.ARRAY_SIZE, direct);
510 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
511 lb1.put(i, 1000);
512 }
513 final LongBuffer lb2 = newLongBuffer(Bindingtest1.ARRAY_SIZE, direct);
514 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
515 lb2.put(i, 10000);
516 }
517
518 final int[] iarray1 = new int[Bindingtest1.ARRAY_SIZE];
519 final int iarray1_offset = 0;
520 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
521 iarray1[i]= 1000;
522 }
523
524 final long[] larray1 = new long[Bindingtest1.ARRAY_SIZE];
525 final int larray1_offset = 0;
526 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
527 larray1[i]= 1000;
528 }
529
530 final long[] larray2 = new long[Bindingtest1.ARRAY_SIZE];
531 final int larray2_offset = 0;
532 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
533 larray2[i]= 10000;
534 }
535
536 result = binding.arrayTestInt32(context, ib1);
537 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
538
539 result = binding.arrayTestInt32(context, iarray1, iarray1_offset);
540 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
541
542 result = binding.arrayTestInt64(context, lb1);
543 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
544
545 result = binding.arrayTestInt64(context, larray1, larray1_offset);
546 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
547
548 result = binding.arrayTestFoo1(context, lb1);
549 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
550
551 result = binding.arrayTestFoo1(context, larray1, larray1_offset);
552 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
553
554 result = binding.arrayTestFooNioOnly(context, lb1);
555 Assert.assertTrue("Wrong result: "+result, 1+8000==result);
556
557 // LongBuffer arrayTestFoo2 ( LongBuffer ) - don't write-back array-arg
558 {
559 lb2.rewind();
560 final LongBuffer lb3 = newLongBuffer(Bindingtest1.ARRAY_SIZE, direct);
561 lb3.put(lb2);
562 lb3.rewind();
563 lb2.rewind();
564
565 // System.out.println("lb3: "+lb3);
566 Assert.assertTrue("Wrong result: "+lb3.capacity(), Bindingtest1.ARRAY_SIZE == lb3.capacity());
567 Assert.assertTrue("Wrong result: "+lb3.remaining(), Bindingtest1.ARRAY_SIZE == lb3.remaining());
568
569 final LongBuffer lbR = binding.arrayTestFoo2(lb3);
570 // System.out.println("lbR: "+lbR);
571
572 Assert.assertNotNull(lbR);
573 Assert.assertTrue("Wrong result: "+lb3.capacity(), Bindingtest1.ARRAY_SIZE == lb3.capacity());
574 Assert.assertTrue("Wrong result: "+lb3.remaining(), Bindingtest1.ARRAY_SIZE == lb3.remaining());
575 Assert.assertTrue("Wrong result: "+lbR.capacity(), Bindingtest1.ARRAY_SIZE == lbR.capacity());
576 Assert.assertTrue("Wrong result: "+lbR.remaining(), Bindingtest1.ARRAY_SIZE == lbR.remaining());
577 int j=0;
578 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
579 Assert.assertTrue("Wrong result: s:"+lb2.get(j)+" c: "+lb3.get(j), lb2.get(j)==lb3.get(j));
580 Assert.assertTrue("Wrong result: s:"+lb3.get(j)+" d: "+lbR.get(j), 1+lb3.get(j)==lbR.get(j));
581 }
582 }
583
584 // LongBuffer arrayTestFoo2 ( long[], int ) - don't write-back array-arg
585 {
586 final long[] larray3 = new long[Bindingtest1.ARRAY_SIZE];
587 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
588 larray3[i]= larray2[i];
589 }
590
591 final LongBuffer lbR = binding.arrayTestFoo2(larray3, 0);
592
593 Assert.assertNotNull(lbR);
594 Assert.assertTrue("Wrong result: "+lbR.capacity(), Bindingtest1.ARRAY_SIZE == lbR.capacity());
595 Assert.assertTrue("Wrong result: "+lbR.remaining(), Bindingtest1.ARRAY_SIZE == lbR.remaining());
596 int j=0;
597 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
598 Assert.assertTrue("Wrong result: s:"+larray2[j]+" c: "+larray3[j], larray2[j]==larray3[j]);
599 Assert.assertTrue("Wrong result: s:"+larray3[j]+" d: "+lbR.get(j), 1+larray3[j]==lbR.get(j));
600 }
601 }
602
603 // void arrayTestFoo3 ( LongBuffer ) - write-back array-arg
604 {
605 lb2.rewind();
606 final LongBuffer lb3 = newLongBuffer(Bindingtest1.ARRAY_SIZE, direct);
607 lb3.put(lb2);
608 lb3.rewind();
609 lb2.rewind();
610
611 // System.out.println("lb3: "+lb3);
612 Assert.assertTrue("Wrong result: "+lb3.capacity(), Bindingtest1.ARRAY_SIZE == lb3.capacity());
613 Assert.assertTrue("Wrong result: "+lb3.remaining(), Bindingtest1.ARRAY_SIZE == lb3.remaining());
614
615 binding.arrayTestFoo3(lb3);
616
617 Assert.assertTrue("Wrong result: "+lb3.capacity(), Bindingtest1.ARRAY_SIZE == lb3.capacity());
618 Assert.assertTrue("Wrong result: "+lb3.remaining(), Bindingtest1.ARRAY_SIZE == lb3.remaining());
619 int j=0;
620 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
621 Assert.assertTrue("Wrong result: s:"+lb2.get(j)+" d: "+lb3.get(j), 1+lb2.get(j)==lb3.get(j));
622 }
623 }
624
625 // void arrayTestFoo3 ( long[], int ) - write-back array-arg
626 {
627 final long[] larray3 = new long[Bindingtest1.ARRAY_SIZE];
628 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
629 larray3[i]= larray2[i];
630 }
631
632 binding.arrayTestFoo3(larray3, 0);
633
634 int j=0;
635 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
636 Assert.assertTrue("Wrong result: s:"+larray2[j]+" d: "+larray3[j], 1+larray2[j]==larray3[j]);
637 }
638 }
639
640 // PointerBuffer arrayTestFoo3ArrayToPtrPtr(LongBuffer)
641 // PointerBuffer arrayTestFoo3PtrPtr(PointerBuffer)
642 {
643 lb2.rewind();
644 final LongBuffer lb3 = newLongBuffer(Bindingtest1.ARRAY_SIZE*Bindingtest1.ARRAY_SIZE, direct);
645 int j;
646 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
647 lb3.put(lb2);
648 lb2.rewind();
649 }
650 lb3.rewind();
651
652 // System.out.println("lb3: "+lb3);
653 Assert.assertTrue("Wrong result: "+lb3.capacity(), Bindingtest1.ARRAY_SIZE*Bindingtest1.ARRAY_SIZE == lb3.capacity());
654 Assert.assertTrue("Wrong result: "+lb3.remaining(), Bindingtest1.ARRAY_SIZE*Bindingtest1.ARRAY_SIZE == lb3.remaining());
655
656 final PointerBuffer pb = binding.arrayTestFoo3ArrayToPtrPtr(lb3);
657 validatePointerBuffer(pb, Bindingtest1.ARRAY_SIZE);
658
659 final PointerBuffer pb2 = binding.arrayTestFoo3PtrPtr(pb);
660 validatePointerBuffer(pb2, Bindingtest1.ARRAY_SIZE);
661 for(j=0; j<Bindingtest1.ARRAY_SIZE*Bindingtest1.ARRAY_SIZE; j++) {
662 Assert.assertEquals("Wrong result: s:"+lb2.get(j%Bindingtest1.ARRAY_SIZE)+" d: "+lb3.get(j),
663 1+lb2.get(j%Bindingtest1.ARRAY_SIZE), lb3.get(j));
664 }
665 Assert.assertEquals(0, binding.arrayTestFoo3PtrPtrValidation(pb2, 10000));
666 }
667
668 // PointerBuffer.alloc*(ARRAY_SIZE)
669 // PointerBuffer.referenceBuffer(LongBuffer.getBuffer)
670 // " "
671 // PointerBuffer arrayTestFoo3PtrPtr(PointerBuffer)
672 {
673 final PointerBuffer pb = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
674 int j;
675 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
676 // the referenced buffer must be direct, non direct is not supported
677 final LongBuffer lb3 = Buffers.newDirectLongBuffer(Bindingtest1.ARRAY_SIZE);
678 lb3.put(lb2);
679 lb2.rewind();
680 lb3.rewind();
681
682 pb.referenceBuffer(lb3);
683 }
684 pb.rewind();
685
686 // System.out.println("lb3: "+lb3);
687 validatePointerBuffer(pb, Bindingtest1.ARRAY_SIZE);
688 Assert.assertNotNull(pb.getReferencedBuffer(0));
689 Assert.assertTrue("Wrong result: "+pb.getReferencedBuffer(0)+" != "+lb2, pb.getReferencedBuffer(0).equals(lb2));
690
691 final PointerBuffer pb2 = binding.arrayTestFoo3PtrPtr(pb); // pb2 is shallow
692 validatePointerBuffer(pb2, Bindingtest1.ARRAY_SIZE);
693 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
694 final LongBuffer i64b = (LongBuffer) pb.getReferencedBuffer(j);
695 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
696 Assert.assertEquals("Wrong result: ["+j+"]["+i+"] s:"+lb2.get(i)+" d: "+i64b.get(i),
697 1+lb2.get(i), i64b.get(i));
698 }
699 }
700 Assert.assertEquals(0, binding.arrayTestFoo3PtrPtrValidation(pb, 10000));
701 }
702
703 // pb = PointerBuffer.alloc*(ARRAY_SIZE)
704 // arrayTestFoo3CopyPtrPtrA(PointerBuffer dst, PointerBuffer src) (Native deep copy w/ alloc)
705 // " "
706 // PointerBuffer arrayTestFoo3PtrPtr(PointerBuffer)
707 {
708 final PointerBuffer pbS = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
709 int j;
710 for(j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
711 // the referenced buffer must be direct, non direct is not supported
712 final LongBuffer lb3 = Buffers.newDirectLongBuffer(Bindingtest1.ARRAY_SIZE);
713 lb3.put(lb2);
714 lb2.rewind();
715 lb3.rewind();
716
717 pbS.referenceBuffer(lb3);
718 }
719 pbS.rewind();
720 validatePointerBuffer(pbS, Bindingtest1.ARRAY_SIZE);
721 Assert.assertNotNull(pbS.getReferencedBuffer(0));
722 Assert.assertTrue("Wrong result: "+pbS.getReferencedBuffer(0)+" != "+lb2, pbS.getReferencedBuffer(0).equals(lb2));
723
724 final PointerBuffer pbD = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
725
726 // System.err.println("\n***pbS "+pbS); System.err.println("***pbD "+pbD);
727 binding.arrayTestFoo3CopyPtrPtrA(pbD, pbS); // pbD is shallow
728 validatePointerBuffer(pbD, Bindingtest1.ARRAY_SIZE);
729
730 final PointerBuffer pbD2 = binding.arrayTestFoo3PtrPtr(pbD); // pbD2 is shallow
731 Assert.assertEquals(0, binding.arrayTestFoo3PtrPtrValidation(pbD, 10000));
732 validatePointerBuffer(pbD2, Bindingtest1.ARRAY_SIZE);
733 Assert.assertEquals(0, binding.arrayTestFoo3PtrPtrValidation(pbD2, 10000));
734 }
735
736 result = binding.bufferTest(lb);
737 Assert.assertTrue("Wrong result: "+result, 10==result);
738
739 result = binding.bufferTestNioOnly(lb);
740 Assert.assertTrue("Wrong result: "+result, 10==result);
741
742 if(direct) {
743 result = binding.bufferTestNioDirectOnly(lb);
744 Assert.assertTrue("Wrong result: "+result, 10==result);
745 } else {
746 Exception e = null;
747 try {
748 binding.bufferTestNioDirectOnly(lb);
749 } catch (final RuntimeException re) {
750 e = re;
751 }
752 Assert.assertNotNull(e);
753 }
754
755 result = binding.doubleTest(context, lb, lb1, bb2, lb2);
756 Assert.assertTrue("Wrong result: "+result, 1+10+8000+100+80000==result);
757
758 result = binding.doubleTest(context, lb, larray1, larray1_offset, bb2, larray2, larray2_offset);
759 Assert.assertTrue("Wrong result: "+result, 1+10+8000+100+80000==result);
760
761 result = binding.doubleTestNioOnly(context, lb, lb1, bb2, lb2);
762 Assert.assertTrue("Wrong result: "+result, 1+10+8000+100+80000==result);
763
764 result = binding.mixedTest(context, lb, lb1);
765 Assert.assertTrue("Wrong result: "+result, 1+10+8000==result);
766
767 result = binding.mixedTest(context, lb, larray1, larray1_offset);
768 Assert.assertTrue("Wrong result: "+result, 1+10+8000==result);
769
770 result = binding.mixedTestNioOnly(context, lb, lb1);
771 Assert.assertTrue("Wrong result: "+result, 1+10+8000==result);
772
773 result = binding.nopTest();
774 Assert.assertTrue("Wrong result: "+result, 42==result);
775
776 i = binding.strToInt("42");
777 Assert.assertTrue("Wrong result: "+i, 42==i);
778
779 final String str = binding.intToStr(42);
780 Assert.assertTrue("Wrong result: "+str, str.equals("42"));
781
782 i = binding.stringArrayRead(new String[] { "1234", "5678", "9a" }, 3);
783 Assert.assertTrue("Wrong result: "+i, 10==i);
784
785 i = binding.stringArrayRead(null, 0);
786 Assert.assertTrue("Wrong result: "+i, 0==i);
787
788 {
789 // one 0xff in each byte array
790 // the referenced buffer must be direct, non direct is not supported
791 final ByteBuffer bbB = Buffers.newDirectByteBuffer(new byte [] {(byte)0xaa, (byte)0xff, (byte)0xba, (byte)0xbe});
792 bbB.rewind();
793 final PointerBuffer pbB = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
794 final PointerBuffer pbL = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
795 for(int j=0; j<Bindingtest1.ARRAY_SIZE; j++) {
796 pbB.referenceBuffer(bbB);
797 pbL.put(bbB.capacity());
798 }
799 pbB.rewind();
800 pbL.rewind();
801 validatePointerBuffer(pbB, Bindingtest1.ARRAY_SIZE);
802 Assert.assertNotNull(pbB.getReferencedBuffer(0));
803 Assert.assertTrue("Wrong result: "+pbB.getReferencedBuffer(0)+" != "+bbB, pbB.getReferencedBuffer(0).equals(bbB));
804 validatePointerBuffer(pbL, Bindingtest1.ARRAY_SIZE);
805 final long temp = pbL.get();
806 Assert.assertTrue("Wrong result: "+temp, temp==bbB.capacity());
807 pbL.rewind();
808 i = binding.binaryArrayRead(pbL, pbB, Bindingtest1.ARRAY_SIZE);
809 Assert.assertTrue("Wrong result: "+i, Bindingtest1.ARRAY_SIZE==i);
810 }
811
812 final IntBuffer ib = newIntBuffer(3, direct);
813 ib.put(0, 1);
814 ib.put(1, 2);
815 ib.put(2, 3);
816
817 final int[] iarray = new int[] { 1, 2, 3 };
818
819 i = binding.intArrayRead(ib, 3);
820 Assert.assertTrue("Wrong result: "+i, 6==i);
821
822 i = binding.intArrayRead(null, 0);
823 Assert.assertTrue("Wrong result: "+i, 0==i);
824
825 i = binding.intArrayRead(iarray, 0, 3);
826 Assert.assertTrue("Wrong result: "+i, 6==i);
827
828 i = binding.intArrayRead(null, 0, 0);
829 Assert.assertTrue("Wrong result: "+i, 0==i);
830
831 {
832 final long cfg_base = 0xAABBCCDD11223344L;
833
834 final PointerBuffer pb = newPointerBuffer(Bindingtest1.ARRAY_SIZE, direct);
835 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
836 long cfg_native;
837 if(Platform.is32Bit()) {
838 cfg_native = (cfg_base+i) & 0x00000000FFFFFFFFL; // umask 1st 32bit
839 } else {
840 cfg_native = (cfg_base+i);
841 }
842 final long cfg = binding.typeTestAnonSingle(cfg_base + i);
843 Assert.assertTrue("Wrong result: 0x"+Long.toHexString(cfg_native)+"+1 != 0x"+Long.toHexString(cfg), (cfg_native+1)==cfg);
844 pb.put(i, cfg_base+i);
845
846 final long t = pb.get(i);
847 Assert.assertTrue("Wrong result: 0x"+Long.toHexString(cfg_native)+" != 0x"+Long.toHexString(t), cfg_native==t);
848 }
849 pb.rewind();
850 final PointerBuffer pb2 = binding.typeTestAnonPointer(pb);
851 Assert.assertTrue("Wrong result: "+pb2.capacity(), Bindingtest1.ARRAY_SIZE == pb2.capacity());
852 Assert.assertTrue("Wrong result: "+pb2.remaining(), Bindingtest1.ARRAY_SIZE == pb2.remaining());
853 for(i=0; i<Bindingtest1.ARRAY_SIZE; i++) {
854 Assert.assertTrue("Wrong result: 0x"+Long.toHexString(pb.get(i))+"+1 != 0x"+Long.toHexString(pb2.get(i)), (pb.get(i)+1)==pb2.get(i));
855 }
856 }
857
858 {
859 final long l0 = 0xAAFFEE;
860 final long l1 = binding.testXID(l0);
861 final long l2 = binding.testXID_2(l0);
862 Assert.assertEquals(l0, l1);
863 Assert.assertEquals(l0, l2);
864
865 final ByteBuffer bb = Buffers.newDirectByteBuffer(AbstractBuffer.POINTER_SIZE);
866 for(int j=0; j<bb.limit(); j++) {
867 bb.put(j, (byte)(0xAA+j));
868 }
869 final ByteBuffer bbOut = binding.testAnonBuffer(bb);
870 Assert.assertEquals(bb, bbOut);
871
872 final ShortBlob sb = ShortBlob.create();
873 sb.setB1((byte)0xAA);
874 sb.setB2((byte)0xEE);
875 final ShortBlob sb_ = binding.testShortBlob(sb);
876 final ShortBlob sb0 = binding.testLPShortBlob0(sb);
877 final ShortBlob sb1 = binding.testLPShortBlob1(sb);
878 final ShortBlob sb2 = binding.testLPShortBlob2(sb);
879 final ShortBlob sb3 = binding.testLPShortBlob3(sb);
880 final ShortBlob sb4 = binding.testShortBlobL1(sb);
881 final ShortBlob sb5 = binding.testShortBlobL2(sb);
882 Assert.assertEquals(sb.getBuffer(), sb_.getBuffer());
883 Assert.assertEquals(sb.getBuffer(), sb0.getBuffer());
884 Assert.assertEquals(sb.getBuffer(), sb1.getBuffer());
885 Assert.assertEquals(sb.getBuffer(), sb2.getBuffer());
886 Assert.assertEquals(sb.getBuffer(), sb3.getBuffer());
887 Assert.assertEquals(sb.getBuffer(), sb4.getBuffer());
888 Assert.assertEquals(sb.getBuffer(), sb5.getBuffer());
889
890 final Int32Struct i32s = Int32Struct.create();
891 i32s.setB1((byte)0x02);
892 i32s.setB2((byte)0x12);
893 i32s.setB3((byte)0x22);
894 i32s.setB4((byte)0x32);
895 final Int32Struct i32s0 = binding.testInt32Struct(i32s);
896 Assert.assertEquals(i32s.getBuffer(), i32s0.getBuffer());
897
898 final AnonBlob ab = binding.testCreateAnonBlob();
899 binding.testDestroyAnonBlob(ab);
900
901 final long ab2 = binding.testCreateAnonBlob2();
902 binding.testDestroyAnonBlob2(ab2);
903
904 final long[] foo = new long[] { 0x1122334455667788L };
905 final LongBuffer fooLB = Buffers.newDirectLongBuffer(foo);
906 final LongBuffer foo1Out = binding.testFooPtr(fooLB);
907 Assert.assertEquals(fooLB, foo1Out);
908 final LongBuffer foo2Out = binding.testFooPtr(foo, 0);
909 Assert.assertEquals(fooLB, foo2Out);
910 }
911
912 {
913 i=41;
914 final int iRes = binding.testDelegate(i);
915 Assert.assertEquals(i+1, iRes);
916 }
917 }
918
919 public void chapter04TestPointerBuffer(final Bindingtest1 binding) throws Exception {
920 final long DEADBEEF = 0x00000000DEADBEEFL;
921
922 {
923 long bbA, bbA2;
924 ByteBuffer bb, bb2;
925 PointerBuffer bbPb;
926
927 final ByteBuffer blob = binding.createAPtrBlob();
928 final PointerBuffer blobPb = safeByteBuffer2PointerBuffer(blob, 1);
929 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & blobPb.get(0));
930
931 binding.arrayTestAVoidPtrTypeDim1Mutable(blobPb); // new memory in [0]
932 Assert.assertTrue(DEADBEEF != ( 0xFFFFFFFF & blobPb.get(0) ) );
933 bb = binding.arrayTestAVoidPtrTypeDim1Immutable(blobPb); // returns memory address of [0], returned as bb (blob)
934 bbA = cleanAddress( binding.getAPtrAddress(bb) ); // address of new memory in [0]
935 Assert.assertEquals(blobPb.get(0), bbA);
936
937 bbPb = safeByteBuffer2PointerBuffer(bb, 1);
938 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & bbPb.get(0));
939 Assert.assertEquals( blobPb.get(0), cleanAddress( binding.getAPtrAddress(bbPb.getBuffer()) ) );
940
941 bb2 = binding.arrayTestAVoidPtrTypeDim0(bb);
942 bbA2 = cleanAddress( binding.getAPtrAddress(bb2) );
943 Assert.assertEquals(bbA, bbA2);
944 binding.releaseAPtrBlob(bb);
945 binding.releaseAPtrBlob(blob);
946 }
947
948 {
949 long bbA, bbA2;
950 ByteBuffer bb;
951 PointerBuffer bbPb;
952
953 final ByteBuffer blob = binding.createAPtrBlob();
954 final PointerBuffer blobPb = safeByteBuffer2PointerBuffer(blob, 1);
955 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & blobPb.get(0));
956
957 binding.arrayTestAIntPtrTypeDim1Mutable(blobPb); // new memory in [0]
958 Assert.assertTrue(DEADBEEF != ( 0xFFFFFFFF & blobPb.get(0) ) );
959 bbA = cleanAddress( binding.arrayTestAIntPtrTypeDim1Immutable(blobPb) ); // returns memory address of [0], returned as intptr_t
960 Assert.assertEquals(blobPb.get(0), bbA);
961 bb = binding.getAPtrMemory(bbA);
962 bbPb = safeByteBuffer2PointerBuffer(bb, 1);
963 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & bbPb.get(0));
964
965 bbA2 = cleanAddress( binding.arrayTestAIntPtrTypeDim0(bbA) );
966 Assert.assertEquals(bbA, bbA2);
967 binding.releaseAPtrBlob(bb);
968 binding.releaseAPtrBlob(blob);
969 }
970
971 {
972 long bbA, bbA2;
973 ByteBuffer bb, bb2;
974 PointerBuffer bbPb;
975
976 final ByteBuffer blob = binding.createAPtrBlob();
977 final PointerBuffer blobPb = safeByteBuffer2PointerBuffer(blob, 1);
978 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & blobPb.get(0));
979
980 binding.arrayTestAPtr1TypeDim1Mutable(blobPb); // new memory in [0]
981 Assert.assertTrue(DEADBEEF != ( 0xFFFFFFFF & blobPb.get(0) ) );
982 bb = binding.arrayTestAPtr1TypeDim1Immutable(blobPb); // returns memory address of [0], returned as bb (blob)
983 bbA = cleanAddress( binding.getAPtrAddress(bb) ); // address of new memory in [0]
984 Assert.assertEquals(blobPb.get(0), bbA);
985
986 bbPb = safeByteBuffer2PointerBuffer(bb, 1);
987 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & bbPb.get(0));
988 Assert.assertEquals(blobPb.get(0), cleanAddress( binding.getAPtrAddress(bbPb.getBuffer()) ) );
989
990 bb2 = binding.arrayTestAPtr1TypeDim0(bb);
991 bbA2 = cleanAddress( binding.getAPtrAddress(bb2) );
992 Assert.assertEquals(bbA, bbA2);
993 binding.releaseAPtrBlob(bb);
994 binding.releaseAPtrBlob(blob);
995
996 }
997
998 {
999 long bbA, bbA2;
1000 ByteBuffer bb;
1001 PointerBuffer bbPb;
1002
1003 final ByteBuffer blob = binding.createAPtrBlob();
1004 final PointerBuffer blobPb = safeByteBuffer2PointerBuffer(blob, 1);
1005 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & blobPb.get(0));
1006
1007 binding.arrayTestAPtr2TypeDim1Mutable(blobPb); // new memory in [0]
1008 Assert.assertTrue(DEADBEEF != ( 0xFFFFFFFF & blobPb.get(0) ) );
1009 bbA = cleanAddress( binding.arrayTestAPtr2TypeDim1Immutable(blobPb) ); // returns memory address of [0], returned as intptr_t
1010 Assert.assertEquals(blobPb.get(0), bbA);
1011 bb = binding.getAPtrMemory(bbA);
1012 bbPb = safeByteBuffer2PointerBuffer(bb, 1);
1013 Assert.assertEquals(DEADBEEF, 0xFFFFFFFF & bbPb.get(0));
1014
1015 bbA2 = cleanAddress( binding.arrayTestAPtr2TypeDim0(bbA) );
1016 Assert.assertEquals(bbA, bbA2);
1017 binding.releaseAPtrBlob(bb);
1018 binding.releaseAPtrBlob(blob);
1019 }
1020
1021 }
1022
1023 /**
1024 * This covers indirect primitive arrays and indirect NIO buffers.
1025 */
1026 public void chapter05TestSomeFunctionsAllIndirect(final Bindingtest1 binding) throws Exception {
1027 int i;
1028
1029 final IntBuffer ib = IntBuffer.allocate(3);
1030 ib.put(0, 1);
1031 ib.put(1, 2);
1032 ib.put(2, 3);
1033
1034 final int[] iarray = new int[] { 1, 2, 3 };
1035
1036 i = binding.intArrayRead(ib, 3);
1037 Assert.assertTrue("Wrong result: "+i, 6==i);
1038
1039 i = binding.intArrayRead(iarray, 0, 3);
1040 Assert.assertTrue("Wrong result: "+i, 6==i);
1041
1042 final int[] src = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
1043 final IntBuffer srcB = IntBuffer.wrap(src);
1044 {
1045 final int[] dst = new int[src.length];
1046 i = binding.intArrayCopy(dst, 0, src, 0, src.length);
1047 System.err.println("ArrayCopy.01: "+Arrays.toString(dst));
1048 Assert.assertTrue("Wrong result: "+i, src.length==i);
1049 Assert.assertTrue(Arrays.equals(src, dst));
1050 }
1051 {
1052 final IntBuffer dstB = IntBuffer.allocate(src.length);
1053 i = binding.intArrayCopy(dstB, srcB, src.length);
1054 System.err.println("ArrayCopy.02: "+Arrays.toString(dstB.array())+", "+dstB);
1055 Assert.assertTrue("Wrong result: "+i, src.length==i);
1056 Assert.assertTrue(Arrays.equals(src, dstB.array()));
1057 }
1058
1059 {
1060 final int[] src36 = new int[] { 4, 5, 6, 7 };
1061 final int[] dst = new int[src36.length];
1062 i = binding.intArrayCopy(dst, 0, src, 3, src36.length);
1063 System.err.println("ArrayCopy.03: "+Arrays.toString(dst));
1064 Assert.assertTrue("Wrong result: "+i, src36.length==i);
1065 Assert.assertTrue(Arrays.equals(src36, dst));
1066 }
1067
1068 final int[] src2 = new int[] { 0, 0, 0, 4, 5, 6, 7, 0, 0, 0 };
1069 {
1070 final int[] dst = new int[src2.length];
1071 i = binding.intArrayCopy(dst, 3, src, 3, 4);
1072 System.err.println("ArrayCopy.04: "+Arrays.toString(dst));
1073 Assert.assertTrue("Wrong result: "+i, 4==i);
1074 Assert.assertTrue(Arrays.equals(src2, dst));
1075 }
1076 {
1077 final IntBuffer dstB = IntBuffer.allocate(src2.length);
1078 {
1079 dstB.position(3);
1080 srcB.position(3);
1081 i = binding.intArrayCopy(dstB, srcB, 4);
1082 dstB.position(0);
1083 srcB.position(0);
1084 }
1085 System.err.println("ArrayCopy.05: "+Arrays.toString(dstB.array())+", "+dstB);
1086 Assert.assertTrue("Wrong result: "+i, 4==i);
1087 Assert.assertTrue(Arrays.equals(src2, dstB.array()));
1088 }
1089 }
1090
1091 public static void assertAPTR(final long expected, final long actual) {
1092 System.err.println("0x"+Long.toHexString(expected)+" == 0x"+Long.toHexString(actual));
1093 if (Platform.is32Bit()) {
1094 int exp32;
1095 int act32;
1096 // if(Platform.isLittleEndian()) {
1097 exp32 = (int) ( expected ) ;
1098 act32 = (int) ( actual ) ;
1099 /* } else {
1100 exp32 = (int) ( expected >> 32 ) ;
1101 act32 = (int) ( actual >> 32 ) ;
1102 } */
1103 System.err.println("0x"+Integer.toHexString(exp32)+" == 0x"+Integer.toHexString(act32));
1104 Assert.assertEquals(exp32, act32);
1105 } else {
1106 Assert.assertEquals(expected, actual);
1107 }
1108 }
1109
1110 public void chapter09TestCompoundAlignment(final Bindingtest1 binding) throws Exception {
1111
1112 final MachineDataInfo.StaticConfig smd = MachineDataInfoRuntime.getStatic();
1113 final MachineDataInfo md = MachineDataInfoRuntime.getRuntime();
1114
1115 System.err.println("static md: "+smd);
1116 System.err.println("runtime md: "+md);
1117 System.err.println("compatible static/runtime: "+md.compatible(smd.md));
1118
1119 // Test compound alignment read
1120 {
1121 final TK_ComplicatedSuperSet cs = binding.createComplicatedSuperSet();
1122 Assert.assertEquals((byte)0xA0, cs.getBits1());
1123
1124 final TK_ComplicatedSubSet sub1 = cs.getSub1();
1125 Assert.assertEquals((byte)0xA1, sub1.getBits1());
1126 Assert.assertEquals(0x12345678, sub1.getId());
1127 Assert.assertEquals((byte)0xA2, sub1.getBits2());
1128 Assert.assertEquals(0x123456789abcdef0L, sub1.getLong0());
1129 Assert.assertEquals((byte)0xA3, sub1.getBits3());
1130 Assert.assertEquals(3.1415926535897932384626433832795, sub1.getReal0(), 0.0);
1131 Assert.assertEquals((byte)0xA4, sub1.getBits4());
1132 Assert.assertEquals(256.12345f, sub1.getReal1(), 0.0);
1133 Assert.assertEquals((byte)0xA5, sub1.getBits5());
1134 Assert.assertEquals(0xdeadbeefL, sub1.getLongX());
1135 Assert.assertEquals((byte)0xA6, sub1.getBits6());
1136
1137 Assert.assertEquals((byte)0xB0, cs.getBits2());
1138
1139 final TK_ComplicatedSubSet sub2 = cs.getSub2();
1140 Assert.assertEquals((byte)0xB1, sub2.getBits1());
1141 Assert.assertEquals(0x12345678, sub2.getId());
1142 Assert.assertEquals((byte)0xB2, sub2.getBits2());
1143 Assert.assertEquals(0x123456789abcdef0L, sub2.getLong0());
1144 Assert.assertEquals((byte)0xB3, sub2.getBits3());
1145 Assert.assertEquals(3.1415926535897932384626433832795, sub2.getReal0(), 0.0);
1146 Assert.assertEquals((byte)0xB4, sub2.getBits4());
1147 Assert.assertEquals(256.12345f, sub2.getReal1(), 0.0);
1148 Assert.assertEquals((byte)0xB5, sub2.getBits5());
1149 Assert.assertEquals(0xdeadbeefL, sub2.getLongX());
1150 Assert.assertEquals((byte)0xB6, sub2.getBits6());
1151
1152 Assert.assertEquals((byte)0xC0, cs.getBits3());
1153
1154 binding.destroyComplicatedSuperSet(cs);
1155 }
1156
1157 /********************************************************************************/
1158
1159 // Test compound alignment write
1160 {
1161 final TK_ComplicatedSuperSet cs = TK_ComplicatedSuperSet.create();
1162 cs.setBits1((byte)0xA0);
1163
1164 final TK_ComplicatedSubSet sub1 = cs.getSub1();
1165 sub1.setBits1((byte)0xA1);
1166 sub1.setId(0x12345678);
1167 sub1.setBits2((byte)0xA2);
1168 sub1.setLong0(0x123456789abcdef0L);
1169 sub1.setBits3((byte)0xA3);
1170 sub1.setReal0(3.1415926535897932384626433832795);
1171 sub1.setBits4((byte)0xA4);
1172 sub1.setReal1(256.12345f);
1173 sub1.setBits5((byte)0xA5);
1174 sub1.setLongX(0xdeadbeefL);
1175 sub1.setBits6((byte)0xA6);
1176
1177 cs.setBits2((byte)0xB0);
1178
1179 final TK_ComplicatedSubSet sub2 = cs.getSub2();
1180 sub2.setBits1((byte)0xB1);
1181 sub2.setId(0x12345678);
1182 sub2.setBits2((byte)0xB2);
1183 sub2.setLong0(0x123456789abcdef0L);
1184 sub2.setBits3((byte)0xB3);
1185 sub2.setReal0(3.1415926535897932384626433832795);
1186 sub2.setBits4((byte)0xB4);
1187 sub2.setReal1(256.12345f);
1188 sub2.setBits5((byte)0xB5);
1189 sub2.setLongX(0xdeadbeefL);
1190 sub2.setBits6((byte)0xB6);
1191
1192 cs.setBits3((byte)0xC0);
1193
1194 Assert.assertTrue(binding.hasInitValues(cs));
1195 }
1196 }
1197
1198 private static void dumpDim(final String pre, final TK_Dimension dim) {
1199 System.err.println(pre+dim.getX()+"/"+dim.getY()+" "+dim.getWidth()+"x"+dim.getHeight());
1200 }
1201 private static void assertDim(final String pre,
1202 final int expX, final int expY, final int expWidth, final int expHeight,
1203 final TK_Dimension hasDim) {
1204 dumpDim(pre, hasDim);
1205 Assert.assertEquals(expX, hasDim.getX());
1206 Assert.assertEquals(expY, hasDim.getY());
1207 Assert.assertEquals(expWidth, hasDim.getWidth());
1208 Assert.assertEquals(expHeight, hasDim.getHeight());
1209 }
1210 private static void dumpDim(final String pre, final int[] pos, final int size[]) {
1211 System.err.println(pre+pos[0]+"/"+pos[1]+" "+size[0]+"x"+size[1]);
1212 }
1213 private static void assertDim(final String pre,
1214 final int expX, final int expY, final int expWidth, final int expHeight,
1215 final int[] pos, final int size[]) {
1216 dumpDim(pre, pos, size);
1217 Assert.assertEquals(expX, pos[0]);
1218 Assert.assertEquals(expY, pos[1]);
1219 Assert.assertEquals(expWidth, size[0]);
1220 Assert.assertEquals(expHeight, size[1]);
1221 }
1222
1223 /** Test compound access call-by-reference */
1224 public void chapter10TestCompoundCallByReference(final Bindingtest1 binding) throws Exception {
1225
1226 final TK_Surface surface = binding.createSurface();
1227
1228 final long surfaceContext = surface.getCtx();
1229 assertAPTR(0x123456789abcdef0L, surfaceContext);
1230
1231 final TK_ContextWrapper ctxWrapper = surface.getCtxWrapper();
1232 final long wrapperContext = ctxWrapper.getCtx();
1233 assertAPTR(0xA23456781abcdef0L, wrapperContext);
1234
1235 final TK_Engine engine = surface.getEngine();
1236 final long engineContext = engine.getCtx();
1237 assertAPTR(0xB23456782abcdef0L, engineContext);
1238 Assert.assertEquals(0x0111, engine.render(0x0100, 0x0010, 0x0001));
1239
1240 surface.setCtx(surfaceContext);
1241 assertAPTR(surfaceContext, surface.getCtx());
1242 assertAPTR(wrapperContext, ctxWrapper.getCtx());
1243 assertAPTR(engineContext, engine.getCtx());
1244 Assert.assertEquals(0x0111, engine.render(0x0100, 0x0010, 0x0001));
1245
1246 ctxWrapper.setCtx(wrapperContext);
1247 assertAPTR(surfaceContext, surface.getCtx());
1248 assertAPTR(wrapperContext, ctxWrapper.getCtx());
1249 assertAPTR(engineContext, engine.getCtx());
1250 Assert.assertEquals(0x0111, engine.render(0x0100, 0x0010, 0x0001));
1251
1252 engine.setCtx(engineContext);
1253 assertAPTR(surfaceContext, surface.getCtx());
1254 assertAPTR(wrapperContext, ctxWrapper.getCtx());
1255 assertAPTR(engineContext, engine.getCtx());
1256 Assert.assertEquals(0x0111, engine.render(0x0100, 0x0010, 0x0001));
1257
1258 final TK_Dimension dimension = surface.getBounds();
1259 dumpDim("ch10: ref-dim ", dimension);
1260 Assert.assertEquals(0x11111111, dimension.getX());
1261 Assert.assertEquals(0x22222222, dimension.getY());
1262 Assert.assertEquals(0x33333333, dimension.getWidth());
1263 Assert.assertEquals(0x44444444, dimension.getHeight());
1264
1265 Assert.assertEquals(2, surface.getClipSize());
1266
1267 final TK_Dimension[] allclips = surface.getClips(0, new TK_Dimension[surface.getClipSize()], 0, surface.getClipSize());
1268
1269 for(int i=0; i<surface.getClipSize(); i++) {
1270 final TK_Dimension clip0 = surface.getClip(i);
1271 Assert.assertEquals(0x44444444 * (i+1) + 0x11111111, clip0.getX());
1272 Assert.assertEquals(0x44444444 * (i+1) + 0x22222222, clip0.getY());
1273 Assert.assertEquals(0x44444444 * (i+1) + 0x33333333, clip0.getWidth());
1274 Assert.assertEquals(0x44444444 * (i+1) + 0x44444444, clip0.getHeight());
1275
1276 final TK_Dimension[] clip1 = new TK_Dimension[1];
1277 surface.getClips(i, clip1, 0, 1);
1278 Assert.assertEquals(0x44444444 * (i+1) + 0x11111111, clip1[0].getX());
1279 Assert.assertEquals(0x44444444 * (i+1) + 0x22222222, clip1[0].getY());
1280 Assert.assertEquals(0x44444444 * (i+1) + 0x33333333, clip1[0].getWidth());
1281 Assert.assertEquals(0x44444444 * (i+1) + 0x44444444, clip1[0].getHeight());
1282
1283 Assert.assertEquals(0x44444444 * (i+1) + 0x11111111, allclips[i].getX());
1284 Assert.assertEquals(0x44444444 * (i+1) + 0x22222222, allclips[i].getY());
1285 Assert.assertEquals(0x44444444 * (i+1) + 0x33333333, allclips[i].getWidth());
1286 Assert.assertEquals(0x44444444 * (i+1) + 0x44444444, allclips[i].getHeight());
1287 }
1288
1289 binding.destroySurface(surface);
1290 }
1291
1292 /** Test compound access call-by-value */
1293 public void chapter11TestCompoundCallByValue(final Bindingtest1 binding) throws Exception {
1294 int sub = 0;
1295 {
1296 final TK_Surface surface = binding.createSurface();
1297 final TK_Dimension dim0 = surface.getBounds();
1298 assertDim("ch11."+sub+": ref-dim ", 0x11111111, 0x22222222, 0x33333333, 0x44444444, dim0);
1299
1300 final TK_Dimension dim1 = binding.getSurfaceBoundsValue(surface);
1301 assertDim("ch11."+sub+": val-dim ", 0x11111111, 0x22222222, 0x33333333, 0x44444444, dim1);
1302
1303 binding.destroySurface(surface);
1304 }
1305 {
1306 sub++;
1307 final TK_Dimension dim0 = binding.getBoundsValue(11, 22, 33, 44);
1308 assertDim("ch11."+sub+": val-dim ", 11, 22, 33, 44, dim0);
1309 sub++;
1310 final TK_Surface surface = binding.getSurfaceValue(dim0);
1311 final TK_Dimension dim1 = binding.getSurfaceBoundsValue(surface);
1312 assertDim("ch11."+sub+": val-dim ", 11, 22, 33, 44, dim1);
1313 sub++;
1314 final boolean sameInstanceByVal = binding.isSameInstanceByVal(dim0, dim1);
1315 final boolean sameInstanceByRef = binding.isSameInstanceByRef(dim0, dim1);
1316 System.err.println("ch11."+sub+": sameInstanceByVal "+sameInstanceByVal);
1317 System.err.println("ch11."+sub+": sameInstanceByRef "+sameInstanceByRef);
1318 Assert.assertFalse(sameInstanceByVal);
1319 Assert.assertFalse(sameInstanceByRef);
1320 }
1321 {
1322 final TK_Dimension dim1 = binding.getBoundsValue(11, 22, 33, 44);
1323 final TK_Dimension dim2 = binding.getBoundsValue(1, 2, 3, 4);
1324 final TK_Dimension[] sumands = { dim1, dim2 };
1325 {
1326 sub++;
1327 final TK_Dimension dimSum = binding.addDimensions(sumands);
1328 assertDim("ch11."+sub+": sum-dimArray ", 11+1, 22+2, 33+3, 44+4, dimSum);
1329 }
1330
1331 final TK_DimensionPair dimPair = TK_DimensionPair.create();
1332 dimPair.setPair(sumands, 0, 0, sumands.length);
1333 {
1334 sub++;
1335 final TK_Dimension[] dimsGet = dimPair.getPair(0, new TK_Dimension[2], 0, 2);
1336 assertDim("ch11."+sub+": dimsGet[0] ", 11, 22, 33, 44, dimsGet[0]);
1337 assertDim("ch11."+sub+": dimsGet[1] ", 1, 2, 3, 4, dimsGet[1]);
1338 }
1339 {
1340 sub++;
1341 final TK_Dimension dimSum = binding.addDimensionPair(dimPair);
1342 assertDim("ch11."+sub+": sum-dimPair ", 11+1, 22+2, 33+3, 44+4, dimSum);
1343 }
1344 {
1345 sub++;
1346 binding.zeroDimensions(sumands);
1347 assertDim("ch11."+sub+": zero-dim[0] ", 0, 0, 0, 0, sumands[0]);
1348 assertDim("ch11."+sub+": zero-dim[1] ", 0, 0, 0, 0, sumands[1]);
1349 }
1350 }
1351 {
1352 sub++;
1353 final TK_Dimension dim0 = binding.getBoundsValue(0, 0, 0, 0);
1354 final TK_Dimension[] dim0A = { dim0 };
1355 binding.copyPrimToDimensions(new int[] { 11, 22}, 0, new int[] { 100, 200}, 0, dim0A);
1356 assertDim("ch11."+sub+": copyPrim2Dim ", 11, 22, 100, 200, dim0);
1357
1358 sub++;
1359 final int[] pos = { 0, 0 };
1360 final int[] size = { 0, 0 };
1361 binding.copyDimensionsToPrim(dim0, pos, 0, size, 0);
1362 assertDim("ch11."+sub+": copyDim2Prim ", 11, 22, 100, 200, pos, size);
1363 }
1364 {
1365 sub++;
1366 final int expRGBAi = 0x112233aa;
1367 final byte[] expRGBAb = { (byte)0xaa, 0x33, 0x22, 0x11 };
1368 final int hasRGBAi = binding.rgbaToInt(expRGBAb, 0);
1369 System.err.println("ch11."+sub+": expRGBAb 0x"+
1370 Integer.toHexString(expRGBAb[3])+", 0x"+
1371 Integer.toHexString(expRGBAb[2])+", 0x"+
1372 Integer.toHexString(expRGBAb[1])+", 0x"+
1373 Integer.toHexString(expRGBAb[0]) );
1374 System.err.println("ch11."+sub+": hasRGBAi 0x"+Integer.toHexString(hasRGBAi));
1375 Assert.assertEquals(expRGBAi, hasRGBAi);
1376
1377 sub++;
1378 final byte[] hasRGBAb = new byte[] { 0, 0, 0, 0 };
1379 binding.intToRgba(hasRGBAi, hasRGBAb, 0);
1380 System.err.println("ch11."+sub+": hasRGBAb 0x"+
1381 Integer.toHexString(hasRGBAb[3])+", 0x"+
1382 Integer.toHexString(hasRGBAb[2])+", 0x"+
1383 Integer.toHexString(hasRGBAb[1])+", 0x"+
1384 Integer.toHexString(hasRGBAb[0]) );
1385 Assert.assertArrayEquals(expRGBAb, hasRGBAb);
1386 }
1387 {
1388 sub++;
1389 final int[] result = { 0 };
1390 binding.addInt(new int[] { 1, 2}, 0, result, 0);
1391 System.err.println("ch11."+sub+": addInt "+result[0]);
1392 Assert.assertEquals(3, result[0]);
1393 }
1394 {
1395 sub++;
1396 final byte[] result = { 0 };
1397 binding.addByte(new byte[] { 1, 2}, 0, result, 0);
1398 System.err.println("ch11."+sub+": addByte "+result[0]);
1399 Assert.assertEquals(3, result[0]);
1400 }
1401 }
1402
1403 /** Primitive.ConstValue.int32.Pointer - read access */
1404 private void chapter12_03aTestTKFieldConstValueInt32ReadAccess(final TK_Field model) {
1405 // Primitive.ConstValue.int32.Array - read only
1406 Assert.assertEquals(88, model.getConstInt32ArrayConstOneElem());
1407 Assert.assertEquals(3, TK_Field.getConstInt32ArrayConstLenElemCount());
1408 {
1409 final int size = TK_Field.getConstInt32ArrayConstLenElemCount();
1410 Assert.assertEquals(3, size);
1411 final int[] all = model.getConstInt32ArrayConstLen(0, new int[size], 0, size);
1412 final IntBuffer allB = model.getConstInt32ArrayConstLen();
1413 Assert.assertEquals(size, all.length);
1414 Assert.assertEquals(size, allB.limit());
1415 for(int i=0; i<size; i++) {
1416 Assert.assertEquals(1 + i, all[i]);
1417 Assert.assertEquals(1 + i, allB.get(i));
1418 final int[] s = model.getConstInt32ArrayConstLen(i /* srcPos */, new int[3] /* dest */, 1 /* destPos */, 1 /* length */);
1419 Assert.assertEquals(1 + i, s[1]);
1420 }
1421 }
1422
1423 // Primitive.ConstValue.int32.Pointer - read
1424 Assert.assertEquals(1, TK_Field.getConstInt32PointerConstOneElemElemCount());
1425 Assert.assertEquals(false, model.isConstInt32PointerConstOneElemNull());
1426 Assert.assertEquals(10, model.getConstInt32PointerConstOneElem());
1427
1428 Assert.assertEquals(0, model.getConstInt32PointerMaxOneElemElemCount());
1429 Assert.assertEquals(true, model.isConstInt32PointerMaxOneElemNull());
1430 {
1431 Exception e = null;
1432 try {
1433 @SuppressWarnings("unused")
1434 final int i = model.getConstInt32PointerMaxOneElem(); // NULL -> exception
1435 } catch(final Exception _e) { e = _e; }
1436 Assert.assertNotNull(e);
1437 System.err.println("Expected exception: "+e);
1438 }
1439
1440 Assert.assertEquals(3, TK_Field.getConstInt32PointerConstLenElemCount());
1441 Assert.assertEquals(false, model.isConstInt32PointerConstLenNull());
1442 {
1443 final int size = TK_Field.getConstInt32PointerConstLenElemCount();
1444 Assert.assertEquals(3, size);
1445 final int[] all = model.getConstInt32PointerConstLen(0, new int[size], 0, size);
1446 final IntBuffer allB = model.getConstInt32PointerConstLen();
1447 Assert.assertEquals(size, all.length);
1448 Assert.assertEquals(size, allB.limit());
1449 for(int i=0; i<size; i++) {
1450 Assert.assertEquals(21 + i, all[i]);
1451 Assert.assertEquals(21 + i, allB.get(i));
1452 final int[] s = model.getConstInt32PointerConstLen(i /* srcPos */, new int[3] /* dest */, 2 /* destPos */, 1 /* length */);
1453 Assert.assertEquals(21 + i, s[2]);
1454 }
1455 }
1456
1457 Assert.assertEquals(0, model.getConstInt32PointerVariaLenElemCount());
1458 Assert.assertEquals(true, model.isConstInt32PointerVariaLenNull());
1459 {
1460 Exception e1 = null;
1461 try {
1462 @SuppressWarnings("unused")
1463 final IntBuffer ib = model.getConstInt32PointerVariaLen(); // NULL -> exception
1464 } catch(final Exception _e) { e1 = _e; }
1465 Assert.assertNotNull(e1);
1466 System.err.println("Expected exception-1: "+e1);
1467
1468 Exception e2 = null;
1469 try {
1470 @SuppressWarnings("unused")
1471 final int[] ia = model.getConstInt32PointerVariaLen(0, new int[0], 0, 0); // NULL -> exception
1472 } catch(final Exception _e) { e2 = _e; }
1473 Assert.assertNotNull(e2);
1474 System.err.println("Expected exception-2: "+e2);
1475 }
1476
1477 {
1478 final int size = model.getConstInt32PointerCustomLenElemCount();
1479 Assert.assertEquals(4, size);
1480 Assert.assertEquals(false, model.isConstInt32PointerCustomLenNull());
1481 final int[] all = model.getConstInt32PointerCustomLen(0, new int[size], 0, size);
1482 final IntBuffer allB = model.getConstInt32PointerCustomLen();
1483 Assert.assertEquals(size, all.length);
1484 Assert.assertEquals(size, allB.limit());
1485 for(int i=0; i<size; i++) {
1486 Assert.assertEquals(31 + i, all[i]);
1487 Assert.assertEquals(31 + i, allB.get(i));
1488 final int[] s = model.getConstInt32PointerCustomLen(i /* srcPos */, new int[1] /* dest */, 0 /* destPos */, 1 /* length */);
1489 Assert.assertEquals(31 + i, s[0]);
1490 }
1491 }
1492 }
1493
1494 /** Primitive.ConstValue.int32.Pointer - write access */
1495 private void chapter12_03bTestTKFieldConstValueInt32WriteAccess(final TK_Field model) {
1496 Assert.assertEquals(0, model.getConstInt32PointerMaxOneElemElemCount());
1497 Assert.assertEquals(true, model.isConstInt32PointerMaxOneElemNull());
1498 model.setConstInt32PointerMaxOneElem(110);
1499 Assert.assertEquals(1, model.getConstInt32PointerMaxOneElemElemCount());
1500 Assert.assertEquals(false, model.isConstInt32PointerMaxOneElemNull());
1501 Assert.assertEquals(110, model.getConstInt32PointerMaxOneElem());
1502 model.releaseConstInt32PointerMaxOneElem();
1503 Assert.assertEquals(0, model.getConstInt32PointerMaxOneElemElemCount());
1504 Assert.assertEquals(true, model.isConstInt32PointerMaxOneElemNull());
1505
1506 Assert.assertEquals(3, TK_Field.getConstInt32PointerConstLenElemCount());
1507 Assert.assertEquals(false, model.isConstInt32PointerConstLenNull());
1508 {
1509 // write 1 via IntBuffer reference get
1510 // FIXME: Sort of violates the contract
1511 {
1512 final IntBuffer ib = model.getConstInt32PointerConstLen();
1513 Assert.assertEquals(3, ib.limit());
1514 for(int i=0; i<3; ++i) {
1515 ib.put(i, 120+i);
1516 }
1517 }
1518 // verify 1
1519 {
1520 final int size = TK_Field.getConstInt32PointerConstLenElemCount();
1521 Assert.assertEquals(3, size);
1522 final int[] all = model.getConstInt32PointerConstLen(0, new int[size], 0, size);
1523 final IntBuffer allB = model.getConstInt32PointerConstLen();
1524 Assert.assertEquals(size, all.length);
1525 Assert.assertEquals(size, allB.limit());
1526 for(int i=0; i<size; i++) {
1527 Assert.assertEquals(120 + i, all[i]);
1528 Assert.assertEquals(120 + i, allB.get(i));
1529 final int[] s = model.getConstInt32PointerConstLen(i, new int[1], 0, 1);
1530 Assert.assertEquals(120 + i, s[0]);
1531 }
1532 }
1533 }
1534
1535 Assert.assertEquals(0, model.getConstInt32PointerVariaLenElemCount());
1536 Assert.assertEquals(true, model.isConstInt32PointerVariaLenNull());
1537 {
1538 Exception e1 = null;
1539 try {
1540 @SuppressWarnings("unused")
1541 final IntBuffer ib = model.getConstInt32PointerVariaLen(); // NULL -> exception
1542 } catch(final Exception _e) { e1 = _e; }
1543 Assert.assertNotNull(e1);
1544 System.err.println("Expected exception-1: "+e1);
1545
1546 Exception e2 = null;
1547 try {
1548 @SuppressWarnings("unused")
1549 final int[] ia = model.getConstInt32PointerVariaLen(0, new int[0], 0, 0); // NULL -> exception
1550 } catch(final Exception _e) { e2 = _e; }
1551 Assert.assertNotNull(e2);
1552 System.err.println("Expected exception-2: "+e2);
1553 }
1554 {
1555 // write 1 via int[] set, also actually allocating initial memory
1556 {
1557 final int[] ia = { 220, 221, 222, 223, 224 };
1558 model.setConstInt32PointerVariaLen(ia, 0, ia.length);
1559 }
1560 // verify 1
1561 {
1562 final int size = model.getConstInt32PointerVariaLenElemCount();
1563 Assert.assertEquals(5, size);
1564 final int[] all = model.getConstInt32PointerVariaLen(0, new int[size], 0, size);
1565 final IntBuffer allB = model.getConstInt32PointerVariaLen();
1566 Assert.assertEquals(size, all.length);
1567 Assert.assertEquals(size, allB.limit());
1568 for(int i=0; i<size; i++) {
1569 Assert.assertEquals(220 + i, all[i]);
1570 Assert.assertEquals(220 + i, allB.get(i));
1571 final int[] s = model.getConstInt32PointerVariaLen(i, new int[1], 0, 1);
1572 Assert.assertEquals(220 + i, s[0]);
1573 }
1574 }
1575 // write 2 via IntBuffer reference get
1576 {
1577 final IntBuffer ib = model.getConstInt32PointerVariaLen();
1578 Assert.assertEquals(5, ib.limit());
1579 for(int i=0; i<5; ++i) {
1580 ib.put(i, 120+i);
1581 }
1582 }
1583 // verify 2
1584 {
1585 final int size = model.getConstInt32PointerVariaLenElemCount();
1586 Assert.assertEquals(5, size);
1587 final int[] all = model.getConstInt32PointerVariaLen(0, new int[size], 0, size);
1588 final IntBuffer allB = model.getConstInt32PointerVariaLen();
1589 Assert.assertEquals(size, all.length);
1590 Assert.assertEquals(size, allB.limit());
1591 for(int i=0; i<size; i++) {
1592 Assert.assertEquals(120 + i, all[i]);
1593 Assert.assertEquals(120 + i, allB.get(i));
1594 final int[] s = model.getConstInt32PointerVariaLen(i, new int[1], 0, 1);
1595 Assert.assertEquals(120 + i, s[0]);
1596 }
1597 }
1598 model.releaseConstInt32PointerVariaLen();
1599 Assert.assertEquals(0, model.getConstInt32PointerVariaLenElemCount());
1600 Assert.assertEquals(true, model.isConstInt32PointerVariaLenNull());
1601 }
1602
1603 {
1604 // write 1 via IntBuffer reference get
1605 {
1606 final int size = model.getConstInt32PointerCustomLenElemCount();
1607 Assert.assertEquals(4, size);
1608 Assert.assertEquals(false, model.isConstInt32PointerCustomLenNull());
1609 final IntBuffer ib = model.getConstInt32PointerCustomLen();
1610 Assert.assertEquals(size, ib.limit());
1611 for(int i=0; i<size; ++i) {
1612 ib.put(i, 120+i);
1613 }
1614 }
1615 // verify 1
1616 {
1617 final int size = model.getConstInt32PointerCustomLenElemCount();
1618 Assert.assertEquals(4, size);
1619 final int[] all = model.getConstInt32PointerCustomLen(0, new int[size], 0, size);
1620 final IntBuffer allB = model.getConstInt32PointerCustomLen();
1621 Assert.assertEquals(size, all.length);
1622 Assert.assertEquals(size, allB.limit());
1623 for(int i=0; i<size; i++) {
1624 Assert.assertEquals(120 + i, all[i]);
1625 Assert.assertEquals(120 + i, allB.get(i));
1626 final int[] s = model.getConstInt32PointerCustomLen(i, new int[1], 0, 1);
1627 Assert.assertEquals(120 + i, s[0]);
1628 }
1629 }
1630 // write 2 via int[] set
1631 {
1632 final int[] ia = { 0, 220, 221, 222, 223, 224, 0 };
1633 model.setConstInt32PointerCustomLen(ia, 1 /* srcPos */, ia.length-2);
1634 }
1635 // verify 2
1636 {
1637 final int size = model.getConstInt32PointerCustomLenElemCount();
1638 Assert.assertEquals(5, size);
1639 final int[] all = model.getConstInt32PointerCustomLen(0, new int[size], 0, size);
1640 final IntBuffer allB = model.getConstInt32PointerCustomLen();
1641 Assert.assertEquals(size, all.length);
1642 Assert.assertEquals(size, allB.limit());
1643 for(int i=0; i<size; i++) {
1644 Assert.assertEquals(220 + i, all[i]);
1645 Assert.assertEquals(220 + i, allB.get(i));
1646 final int[] s = model.getConstInt32PointerCustomLen(i, new int[1], 0, 1);
1647 Assert.assertEquals(220 + i, s[0]);
1648 }
1649 }
1650 model.releaseConstInt32PointerCustomLen(); // FIXME: Ownership is ambiguous, may leak native allocated memory, not free'ed!
1651 Assert.assertEquals(0, model.getConstInt32PointerCustomLenElemCount());
1652 Assert.assertEquals(true, model.isConstInt32PointerCustomLenNull());
1653 }
1654 }
1655
1656 /** Primitive.VariaValue.int32.Pointer - read access */
1657 private void chapter12_04aTestTKFieldVariaValueInt32ReadAccess(final TK_Field model) {
1658 // Primitive.ConstValue.int32.Array - read only
1659 Assert.assertEquals(88, model.getVariaInt32ArrayConstOneElem());
1660 Assert.assertEquals(3, TK_Field.getVariaInt32ArrayConstLenElemCount());
1661 {
1662 final int size = TK_Field.getVariaInt32ArrayConstLenElemCount();
1663 Assert.assertEquals(3, size);
1664 final int[] all = model.getVariaInt32ArrayConstLen(0, new int[size], 0, size);
1665 final IntBuffer allB = model.getVariaInt32ArrayConstLen();
1666 Assert.assertEquals(size, all.length);
1667 Assert.assertEquals(size, allB.limit());
1668 for(int i=0; i<size; i++) {
1669 Assert.assertEquals(1 + i, all[i]);
1670 Assert.assertEquals(1 + i, allB.get(i));
1671 final int[] s = model.getVariaInt32ArrayConstLen(i /* srcPos */, new int[3] /* dest */, 1 /* destPos */, 1 /* length */);
1672 Assert.assertEquals(1 + i, s[1]);
1673 }
1674 }
1675
1676 // Primitive.ConstValue.int32.Pointer - read
1677 Assert.assertEquals(1, TK_Field.getVariaInt32PointerConstOneElemElemCount());
1678 Assert.assertEquals(false, model.isVariaInt32PointerConstOneElemNull());
1679 Assert.assertEquals(10, model.getVariaInt32PointerConstOneElem());
1680
1681 Assert.assertEquals(0, model.getVariaInt32PointerMaxOneElemElemCount());
1682 Assert.assertEquals(true, model.isVariaInt32PointerMaxOneElemNull());
1683 {
1684 Exception e = null;
1685 try {
1686 @SuppressWarnings("unused")
1687 final int i = model.getVariaInt32PointerMaxOneElem(); // NULL -> exception
1688 } catch(final Exception _e) { e = _e; }
1689 Assert.assertNotNull(e);
1690 System.err.println("Expected exception: "+e);
1691 }
1692
1693 Assert.assertEquals(3, TK_Field.getVariaInt32PointerConstLenElemCount());
1694 Assert.assertEquals(false, model.isVariaInt32PointerConstLenNull());
1695 {
1696 final int size = TK_Field.getVariaInt32PointerConstLenElemCount();
1697 Assert.assertEquals(3, size);
1698 final int[] all = model.getVariaInt32PointerConstLen(0, new int[size], 0, size);
1699 final IntBuffer allB = model.getVariaInt32PointerConstLen();
1700 Assert.assertEquals(size, all.length);
1701 Assert.assertEquals(size, allB.limit());
1702 for(int i=0; i<size; i++) {
1703 Assert.assertEquals(21 + i, all[i]);
1704 Assert.assertEquals(21 + i, allB.get(i));
1705 final int[] s = model.getVariaInt32PointerConstLen(i /* srcPos */, new int[3] /* dest */, 2 /* destPos */, 1 /* length */);
1706 Assert.assertEquals(21 + i, s[2]);
1707 }
1708 }
1709
1710 Assert.assertEquals(0, model.getVariaInt32PointerVariaLenElemCount());
1711 Assert.assertEquals(true, model.isVariaInt32PointerVariaLenNull());
1712 {
1713 Exception e1 = null;
1714 try {
1715 @SuppressWarnings("unused")
1716 final IntBuffer ib = model.getVariaInt32PointerVariaLen(); // NULL -> exception
1717 } catch(final Exception _e) { e1 = _e; }
1718 Assert.assertNotNull(e1);
1719 System.err.println("Expected exception-1: "+e1);
1720
1721 Exception e2 = null;
1722 try {
1723 @SuppressWarnings("unused")
1724 final int[] ia = model.getVariaInt32PointerVariaLen(0, new int[0], 0, 0); // NULL -> exception
1725 } catch(final Exception _e) { e2 = _e; }
1726 Assert.assertNotNull(e2);
1727 System.err.println("Expected exception-2: "+e2);
1728 }
1729
1730 {
1731 final int size = model.getVariaInt32PointerCustomLenElemCount();
1732 Assert.assertEquals(4, size);
1733 Assert.assertEquals(false, model.isVariaInt32PointerCustomLenNull());
1734 final int[] all = model.getVariaInt32PointerCustomLen(0, new int[size], 0, size);
1735 final IntBuffer allB = model.getVariaInt32PointerCustomLen();
1736 Assert.assertEquals(size, all.length);
1737 Assert.assertEquals(size, allB.limit());
1738 for(int i=0; i<size; i++) {
1739 Assert.assertEquals(31 + i, all[i]);
1740 Assert.assertEquals(31 + i, allB.get(i));
1741 final int[] s = model.getVariaInt32PointerCustomLen(i /* srcPos */, new int[1] /* dest */, 0 /* destPos */, 1 /* length */);
1742 Assert.assertEquals(31 + i, s[0]);
1743 }
1744 }
1745 }
1746
1747 /** Primitive.VariaValue.int32.Pointer - write access */
1748 private void chapter12_04bTestTKFieldVariaValueInt32WriteAccess(final TK_Field model) {
1749 Assert.assertEquals(1, TK_Field.getVariaInt32PointerConstOneElemElemCount());
1750 Assert.assertEquals(false, model.isVariaInt32PointerConstOneElemNull());
1751 model.setVariaInt32PointerConstOneElem(109);
1752 Assert.assertEquals(109, model.getVariaInt32PointerConstOneElem());
1753
1754 Assert.assertEquals(0, model.getVariaInt32PointerMaxOneElemElemCount());
1755 Assert.assertEquals(true, model.isVariaInt32PointerMaxOneElemNull());
1756 model.setVariaInt32PointerMaxOneElem(110);
1757 Assert.assertEquals(1, model.getVariaInt32PointerMaxOneElemElemCount());
1758 Assert.assertEquals(false, model.isVariaInt32PointerMaxOneElemNull());
1759 Assert.assertEquals(110, model.getVariaInt32PointerMaxOneElem());
1760 model.releaseVariaInt32PointerMaxOneElem();
1761 Assert.assertEquals(0, model.getVariaInt32PointerMaxOneElemElemCount());
1762 Assert.assertEquals(true, model.isVariaInt32PointerMaxOneElemNull());
1763
1764 Assert.assertEquals(3, TK_Field.getVariaInt32PointerConstLenElemCount());
1765 Assert.assertEquals(false, model.isVariaInt32PointerConstLenNull());
1766 {
1767 // write 1 via IntBuffer reference get
1768 {
1769 final IntBuffer ib = model.getVariaInt32PointerConstLen();
1770 Assert.assertEquals(3, ib.limit());
1771 for(int i=0; i<3; ++i) {
1772 ib.put(i, 120+i);
1773 }
1774 }
1775 // verify 1
1776 {
1777 final int size = TK_Field.getVariaInt32PointerConstLenElemCount();
1778 Assert.assertEquals(3, size);
1779 final int[] all = model.getVariaInt32PointerConstLen(0, new int[size], 0, size);
1780 final IntBuffer allB = model.getVariaInt32PointerConstLen();
1781 Assert.assertEquals(size, all.length);
1782 Assert.assertEquals(size, allB.limit());
1783 for(int i=0; i<size; i++) {
1784 Assert.assertEquals(120 + i, all[i]);
1785 Assert.assertEquals(120 + i, allB.get(i));
1786 final int[] s = model.getVariaInt32PointerConstLen(i, new int[1], 0, 1);
1787 Assert.assertEquals(120 + i, s[0]);
1788 }
1789 }
1790 // write 2 via int[] set
1791 {
1792 final int[] ia = { 220, 221, 222 };
1793 model.setVariaInt32PointerConstLen(ia, 0, 0, ia.length);
1794 }
1795 // verify 2
1796 {
1797 final int size = TK_Field.getVariaInt32PointerConstLenElemCount();
1798 Assert.assertEquals(3, size);
1799 final int[] all = model.getVariaInt32PointerConstLen(0, new int[size], 0, size);
1800 final IntBuffer allB = model.getVariaInt32PointerConstLen();
1801 Assert.assertEquals(size, all.length);
1802 Assert.assertEquals(size, allB.limit());
1803 for(int i=0; i<size; i++) {
1804 System.err.printf("%d/%d: A %d, B %d%n", i, size, all[i], allB.get(i));
1805 }
1806 for(int i=0; i<size; i++) {
1807 Assert.assertEquals(220 + i, all[i]);
1808 Assert.assertEquals(220 + i, allB.get(i));
1809 final int[] s = model.getVariaInt32PointerConstLen(i, new int[1], 0, 1);
1810 Assert.assertEquals(220 + i, s[0]);
1811 }
1812 }
1813 // write 3 via int[] single set @ offset
1814 {
1815 final int[] ia = { 0, 321, 322 };
1816 model.setVariaInt32PointerConstLen(ia, 1, 1, 2);
1817 }
1818 // verify 3
1819 {
1820 final int size = TK_Field.getVariaInt32PointerConstLenElemCount();
1821 Assert.assertEquals(3, size);
1822 final int[] all = model.getVariaInt32PointerConstLen(0, new int[size], 0, size);
1823 final IntBuffer allB = model.getVariaInt32PointerConstLen();
1824 Assert.assertEquals(size, all.length);
1825 Assert.assertEquals(size, allB.limit());
1826 for(int i=0; i<1; i++) {
1827 Assert.assertEquals(220 + i, all[i]);
1828 Assert.assertEquals(220 + i, allB.get(i));
1829 final int[] s = model.getVariaInt32PointerConstLen(i, new int[1], 0, 1);
1830 Assert.assertEquals(220 + i, s[0]);
1831 }
1832 for(int i=1; i<size; i++) {
1833 Assert.assertEquals(320 + i, all[i]);
1834 Assert.assertEquals(320 + i, allB.get(i));
1835 final int[] s = model.getVariaInt32PointerConstLen(i, new int[1], 0, 1);
1836 Assert.assertEquals(320 + i, s[0]);
1837 }
1838 }
1839 }
1840
1841 Assert.assertEquals(0, model.getVariaInt32PointerVariaLenElemCount());
1842 Assert.assertEquals(true, model.isVariaInt32PointerVariaLenNull());
1843 {
1844 Exception e1 = null;
1845 try {
1846 @SuppressWarnings("unused")
1847 final IntBuffer ib = model.getVariaInt32PointerVariaLen(); // NULL -> exception
1848 } catch(final Exception _e) { e1 = _e; }
1849 Assert.assertNotNull(e1);
1850 System.err.println("Expected exception-1: "+e1);
1851
1852 Exception e2 = null;
1853 try {
1854 @SuppressWarnings("unused")
1855 final int[] ia = model.getVariaInt32PointerVariaLen(0, new int[0], 0, 0); // NULL -> exception
1856 } catch(final Exception _e) { e2 = _e; }
1857 Assert.assertNotNull(e2);
1858 System.err.println("Expected exception-2: "+e2);
1859 }
1860 {
1861 // write 1 via int[] set, also actually allocating initial memory
1862 {
1863 final int[] ia = { 220, 221, 222, 223, 224 };
1864 model.setVariaInt32PointerVariaLen(false /* subset */, ia, 0, 0, ia.length);
1865 }
1866 // verify 1
1867 {
1868 final int size = model.getVariaInt32PointerVariaLenElemCount();
1869 Assert.assertEquals(5, size);
1870 final int[] all = model.getVariaInt32PointerVariaLen(0, new int[size], 0, size);
1871 final IntBuffer allB = model.getVariaInt32PointerVariaLen();
1872 Assert.assertEquals(size, all.length);
1873 Assert.assertEquals(size, allB.limit());
1874 for(int i=0; i<size; i++) {
1875 Assert.assertEquals(220 + i, all[i]);
1876 Assert.assertEquals(220 + i, allB.get(i));
1877 final int[] s = model.getVariaInt32PointerVariaLen(i, new int[1], 0, 1);
1878 Assert.assertEquals(220 + i, s[0]);
1879 }
1880 }
1881 // write 2 via IntBuffer reference get
1882 {
1883 final IntBuffer ib = model.getVariaInt32PointerVariaLen();
1884 Assert.assertEquals(5, ib.limit());
1885 for(int i=0; i<5; ++i) {
1886 ib.put(i, 120+i);
1887 }
1888 }
1889 // verify 2
1890 {
1891 final int size = model.getVariaInt32PointerVariaLenElemCount();
1892 Assert.assertEquals(5, size);
1893 final int[] all = model.getVariaInt32PointerVariaLen(0, new int[size], 0, size);
1894 final IntBuffer allB = model.getVariaInt32PointerVariaLen();
1895 Assert.assertEquals(size, all.length);
1896 Assert.assertEquals(size, allB.limit());
1897 for(int i=0; i<size; i++) {
1898 Assert.assertEquals(120 + i, all[i]);
1899 Assert.assertEquals(120 + i, allB.get(i));
1900 final int[] s = model.getVariaInt32PointerVariaLen(i, new int[1], 0, 1);
1901 Assert.assertEquals(120 + i, s[0]);
1902 }
1903 }
1904 model.releaseVariaInt32PointerVariaLen();
1905 Assert.assertEquals(0, model.getVariaInt32PointerVariaLenElemCount());
1906 Assert.assertEquals(true, model.isVariaInt32PointerVariaLenNull());
1907 }
1908
1909 {
1910 // write 1 via IntBuffer reference get
1911 {
1912 final int size = model.getVariaInt32PointerCustomLenElemCount();
1913 Assert.assertEquals(4, size);
1914 Assert.assertEquals(false, model.isVariaInt32PointerCustomLenNull());
1915 final IntBuffer ib = model.getVariaInt32PointerCustomLen();
1916 Assert.assertEquals(size, ib.limit());
1917 for(int i=0; i<size; ++i) {
1918 ib.put(i, 120+i);
1919 }
1920 }
1921 // verify 1
1922 {
1923 final int size = model.getVariaInt32PointerCustomLenElemCount();
1924 Assert.assertEquals(4, size);
1925 final int[] all = model.getVariaInt32PointerCustomLen(0, new int[size], 0, size);
1926 final IntBuffer allB = model.getVariaInt32PointerCustomLen();
1927 Assert.assertEquals(size, all.length);
1928 Assert.assertEquals(size, allB.limit());
1929 for(int i=0; i<size; i++) {
1930 Assert.assertEquals(120 + i, all[i]);
1931 Assert.assertEquals(120 + i, allB.get(i));
1932 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[1], 0, 1);
1933 Assert.assertEquals(120 + i, s[0]);
1934 }
1935 }
1936 // write 2 via int[] set all, keep 1st element by writing to destPos 1, 5 elements total, 4 written
1937 {
1938 final int[] ia = { 0, 220, 221, 222, 223, 224, 0 };
1939 model.setVariaInt32PointerCustomLen(false /* subset */, ia, 2 /* srcPos */, 1 /* destPos */, ia.length-1-2);
1940 }
1941 // verify 2
1942 {
1943 final int size = model.getVariaInt32PointerCustomLenElemCount();
1944 Assert.assertEquals(5, size);
1945 final int[] all = model.getVariaInt32PointerCustomLen(0, new int[size], 0, size);
1946 final IntBuffer allB = model.getVariaInt32PointerCustomLen();
1947 Assert.assertEquals(size, all.length);
1948 Assert.assertEquals(size, allB.limit());
1949 for(int i=0; i<1; i++) {
1950 Assert.assertEquals(120 + i, all[i]);
1951 Assert.assertEquals(120 + i, allB.get(i));
1952 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[1], 0, 1);
1953 Assert.assertEquals(120 + i, s[0]);
1954 }
1955 for(int i=1; i<size; i++) {
1956 Assert.assertEquals(220 + i, all[i]);
1957 Assert.assertEquals(220 + i, allB.get(i));
1958 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[1], 0, 1);
1959 Assert.assertEquals(220 + i, s[0]);
1960 }
1961 }
1962 // write 3 via int[] set a subset only
1963 {
1964 final int[] ia = { 0, 0, 322, 323, 324, 0, 0 };
1965 model.setVariaInt32PointerCustomLen(true /* subset */, ia, 2 /* srcPos */, 2 /* destPos */, 3);
1966 }
1967 // verify 2
1968 {
1969 final int size = model.getVariaInt32PointerCustomLenElemCount();
1970 Assert.assertEquals(5, size);
1971 final int[] all = model.getVariaInt32PointerCustomLen(0, new int[size], 0, size);
1972 final IntBuffer allB = model.getVariaInt32PointerCustomLen();
1973 Assert.assertEquals(size, all.length);
1974 Assert.assertEquals(size, allB.limit());
1975 for(int i=0; i<size; i++) {
1976 System.err.printf("%d/%d: A %d, B %d%n", i, size, all[i], allB.get(i));
1977 }
1978 for(int i=0; i<1; i++) {
1979 Assert.assertEquals(120 + i, all[i]);
1980 Assert.assertEquals(120 + i, allB.get(i));
1981 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[1], 0, 1);
1982 Assert.assertEquals(120 + i, s[0]);
1983 }
1984 for(int i=1; i<2; i++) {
1985 Assert.assertEquals(220 + i, all[i]);
1986 Assert.assertEquals(220 + i, allB.get(i));
1987 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[3], 1, 1);
1988 Assert.assertEquals(220 + i, s[1]);
1989 }
1990 for(int i=2; i<size; i++) {
1991 Assert.assertEquals(320 + i, all[i]);
1992 Assert.assertEquals(320 + i, allB.get(i));
1993 final int[] s = model.getVariaInt32PointerCustomLen(i, new int[3], 2, 1);
1994 Assert.assertEquals(320 + i, s[2]);
1995 }
1996 }
1997 model.releaseVariaInt32PointerCustomLen(); // FIXME: Ownership is ambiguous, may leak native allocated memory, not free'ed!
1998 Assert.assertEquals(0, model.getVariaInt32PointerCustomLenElemCount());
1999 Assert.assertEquals(true, model.isVariaInt32PointerCustomLenNull());
2000 }
2001 }
2002
2003
2004 /** Struct */
2005 private void chapter12_05aTestTKFieldConstValueStructReadAccess(final TK_Field model) {
2006 // field: structArrayOneElem
2007 // CType['TK_Dimension *', size [fixed false, lnx64 16], [array*1]], with array length of 1
2008 {
2009 final TK_Dimension all = model.getConstStructArrayConstOneElem();
2010 Assert.assertEquals(51, all.getX());
2011 Assert.assertEquals(52, all.getY());
2012 Assert.assertEquals(53, all.getWidth());
2013 Assert.assertEquals(54, all.getHeight());
2014 }
2015
2016 // field: structPointerCustomLen
2017 // CType['TK_Dimension *', size [fixed false, lnx64 8], [pointer*1]], with array length of getStructPointerCustomLenVal()
2018 {
2019 final int size = model.getConstStructPointerCustomLenElemCount();
2020 Assert.assertEquals(4, size);
2021 final TK_Dimension[] all = model.getConstStructPointerCustomLen(0, new TK_Dimension[size], 0, size);
2022 for(int i=0; i<size; i++) {
2023 Assert.assertEquals(131 + i * 10, all[i].getX());
2024 Assert.assertEquals(132 + i * 10, all[i].getY());
2025 Assert.assertEquals(133 + i * 10, all[i].getWidth());
2026 Assert.assertEquals(134 + i * 10, all[i].getHeight());
2027 }
2028 }
2029
2030 // field: structPointerOneElem
2031 // CType['TK_Dimension *', size [fixed false, lnx64 8], [pointer*1]], with array length of 1
2032 {
2033 final TK_Dimension all = model.getConstStructPointerConstOneElem();
2034 Assert.assertEquals(91, all.getX());
2035 Assert.assertEquals(92, all.getY());
2036 Assert.assertEquals(93, all.getWidth());
2037 Assert.assertEquals(94, all.getHeight());
2038
2039 }
2040 }
2041
2042 private static TK_Dimension createTKDim(final int x, final int y, final int w, final int h) {
2043 return TK_Dimension.create().setX(x).setY(y).setWidth(w).setHeight(h);
2044 }
2045 private static boolean equals(final TK_Dimension a, final TK_Dimension b) {
2046 if( a == b ) {
2047 return true;
2048 }
2049 return a.getX() == b.getX() &&
2050 a.getY() == b.getY() &&
2051 a.getWidth() == b.getWidth() &&
2052 a.getHeight() == b.getHeight();
2053 }
2054
2055 /** Primitive.VariaValue.Struct.Pointer - write access */
2056 private void chapter12_06bTestTKFieldVariaValueStructWriteAccess(final TK_Field model) {
2057 {
2058 final TK_Dimension val = createTKDim(0, 1, 50, 61);
2059 model.setVariaStructElement(val);
2060 Assert.assertTrue( equals(val, model.getVariaStructElement()) );
2061 }
2062
2063 Assert.assertEquals(1, TK_Field.getVariaStructPointerConstOneElemElemCount());
2064 Assert.assertEquals(false, model.isVariaStructPointerConstOneElemNull());
2065 {
2066 final TK_Dimension val = createTKDim(10, 11, 100, 111);
2067 model.setVariaStructPointerConstOneElem(val);
2068 Assert.assertTrue( equals(val, model.getVariaStructPointerConstOneElem()) );
2069 }
2070
2071 Assert.assertEquals(0, model.getVariaStructPointerMaxOneElemElemCount());
2072 Assert.assertEquals(true, model.isVariaStructPointerMaxOneElemNull());
2073 {
2074 final TK_Dimension val = createTKDim(20, 21, 200, 211);
2075 model.setVariaStructPointerMaxOneElem(val);
2076 Assert.assertEquals(1, model.getVariaStructPointerMaxOneElemElemCount());
2077 Assert.assertEquals(false, model.isVariaStructPointerMaxOneElemNull());
2078 Assert.assertTrue( equals(val, model.getVariaStructPointerMaxOneElem()) );
2079 model.releaseVariaStructPointerMaxOneElem();
2080 Assert.assertEquals(0, model.getVariaStructPointerMaxOneElemElemCount());
2081 Assert.assertEquals(true, model.isVariaStructPointerMaxOneElemNull());
2082 }
2083
2084 Assert.assertEquals(3, TK_Field.getVariaStructPointerConstLenElemCount());
2085 Assert.assertEquals(false, model.isVariaStructPointerConstLenNull());
2086 {
2087 // No write/verify 1 via ByteBuffer reference
2088
2089 // write 2 via TK_Dimension[] set
2090 {
2091 final TK_Dimension[] all = new TK_Dimension[3];
2092 for(int i=0; i<3; ++i) {
2093 all[i] = createTKDim(i*10, i*10+1, 100+i*10, 100+i*10+1);
2094 }
2095 model.setVariaStructPointerConstLen(all, 0, 0, 3);
2096 }
2097 // verify 2
2098 {
2099 final int size = TK_Field.getVariaStructPointerConstLenElemCount();
2100 Assert.assertEquals(3, size);
2101 final TK_Dimension[] all = model.getVariaStructPointerConstLen(0, new TK_Dimension[3], 0, 3);
2102 Assert.assertEquals(size, all.length);
2103 for(int i=0; i<size; i++) {
2104 final TK_Dimension val = createTKDim(i*10, i*10+1, 100+i*10, 100+i*10+1);
2105 Assert.assertTrue( equals(val, all[i]) );
2106 final TK_Dimension[] s = model.getVariaStructPointerConstLen(i, new TK_Dimension[1], 0, 1);
2107 Assert.assertTrue( equals(val, s[0]) );
2108 }
2109 }
2110 // write 3 via int[] single set @ offset
2111 {
2112 final TK_Dimension[] all = new TK_Dimension[3];
2113 for(int i=1; i<3; ++i) {
2114 all[i] = createTKDim(i*20, i*20+1, 100+i*20, 100+i*20+1);
2115 }
2116 model.setVariaStructPointerConstLen(all, 1, 1, 2);
2117 }
2118 // verify 3
2119 {
2120 final int size = TK_Field.getVariaStructPointerConstLenElemCount();
2121 Assert.assertEquals(3, size);
2122 final TK_Dimension[] all = model.getVariaStructPointerConstLen(0, new TK_Dimension[3], 0, 3);
2123 Assert.assertEquals(size, all.length);
2124 for(int i=0; i<1; i++) {
2125 final TK_Dimension val = createTKDim(i*10, i*10+1, 100+i*10, 100+i*10+1);
2126 Assert.assertTrue( equals(val, all[i]) );
2127 final TK_Dimension[] s = model.getVariaStructPointerConstLen(i, new TK_Dimension[1], 0, 1);
2128 Assert.assertTrue( equals(val, s[0]) );
2129 }
2130 for(int i=1; i<size; i++) {
2131 final TK_Dimension val = createTKDim(i*20, i*20+1, 100+i*20, 100+i*20+1);
2132 Assert.assertTrue( equals(val, all[i]) );
2133 final TK_Dimension[] s = model.getVariaStructPointerConstLen(i, new TK_Dimension[1], 0, 1);
2134 Assert.assertTrue( equals(val, s[0]) );
2135 }
2136 }
2137 }
2138
2139 Assert.assertEquals(0, model.getVariaStructPointerVariaLenElemCount());
2140 Assert.assertEquals(true, model.isVariaStructPointerVariaLenNull());
2141 /**
2142 {
2143 Exception e1 = null;
2144 try {
2145 @SuppressWarnings("unused")
2146 final IntBuffer ib = model.getVariaStructPointerVariaLen(); // NULL -> exception
2147 } catch(final Exception _e) { e1 = _e; }
2148 Assert.assertNotNull(e1);
2149 System.err.println("Expected exception-1: "+e1);
2150
2151 Exception e2 = null;
2152 try {
2153 @SuppressWarnings("unused")
2154 final int[] ia = model.getVariaStructPointerVariaLen(0, new int[0], 0, 0); // NULL -> exception
2155 } catch(final Exception _e) { e2 = _e; }
2156 Assert.assertNotNull(e2);
2157 System.err.println("Expected exception-2: "+e2);
2158 }
2159 {
2160 // write 1 via int[] set, also actually allocating initial memory
2161 {
2162 final int[] ia = { 220, 221, 222, 223, 224 };
2163 model.setVariaStructPointerVariaLen(false, ia, 0, 0, ia.length);
2164 }
2165 // verify 1
2166 {
2167 final int size = model.getVariaStructPointerVariaLenElemCount();
2168 Assert.assertEquals(5, size);
2169 final int[] all = model.getVariaStructPointerVariaLen(0, new int[size], 0, size);
2170 final IntBuffer allB = model.getVariaStructPointerVariaLen();
2171 Assert.assertEquals(size, all.length);
2172 Assert.assertEquals(size, allB.limit());
2173 for(int i=0; i<size; i++) {
2174 Assert.assertEquals(220 + i, all[i]);
2175 Assert.assertEquals(220 + i, allB.get(i));
2176 final int[] s = model.getVariaStructPointerVariaLen(i, new int[1], 0, 1);
2177 Assert.assertEquals(220 + i, s[0]);
2178 }
2179 }
2180 // write 2 via IntBuffer reference get
2181 {
2182 final IntBuffer ib = model.getVariaStructPointerVariaLen();
2183 Assert.assertEquals(5, ib.limit());
2184 for(int i=0; i<5; ++i) {
2185 ib.put(i, 120+i);
2186 }
2187 }
2188 // verify 2
2189 {
2190 final int size = model.getVariaStructPointerVariaLenElemCount();
2191 Assert.assertEquals(5, size);
2192 final int[] all = model.getVariaStructPointerVariaLen(0, new int[size], 0, size);
2193 final IntBuffer allB = model.getVariaStructPointerVariaLen();
2194 Assert.assertEquals(size, all.length);
2195 Assert.assertEquals(size, allB.limit());
2196 for(int i=0; i<size; i++) {
2197 Assert.assertEquals(120 + i, all[i]);
2198 Assert.assertEquals(120 + i, allB.get(i));
2199 final int[] s = model.getVariaStructPointerVariaLen(i, new int[1], 0, 1);
2200 Assert.assertEquals(120 + i, s[0]);
2201 }
2202 }
2203 model.releaseVariaStructPointerVariaLen();
2204 Assert.assertEquals(0, model.getVariaStructPointerVariaLenElemCount());
2205 Assert.assertEquals(true, model.isVariaStructPointerVariaLenNull());
2206 }
2207
2208 {
2209 // write 1 via IntBuffer reference get
2210 {
2211 final int size = model.getVariaStructPointerCustomLenElemCount();
2212 Assert.assertEquals(4, size);
2213 Assert.assertEquals(false, model.isVariaStructPointerCustomLenNull());
2214 final IntBuffer ib = model.getVariaStructPointerCustomLen();
2215 Assert.assertEquals(size, ib.limit());
2216 for(int i=0; i<size; ++i) {
2217 ib.put(i, 120+i);
2218 }
2219 }
2220 // verify 1
2221 {
2222 final int size = model.getVariaStructPointerCustomLenElemCount();
2223 Assert.assertEquals(4, size);
2224 final int[] all = model.getVariaStructPointerCustomLen(0, new int[size], 0, size);
2225 final IntBuffer allB = model.getVariaStructPointerCustomLen();
2226 Assert.assertEquals(size, all.length);
2227 Assert.assertEquals(size, allB.limit());
2228 for(int i=0; i<size; i++) {
2229 Assert.assertEquals(120 + i, all[i]);
2230 Assert.assertEquals(120 + i, allB.get(i));
2231 final int[] s = model.getVariaStructPointerCustomLen(i, new int[1], 0, 1);
2232 Assert.assertEquals(120 + i, s[0]);
2233 }
2234 }
2235 // write 2 via int[] set all, keep 1st element by writing to destPos 1, 5 elements total, 4 written
2236 {
2237 final int[] ia = { 0, 220, 221, 222, 223, 224, 0 };
2238 model.setVariaStructPointerCustomLen(false, ia, 2, 1, ia.length-1-2);
2239 }
2240 // verify 2
2241 {
2242 final int size = model.getVariaStructPointerCustomLenElemCount();
2243 Assert.assertEquals(5, size);
2244 final int[] all = model.getVariaStructPointerCustomLen(0, new int[size], 0, size);
2245 final IntBuffer allB = model.getVariaStructPointerCustomLen();
2246 Assert.assertEquals(size, all.length);
2247 Assert.assertEquals(size, allB.limit());
2248 for(int i=0; i<1; i++) {
2249 Assert.assertEquals(120 + i, all[i]);
2250 Assert.assertEquals(120 + i, allB.get(i));
2251 final int[] s = model.getVariaStructPointerCustomLen(i, new int[1], 0, 1);
2252 Assert.assertEquals(120 + i, s[0]);
2253 }
2254 for(int i=1; i<size; i++) {
2255 Assert.assertEquals(220 + i, all[i]);
2256 Assert.assertEquals(220 + i, allB.get(i));
2257 final int[] s = model.getVariaStructPointerCustomLen(i, new int[1], 0, 1);
2258 Assert.assertEquals(220 + i, s[0]);
2259 }
2260 }
2261 // write 3 via int[] set a subset only
2262 {
2263 final int[] ia = { 0, 0, 322, 323, 324, 0, 0 };
2264 model.setVariaStructPointerCustomLen(true, ia, 2, 2, 3);
2265 }
2266 // verify 2
2267 {
2268 final int size = model.getVariaStructPointerCustomLenElemCount();
2269 Assert.assertEquals(5, size);
2270 final int[] all = model.getVariaStructPointerCustomLen(0, new int[size], 0, size);
2271 final IntBuffer allB = model.getVariaStructPointerCustomLen();
2272 Assert.assertEquals(size, all.length);
2273 Assert.assertEquals(size, allB.limit());
2274 for(int i=0; i<size; i++) {
2275 System.err.printf("%d/%d: A %d, B %d%n", i, size, all[i], allB.get(i));
2276 }
2277 for(int i=0; i<1; i++) {
2278 Assert.assertEquals(120 + i, all[i]);
2279 Assert.assertEquals(120 + i, allB.get(i));
2280 final int[] s = model.getVariaStructPointerCustomLen(i, new int[1], 0, 1);
2281 Assert.assertEquals(120 + i, s[0]);
2282 }
2283 for(int i=1; i<2; i++) {
2284 Assert.assertEquals(220 + i, all[i]);
2285 Assert.assertEquals(220 + i, allB.get(i));
2286 final int[] s = model.getVariaStructPointerCustomLen(i, new int[3], 1, 1);
2287 Assert.assertEquals(220 + i, s[1]);
2288 }
2289 for(int i=2; i<size; i++) {
2290 Assert.assertEquals(320 + i, all[i]);
2291 Assert.assertEquals(320 + i, allB.get(i));
2292 final int[] s = model.getVariaStructPointerCustomLen(i, new int[3], 2, 1);
2293 Assert.assertEquals(320 + i, s[2]);
2294 }
2295 }
2296 model.releaseVariaStructPointerCustomLen(); // FIXME: Ownership is ambiguous, may leak native allocated memory, not free'ed!
2297 Assert.assertEquals(0, model.getVariaStructPointerCustomLenElemCount());
2298 Assert.assertEquals(true, model.isVariaStructPointerCustomLenNull());
2299 }
2300 */
2301 }
2302
2303 private static ByteBuffer toEOSByteBuffer(final String val, final Charset cs) {
2304 final byte[] ba = val.getBytes( cs );
2305 final ByteBuffer bb = Buffers.newDirectByteBuffer( ba.length + 1 );
2306 bb.put(ba);
2307 bb.put((byte)0);
2308 bb.rewind();
2309 return bb;
2310 }
2311
2312 /** String - Read Access */
2313 private void chapter12_10aTestTKFieldConstStringReadAccess(final TK_Field model) {
2314 {
2315 final int expStrLen = 12; // w/o EOS
2316 final String exp = "Hello Array1";
2317 final ByteBuffer expBB = toEOSByteBuffer( exp, TK_Field.getCharset() );
2318 Assert.assertEquals(expStrLen, exp.length());
2319 Assert.assertEquals(expStrLen+1, expBB.limit());
2320 Assert.assertEquals(expStrLen+1, TK_Field.getConstCharArrayConstLenElemCount());
2321
2322 final ByteBuffer hasBB = model.getConstCharArrayConstLen();
2323 Assert.assertEquals(expStrLen+1, hasBB.limit());
2324 Assert.assertEquals(expBB, hasBB);
2325
2326 final String has = model.getConstCharArrayConstLenAsString();
2327 System.err.println("has '"+has+"'");
2328 // dumpStringChars("has", has);
2329 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2330 Assert.assertEquals(exp, has);
2331 }
2332 {
2333 final int expStrLen = 14; // w/o EOS
2334 final String exp = "Hello CString1";
2335 final ByteBuffer expBB = toEOSByteBuffer( exp, TK_Field.getCharset() );
2336 Assert.assertEquals(expStrLen, exp.length());
2337 Assert.assertEquals(expStrLen+1, expBB.limit());
2338 Assert.assertEquals(expStrLen+1, TK_Field.getConstCharPointerConstLenElemCount());
2339 Assert.assertEquals(false, model.isConstCharPointerConstLenNull());
2340
2341 final ByteBuffer hasBB = model.getConstCharPointerConstLen();
2342 Assert.assertEquals(expStrLen+1, hasBB.limit());
2343 Assert.assertEquals(expBB, hasBB);
2344
2345 final String has = model.getConstCharPointerConstLenAsString();
2346 System.err.println("has '"+has+"'");
2347 // dumpStringChars("has", has);
2348 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2349 Assert.assertEquals(exp, has);
2350 }
2351 {
2352 Assert.assertEquals(0, model.getConstCharPointerVariaLenElemCount());
2353 Assert.assertEquals(true, model.isConstCharPointerVariaLenNull());
2354 }
2355 {
2356 final int expStrLen = 14; // w/o EOS
2357 final String exp = "Hello CString3";
2358 final ByteBuffer expBB = toEOSByteBuffer( exp, TK_Field.getCharset() );
2359 Assert.assertEquals(expStrLen, exp.length());
2360 Assert.assertEquals(expStrLen+1, expBB.limit());
2361 Assert.assertEquals(expStrLen+1, model.getConstCharPointerCustomLenElemCount());
2362 Assert.assertEquals(false, model.isConstCharPointerCustomLenNull());
2363
2364 final ByteBuffer hasBB = model.getConstCharPointerCustomLen();
2365 Assert.assertEquals(expStrLen+1, hasBB.limit());
2366 Assert.assertEquals(expBB, hasBB);
2367
2368 final String has = model.getConstCharPointerCustomLenAsString();
2369 System.err.println("has '"+has+"'");
2370 // dumpStringChars("has", has);
2371 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2372 Assert.assertEquals(exp, has);
2373 }
2374 }
2375
2376 /** String - Read Access */
2377 private void chapter12_11aTestTKFieldConstStringOnlyReadAccess(final TK_Field model) {
2378 {
2379 final int expStrLen = 12; // w/o EOS
2380 final String exp = "Hello Array1";
2381 Assert.assertEquals(expStrLen, exp.length());
2382 Assert.assertEquals(expStrLen+1, TK_Field.getConstStringOnlyArrayConstLenElemCount());
2383
2384 final String has = model.getConstStringOnlyArrayConstLen();
2385 System.err.println("has '"+has+"'");
2386 // dumpStringChars("has", has);
2387 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2388 Assert.assertEquals(exp, has);
2389 }
2390 {
2391 final int expStrLen = 14; // w/o EOS
2392 final String exp = "Hello CString1";
2393 Assert.assertEquals(expStrLen, exp.length());
2394 Assert.assertEquals(expStrLen+1, TK_Field.getConstStringOnlyPointerConstLenElemCount());
2395 Assert.assertEquals(false, model.isConstStringOnlyPointerConstLenNull());
2396
2397 final String has = model.getConstStringOnlyPointerConstLen();
2398 System.err.println("has '"+has+"'");
2399 // dumpStringChars("has", has);
2400 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2401 Assert.assertEquals(exp, has);
2402 }
2403 {
2404 Assert.assertEquals(0, model.getConstStringOnlyPointerVariaLenElemCount());
2405 Assert.assertEquals(true, model.isConstStringOnlyPointerVariaLenNull());
2406 }
2407 {
2408 final int expStrLen = 14; // w/o EOS
2409 final String exp = "Hello CString3";
2410 Assert.assertEquals(expStrLen, exp.length());
2411 Assert.assertEquals(expStrLen+1, model.getConstStringOnlyPointerCustomLenElemCount());
2412 Assert.assertEquals(false, model.isConstStringOnlyPointerCustomLenNull());
2413
2414 final String has = model.getConstStringOnlyPointerCustomLen();
2415 System.err.println("has '"+has+"'");
2416 // dumpStringChars("has", has);
2417 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2418 Assert.assertEquals(exp, has);
2419 }
2420 }
2421
2422 /** String - Write Access */
2423 private void chapter12_11bTestTKFieldConstStringOnlyWriteAccess(final TK_Field model) {
2424 {
2425 Assert.assertEquals(0, model.getConstStringOnlyPointerVariaLenElemCount());
2426 Assert.assertEquals(true, model.isConstStringOnlyPointerVariaLenNull());
2427
2428 final int expStrLen = 15; // w/o EOS
2429 final String exp = "Hello World2222";
2430 Assert.assertEquals(expStrLen, exp.length());
2431
2432 model.setConstStringOnlyPointerVariaLen(exp);
2433 Assert.assertEquals(expStrLen+1, model.getConstStringOnlyPointerVariaLenElemCount());
2434 Assert.assertEquals(false, model.isConstStringOnlyPointerVariaLenNull());
2435
2436 final String has = model.getConstStringOnlyPointerVariaLen();
2437 System.err.println("has '"+has+"'");
2438 // dumpStringChars("has", has);
2439 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2440 Assert.assertEquals(exp, has);
2441
2442 model.releaseConstStringOnlyPointerVariaLen();
2443 Assert.assertEquals(0, model.getConstStringOnlyPointerVariaLenElemCount());
2444 Assert.assertEquals(true, model.isConstStringOnlyPointerVariaLenNull());
2445 }
2446 {
2447 final int expStrLen = 15; // w/o EOS
2448 final String exp = "Hello World4444";
2449 Assert.assertEquals(expStrLen, exp.length());
2450 Assert.assertEquals(false, model.isConstStringOnlyPointerCustomLenNull());
2451
2452 model.setConstStringOnlyPointerCustomLen(exp);
2453 Assert.assertEquals(expStrLen+1, model.getConstStringOnlyPointerCustomLenElemCount());
2454 Assert.assertEquals(false, model.isConstStringOnlyPointerCustomLenNull());
2455
2456 final String has = model.getConstStringOnlyPointerCustomLen();
2457 System.err.println("has '"+has+"'");
2458 // dumpStringChars("has", has);
2459 Assert.assertEquals(expStrLen, has.length()); // w/o EOS
2460 Assert.assertEquals(exp, has);
2461
2462 model.releaseConstStringOnlyPointerCustomLen(); // FIXME: Ownership is ambiguous, may leak native allocated memory, not free'ed!
2463 Assert.assertEquals(0, model.getConstStringOnlyPointerCustomLenElemCount());
2464 Assert.assertEquals(true, model.isConstStringOnlyPointerCustomLenNull());
2465 }
2466 }
2467
2468 /** Test array and pointer bindings of structs */
2469 public void chapter12TestTKField(final Bindingtest1 binding) throws Exception {
2470 Assert.assertEquals(false, TK_Field.usesNativeCode());
2471 Assert.assertNotEquals(0, TK_Field.size());
2472
2473 final TK_Field model0 = binding.createTKField();
2474 {
2475 // Fetch native memory
2476 final ByteBuffer bb0 = model0.getBuffer();
2477 Assert.assertNotNull(bb0);
2478 Assert.assertEquals(TK_Field.size(), bb0.limit());
2479
2480 final long addr0 = model0.getDirectBufferAddress();
2481 Assert.assertNotEquals(0, addr0);
2482
2483 // Compare a reference by memory address -> identical!
2484 {
2485 final TK_Field model1 = TK_Field.derefPointer(addr0);
2486 final ByteBuffer bb1 = model1.getBuffer();
2487 Assert.assertNotNull(bb1);
2488 Assert.assertEquals(TK_Field.size(), bb1.limit());
2489
2490 final long addr1 = model1.getDirectBufferAddress();
2491 Assert.assertNotEquals(0, addr1);
2492 }
2493 }
2494
2495 chapter12_03aTestTKFieldConstValueInt32ReadAccess(model0);
2496 chapter12_03bTestTKFieldConstValueInt32WriteAccess(model0);
2497 chapter12_04aTestTKFieldVariaValueInt32ReadAccess(model0);
2498 chapter12_04bTestTKFieldVariaValueInt32WriteAccess(model0);
2499 chapter12_05aTestTKFieldConstValueStructReadAccess(model0);
2500 chapter12_06bTestTKFieldVariaValueStructWriteAccess(model0);
2501 chapter12_10aTestTKFieldConstStringReadAccess(model0);
2502 chapter12_11aTestTKFieldConstStringOnlyReadAccess(model0);
2503 chapter12_11bTestTKFieldConstStringOnlyWriteAccess(model0);
2504
2505 binding.destroyTKField(model0);
2506 }
2507 @SuppressWarnings("unused")
2508 private void dumpStringChars(final String prefix, final String s) {
2509 final int len = s.length();
2510 for(int i=0; i<len; i++) {
2511 final char c = s.charAt(i);
2512 System.err.printf("%s %3d: 0x%X %c%n", prefix, i, (int)c, c);
2513 }
2514 }
2515
2516 public void chapter13TestTKFieldImmutable(final Bindingtest1 binding) throws Exception {
2517 Assert.assertEquals(false, TK_FieldImmutable.usesNativeCode());
2518
2519 final TK_FieldImmutable model = binding.createTKFieldImmutable();
2520
2521 binding.destroyTKFieldImmutable(model);
2522 }
2523
2524 public void chapter14TestTKMixed(final Bindingtest1 binding) throws Exception {
2525 Assert.assertEquals(false, TK_ModelMixed.usesNativeCode());
2526
2527 final TK_ModelMixed model = binding.createTKModelMixed();
2528
2529 Assert.assertEquals(4*4, TK_ModelMixed.getMat4x4ElemCount());
2530 {
2531 final FloatBuffer mat4x4 = model.getMat4x4();
2532 Assert.assertEquals(4*4, mat4x4.limit());
2533 for(int i=0; i<4; i++) {
2534 final float[] vec4 = model.getMat4x4(i*4, new float[4], 0, 4);
2535 for(int j=0; j<4; j++) {
2536 Assert.assertEquals(i*4+j, mat4x4.get(i*4+j), EPSILON);
2537 Assert.assertEquals(i*4+j, vec4[j], EPSILON);
2538 }
2539 }
2540 }
2541 {
2542 final float[] data = { 11, 12, 13, 14,
2543 21, 22, 23, 24,
2544 31, 32, 33, 34,
2545 41, 42, 43, 44 };
2546 model.setMat4x4(data, 0*4, 0*4, 4);
2547 model.setMat4x4(data, 1*4, 1*4, 4);
2548 model.setMat4x4(data, 2*4, 2*4, 4);
2549 model.setMat4x4(data, 3*4, 3*4, 4);
2550
2551 final FloatBuffer mat4x4 = model.getMat4x4();
2552 Assert.assertEquals(4*4, mat4x4.limit());
2553 for(int i=0; i<4; i++) {
2554 final float[] vec4 = model.getMat4x4(i*4, new float[4], 0, 4);
2555 for(int j=0; j<4; j++) {
2556 Assert.assertEquals((i+1)*10+(j+1), mat4x4.get(i*4+j), EPSILON);
2557 Assert.assertEquals((i+1)*10+(j+1), vec4[j], EPSILON);
2558 }
2559 }
2560 }
2561
2562 final long surfaceContext = model.getCtx();
2563 assertAPTR(0x123456789abcdef0L, surfaceContext);
2564
2565 model.setCtx(surfaceContext);
2566 assertAPTR(surfaceContext, model.getCtx());
2567 }
2568
2569 public void chapter15TestCustomJNICode(final Bindingtest1 binding) throws Exception {
2570 Assert.assertEquals(Bindingtest1.FOO_VALUE, binding.getFoo());
2571 }
2572
2573 public void chapter16TestPascalString(final Bindingtest1 binding) throws Exception {
2574 Assert.assertEquals(false, TK_ModelMixed.usesNativeCode());
2575 Assert.assertEquals( 2, binding.addStrlenAndPascalStrLen("1"));
2576 Assert.assertEquals( 8, binding.addStrlenAndPascalStrLen("1234"));
2577 Assert.assertEquals(10, binding.addStrlenAndPascalStrLen("12345"));
2578 Assert.assertEquals( 0, binding.addStrlenAndPascalStrLen(null));
2579 }
2580
2581}
com.jogamp.common.nio.AbstractBuffer
Definition: AbstractBuffer.java:44
com.jogamp.common.nio.AbstractBuffer.getBuffer
final Buffer getBuffer()
Definition: AbstractBuffer.java:151
com.jogamp.common.nio.AbstractBuffer.capacity
final int capacity
Definition: AbstractBuffer.java:50
com.jogamp.common.nio.AbstractBuffer.POINTER_SIZE
static final int POINTER_SIZE
Platform dependent pointer size in bytes, i.e.
Definition: AbstractBuffer.java:46
com.jogamp.common.nio.AbstractBuffer.remaining
final int remaining()
Definition: AbstractBuffer.java:118
com.jogamp.common.nio.AbstractBuffer.rewind
final B rewind()
Definition: AbstractBuffer.java:145
com.jogamp.common.nio.Buffers
Utility methods allowing easy java.nio.Buffer manipulations.
Definition: Buffers.java:70
com.jogamp.common.nio.Buffers.newDirectLongBuffer
static LongBuffer newDirectLongBuffer(final int numElements)
Allocates a new direct LongBuffer with the specified number of elements.
Definition: Buffers.java:176
com.jogamp.common.nio.Buffers.newDirectByteBuffer
static ByteBuffer newDirectByteBuffer(final int numElements)
Allocates a new direct ByteBuffer with the specified number of elements.
Definition: Buffers.java:92
com.jogamp.common.nio.Buffers.SIZEOF_LONG
static final int SIZEOF_LONG
Definition: Buffers.java:82
com.jogamp.common.nio.Buffers.newDirectIntBuffer
static IntBuffer newDirectIntBuffer(final int numElements)
Allocates a new direct IntBuffer with the specified number of elements.
Definition: Buffers.java:155
com.jogamp.common.nio.PointerBuffer
Hardware independent container holding an array of native pointer, while its getDirectBufferAddress()...
Definition: PointerBuffer.java:55
com.jogamp.common.nio.PointerBuffer.getReferencedBuffer
final Buffer getReferencedBuffer(final int index)
Definition: PointerBuffer.java:314
com.jogamp.common.nio.PointerBuffer.wrap
static PointerBuffer wrap(final ByteBuffer src)
Wraps given ByteBuffer src up to it ByteBuffer#capacity()/POINTER_SIZE pointers.
Definition: PointerBuffer.java:95
com.jogamp.common.nio.PointerBuffer.referenceBuffer
final PointerBuffer referenceBuffer(final int index, final Buffer bb)
Put the address of the given direct Buffer at the given position of this pointer array.
Definition: PointerBuffer.java:287
com.jogamp.common.nio.PointerBuffer.put
final PointerBuffer put(final PointerBuffer src)
Definition: PointerBuffer.java:149
com.jogamp.common.nio.PointerBuffer.get
final long get(final int idx)
Absolute get method.
Definition: PointerBuffer.java:179
com.jogamp.common.os.MachineDataInfo
Machine data description for alignment and size onle, see com.jogamp.gluegen.
Definition: MachineDataInfo.java:57
com.jogamp.common.os.MachineDataInfo.compatible
final boolean compatible(final MachineDataInfo md)
Checks whether two MachineDataInfo objects are equal.
Definition: MachineDataInfo.java:335
com.jogamp.common.os.Platform
Utility class for querying platform specific properties.
Definition: Platform.java:58
com.jogamp.common.os.Platform.initSingleton
static void initSingleton()
kick off static initialization of platform property information and native gluegen_rt lib loading
Definition: Platform.java:359
com.jogamp.common.os.Platform.is32Bit
static boolean is32Bit()
Returns true if this JVM/ARCH is 32bit.
Definition: Platform.java:423
com.jogamp.gluegen.test.junit.generation.BaseClass
Definition: BaseClass.java:51
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter13TestTKFieldImmutable
void chapter13TestTKFieldImmutable(final Bindingtest1 binding)
Definition: BaseClass.java:2516
com.jogamp.gluegen.test.junit.generation.BaseClass.EPSILON
static final float EPSILON
Definition: BaseClass.java:78
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter09TestCompoundAlignment
void chapter09TestCompoundAlignment(final Bindingtest1 binding)
Definition: BaseClass.java:1110
com.jogamp.gluegen.test.junit.generation.BaseClass.assertAPTR
static void assertAPTR(final long expected, final long actual)
Definition: BaseClass.java:1091
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter__TestCoverageSignature
void chapter__TestCoverageSignature(final Bindingtest1 binding)
Verifies if all generated method signatures are completed, ie a compilation only coverage test withou...
Definition: BaseClass.java:84
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter16TestPascalString
void chapter16TestPascalString(final Bindingtest1 binding)
Definition: BaseClass.java:2573
com.jogamp.gluegen.test.junit.generation.BaseClass.testClassExist
void testClassExist(final String name)
Verifies the existence and creation of the generated class.
Definition: BaseClass.java:60
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter01TestStaticConstants
void chapter01TestStaticConstants(final Bindingtest1 binding)
Verifies if all generated static constant values are completed, and whether their value is as expecte...
Definition: BaseClass.java:224
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter15TestCustomJNICode
void chapter15TestCustomJNICode(final Bindingtest1 binding)
Definition: BaseClass.java:2569
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter11TestCompoundCallByValue
void chapter11TestCompoundCallByValue(final Bindingtest1 binding)
Test compound access call-by-value.
Definition: BaseClass.java:1293
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter12TestTKField
void chapter12TestTKField(final Bindingtest1 binding)
Test array and pointer bindings of structs
Definition: BaseClass.java:2469
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter04TestPointerBuffer
void chapter04TestPointerBuffer(final Bindingtest1 binding)
Definition: BaseClass.java:919
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter10TestCompoundCallByReference
void chapter10TestCompoundCallByReference(final Bindingtest1 binding)
Test compound access call-by-reference.
Definition: BaseClass.java:1224
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter03TestCoverageFunctionalityNIOAndPrimitiveArray
void chapter03TestCoverageFunctionalityNIOAndPrimitiveArray(final Bindingtest1 binding, final boolean direct)
Verifies if all methods / signatures are properly generated, can be invoked and functions.
Definition: BaseClass.java:492
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter14TestTKMixed
void chapter14TestTKMixed(final Bindingtest1 binding)
Definition: BaseClass.java:2524
com.jogamp.gluegen.test.junit.generation.BaseClass.chapter05TestSomeFunctionsAllIndirect
void chapter05TestSomeFunctionsAllIndirect(final Bindingtest1 binding)
This covers indirect primitive arrays and indirect NIO buffers.
Definition: BaseClass.java:1026
com.jogamp.junit.util.SingletonJunitCase
Definition: SingletonJunitCase.java:39