1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
/* Copyright (c) 2007 Timothy Wall, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
import java.util.List;
import junit.framework.TestCase;
/** General structure by value functionality tests. */
public class StructureByValueTest extends TestCase {
public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(StructureByValueTest.class);
}
public static class TestNativeMappedInStructure extends Structure {
public static class ByValue extends TestNativeMappedInStructure implements Structure.ByValue { }
public static final List<String> FIELDS = createFieldsOrder("field");
public NativeLong field;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
@Structure.FieldOrder({"t1", "t2", "t3"})
public static class DemoStructureDifferentArrayLengths extends Structure {
public static class ByValue extends DemoStructureDifferentArrayLengths implements Structure.ByValue {
}
public double t1[] = new double[3];
public double t2[] = new double[4];
public double t3[] = new double[5];
}
public void testNativeMappedInByValue() {
new TestNativeMappedInStructure.ByValue();
}
public interface TestLibrary extends Library {
byte testStructureByValueArgument8(ByValue8 arg);
short testStructureByValueArgument16(ByValue16 arg);
int testStructureByValueArgument32(ByValue32 arg);
long testStructureByValueArgument64(ByValue64 arg);
long testStructureByValueArgument128(ByValue128 arg);
DemoStructureDifferentArrayLengths.ByValue returnLastElementOfComponentsDSDAL(DemoStructureDifferentArrayLengths.ByValue ts, int debug);
}
TestLibrary lib;
@Override
protected void setUp() {
lib = Native.load("testlib", TestLibrary.class);
}
@Override
protected void tearDown() {
lib = null;
}
public static abstract class ByValueStruct extends Structure implements Structure.ByValue { }
public static class ByValue8 extends ByValueStruct {
public static final List<String> FIELDS = createFieldsOrder("data");
public byte data;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
public static class ByValue16 extends ByValueStruct {
public static final List<String> FIELDS = createFieldsOrder("data");
public short data;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
public static class ByValue32 extends ByValueStruct {
public static final List<String> FIELDS = createFieldsOrder("data");
public int data;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
public static class ByValue64 extends ByValueStruct {
public static final List<String> FIELDS = createFieldsOrder("data");
public long data;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
public static class ByValue128 extends ByValueStruct {
public static final List<String> FIELDS = createFieldsOrder("data", "data1");
public long data, data1;
@Override
protected List<String> getFieldOrder() {
return FIELDS;
}
}
final long MAGIC = 0x0123456789ABCDEFL;
public void testStructureArgByValue8() {
ByValue8 data = new ByValue8();
final byte DATA = (byte)MAGIC;
data.data = DATA;
assertEquals("Failed to pass 8-bit struct by value",
DATA, lib.testStructureByValueArgument8(data));
}
public void testStructureArgByValue16() {
ByValue16 data = new ByValue16();
final short DATA = (short)MAGIC;
data.data = DATA;
assertEquals("Failed to pass 16-bit struct by value",
DATA, lib.testStructureByValueArgument16(data));
}
public void testStructureArgByValue32() {
ByValue32 data = new ByValue32();
final int DATA = (int)MAGIC;
data.data = DATA;
assertEquals("Failed to pass 32-bit struct by value",
DATA, lib.testStructureByValueArgument32(data));
}
public void testStructureArgByValue64() {
ByValue64 data = new ByValue64();
final long DATA = MAGIC;
data.data = DATA;
assertEquals("Failed to pass 64-bit struct by value",
DATA, lib.testStructureByValueArgument64(data));
}
public void testStructureArgByValue128() {
ByValue128 data = new ByValue128();
final long DATA = MAGIC;
data.data = DATA;
data.data1 = DATA;
assertEquals("Failed to pass 128-bit struct by value",
2*DATA, lib.testStructureByValueArgument128(data));
}
public void testStructureDifferentArrayLengths() {
// returnLastElementOfComponentsDSDAL copies the last element of the
// components of t1, t2 and t3, which are double arrays with lengths
// 3, 4 and 5. In the observed case JNA created ffi_types for primitive
// arrays with wrong element count (the first array definition was used)
DemoStructureDifferentArrayLengths.ByValue ts = new DemoStructureDifferentArrayLengths.ByValue();
ts.t1 = new double[]{1, 1, 1};
ts.t2 = new double[]{2, 2, 2, 2};
ts.t3 = new double[]{3, 3, 3, 3, 3};
DemoStructureDifferentArrayLengths.ByValue result = lib.returnLastElementOfComponentsDSDAL(ts, 0);
assertEquals(1.0d, result.t1[2], 0.1d);
assertEquals(2.0d, result.t2[3], 0.1d);
assertEquals(3.0d, result.t3[4], 0.1d);
}
}
|