use of java.nio.DoubleBuffer in project robovm by robovm.
the class StructTest method testStructWithArrayDoubleArrayAsBuffer.
@Test
public void testStructWithArrayDoubleArrayAsBuffer() {
assertEquals(192, StructWithArray.sizeOf());
final int D1 = 24;
StructWithArray s = new StructWithArray();
DoublePtr p = s.doubleArrayAsPtr();
DoubleBuffer b1;
DoubleBuffer b2;
DoubleBuffer b3;
for (int i = 0; i < D1; i++) {
p.next(i).set(i + 1);
}
b1 = s.doubleArrayAsBuffer();
assertEquals(D1, b1.capacity());
assertEquals(D1, b1.limit());
assertEquals(0, b1.position());
for (int i = 0; i < D1; i++) {
assertEquals(i + 1, b1.get(i), 0.0001);
}
b2 = ByteBuffer.allocateDirect(D1 * 8).order(ByteOrder.nativeOrder()).asDoubleBuffer();
for (int i = 0; i < D1; i++) {
b2.put(i, 2 * (i + 1));
}
s.doubleArrayAsBuffer(b2);
for (int i = 0; i < D1; i++) {
assertEquals(2 * (i + 1), p.next(i).get(), 0.0001);
}
b3 = DoubleBuffer.allocate(D1);
assertFalse(b3.isDirect());
for (int i = 0; i < D1; i++) {
b3.put(i, 3 * (i + 1));
}
s.doubleArrayAsBuffer(b3);
for (int i = 0; i < D1; i++) {
assertEquals(3 * (i + 1), p.next(i).get(), 0.0001);
}
try {
s.doubleArrayAsBuffer(DoubleBuffer.allocate(D1 / 2));
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException e) {
}
}
use of java.nio.DoubleBuffer in project robovm by robovm.
the class MachineSizedFloatPtr method get.
/**
* Copies {@code count} floats from the memory pointed to by this
* {@link MachineSizedFloatPtr} to {@code dst} starting at offset {@code offset}.
* Does {@code double} to {@code float} conversion if running on
* a 64-bit platform.
*
* @param dst the destination.
* @param offset the offset within the destination array to start copying to.
* @param count the number of elements to copy.
*/
public void get(float[] dst, int offset, int count) {
if (_sizeOf() == 4) {
asFloatBuffer(count).get(dst, offset, count);
} else {
Arrays.checkOffsetAndCount(dst.length, offset, count);
DoubleBuffer buf = asDoubleBuffer(count);
for (int i = 0; i < count; i++) {
dst[i + offset] = (float) buf.get();
}
}
}
use of java.nio.DoubleBuffer in project robovm by robovm.
the class ByteBufferTest method testAsDoubleBuffer.
public void testAsDoubleBuffer() {
DoubleBuffer doubleBuffer;
byte[] bytes = new byte[8];
double value;
// test BIG_ENDIAN double buffer, read
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
doubleBuffer = buf.asDoubleBuffer();
assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
while (doubleBuffer.remaining() > 0) {
buf.get(bytes);
value = doubleBuffer.get();
if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double.isNaN(value))) {
assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
}
}
// test LITTLE_ENDIAN double buffer, read
buf.clear();
buf.order(ByteOrder.LITTLE_ENDIAN);
doubleBuffer = buf.asDoubleBuffer();
assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
while (doubleBuffer.remaining() > 0) {
buf.get(bytes);
value = doubleBuffer.get();
if (!(Double.isNaN(bytes2double(bytes, buf.order())) && Double.isNaN(value))) {
assertEquals(bytes2double(bytes, buf.order()), value, 0.00);
}
}
if (!buf.isReadOnly()) {
// test BIG_ENDIAN double buffer, write
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
doubleBuffer = buf.asDoubleBuffer();
assertSame(ByteOrder.BIG_ENDIAN, doubleBuffer.order());
while (doubleBuffer.remaining() > 0) {
value = (double) doubleBuffer.remaining();
doubleBuffer.put(value);
buf.get(bytes);
assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order())));
}
// test LITTLE_ENDIAN double buffer, write
buf.clear();
buf.order(ByteOrder.LITTLE_ENDIAN);
doubleBuffer = buf.asDoubleBuffer();
assertSame(ByteOrder.LITTLE_ENDIAN, doubleBuffer.order());
while (doubleBuffer.remaining() > 0) {
value = (double) doubleBuffer.remaining();
doubleBuffer.put(value);
buf.get(bytes);
assertTrue(Arrays.equals(bytes, double2bytes(value, buf.order())));
}
}
buf.clear();
buf.order(ByteOrder.BIG_ENDIAN);
}
use of java.nio.DoubleBuffer in project robovm by robovm.
the class DoubleBufferTest method testGetdoubleArrayintint.
/*
* Class under test for java.nio.DoubleBuffer get(double[], int, int)
*/
public void testGetdoubleArrayintint() {
buf.clear();
double[] array = new double[buf.capacity()];
try {
buf.get(new double[buf.capacity() + 1], 0, buf.capacity() + 1);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (BufferUnderflowException e) {
// expected
}
assertEquals(buf.position(), 0);
try {
buf.get(array, -1, array.length);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
buf.get(array, array.length, 0);
try {
buf.get(array, array.length + 1, 1);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
assertEquals(buf.position(), 0);
try {
buf.get(array, 2, -1);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
buf.get((double[]) null, 0, -1);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (NullPointerException e) {
// expected
}
try {
buf.get(array, 2, array.length);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
buf.get(array, 1, Integer.MAX_VALUE);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (BufferUnderflowException expected) {
} catch (IndexOutOfBoundsException expected) {
}
try {
buf.get(array, Integer.MAX_VALUE, 1);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
assertEquals(buf.position(), 0);
buf.clear();
DoubleBuffer ret = buf.get(array, 0, array.length);
assertEquals(buf.position(), buf.capacity());
assertContentEquals(buf, array, 0, array.length);
assertSame(ret, buf);
}
use of java.nio.DoubleBuffer in project robovm by robovm.
the class DoubleBufferTest method testPutintdouble.
/*
* Class under test for java.nio.DoubleBuffer put(int, double)
*/
public void testPutintdouble() {
buf.clear();
for (int i = 0; i < buf.capacity(); i++) {
assertEquals(buf.position(), 0);
DoubleBuffer ret = buf.put(i, (double) i);
assertEquals(buf.get(i), (double) i, 0.0);
assertSame(ret, buf);
}
try {
buf.put(-1, 0);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
try {
buf.put(buf.limit(), 0);
//$NON-NLS-1$
fail("Should throw Exception");
} catch (IndexOutOfBoundsException e) {
// expected
}
}
Aggregations