Search in sources :

Example 1 with ByteIndexer

use of org.bytedeco.javacpp.indexer.ByteIndexer in project javacpp by bytedeco.

the class IndexerTest method testBooleanIndexer.

@Test
public void testBooleanIndexer() {
    System.out.println("BooleanIndexer");
    long size = 7 * 5 * 3 * 2;
    long[] sizes = { 7, 5, 3, 2 };
    long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
    final BooleanPointer ptr = new BooleanPointer(size);
    for (int i = 0; i < size; i++) {
        ptr.position(i).put(i % 2 != 0);
    }
    BooleanIndexer arrayIndexer = BooleanIndexer.create(ptr.position(0), sizes, strides, false);
    BooleanIndexer directIndexer = BooleanIndexer.create(ptr.position(0), sizes, strides, true);
    ByteIndexer byteIndexer = ByteIndexer.create(new BytePointer(ptr).position(0), sizes, strides, true);
    int n = 0;
    for (int i = 0; i < sizes[0]; i++) {
        assertEquals(n % 2 != 0, arrayIndexer.get(i));
        assertEquals(n % 2 != 0, directIndexer.get(i));
        assertEquals(n % 2 != 0 ? 1 : 0, byteIndexer.get(i));
        for (int j = 0; j < sizes[1]; j++) {
            assertEquals(n % 2 != 0, arrayIndexer.get(i, j));
            assertEquals(n % 2 != 0, directIndexer.get(i, j));
            assertEquals(n % 2 != 0 ? 1 : 0, byteIndexer.get(i, j));
            for (int k = 0; k < sizes[2]; k++) {
                assertEquals(n % 2 != 0, arrayIndexer.get(i, j, k));
                assertEquals(n % 2 != 0, directIndexer.get(i, j, k));
                assertEquals(n % 2 != 0 ? 1 : 0, byteIndexer.get(i, j, k));
                for (int m = 0; m < sizes[3]; m++) {
                    long[] index = { i, j, k, m };
                    assertEquals(n % 2 != 0, arrayIndexer.get(index));
                    assertEquals(n % 2 != 0, directIndexer.get(index));
                    assertEquals(n % 2 != 0 ? 1 : 0, byteIndexer.get(index));
                    arrayIndexer.put(index, n % 3 != 0);
                    directIndexer.put(index, n % 4 != 0);
                    n++;
                }
            }
        }
    }
    try {
        arrayIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        directIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    System.out.println("arrayIndexer" + arrayIndexer);
    System.out.println("directIndexer" + directIndexer);
    for (int i = 0; i < size; i++) {
        assertEquals(i % 4 != 0, ptr.position(i).get());
    }
    arrayIndexer.release();
    for (int i = 0; i < size; i++) {
        assertEquals(i % 3 != 0, ptr.position(i).get());
    }
    System.gc();
    if (Loader.sizeof(Pointer.class) > 4)
        try {
            long longSize = 0x80000000L + 8192;
            final BooleanPointer longPointer = new BooleanPointer(longSize);
            assertEquals(longSize, longPointer.capacity());
            BooleanIndexer longIndexer = BooleanIndexer.create(longPointer);
            assertEquals(longIndexer.pointer(), longPointer);
            for (long i = 0; i < 8192; i++) {
                longPointer.put(longSize - i - 1, i % 2 != 0);
            }
            for (long i = 0; i < 8192; i++) {
                assertEquals(longIndexer.get(longSize - i - 1), i % 2 != 0);
            }
            System.out.println("longIndexer[0x" + Long.toHexString(longSize) + " - 8192] = " + longIndexer.get(longSize - 8192));
        } catch (OutOfMemoryError e) {
            System.out.println(e);
        }
    System.out.println();
}
Also used : BooleanIndexer(org.bytedeco.javacpp.indexer.BooleanIndexer) ByteIndexer(org.bytedeco.javacpp.indexer.ByteIndexer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) Test(org.junit.Test)

Example 2 with ByteIndexer

use of org.bytedeco.javacpp.indexer.ByteIndexer in project javacpp by bytedeco.

the class IndexerTest method testByteIndexer.

@Test
public void testByteIndexer() {
    System.out.println("ByteIndexer");
    long size = 7 * 5 * 3 * 2;
    long[] sizes = { 7, 5, 3, 2 };
    long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
    final BytePointer ptr = new BytePointer(size);
    for (int i = 0; i < size; i++) {
        ptr.position(i).put((byte) i);
    }
    ByteIndexer arrayIndexer = ByteIndexer.create(ptr.position(0), sizes, strides, false);
    ByteIndexer directIndexer = ByteIndexer.create(ptr.position(0), sizes, strides, true);
    int n = 0;
    for (int i = 0; i < sizes[0]; i++) {
        assertEquals(n, arrayIndexer.get(i) & 0xFF);
        assertEquals(n, directIndexer.get(i) & 0xFF);
        for (int j = 0; j < sizes[1]; j++) {
            assertEquals(n, arrayIndexer.get(i, j) & 0xFF);
            assertEquals(n, directIndexer.get(i, j) & 0xFF);
            for (int k = 0; k < sizes[2]; k++) {
                assertEquals(n, arrayIndexer.get(i, j, k) & 0xFF);
                assertEquals(n, directIndexer.get(i, j, k) & 0xFF);
                for (int m = 0; m < sizes[3]; m++) {
                    long[] index = { i, j, k, m };
                    assertEquals(n, arrayIndexer.get(index) & 0xFF);
                    assertEquals(n, directIndexer.get(index) & 0xFF);
                    arrayIndexer.put(index, (byte) (n + 1));
                    directIndexer.put(index, (byte) (n + 2));
                    n++;
                }
            }
        }
    }
    try {
        arrayIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    try {
        directIndexer.get(size);
        fail("IndexOutOfBoundsException should have been thrown.");
    } catch (IndexOutOfBoundsException e) {
    }
    byte byteValue = 0x02;
    short shortValue = 0x0203;
    int intValue = 0x02030405;
    long longValue = 0x0203040506070809L;
    if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) {
        shortValue = Short.reverseBytes(shortValue);
        intValue = Integer.reverseBytes(intValue);
        longValue = Long.reverseBytes(longValue);
    }
    float floatValue = Float.intBitsToFloat(intValue);
    double doubleValue = Double.longBitsToDouble(longValue);
    float halfValue = HalfIndexer.toFloat(shortValue);
    float bfloat16Value = Bfloat16Indexer.toFloat(shortValue);
    boolean booleanValue = byteValue != 0;
    assertEquals(shortValue, arrayIndexer.getShort(1));
    assertEquals(intValue, arrayIndexer.getInt(1));
    assertEquals(longValue, arrayIndexer.getLong(1));
    assertEquals(floatValue, arrayIndexer.getFloat(1), 0.0);
    assertEquals(doubleValue, arrayIndexer.getDouble(1), 0.0);
    assertEquals(shortValue, arrayIndexer.getChar(1));
    assertEquals(byteValue, arrayIndexer.getUByte(1));
    assertEquals(shortValue, arrayIndexer.getUShort(1));
    assertEquals(intValue, arrayIndexer.getUInt(1));
    assertEquals(longValue, arrayIndexer.getULong(1).longValue());
    assertEquals(halfValue, arrayIndexer.getHalf(1), 0.0);
    assertEquals(bfloat16Value, arrayIndexer.getBfloat16(1), 0.0);
    assertEquals(booleanValue, arrayIndexer.getBoolean(1));
    System.out.println("arrayIndexer" + arrayIndexer);
    System.out.println("directIndexer" + directIndexer);
    for (int i = 0; i < size; i++) {
        assertEquals(i + 2, ptr.position(i).get() & 0xFF);
    }
    arrayIndexer.release();
    for (int i = 0; i < size; i++) {
        assertEquals(i + 1, ptr.position(i).get() & 0xFF);
    }
    assertEquals(shortValue, directIndexer.getShort(1));
    assertEquals(intValue, directIndexer.getInt(1));
    assertEquals(longValue, directIndexer.getLong(1));
    assertEquals(floatValue, directIndexer.getFloat(1), 0.0);
    assertEquals(doubleValue, directIndexer.getDouble(1), 0.0);
    assertEquals(shortValue, directIndexer.getChar(1));
    assertEquals(byteValue, directIndexer.getUByte(1));
    assertEquals(shortValue, directIndexer.getUShort(1));
    assertEquals(intValue, directIndexer.getUInt(1));
    assertEquals(longValue, directIndexer.getULong(1).longValue());
    assertEquals(halfValue, directIndexer.getHalf(1), 0.0);
    assertEquals(bfloat16Value, directIndexer.getBfloat16(1), 0.0);
    assertEquals(booleanValue, directIndexer.getBoolean(1));
    System.gc();
    if (Loader.sizeof(Pointer.class) > 4)
        try {
            long longSize = 0x80000000L + 8192;
            final BytePointer longPointer = new BytePointer(longSize);
            assertEquals(longSize, longPointer.capacity());
            ByteIndexer longIndexer = ByteIndexer.create(longPointer);
            assertEquals(longIndexer.pointer(), longPointer);
            for (long i = 0; i < 8192; i++) {
                longPointer.put(longSize - i - 1, (byte) i);
            }
            for (long i = 0; i < 8192; i++) {
                assertEquals(longIndexer.get(longSize - i - 1), (byte) i);
            }
            System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
        } catch (OutOfMemoryError e) {
            System.out.println(e);
        }
    System.out.println();
}
Also used : ByteIndexer(org.bytedeco.javacpp.indexer.ByteIndexer) UByteIndexer(org.bytedeco.javacpp.indexer.UByteIndexer) Test(org.junit.Test)

Aggregations

ByteIndexer (org.bytedeco.javacpp.indexer.ByteIndexer)2 UByteIndexer (org.bytedeco.javacpp.indexer.UByteIndexer)2 Test (org.junit.Test)2 BooleanIndexer (org.bytedeco.javacpp.indexer.BooleanIndexer)1