Search in sources :

Example 31 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class StructTest method testStructWithArrayShortArrayAsBuffer.

@Test
public void testStructWithArrayShortArrayAsBuffer() {
    assertEquals(192, StructWithArray.sizeOf());
    final int D1 = 24;
    StructWithArray s = new StructWithArray();
    ShortPtr p = s.shortArrayAsPtr();
    ShortBuffer b1;
    ShortBuffer b2;
    ShortBuffer b3;
    for (int i = 0; i < D1; i++) {
        p.next(i).set((short) (i + 1));
    }
    b1 = s.shortArrayAsBuffer();
    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));
    }
    b2 = ByteBuffer.allocateDirect(D1 * 2).order(ByteOrder.nativeOrder()).asShortBuffer();
    for (int i = 0; i < D1; i++) {
        b2.put(i, (short) (2 * (i + 1)));
    }
    s.shortArrayAsBuffer(b2);
    for (int i = 0; i < D1; i++) {
        assertEquals(2 * (i + 1), p.next(i).get() & 0xffff);
    }
    b3 = ShortBuffer.allocate(D1);
    assertFalse(b3.isDirect());
    for (int i = 0; i < D1; i++) {
        b3.put(i, (short) (3 * (i + 1)));
    }
    s.shortArrayAsBuffer(b3);
    for (int i = 0; i < D1; i++) {
        assertEquals(3 * (i + 1), p.next(i).get() & 0xffff);
    }
    try {
        s.shortArrayAsBuffer(ShortBuffer.allocate(D1 / 2));
        fail("Expected IllegalArgumentException");
    } catch (IllegalArgumentException e) {
    }
}
Also used : ShortPtr(org.robovm.rt.bro.ptr.ShortPtr) ShortBuffer(java.nio.ShortBuffer) Test(org.junit.Test)

Example 32 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class ByteBufferTest method testAsShortBuffer.

public void testAsShortBuffer() {
    ShortBuffer shortBuffer;
    byte[] bytes = new byte[2];
    short value;
    // test BIG_ENDIAN short buffer, read
    buf.clear();
    buf.order(ByteOrder.BIG_ENDIAN);
    shortBuffer = buf.asShortBuffer();
    assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
    while (shortBuffer.remaining() > 0) {
        buf.get(bytes);
        value = shortBuffer.get();
        assertEquals(bytes2short(bytes, buf.order()), value);
    }
    // test LITTLE_ENDIAN short buffer, read
    buf.clear();
    buf.order(ByteOrder.LITTLE_ENDIAN);
    shortBuffer = buf.asShortBuffer();
    assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
    while (shortBuffer.remaining() > 0) {
        buf.get(bytes);
        value = shortBuffer.get();
        assertEquals(bytes2short(bytes, buf.order()), value);
    }
    if (!buf.isReadOnly()) {
        // test BIG_ENDIAN short buffer, write
        buf.clear();
        buf.order(ByteOrder.BIG_ENDIAN);
        shortBuffer = buf.asShortBuffer();
        assertSame(ByteOrder.BIG_ENDIAN, shortBuffer.order());
        while (shortBuffer.remaining() > 0) {
            value = (short) shortBuffer.remaining();
            shortBuffer.put(value);
            buf.get(bytes);
            assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order())));
        }
        // test LITTLE_ENDIAN short buffer, write
        buf.clear();
        buf.order(ByteOrder.LITTLE_ENDIAN);
        shortBuffer = buf.asShortBuffer();
        assertSame(ByteOrder.LITTLE_ENDIAN, shortBuffer.order());
        while (shortBuffer.remaining() > 0) {
            value = (short) shortBuffer.remaining();
            shortBuffer.put(value);
            buf.get(bytes);
            assertTrue(Arrays.equals(bytes, short2bytes(value, buf.order())));
        }
    }
    buf.clear();
    buf.order(ByteOrder.BIG_ENDIAN);
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 33 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class ShortBufferTest method testPutintshort.

/*
     * Class under test for java.nio.ShortBuffer put(int, short)
     */
public void testPutintshort() {
    buf.clear();
    for (int i = 0; i < buf.capacity(); i++) {
        assertEquals(buf.position(), 0);
        ShortBuffer ret = buf.put(i, (short) i);
        assertEquals(buf.get(i), (short) i);
        assertSame(ret, buf);
    }
    try {
        buf.put(-1, (short) 0);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(buf.limit(), (short) 0);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
}
Also used : ShortBuffer(java.nio.ShortBuffer)

Example 34 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class ShortBufferTest method testPutshortArrayintint.

/*
     * Class under test for java.nio.ShortBuffer put(short[], int, int)
     */
public void testPutshortArrayintint() {
    buf.clear();
    short[] array = new short[buf.capacity()];
    try {
        buf.put(new short[buf.capacity() + 1], 0, buf.capacity() + 1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException e) {
    // expected
    }
    assertEquals(buf.position(), 0);
    try {
        buf.put(array, -1, array.length);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, array.length + 1, 0);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    buf.put(array, array.length, 0);
    assertEquals(buf.position(), 0);
    try {
        buf.put(array, 0, -1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put((short[]) null, 0, -1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (NullPointerException e) {
    // expected
    }
    try {
        buf.put(array, 2, array.length);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, Integer.MAX_VALUE, 1);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (IndexOutOfBoundsException e) {
    // expected
    }
    try {
        buf.put(array, 1, Integer.MAX_VALUE);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException expected) {
    } catch (IndexOutOfBoundsException expected) {
    }
    assertEquals(buf.position(), 0);
    loadTestData2(array, 0, array.length);
    ShortBuffer ret = buf.put(array, 0, array.length);
    assertEquals(buf.position(), buf.capacity());
    assertContentEquals(buf, array, 0, array.length);
    assertSame(ret, buf);
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ShortBuffer(java.nio.ShortBuffer)

Example 35 with ShortBuffer

use of java.nio.ShortBuffer in project robovm by robovm.

the class ShortBufferTest method testPutshortArray.

/*
     * Class under test for java.nio.ShortBuffer put(short[])
     */
public void testPutshortArray() {
    short[] array = new short[1];
    buf.clear();
    for (int i = 0; i < buf.capacity(); i++) {
        assertEquals(buf.position(), i);
        array[0] = (short) i;
        ShortBuffer ret = buf.put(array);
        assertEquals(buf.get(i), (short) i);
        assertSame(ret, buf);
    }
    try {
        buf.put(array);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (BufferOverflowException e) {
    // expected
    }
    try {
        buf.position(buf.limit());
        buf.put((short[]) null);
        //$NON-NLS-1$
        fail("Should throw Exception");
    } catch (NullPointerException e) {
    // expected
    }
}
Also used : BufferOverflowException(java.nio.BufferOverflowException) ShortBuffer(java.nio.ShortBuffer)

Aggregations

ShortBuffer (java.nio.ShortBuffer)227 ByteBuffer (java.nio.ByteBuffer)78 FloatBuffer (java.nio.FloatBuffer)54 IntBuffer (java.nio.IntBuffer)45 DoubleBuffer (java.nio.DoubleBuffer)23 LongBuffer (java.nio.LongBuffer)16 Test (org.junit.Test)14 Buffer (java.nio.Buffer)11 BufferOverflowException (java.nio.BufferOverflowException)11 CharBuffer (java.nio.CharBuffer)11 VertexBuffer (com.jme3.scene.VertexBuffer)8 BufferUnderflowException (java.nio.BufferUnderflowException)7 BytePointer (org.bytedeco.javacpp.BytePointer)7 IndexBuffer (com.jme3.scene.mesh.IndexBuffer)6 IOException (java.io.IOException)5 Vector3f (com.jme3.math.Vector3f)4 ArrayList (java.util.ArrayList)4 Bitmap (android.graphics.Bitmap)3 Mesh (com.jme3.scene.Mesh)3 InvalidMarkException (java.nio.InvalidMarkException)3