Search in sources :

Example 6 with DoubleBuffer

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) {
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) DoublePtr(org.robovm.rt.bro.ptr.DoublePtr) Test(org.junit.Test)

Example 7 with DoubleBuffer

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();
        }
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 8 with DoubleBuffer

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);
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Example 9 with DoubleBuffer

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);
}
Also used : DoubleBuffer(java.nio.DoubleBuffer) BufferUnderflowException(java.nio.BufferUnderflowException)

Example 10 with DoubleBuffer

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
    }
}
Also used : DoubleBuffer(java.nio.DoubleBuffer)

Aggregations

DoubleBuffer (java.nio.DoubleBuffer)162 ByteBuffer (java.nio.ByteBuffer)39 FloatBuffer (java.nio.FloatBuffer)26 IntBuffer (java.nio.IntBuffer)25 ShortBuffer (java.nio.ShortBuffer)22 LongBuffer (java.nio.LongBuffer)14 CharBuffer (java.nio.CharBuffer)11 BufferOverflowException (java.nio.BufferOverflowException)8 IOException (java.io.IOException)5 BufferUnderflowException (java.nio.BufferUnderflowException)5 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ServerDenseDoubleRow (com.tencent.angel.ps.impl.matrix.ServerDenseDoubleRow)4 Test (org.junit.Test)4 InvalidMarkException (java.nio.InvalidMarkException)3 Random (java.util.Random)3 BytePointer (org.bytedeco.javacpp.BytePointer)3 DoublePointer (org.bytedeco.javacpp.DoublePointer)3 FloatPointer (org.bytedeco.javacpp.FloatPointer)3 IntPointer (org.bytedeco.javacpp.IntPointer)3