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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
|
/* 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.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.DoubleBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import java.nio.ShortBuffer;
import junit.framework.TestCase;
/** Exercise a range of native methods.
*
* @author twall@users.sf.net
*/
//@SuppressWarnings("unused")
public class BufferArgumentsMarshalTest extends TestCase {
public static interface TestLibrary extends Library {
// ByteBuffer alternative definitions
int fillInt8Buffer(ByteBuffer buf, int len, byte value);
int fillInt16Buffer(ByteBuffer buf, int len, short value);
int fillInt32Buffer(ByteBuffer buf, int len, int value);
int fillInt64Buffer(ByteBuffer buf, int len, long value);
int fillFloatBuffer(ByteBuffer buf, int len, float value);
int fillDoubleBuffer(ByteBuffer buf, int len, double value);
// {Short|Int|Long|,Float|Double}Buffer alternative definitions
int fillInt16Buffer(ShortBuffer buf, int len, short value);
int fillInt32Buffer(IntBuffer buf, int len, int value);
int fillInt64Buffer(LongBuffer buf, int len, long value);
int fillFloatBuffer(FloatBuffer buf, int len, float value);
int fillDoubleBuffer(DoubleBuffer buf, int len, double value);
}
TestLibrary lib;
@Override
protected void setUp() {
lib = Native.load("testlib", TestLibrary.class);
}
@Override
protected void tearDown() {
lib = null;
}
public void testByteBufferArgument() {
ByteBuffer buf = ByteBuffer.allocate(1024).order(ByteOrder.nativeOrder());
final byte MAGIC = (byte)0xED;
lib.fillInt8Buffer(buf, 1024, MAGIC);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, buf.get(i));
}
}
public void testShortBufferArgument() {
ShortBuffer buf = ShortBuffer.allocate(1024);
final short MAGIC = (short)0xABED;
lib.fillInt16Buffer(buf, 1024, MAGIC);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, buf.get(i));
}
}
public void testIntBufferArgument() {
IntBuffer buf = IntBuffer.allocate(1024);
final int MAGIC = 0xABEDCF23;
lib.fillInt32Buffer(buf, 1024, MAGIC);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, buf.get(i));
}
}
public void testLongBufferArgument() {
LongBuffer buf = LongBuffer.allocate(1024);
final long MAGIC = 0x1234567887654321L;
lib.fillInt64Buffer(buf, 1024, MAGIC);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, buf.get(i));
}
}
public void testDirectByteBufferArgument() {
ByteBuffer buf = ByteBuffer.allocateDirect(1024).order(ByteOrder.nativeOrder());
final byte MAGIC = (byte)0xED;
lib.fillInt8Buffer(buf, 1024, MAGIC);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, buf.get(i));
}
buf.position(512);
lib.fillInt8Buffer(buf, 512, (byte)0);
for (int i=0;i < buf.capacity();i++) {
assertEquals("Bad value at index " + i,
i < 512 ? MAGIC : 0, buf.get(i));
}
}
public void testDirectShortBufferArgument() {
ByteBuffer buf = ByteBuffer.allocateDirect(1024*2).order(ByteOrder.nativeOrder());
ShortBuffer shortBuf = buf.asShortBuffer();
final short MAGIC = (short)0xABED;
lib.fillInt16Buffer(shortBuf, 1024, MAGIC);
for (int i=0;i < shortBuf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, shortBuf.get(i));
}
}
public void testDirectIntBufferArgument() {
ByteBuffer buf = ByteBuffer.allocateDirect(1024*4).order(ByteOrder.nativeOrder());
IntBuffer intBuf = buf.asIntBuffer();
final int MAGIC = 0xABEDCF23;
lib.fillInt32Buffer(intBuf, 1024, MAGIC);
for (int i=0;i < intBuf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, intBuf.get(i));
}
}
public void testDirectLongBufferArgument() {
ByteBuffer buf = ByteBuffer.allocateDirect(1024*8).order(ByteOrder.nativeOrder());
LongBuffer longBuf = buf.asLongBuffer();
final long MAGIC = 0x1234567887654321L;
lib.fillInt64Buffer(longBuf, 1024, MAGIC);
for (int i=0;i < longBuf.capacity();i++) {
assertEquals("Bad value at index " + i, MAGIC, longBuf.get(i));
}
}
public void testWrappedByteArrayArgument() {
byte[] array = new byte[1024];
ByteBuffer buf = ByteBuffer.wrap(array, 512, 512);
final byte MAGIC = (byte)0xAB;
lib.fillInt8Buffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public void testWrappedShortArrayArgument() {
short[] array = new short[1024];
ShortBuffer buf = ShortBuffer.wrap(array, 512, 512);
final short MAGIC = (short)0xABED;
lib.fillInt16Buffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public void testWrappedIntArrayArgument() {
int[] array = new int[1024];
IntBuffer buf = IntBuffer.wrap(array, 512, 512);
final int MAGIC = 0xABEDCF23;
lib.fillInt32Buffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public void testWrappedLongArrayArguent() {
long[] array = new long[1024];
LongBuffer buf = LongBuffer.wrap(array, 512, 512);
final long MAGIC = 0x1234567887654321L;
lib.fillInt64Buffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public void testWrappedFloatArrayArguent() {
float[] array = new float[1024];
FloatBuffer buf = FloatBuffer.wrap(array, 512, 512);
final float MAGIC = -118.625f;
lib.fillFloatBuffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public void testWrappedDoubleArrayArguent() {
double[] array = new double[1024];
DoubleBuffer buf = DoubleBuffer.wrap(array, 512, 512);
final double MAGIC = -118.625;
lib.fillDoubleBuffer(buf, 512, MAGIC);
for (int i=0;i < array.length;i++) {
assertEquals("Bad value at index " + i,
i < 512 ? 0 : MAGIC, array[i]);
}
}
public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(BufferArgumentsMarshalTest.class);
}
}
|