use of org.bytedeco.javacpp.indexer.UIntIndexer in project javacpp by bytedeco.
the class IndexerTest method testUIntIndexer.
@Test
public void testUIntIndexer() {
System.out.println("UIntIndexer");
long size = 7 * 5 * 3 * 2;
long[] sizes = { 7, 5, 3, 2 };
long[] strides = { 5 * 3 * 2, 3 * 2, 2, 1 };
final IntPointer ptr = new IntPointer(size);
long start = 0x30000000L;
for (int i = 0; i < size; i++) {
ptr.position(i).put((int) (i + start));
}
UIntIndexer arrayIndexer = UIntIndexer.create(ptr.position(0), sizes, strides, false);
UIntIndexer directIndexer = UIntIndexer.create(ptr.position(0), sizes, strides, true);
long n = start;
for (int i = 0; i < sizes[0]; i++) {
assertEquals(n, arrayIndexer.get(i));
assertEquals(n, directIndexer.get(i));
for (int j = 0; j < sizes[1]; j++) {
assertEquals(n, arrayIndexer.get(i, j));
assertEquals(n, directIndexer.get(i, j));
for (int k = 0; k < sizes[2]; k++) {
assertEquals(n, arrayIndexer.get(i, j, k));
assertEquals(n, directIndexer.get(i, j, k));
for (int m = 0; m < sizes[3]; m++) {
long[] index = { i, j, k, m };
assertEquals(n, arrayIndexer.get(index));
assertEquals(n, directIndexer.get(index));
arrayIndexer.put(index, 2 * n);
directIndexer.put(index, 3 * n);
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(3 * (i + start), ptr.position(i).get() & 0xFFFFFFFFL);
}
arrayIndexer.release();
for (int i = 0; i < size; i++) {
assertEquals(2 * (i + start), ptr.position(i).get() & 0xFFFFFFFFL);
}
System.gc();
if (Loader.sizeof(Pointer.class) > 4)
try {
long longSize = 0x80000000L + 8192;
final IntPointer longPointer = new IntPointer(longSize);
assertEquals(longSize, longPointer.capacity());
UIntIndexer longIndexer = UIntIndexer.create(longPointer);
assertEquals(longIndexer.pointer(), longPointer);
for (long i = 0; i < 8192; i++) {
longPointer.put(longSize - i - 1, (int) i);
}
for (long i = 0; i < 8192; i++) {
assertEquals(longIndexer.get(longSize - i - 1), i & 0xFFFFFFFFL);
}
System.out.println("longIndexer[0x" + Long.toHexString(longSize - 8192) + "] = " + longIndexer.get(longSize - 8192));
} catch (OutOfMemoryError e) {
System.out.println(e);
}
System.out.println();
}
Aggregations