Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,723 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.Buffer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.ReadOnlyException;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class NativeWritableMemoryImplTest {
+
+ //Simple Native direct
+
+ @Test
+ public void checkNativeCapacityAndClose() throws Exception {
+ int memCapacity = 64;
+ WritableHandle wmh = WritableMemory.allocateDirect(memCapacity);
+ WritableMemory mem = wmh.getWritable();
+ assertEquals(memCapacity, mem.getCapacity());
+
+ wmh.close(); //intentional
+ assertFalse(mem.isValid());
+
+ wmh.close(); //intentional, nothing to free
+ }
+
+ //Simple Native arrays
+
+ @Test
+ public void checkBooleanArray() {
+ boolean[] srcArray = { true, false, true, false, false, true, true, false
};
+ boolean[] dstArray = new boolean[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getBooleanArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getBooleanArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ assertTrue(mem.hasArray());
+ }
+
+ @Test
+ public void checkByteArray() {
+ byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ byte[] dstArray = new byte[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getByteArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getByteArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkCharArray() {
+ char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
+ char[] dstArray = new char[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getCharArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getCharArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkShortArray() {
+ short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ short[] dstArray = new short[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getShortArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getShortArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkIntArray() {
+ int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ int[] dstArray = new int[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getIntArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getIntArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkLongArray() {
+ long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ long[] dstArray = new long[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getLongArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getLongArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkFloatArray() {
+ float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ float[] dstArray = new float[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getFloatArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getFloatArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkDoubleArray() {
+ double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ double[] dstArray = new double[8];
+
+ Memory mem = Memory.wrap(srcArray);
+ mem.getDoubleArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ wmem.getDoubleArray(0, dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ }
+
+ @Test
+ public void checkNativeBaseBound() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.toHexString("Force Assertion Error", memCapacity, 8);
+ } catch (IllegalArgumentException e) {
+ //ok
+ }
+ }
+
+ @Test
+ public void checkNativeSrcArrayBound() throws Exception {
+ long memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ byte[] srcArray = { 1, -2, 3, -4 };
+ mem.putByteArray(0L, srcArray, 0, 5);
+ } catch (IllegalArgumentException e) {
+ //pass
+ }
+ }
+
+ //Copy Within tests
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void checkDegenerateCopyTo() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ wmem.copyTo(0, wmem, 0, 64);
+ }
+
+ @Test
+ public void checkCopyWithinNativeSmall() throws Exception {
+ int memCapacity = 64;
+ int half = memCapacity/2;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.clear();
+
+ for (int i=0; i<half; i++) { //fill first half
+ mem.putByte(i, (byte) i);
+ }
+
+ mem.copyTo(0, mem, half, half);
+
+ for (int i=0; i<half; i++) {
+ assertEquals(mem.getByte(i+half), (byte) i);
+ }
+ }
+ }
+
+ @Test
+ public void checkCopyWithinNativeLarge() throws Exception {
+ int memCapacity = (2 << 20) + 64;
+ int memCapLongs = memCapacity / 8;
+ int halfBytes = memCapacity / 2;
+ int halfLongs = memCapLongs / 2;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.clear();
+
+ for (int i=0; i < halfLongs; i++) {
+ mem.putLong(i*8, i);
+ }
+
+ mem.copyTo(0, mem, halfBytes, halfBytes);
+
+ for (int i=0; i < halfLongs; i++) {
+ assertEquals(mem.getLong((i + halfLongs)*8), i);
+ }
+ }
+ }
+
+ @Test
+ public void checkCopyWithinNativeSrcBound() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.copyTo(32, mem, 32, 33); //hit source bound check
+ fail("Did Not Catch Assertion Error: source bound");
+ }
+ catch (IllegalArgumentException e) {
+ //pass
+ }
+ }
+
+ @Test
+ public void checkCopyWithinNativeDstBound() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.copyTo(0, mem, 32, 33); //hit dst bound check
+ fail("Did Not Catch Assertion Error: dst bound");
+ }
+ catch (IllegalArgumentException e) {
+ //pass
+ }
+ }
+
+ @Test
+ public void checkCopyCrossNativeSmall() throws Exception {
+ int memCapacity = 64;
+
+ try (WritableHandle wrh1 = WritableMemory.allocateDirect(memCapacity);
+ WritableHandle wrh2 = WritableMemory.allocateDirect(memCapacity))
+ {
+ WritableMemory mem1 = wrh1.getWritable();
+ WritableMemory mem2 = wrh2.getWritable();
+
+ for (int i=0; i < memCapacity; i++) {
+ mem1.putByte(i, (byte) i);
+ }
+ mem2.clear();
+ mem1.copyTo(0, mem2, 0, memCapacity);
+
+ for (int i=0; i<memCapacity; i++) {
+ assertEquals(mem2.getByte(i), (byte) i);
+ }
+ wrh1.close();
+ wrh2.close();
+ }
+ }
+
+ @Test
+ public void checkCopyCrossNativeLarge() throws Exception {
+ int memCapacity = (2<<20) + 64;
+ int memCapLongs = memCapacity / 8;
+
+ try (WritableHandle wrh1 = WritableMemory.allocateDirect(memCapacity);
+ WritableHandle wrh2 = WritableMemory.allocateDirect(memCapacity))
+ {
+ WritableMemory mem1 = wrh1.getWritable();
+ WritableMemory mem2 = wrh2.getWritable();
+
+ for (int i=0; i < memCapLongs; i++) {
+ mem1.putLong(i*8, i);
+ }
+ mem2.clear();
+
+ mem1.copyTo(0, mem2, 0, memCapacity);
+
+ for (int i=0; i<memCapLongs; i++) {
+ assertEquals(mem2.getLong(i*8), i);
+ }
+ }
+ }
+
+ @Test
+ public void checkCopyCrossNativeAndByteArray() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh1 = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem1 = wrh1.getWritable();
+
+ for (int i= 0; i < mem1.getCapacity(); i++) {
+ mem1.putByte(i, (byte) i);
+ }
+
+ WritableMemory mem2 = WritableMemory.allocate(memCapacity);
+ mem1.copyTo(8, mem2, 16, 16);
+
+ for (int i=0; i<16; i++) {
+ assertEquals(mem1.getByte(8+i), mem2.getByte(16+i));
+ }
+ //println(mem2.toHexString("Mem2", 0, (int)mem2.getCapacity()));
+ }
+ }
+
+ @Test
+ public void checkCopyCrossRegionsSameNative() throws Exception {
+ int memCapacity = 128;
+
+ try (WritableHandle wrh1 = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem1 = wrh1.getWritable();
+
+ for (int i= 0; i < mem1.getCapacity(); i++) {
+ mem1.putByte(i, (byte) i);
+ }
+ //println(mem1.toHexString("Mem1", 0, (int)mem1.getCapacity()));
+
+ Memory reg1 = mem1.region(8, 16);
+ //println(reg1.toHexString("Reg1", 0, (int)reg1.getCapacity()));
+
+ WritableMemory reg2 = mem1.writableRegion(24, 16);
+ //println(reg2.toHexString("Reg2", 0, (int)reg2.getCapacity()));
+ reg1.copyTo(0, reg2, 0, 16);
+
+ for (int i=0; i<16; i++) {
+ assertEquals(reg1.getByte(i), reg2.getByte(i));
+ assertEquals(mem1.getByte(8+i), mem1.getByte(24+i));
+ }
+ //println(mem1.toHexString("Mem1", 0, (int)mem1.getCapacity()));
+ }
+ }
+
+ @Test
+ public void checkCopyCrossNativeArrayAndHierarchicalRegions() throws
Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh1 = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem1 = wrh1.getWritable();
+
+ for (int i= 0; i < mem1.getCapacity(); i++) { //fill with numbers
+ mem1.putByte(i, (byte) i);
+ }
+ //println(mem1.toHexString("Mem1", 0, (int)mem1.getCapacity()));
+
+ WritableMemory mem2 = WritableMemory.allocate(memCapacity);
+
+ Memory reg1 = mem1.region(8, 32);
+ Memory reg1B = reg1.region(8, 16);
+ //println(reg1.toHexString("Reg1", 0, (int)reg1.getCapacity()));
+ //println(reg1B.toHexString("Reg1B", 0, (int)reg1B.getCapacity()));
+
+ WritableMemory reg2 = mem2.writableRegion(32, 16);
+ reg1B.copyTo(0, reg2, 0, 16);
+ //println(reg2.toHexString("Reg2", 0, (int)reg2.getCapacity()));
+
+ //println(mem2.toHexString("Mem2", 0, (int)mem2.getCapacity()));
+ for (int i = 32, j = 16; i < 40; i++, j++) {
+ assertEquals(mem2.getByte(i), j);
+ }
+ }
+
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void checkRegionBounds() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem = wrh.getWritable();
+ mem.writableRegion(1, 64);
+ }
+ }
+
+ @Test
+ public void checkByteBufferWrap() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocate(memCapacity);
+ byteBuf.order(ByteOrder.nativeOrder());
+
+ for (int i=0; i<memCapacity; i++) {
+ byteBuf.put(i, (byte) i);
+ }
+
+ WritableMemory wmem = WritableMemory.writableWrap(byteBuf);
+
+ for (int i=0; i<memCapacity; i++) {
+ assertEquals(wmem.getByte(i), byteBuf.get(i));
+ }
+
+ assertTrue(wmem.hasByteBuffer());
+ ByteBuffer byteBuf2 = wmem.getByteBuffer();
+ assertEquals(byteBuf2, byteBuf);
+ //println( mem.toHexString("HeapBB", 0, memCapacity));
+ }
+
+ @Test
+ public void checkWrapWithBBReadonly1() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocate(memCapacity);
+ byteBuf.order(ByteOrder.nativeOrder());
+
+ for (int i = 0; i < memCapacity; i++) {
+ byteBuf.put(i, (byte) i);
+ }
+
+ Memory mem = WritableMemory.writableWrap(byteBuf);
+
+ for (int i = 0; i < memCapacity; i++) {
+ assertEquals(mem.getByte(i), byteBuf.get(i));
+ }
+
+ //println(mem.toHexString("HeapBB", 0, memCapacity));
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void checkWrapWithBBReadonly2() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocate(memCapacity);
+ byteBuf.order(ByteOrder.nativeOrder());
+ ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
+
+ WritableMemory.writableWrap(byteBufRO);
+ }
+
+ @Test
+ public void checkWrapWithDirectBBReadonly() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
+ byteBuf.order(ByteOrder.nativeOrder());
+
+ for (int i = 0; i < memCapacity; i++) {
+ byteBuf.put(i, (byte) i);
+ }
+ ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
+ byteBufRO.order(ByteOrder.nativeOrder());
+
+ Memory mem = Memory.wrap(byteBufRO);
+
+ for (int i = 0; i < memCapacity; i++) {
+ assertEquals(mem.getByte(i), byteBuf.get(i));
+ }
+
+ //println(mem.toHexString("HeapBB", 0, memCapacity));
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void checkWrapWithDirectBBReadonlyPut() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
+ ByteBuffer byteBufRO = byteBuf.asReadOnlyBuffer();
+ byteBufRO.order(ByteOrder.nativeOrder());
+
+ WritableMemory.writableWrap(byteBufRO);
+ }
+
+ @Test
+ public void checkByteBufferWrapDirectAccess() {
+ int memCapacity = 64;
+ ByteBuffer byteBuf = ByteBuffer.allocateDirect(memCapacity);
+ byteBuf.order(ByteOrder.nativeOrder());
+
+ for (int i=0; i<memCapacity; i++) {
+ byteBuf.put(i, (byte) i);
+ }
+
+ Memory mem = Memory.wrap(byteBuf);
+
+ for (int i=0; i<memCapacity; i++) {
+ assertEquals(mem.getByte(i), byteBuf.get(i));
+ }
+
+ //println( mem.toHexString("HeapBB", 0, memCapacity));
+ }
+
+ @Test
+ public void checkIsDirect() throws Exception {
+ int memCapacity = 64;
+ WritableMemory mem = WritableMemory.allocate(memCapacity);
+ assertFalse(mem.isDirect());
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ mem = wrh.getWritable();
+ assertTrue(mem.isDirect());
+ wrh.close();
+ }
+ }
+
+ @Test
+ public void checkIsReadOnly() {
+ long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+
+ WritableMemory wmem = WritableMemory.writableWrap(srcArray);
+ assertFalse(wmem.isReadOnly());
+
+ Memory memRO = wmem;
+ assertFalse(memRO.isReadOnly());
+
+ for (int i = 0; i < wmem.getCapacity(); i++) {
+ assertEquals(wmem.getByte(i), memRO.getByte(i));
+ }
+ }
+
+ @Test
+ public void checkGoodBounds() {
+ UnsafeUtil.checkBounds(50, 50, 100);
+ }
+
+ @Test
+ public void checkCompareToHeap() {
+ byte[] arr1 = new byte[] {0, 1, 2, 3};
+ byte[] arr2 = new byte[] {0, 1, 2, 4};
+ byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
+
+ Memory mem1 = Memory.wrap(arr1);
+ Memory mem2 = Memory.wrap(arr2);
+ Memory mem3 = Memory.wrap(arr3);
+ Memory mem4 = Memory.wrap(arr3); //same resource
+
+ int comp = mem1.compareTo(0, 3, mem2, 0, 3);
+ assertEquals(comp, 0);
+ comp = mem1.compareTo(0, 4, mem2, 0, 4);
+ assertEquals(comp, -1);
+ comp = mem2.compareTo(0, 4, mem1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = mem1.compareTo(0, 4, mem3, 0, 5);
+ assertEquals(comp, -1);
+ comp = mem3.compareTo(0, 5, mem1, 0, 4);
+ assertEquals(comp, 1);
+ comp = mem3.compareTo(0, 5, mem4, 0, 5);
+ assertEquals(comp, 0);
+ comp = mem3.compareTo(0, 4, mem4, 1, 4);
+ assertEquals(comp, -1);
+ mem3.checkValidAndBounds(0, 5);
+ }
+
+ @Test
+ public void checkCompareToDirect() throws Exception {
+ byte[] arr1 = new byte[] {0, 1, 2, 3};
+ byte[] arr2 = new byte[] {0, 1, 2, 4};
+ byte[] arr3 = new byte[] {0, 1, 2, 3, 4};
+
+ try (WritableHandle h1 = WritableMemory.allocateDirect(4);
+ WritableHandle h2 = WritableMemory.allocateDirect(4);
+ WritableHandle h3 = WritableMemory.allocateDirect(5))
+ {
+ WritableMemory mem1 = h1.getWritable();
+ mem1.putByteArray(0, arr1, 0, 4);
+
+ WritableMemory mem2 = h2.getWritable();
+ mem2.putByteArray(0, arr2, 0, 4);
+
+ WritableMemory mem3 = h3.getWritable();
+ mem3.putByteArray(0, arr3, 0, 5);
+
+ int comp = mem1.compareTo(0, 3, mem2, 0, 3);
+ assertEquals(comp, 0);
+ comp = mem1.compareTo(0, 4, mem2, 0, 4);
+ assertEquals(comp, -1);
+ comp = mem2.compareTo(0, 4, mem1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = mem1.compareTo(0, 4, mem3, 0, 5);
+ assertEquals(comp, -1);
+ comp = mem3.compareTo(0, 5, mem1, 0, 4);
+ assertEquals(comp, 1);
+ }
+ }
+
+ @Test
+ public void testCompareToSameStart() {
+ Memory mem = WritableMemory.allocate(3);
+ assertEquals(-1, mem.compareTo(0, 1, mem, 0, 2));
+ assertEquals(0, mem.compareTo(1, 1, mem, 1, 1));
+ assertEquals(1, mem.compareTo(1, 2, mem, 1, 1));
+ }
+
+ @Test
+ public void checkAsBuffer() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ wbuf.setPosition(32);
+ for (int i = 32; i < 64; i++) { wbuf.putByte((byte)i); }
+ //println(wbuf.toHexString("Buf", 0, (int)wbuf.getCapacity()));
+
+ Buffer buf = wmem.asBuffer();
+ buf.setPosition(32);
+ for (int i = 32; i < 64; i++) {
+ assertEquals(buf.getByte(), i);
+ }
+ }
+
+ @Test
+ public void checkCumAndRegionOffset() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ WritableMemory reg = wmem.writableRegion(32, 32);
+ assertEquals(reg.getRegionOffset(), 32);
+ assertEquals(reg.getRegionOffset(0), 32);
+ assertEquals(reg.getCumulativeOffset(), 32 + 16);
+ assertEquals(reg.getCumulativeOffset(0), 32 + 16);
+ }
+
+ @Test
+ public void checkIsSameResource() {
+ byte[] byteArr = new byte[64];
+ WritableMemory wmem1 = WritableMemory.writableWrap(byteArr);
+ WritableMemory wmem2 = WritableMemory.writableWrap(byteArr);
+ assertTrue(wmem1.isSameResource(wmem2));
+ }
+
+ @Test
+ public void checkAsWritableBufferWithBB() {
+ ByteBuffer byteBuf = ByteBuffer.allocate(64);
+ byteBuf.position(16);
+ byteBuf.limit(48);
+ WritableMemory wmem = WritableMemory.writableWrap(byteBuf);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ assertEquals(wbuf.getCapacity(), 64);
+ assertEquals(wbuf.getPosition(), 0);
+ assertEquals(wbuf.getEnd(), 64);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void checkAsWritableRegionRO() {
+ ByteBuffer byteBuf = ByteBuffer.allocate(64);
+ WritableMemory wmem = (WritableMemory) Memory.wrap(byteBuf);
+ wmem.writableRegion(0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void checkAsWritableBufferRO() {
+ ByteBuffer byteBuf = ByteBuffer.allocate(64);
+ WritableMemory wmem = (WritableMemory) Memory.wrap(byteBuf);
+ wmem.asWritableBuffer();
+ }
+
+ @Test void checkZeroMemory() {
+ WritableMemory wmem = WritableMemory.allocate(8);
+ WritableMemory reg = wmem.writableRegion(0, 0);
+ assertEquals(reg.getCapacity(), 0);
+ }
+
+ @Test
+ public void checkAsBufferNonNative() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ wmem.putShort(0, (short) 1);
+ Buffer buf = wmem.asBuffer(Util.NON_NATIVE_BYTE_ORDER);
+ assertEquals(buf.getShort(0), 256);
+ }
+
+ @Test
+ public void printlnTest() {
+ println("PRINTING: "+this.getClass().getName());
+ }
+
+ /**
+ * @param s value to print
+ */
+ static void println(String s) {
+ //System.out.println(s); //disable here
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NioBitsTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NioBitsTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NioBitsTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+/**
+ * @author Lee Rhodes
+ */
+public class NioBitsTest {
+
+ @Test
+ public void checkVMParams() {
+ println("Max MemoryImpl: " + NioBits.getMaxDirectByteBufferMemory());
+ println("Page Aligned: " + NioBits.isPageAligned());
+ println("Page Size: " + NioBits.pageSize());
+ }
+
+ @Test
+ public void checkGetAtomicFields() {
+ //testing this beyond 2GB may not work on JVMs < 8GB.
+ //This should be checked manually
+ // long cap = 1024L + Integer.MAX_VALUE;
+ long cap = 1L << 10;
+ printStats();
+ NioBits.reserveMemory(cap, cap);
+ printStats();
+ NioBits. unreserveMemory(cap, cap);
+ printStats();
+ }
+
+ @Test
+ public void checkPageCount() {
+ assertEquals(NioBits.pageCount(0), 0);
+ assertEquals(NioBits.pageCount(1), 1);
+ }
+
+ private static void printStats() {
+ long count = NioBits.getDirectAllocationsCount();
+ long resMem = NioBits.getReservedMemory();
+ long totCap = NioBits.getTotalCapacity();
+ long maxDBBmem = NioBits.getMaxDirectByteBufferMemory();
+ String s = String.format("%,10d\t%,15d\t%,15d\t%,15d", count, resMem,
totCap, maxDBBmem);
+ println(s);
+ }
+
+ @Test
+ public void printlnTest() {
+ println("PRINTING: " + this.getClass().getName());
+ }
+
+ /**
+ * @param s value to print
+ */
+ static void println(final String s) {
+ //System.out.println(s); //disable here
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NioBitsTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertEquals;
+
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.Buffer;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+/**
+ * @author Lee Rhodes
+ */
+public class NonNativeWritableBufferImplTest {
+
+//Check primitives
+ @Test
+ public void checkCharacters() {
+ int n = 8;
+ int m = Character.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ char ch = 'a';
+ for (int i = 0; i < n; i++) { wbuf.putChar(i * m, ch++); }
+ ch = 'a';
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getChar(i * m), ch++);
+ }
+ ch = 'a';
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putChar(ch++); }
+ ch = 'a';
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getChar(), ch++);
+ }
+ //getArr & putArr
+ char[] cArr = new char[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getCharArray(cArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putCharArray(cArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkDoubles() {
+ int n = 8;
+ int m = Double.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ double dbl = 1.0;
+ for (int i = 0; i < n; i++) { wbuf.putDouble(i * m, dbl++); }
+ dbl = 1.0;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getDouble(i * m), dbl++);
+ }
+ dbl = 1.0;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putDouble(dbl++); }
+ dbl = 1.0;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getDouble(), dbl++);
+ }
+ //getArr & putArr
+ double[] dblArr = new double[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getDoubleArray(dblArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putDoubleArray(dblArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkFloats() {
+ int n = 8;
+ int m = Float.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ float flt = 1.0F;
+ for (int i = 0; i < n; i++) { wbuf.putFloat(i * m, flt++); }
+ flt = 1.0F;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getFloat(i * m), flt++);
+ }
+ flt = 1.0F;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putFloat(flt++); }
+ flt = 1.0F;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getFloat(), flt++);
+ }
+ //getArr & putArr
+ float[] fltArr = new float[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getFloatArray(fltArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putFloatArray(fltArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkInts() {
+ int n = 8;
+ int m = Integer.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ int intg = 1;
+ for (int i = 0; i < n; i++) { wbuf.putInt(i * m, intg++); }
+ intg = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getInt(i * m), intg++);
+ }
+ intg = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putInt(intg++); }
+ intg = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getInt(), intg++);
+ }
+ //getArr & putArr
+ int[] intArr = new int[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getIntArray(intArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putIntArray(intArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkLongs() {
+ int n = 8;
+ int m = Long.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ long lng = 1;
+ for (int i = 0; i < n; i++) { wbuf.putLong(i * m, lng++); }
+ lng = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getLong(i * m), lng++);
+ }
+ lng = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putLong(lng++); }
+ lng = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getLong(), lng++);
+ }
+ //getArr & putArr
+ long[] longArr = new long[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getLongArray(longArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putLongArray(longArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkShorts() {
+ int n = 8;
+ int m = Short.BYTES;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ short sht = 1;
+ for (int i = 0; i < n; i++) { wbuf.putShort(i * m, sht++); }
+ sht = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getShort(i * m), sht++);
+ }
+ sht = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) { wbuf.putShort(sht++); }
+ sht = 1;
+ wbuf.setPosition(0);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wbuf.getShort(), sht++);
+ }
+ //getArr & putArr
+ short[] shortArr = new short[n]; //native
+ wbuf.setPosition(0);
+ wbuf.getShortArray(shortArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf2 = wmem2.asWritableBuffer();
+ wbuf2.putShortArray(shortArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ //check Duplicate, Region
+ @Test
+ public void checkDuplicate() {
+ byte[] bArr = new byte[8];
+ WritableMemory wmem = WritableMemory.writableWrap(bArr,
ByteOrder.BIG_ENDIAN);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ WritableBuffer wdup = wbuf.writableDuplicate();
+ assertEquals(wdup.getTypeByteOrder(), ByteOrder.BIG_ENDIAN);
+
+ WritableBuffer wreg = wbuf.writableRegion();
+ assertEquals(wreg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN);
+ }
+
+ @Test
+ public void checkDuplicateZeros() {
+ byte[] bArr = new byte[0];
+ WritableMemory wmem = WritableMemory.writableWrap(bArr,
ByteOrder.BIG_ENDIAN);
+ Buffer buf = wmem.asBuffer();
+ Buffer dup = buf.duplicate();
+ assertEquals(dup.getTypeByteOrder(), ByteOrder.LITTLE_ENDIAN);
+
+ Buffer reg = buf.region();
+ assertEquals(reg.getTypeByteOrder(), ByteOrder.LITTLE_ENDIAN);
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,219 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertEquals;
+
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+/**
+ * @author Lee Rhodes
+ */
+public class NonNativeWritableMemoryImplTest {
+ private byte[] bArr = new byte[8];
+ private final WritableMemory wmem = WritableMemory.writableWrap(bArr,
ByteOrder.BIG_ENDIAN);
+
+//Check primitives
+ @Test
+ public void checkCharacters() {
+ int m = Character.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ for (int i = 0; i < n; i++) { wmem1.putChar(i * m, (char) i++); }
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getChar(i * m), (char) i++);
+ }
+ //getArr & putArr
+ char[] cArr = new char[n]; //native
+ wmem1.getCharArray(0, cArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putCharArray(0, cArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkDoubles() {
+ int m = Double.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ double dbl = 1.0;
+ for (int i = 0; i < n; i++) { wmem1.putDouble(i * m, dbl++); }
+ dbl = 1.0;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getDouble(i * m), dbl++);
+ }
+ //getArr & putArr
+ double[] dblArr = new double[n]; //native
+ wmem1.getDoubleArray(0, dblArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putDoubleArray(0, dblArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkFloats() {
+ int m = Float.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ float flt = 1.0F;
+ for (int i = 0; i < n; i++) { wmem1.putFloat(i * m, flt++); }
+ flt = 1.0F;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getFloat(i * m), flt++);
+ }
+ //getArr & putArr
+ float[] fltArr = new float[n]; //native
+ wmem1.getFloatArray(0, fltArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putFloatArray(0, fltArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkInts() {
+ int m = Integer.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ int intg = 1;
+ for (int i = 0; i < n; i++) { wmem1.putInt(i * m, intg++); }
+ intg = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getInt(i * m), intg++);
+ }
+ //getArr & putArr
+ int[] intArr = new int[n]; //native
+ wmem1.getIntArray(0, intArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putIntArray(0, intArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkLongs() {
+ int m = Long.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ long lng = 1;
+ for (int i = 0; i < n; i++) { wmem1.putLong(i * m, lng++); }
+ lng = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getLong(i * m), lng++);
+ }
+ //getArr & putArr
+ long[] longArr = new long[n]; //native
+ wmem1.getLongArray(0, longArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putLongArray(0, longArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ @Test
+ public void checkShorts() {
+ int m = Short.BYTES;
+ int n = ((1 << 20) / m) + m;
+ byte[] arr1 = new byte[n * m]; //non-native
+ //put & get
+ WritableMemory wmem1 = WritableMemory.writableWrap(arr1,
ByteOrder.BIG_ENDIAN);
+ short sht = 1;
+ for (int i = 0; i < n; i++) { wmem1.putShort(i * m, sht++); }
+ sht = 1;
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem1.getShort(i * m), sht++);
+ }
+ //getArr & putArr
+ short[] shortArr = new short[n]; //native
+ wmem1.getShortArray(0, shortArr, 0, n); //wmem is non-native
+ byte[] arr2 = new byte[n * m];
+ WritableMemory wmem2 = WritableMemory.writableWrap(arr2,
ByteOrder.BIG_ENDIAN);
+ wmem2.putShortArray(0, shortArr, 0, n);
+ assertEquals(arr2, arr1);
+ }
+
+ //check Atomic Write Methods
+
+
+ @Test
+ public void testGetAndAddLong() {
+ wmem.getAndAddLong(0, 1L);
+ try {
+ wmem.getAndAddLong(1, 1L);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetAndSetLong() {
+ wmem.getAndSetLong(0, 1L);
+ try {
+ wmem.getAndSetLong(1, 1L);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testCompareAndSwapLong() {
+ wmem.compareAndSwapLong(0, 0L, 1L);
+ try {
+ wmem.compareAndSwapLong(1, 0L, 1L);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ //check Region
+ @Test
+ public void checkRegion() {
+ WritableMemory wreg = wmem.writableRegion(0, wmem.getCapacity());
+ assertEquals(wreg.getTypeByteOrder(), ByteOrder.BIG_ENDIAN);
+ }
+
+ @Test
+ public void checkRegionZeros() {
+ byte[] bArr1 = new byte[0];
+ WritableMemory wmem1 = WritableMemory.writableWrap(bArr1,
ByteOrder.BIG_ENDIAN);
+ Memory reg = wmem1.region(0, wmem1.getCapacity());
+ assertEquals(reg.getTypeByteOrder(), ByteOrder.LITTLE_ENDIAN);
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,194 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.Buffer;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.WritableMapHandle;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+/**
+ * @author Lee Rhodes
+ */
+public class SpecificLeafTest {
+
+ @Test
+ public void checkByteBufferLeafs() {
+ int bytes = 128;
+ ByteBuffer bb = ByteBuffer.allocate(bytes);
+ bb.order(ByteOrder.nativeOrder());
+
+ Memory mem = Memory.wrap(bb).region(0, bytes, ByteOrder.nativeOrder());
+ assertTrue(((BaseStateImpl)mem).isBBType());
+ assertTrue(mem.isReadOnly());
+ checkCrossLeafTypeIds(mem);
+ Buffer buf = mem.asBuffer().region(0, bytes, ByteOrder.nativeOrder());
+
+ bb.order(Util.NON_NATIVE_BYTE_ORDER);
+ Memory mem2 = Memory.wrap(bb).region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf2 = mem2.asBuffer().region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf3 = buf2.duplicate();
+
+ assertTrue(((BaseStateImpl)mem).isRegionType());
+ assertTrue(((BaseStateImpl)mem2).isRegionType());
+ assertTrue(((BaseStateImpl)buf).isRegionType());
+ assertTrue(((BaseStateImpl)buf2).isRegionType());
+ assertTrue(((BaseStateImpl)buf3).isDuplicateType());
+ }
+
+ @Test
+ public void checkDirectLeafs() throws Exception {
+ int bytes = 128;
+ try (WritableHandle h = WritableMemory.allocateDirect(bytes)) {
+ WritableMemory wmem = h.getWritable(); //native mem
+ assertTrue(((BaseStateImpl)wmem).isDirectType());
+ assertFalse(wmem.isReadOnly());
+ checkCrossLeafTypeIds(wmem);
+ WritableMemory nnwmem = wmem.writableRegion(0, bytes,
Util.NON_NATIVE_BYTE_ORDER);
+
+ Memory mem = wmem.region(0, bytes, ByteOrder.nativeOrder());
+ Buffer buf = mem.asBuffer().region(0, bytes, ByteOrder.nativeOrder());
+
+
+ Memory mem2 = nnwmem.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf2 = mem2.asBuffer().region(0, bytes,
Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf3 = buf2.duplicate();
+
+ assertTrue(((BaseStateImpl)mem).isRegionType());
+ assertTrue(((BaseStateImpl)mem2).isRegionType());
+ assertTrue(((BaseStateImpl)buf).isRegionType());
+ assertTrue(((BaseStateImpl)buf2).isRegionType());
+ assertTrue(((BaseStateImpl)buf3).isDuplicateType());
+ }
+ }
+
+ @Test
+ public void checkMapLeafs() throws Exception {
+ File file = new File("TestFile2.bin");
+ if (file.exists()) {
+ try {
+ java.nio.file.Files.delete(file.toPath());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+ assertTrue(file.createNewFile());
+ assertTrue(file.setWritable(true, false)); //writable=true, ownerOnly=false
+ assertTrue(file.isFile());
+ file.deleteOnExit(); //comment out if you want to examine the file.
+
+ final long bytes = 128;
+
+ try (WritableMapHandle h = WritableMemory.writableMap(file, 0L, bytes,
ByteOrder.nativeOrder())) {
+ WritableMemory mem = h.getWritable(); //native mem
+ assertTrue(((BaseStateImpl)mem).isMapType());
+ assertFalse(mem.isReadOnly());
+ checkCrossLeafTypeIds(mem);
+ Memory nnreg = mem.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+
+ Memory reg = mem.region(0, bytes, ByteOrder.nativeOrder());
+ Buffer buf = reg.asBuffer().region(0, bytes, ByteOrder.nativeOrder());
+ Buffer buf4 = buf.duplicate();
+
+ Memory reg2 = nnreg.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf2 = reg2.asBuffer().region(0, bytes,
Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf3 = buf2.duplicate();
+
+ assertTrue(((BaseStateImpl)reg).isRegionType());
+ assertTrue(((BaseStateImpl)reg2).isRegionType());
+ assertTrue(((BaseStateImpl)buf).isRegionType());
+ assertTrue(((BaseStateImpl)buf2).isRegionType());
+ assertTrue(((BaseStateImpl)buf3).isDuplicateType());
+ assertTrue(((BaseStateImpl)buf4).isDuplicateType());
+ }
+ }
+
+ @Test
+ public void checkHeapLeafs() {
+ int bytes = 128;
+ Memory mem = Memory.wrap(new byte[bytes]);
+ assertTrue(((BaseStateImpl)mem).isHeapType());
+ assertTrue(((BaseStateImpl)mem).isReadOnlyType());
+ checkCrossLeafTypeIds(mem);
+ Memory nnreg = mem.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+
+ Memory reg = mem.region(0, bytes, ByteOrder.nativeOrder());
+ Buffer buf = reg.asBuffer().region(0, bytes, ByteOrder.nativeOrder());
+ Buffer buf4 = buf.duplicate();
+
+ Memory reg2 = nnreg.region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf2 = reg2.asBuffer().region(0, bytes, Util.NON_NATIVE_BYTE_ORDER);
+ Buffer buf3 = buf2.duplicate();
+
+ assertFalse(((BaseStateImpl)mem).isRegionType());
+ assertTrue(((BaseStateImpl)reg2).isRegionType());
+ assertTrue(((BaseStateImpl)buf).isRegionType());
+ assertTrue(((BaseStateImpl)buf2).isRegionType());
+ assertTrue(((BaseStateImpl)buf3).isDuplicateType());
+ assertTrue(((BaseStateImpl)buf4).isDuplicateType());
+ }
+
+ private static void checkCrossLeafTypeIds(Memory mem) {
+ Memory reg1 = mem.region(0, mem.getCapacity());
+ assertTrue(((BaseStateImpl)reg1).isRegionType());
+
+ Buffer buf1 = reg1.asBuffer();
+ assertTrue(((BaseStateImpl)buf1).isRegionType());
+ assertTrue(((BaseStateImpl)buf1).isBufferType());
+ assertTrue(buf1.isReadOnly());
+
+ Buffer buf2 = buf1.duplicate();
+ assertTrue(((BaseStateImpl)buf2).isRegionType());
+ assertTrue(((BaseStateImpl)buf2).isBufferType());
+ assertTrue(((BaseStateImpl)buf2).isDuplicateType());
+ assertTrue(buf2.isReadOnly());
+
+ Memory mem2 = buf1.asMemory(); //
+ assertTrue(((BaseStateImpl)mem2).isRegionType());
+ assertFalse(((BaseStateImpl)mem2).isBufferType());
+ assertFalse(((BaseStateImpl)mem2).isDuplicateType());
+ assertTrue(mem2.isReadOnly());
+
+ Buffer buf3 = buf1.duplicate(Util.NON_NATIVE_BYTE_ORDER);
+ assertTrue(((BaseStateImpl)buf3).isRegionType());
+ assertTrue(((BaseStateImpl)buf3).isBufferType());
+ assertTrue(((BaseStateImpl)buf3).isDuplicateType());
+ assertTrue(((BaseStateImpl)buf3).isNonNativeType());
+ assertTrue(buf3.isReadOnly());
+
+ Memory mem3 = buf3.asMemory();
+ assertTrue(((BaseStateImpl)mem3).isRegionType());
+ assertFalse(((BaseStateImpl)mem3).isBufferType());
+ assertTrue(((BaseStateImpl)mem3).isDuplicateType());
+ assertTrue(((BaseStateImpl)mem3).isNonNativeType());
+ assertTrue(mem3.isReadOnly());
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/SpecificLeafTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UnsafeUtilTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UnsafeUtilTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UnsafeUtilTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.testng.annotations.Test;
+
+
+/**
+ * @author Lee Rhodes
+ */
+public class UnsafeUtilTest {
+ long testField = 1; //Do not remove & cannot be static. Used in reflection
check.
+
+
+ @Test
+ public void checkJdkString() {
+ String jdkVer;
+ int[] p = new int[2];
+ String[] good1_Strings = {"1.8.0_121", "8", "9", "10", "11", "12", "13"};
+ int len = good1_Strings.length;
+ for (int i = 0; i < len; i++) {
+ jdkVer = good1_Strings[i];
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
+ int jdkMajor = (p[0] == 1) ? p[1] : p[0]; //model the actual JDK_MAJOR
+ if (p[0] == 1) { assertTrue(jdkMajor == p[1]); }
+ if (p[0] > 1 ) { assertTrue(jdkMajor == p[0]); }
+ }
+ try {
+ jdkVer = "14.0.4"; //ver 14 string
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
+ fail();
+ } catch (IllegalArgumentException e) {
+ println("" + e);
+ }
+
+ try {
+ jdkVer = "1.7.0_80"; //1.7 string
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]);
+ fail();
+ } catch (IllegalArgumentException e) {
+ println("" + e);
+ }
+ try {
+ jdkVer = "1.6.0_65"; //valid string but < 1.7
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
+ fail();
+ } catch (IllegalArgumentException e) {
+ println("" + e);
+ }
+ try {
+ jdkVer = "b"; //invalid string
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
+ fail();
+ } catch (IllegalArgumentException e) {
+ println("" + e);
+ }
+ try {
+ jdkVer = ""; //invalid string
+ p = UnsafeUtil.parseJavaVersion(jdkVer);
+ UnsafeUtil.checkJavaVersion(jdkVer, p[0], p[1]); //throws
+ fail();
+ } catch (IllegalArgumentException e) {
+ println("" + e);
+ }
+ }
+
+ @Test
+ public void checkFieldOffset() {
+ assertEquals(testField, 1);
+ long offset = UnsafeUtil.getFieldOffset(this.getClass(), "testField");
+ assertEquals(offset, 16);
+ try {
+ offset = UnsafeUtil.getFieldOffset(this.getClass(), "testField2");
+ fail();
+ } catch (IllegalStateException e) {
+ //OK
+ }
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void checkInts() {
+ Ints.checkedCast(1L << 32);
+ }
+
+ @SuppressWarnings("restriction")
+ @Test
+ public void checkArrayBaseOffset()
+ {
+ final List<Class<?>> classes = new ArrayList<>();
+ classes.add(byte[].class);
+ classes.add(int[].class);
+ classes.add(long[].class);
+ classes.add(float[].class);
+ classes.add(double[].class);
+ classes.add(boolean[].class);
+ classes.add(short[].class);
+ classes.add(char[].class);
+ classes.add(Object[].class);
+ classes.add(byte[][].class); // An array type that is not cached
+
+ for (Class<?> clazz : classes) {
+ assertEquals(
+ UnsafeUtil.getArrayBaseOffset(clazz),
+ UnsafeUtil.unsafe.arrayBaseOffset(clazz),
+ clazz.getTypeName()
+ );
+ }
+ }
+
+ @Test
+ public void printlnTest() {
+ println("PRINTING: "+this.getClass().getName());
+ }
+
+ /**
+ * @param s String to print
+ */
+ static void println(final String s) {
+ //System.out.println(s);
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UnsafeUtilTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Utf8Test.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Utf8Test.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Utf8Test.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,516 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.fail;
+
+import java.io.IOException;
+import java.nio.CharBuffer;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.Utf8CodingException;
+import org.apache.datasketches.memory.WritableMemory;
+import org.apache.datasketches.memory.internal.Util.RandomCodePoints;
+import org.testng.annotations.Test;
+
+import com.google.protobuf.ByteString;
+
+/**
+ * Adapted version of
+ *
https://github.com/protocolbuffers/protobuf/blob/master/java/core/src/test/java/com/google/protobuf/DecodeUtf8Test.java
+ *
+ * Copyright 2008 Google Inc. All rights reserved.
+ * https://developers.google.com/protocol-buffers/
+ * See LICENSE.
+ */
+public class Utf8Test {
+
+ @Test
+ public void testRoundTripAllValidCodePoints() throws IOException { //the
non-surrogate code pts
+ for (int cp = Character.MIN_CODE_POINT; cp < Character.MAX_CODE_POINT;
cp++) {
+ if (!isSurrogateCodePoint(cp)) {
+ String refStr = new String(Character.toChars(cp));
+ assertRoundTrips(refStr);
+ }
+ }
+ }
+
+ @Test
+ public void testPutInvalidChars() { //The surrogates must be a pair, thus
invalid alone
+ WritableMemory mem = WritableMemory.allocate(10);
+ WritableMemory emptyMem = WritableMemory.allocate(0);
+ for (int c = Character.MIN_SURROGATE; c <= Character.MAX_SURROGATE; c++) {
+ assertSurrogate(mem, (char) c);
+ assertSurrogate(emptyMem, (char) c);
+ }
+ }
+
+ private static void assertSurrogate(WritableMemory mem, char c) {
+ try {
+ mem.putCharsToUtf8(0, new String(new char[] {c}));
+ fail();
+ } catch (Utf8CodingException e) {
+ // Expected.
+ }
+ }
+
+ @Test
+ public void testPutInvaidSurrogatePairs() {
+ WritableMemory mem = WritableMemory.allocate(4);
+ StringBuilder sb = new StringBuilder();
+ sb.append(Character.MIN_HIGH_SURROGATE);
+ sb.append(Character.MAX_HIGH_SURROGATE);
+ try {
+ mem.putCharsToUtf8(0, sb);
+ } catch (Utf8CodingException e) {
+ //Expected;
+ }
+ }
+
+ @Test
+ public void testPutHighBMP() {
+ WritableMemory mem = WritableMemory.allocate(2);
+ StringBuilder sb = new StringBuilder();
+ sb.append("\uE000");
+ try {
+ mem.putCharsToUtf8(0, sb);
+ } catch (Utf8CodingException e) {
+ //Expected;
+ }
+ }
+
+ @Test
+ public void testPutExtendedAscii() {
+ WritableMemory mem = WritableMemory.allocate(1);
+ StringBuilder sb = new StringBuilder();
+ sb.append("\u07FF");
+ try {
+ mem.putCharsToUtf8(0, sb);
+ } catch (Utf8CodingException e) {
+ //Expected;
+ }
+ }
+
+ @Test
+ public void testPutOneAsciiToEmpty() {
+ WritableMemory mem = WritableMemory.allocate(0);
+ StringBuilder sb = new StringBuilder();
+ sb.append("a");
+ try {
+ mem.putCharsToUtf8(0, sb);
+ } catch (Utf8CodingException e) {
+ //Expected;
+ }
+ }
+
+ @Test
+ public void testPutValidSurrogatePair() {
+ WritableMemory mem = WritableMemory.allocate(4);
+ StringBuilder sb = new StringBuilder();
+ sb.append(Character.MIN_HIGH_SURROGATE);
+ sb.append(Character.MIN_LOW_SURROGATE);
+ mem.putCharsToUtf8(0, sb);
+ }
+
+ // Test all 1, 2, 3 invalid byte combinations. Valid ones would have been
covered above.
+
+ @Test
+ public void testOneByte() {
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ ByteString bs = ByteString.copyFrom(new byte[] {(byte) i });
+ if (!bs.isValidUtf8()) { //from -128 to -1
+ assertInvalid(bs.toByteArray());
+ } else {
+ valid++; //from 0 to 127
+ }
+ }
+ assertEquals(IsValidUtf8TestUtil.EXPECTED_ONE_BYTE_ROUNDTRIPPABLE_COUNT,
valid);
+ }
+
+ @Test
+ public void testTwoBytes() {
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
+ ByteString bs = ByteString.copyFrom(new byte[]{(byte) i, (byte) j});
+ if (!bs.isValidUtf8()) {
+ assertInvalid(bs.toByteArray());
+ } else {
+ valid++;
+ }
+ }
+ }
+ assertEquals(IsValidUtf8TestUtil.EXPECTED_TWO_BYTE_ROUNDTRIPPABLE_COUNT,
valid);
+ }
+
+ //@Test
+ //This test is very long, and doesn't cover the 4-byte combinations.
+ // This is replaced by the test following which does cover some 4-byte
combinations.
+ public void testThreeBytes() {
+ // Travis' OOM killer doesn't like this test
+ if (System.getenv("TRAVIS") == null) {
+ int count = 0;
+ int valid = 0;
+ for (int i = Byte.MIN_VALUE; i <= Byte.MAX_VALUE; i++) {
+ for (int j = Byte.MIN_VALUE; j <= Byte.MAX_VALUE; j++) {
+ for (int k = Byte.MIN_VALUE; k <= Byte.MAX_VALUE; k++) {
+ byte[] bytes = new byte[]{(byte) i, (byte) j, (byte) k};
+ ByteString bs = ByteString.copyFrom(bytes);
+ if (!bs.isValidUtf8()) {
+ assertInvalid(bytes);
+ } else {
+ valid++;
+ }
+ count++;
+ if ((count % 1000000L) == 0) {
+ println("Processed " + (count / 1000000L) + " million
characters");
+ }
+ }
+ }
+ }
+
assertEquals(IsValidUtf8TestUtil.EXPECTED_THREE_BYTE_ROUNDTRIPPABLE_COUNT,
valid);
+ }
+ }
+
+ /* These code points can be used by the following test to customize
different regions of the
+ * Code Point space. This randomized test can replace the exhaustive
+ * combinatorially explosive previous test, which doesn't cover the 4 byte
combinations.
+ */
+ static final int min1ByteCP = 0; //ASCII
+ static final int min2ByteCP = 0X000080;
+ static final int min3ByteCP = 0X000800;
+ static final int min4ByteCP = Character.MIN_SUPPLEMENTARY_CODE_POINT;
//0X010000;
+ static final int minPlane2CP = 0X020000;
+ static final int maxCodePoint = Character.MAX_CODE_POINT;
//0X10FFFF
+ static final int minSurr = Character.MIN_SURROGATE;
//0X00D800;
+ static final int maxSurr = Character.MAX_SURROGATE;
//0X00E000;
+
+ @Test
+ //randomly selects CP from a range that include 1, 2, 3 and 4 byte encodings.
+ // with 50% coming from plane 0 and 50% coming from plane 1.
+ public void checkRandomValidCodePoints() {
+ RandomCodePoints rcp = new RandomCodePoints(true);
+ int numCP = 1000;
+ int[] cpArr = new int[numCP];
+ rcp.fillCodePointArray(cpArr, 0, minPlane2CP);
+ String rcpStr = new String(cpArr, 0, numCP);
+ //println(rcpStr);
+ WritableMemory wmem = WritableMemory.allocate(4 * numCP);
+ int utf8Bytes = (int) wmem.putCharsToUtf8(0, rcpStr);
+
+ StringBuilder sb = new StringBuilder();
+ try {
+ wmem.getCharsFromUtf8(0L, utf8Bytes, (Appendable) sb);
+ } catch (IOException | Utf8CodingException e) {
+ throw new RuntimeException(e);
+ }
+ checkStrings(sb.toString(), rcpStr);
+
+ CharBuffer cb = CharBuffer.allocate(rcpStr.length());
+ try {
+ wmem.getCharsFromUtf8(0L, utf8Bytes, cb);
+ } catch (IOException | Utf8CodingException e) {
+ throw new RuntimeException(e);
+ }
+ String cbStr = sb.toString();
+ assertEquals(cbStr.length(), rcpStr.length());
+ checkStrings(cbStr, rcpStr);
+ }
+
+ @Test
+ public void checkRandomValidCodePoints2() {
+ //checks the non-deterministic constructor
+ @SuppressWarnings("unused")
+ RandomCodePoints rcp = new RandomCodePoints(false);
+ }
+
+
+ /**
+ * Tests that round tripping of a sample of four byte permutations work.
+ */
+ @Test
+ public void testInvalid_4BytesSamples() {
+ // Bad trailing bytes
+ assertInvalid(0xF0, 0xA4, 0xAD, 0x7F);
+ assertInvalid(0xF0, 0xA4, 0xAD, 0xC0);
+
+ // Special cases for byte2
+ assertInvalid(0xF0, 0x8F, 0xAD, 0xA2);
+ assertInvalid(0xF4, 0x90, 0xAD, 0xA2);
+ }
+
+ @Test
+ public void testRealStrings() throws IOException {
+ // English
+ assertRoundTrips("The quick brown fox jumps over the lazy dog");
+ // German
+ assertRoundTrips("Quizdeltagerne spiste jordb\u00e6r med fl\u00f8de, mens
cirkusklovnen");
+ // Japanese
+ assertRoundTrips(
+
"\u3044\u308d\u306f\u306b\u307b\u3078\u3068\u3061\u308a\u306c\u308b\u3092");
+ // Hebrew
+ assertRoundTrips(
+ "\u05d3\u05d2 \u05e1\u05e7\u05e8\u05df \u05e9\u05d8 \u05d1\u05d9\u05dd
"
+ + "\u05de\u05d0\u05d5\u05db\u05d6\u05d1 \u05d5\u05dc\u05e4\u05ea\u05e2"
+ + " \u05de\u05e6\u05d0 \u05dc\u05d5 \u05d7\u05d1\u05e8\u05d4 "
+ + "\u05d0\u05d9\u05da \u05d4\u05e7\u05dc\u05d9\u05d8\u05d4");
+ // Thai
+ assertRoundTrips(
+ " \u0e08\u0e07\u0e1d\u0e48\u0e32\u0e1f\u0e31\u0e19\u0e1e\u0e31\u0e12"
+ + "\u0e19\u0e32\u0e27\u0e34\u0e0a\u0e32\u0e01\u0e32\u0e23");
+ // Chinese
+ assertRoundTrips(
+
"\u8fd4\u56de\u94fe\u4e2d\u7684\u4e0b\u4e00\u4e2a\u4ee3\u7406\u9879\u9009\u62e9\u5668");
+ // Chinese with 4-byte chars
+
assertRoundTrips("\uD841\uDF0E\uD841\uDF31\uD841\uDF79\uD843\uDC53\uD843\uDC78"
+ +
"\uD843\uDC96\uD843\uDCCF\uD843\uDCD5\uD843\uDD15\uD843\uDD7C\uD843\uDD7F"
+ +
"\uD843\uDE0E\uD843\uDE0F\uD843\uDE77\uD843\uDE9D\uD843\uDEA2");
+ // Mixed
+ assertRoundTrips(
+ "The quick brown
\u3044\u308d\u306f\u306b\u307b\u3078\u8fd4\u56de\u94fe"
+ + "\u4e2d\u7684\u4e0b\u4e00");
+ }
+
+ @Test
+ public void checkNonEmptyDestinationForDecode() {
+ StringBuilder sb = new StringBuilder();
+ sb.append("abc"); //current contents of destination
+ int startChars = sb.toString().toCharArray().length;
+ String refStr = "Quizdeltagerne spiste jordb\u00e6r med fl\u00f8de, mens
cirkusklovnen";
+ byte[] refByteArr = refStr.getBytes(UTF_8);
+ int addBytes = refByteArr.length;
+ WritableMemory refMem = WritableMemory.writableWrap(refByteArr);
+ int decodedChars = refMem.getCharsFromUtf8(0, addBytes, sb);
+ String finalStr = sb.toString();
+ int finalChars = finalStr.toCharArray().length;
+ assertEquals(decodedChars + startChars, finalChars);
+ println("Decoded chars: " + decodedChars);
+ println("Final chars: " + finalChars);
+ println(sb.toString());
+ }
+
+ @Test
+ public void checkNonEmptyDestinationForEncode() {
+ String refStr = "Quizdeltagerne spiste jordb\u00e6r med fl\u00f8de, mens
cirkusklovnen";
+ byte[] refByteArr = refStr.getBytes(UTF_8);
+ int refBytes = refByteArr.length;
+ int offset = 100;
+ WritableMemory tgtMem = WritableMemory.allocate(refBytes + offset);
+ long bytesEncoded = tgtMem.putCharsToUtf8(offset, refStr);
+ assertEquals(bytesEncoded, refBytes);
+ }
+
+ @Test
+ public void testOverlong() {
+ assertInvalid(0xc0, 0xaf);
+ assertInvalid(0xe0, 0x80, 0xaf);
+ assertInvalid(0xf0, 0x80, 0x80, 0xaf);
+
+ // Max overlong
+ assertInvalid(0xc1, 0xbf);
+ assertInvalid(0xe0, 0x9f, 0xbf);
+ assertInvalid(0xf0 ,0x8f, 0xbf, 0xbf);
+
+ // null overlong
+ assertInvalid(0xc0, 0x80);
+ assertInvalid(0xe0, 0x80, 0x80);
+ assertInvalid(0xf0, 0x80, 0x80, 0x80);
+ }
+
+ @Test
+ public void testIllegalCodepoints() {
+ // Single surrogate
+ assertInvalid(0xed, 0xa0, 0x80);
+ assertInvalid(0xed, 0xad, 0xbf);
+ assertInvalid(0xed, 0xae, 0x80);
+ assertInvalid(0xed, 0xaf, 0xbf);
+ assertInvalid(0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xbe, 0x80);
+ assertInvalid(0xed, 0xbf, 0xbf);
+
+ // Paired surrogates
+ assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xae, 0x80, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf);
+ assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80);
+ assertInvalid(0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf);
+ }
+
+ @Test
+ public void testBufferSlice() throws IOException {
+ String str = "The quick brown fox jumps over the lazy dog";
+ assertRoundTrips(str, 4, 10, 4);
+ assertRoundTrips(str, 0, str.length(), 0);
+ }
+
+ @Test
+ public void testInvalidBufferSlice() { //these are pure Memory bounds
violations
+ byte[] bytes = "The quick brown fox jumps over the lazy
dog".getBytes(UTF_8);
+ assertInvalidSlice(bytes, bytes.length - 3, 4);
+ assertInvalidSlice(bytes, bytes.length, 1);
+ assertInvalidSlice(bytes, bytes.length + 1, 0);
+ assertInvalidSlice(bytes, 0, bytes.length + 1);
+ }
+
+ private static void assertInvalid(int... bytesAsInt) { //invalid byte
sequences
+ byte[] bytes = new byte[bytesAsInt.length];
+ for (int i = 0; i < bytesAsInt.length; i++) {
+ bytes[i] = (byte) bytesAsInt[i];
+ }
+ assertInvalid(bytes);
+ }
+
+ private static void assertInvalid(byte[] bytes) {
+ int bytesLen = bytes.length;
+ try {
+ Memory.wrap(bytes).getCharsFromUtf8(0, bytesLen, new StringBuilder());
+ fail();
+ } catch (Utf8CodingException e) {
+ // Expected.
+ }
+ try {
+ CharBuffer cb = CharBuffer.allocate(bytesLen);
+ Memory.wrap(bytes).getCharsFromUtf8(0, bytesLen, cb);
+ fail();
+ } catch (Utf8CodingException | IOException e) {
+ // Expected.
+ }
+ }
+
+ private static void assertInvalidSlice(byte[] bytes, int index, int size) {
+ try {
+ Memory mem = Memory.wrap(bytes);
+ mem.getCharsFromUtf8(index, size, new StringBuilder());
+ fail();
+ } catch (IllegalArgumentException e) { //Pure bounds violation
+ // Expected.
+ }
+ }
+
+ /**
+ * Performs round-trip test using the given reference string
+ * @param refStr the reference string
+ * @throws IOException
+ */
+ private static void assertRoundTrips(String refStr) throws IOException {
+ assertRoundTrips(refStr, refStr.toCharArray().length, 0, -1);
+ }
+
+ /**
+ * Performs round-trip test using the given reference string
+ * @param refStr the reference string
+ * @param refSubCharLen the number of characters expected to be decoded
+ * @param offsetBytes starting utf8 byte offset
+ * @param utf8LengthBytes length of utf8 bytes
+ * @throws IOException
+ */
+ private static void assertRoundTrips(String refStr, int refSubCharLen, int
offsetBytes,
+ int utf8LengthBytes) throws IOException {
+ byte[] refByteArr = refStr.getBytes(UTF_8);
+ if (utf8LengthBytes == -1) {
+ utf8LengthBytes = refByteArr.length;
+ }
+ Memory refMem = Memory.wrap(refByteArr);
+
+ byte[] refByteArr2 = new byte[refByteArr.length + 1];
+ System.arraycopy(refByteArr, 0, refByteArr2, 1, refByteArr.length);
+ Memory refReg = Memory.wrap(refByteArr2).region(1, refByteArr.length);
+
+ WritableMemory dstMem = WritableMemory.allocate(refByteArr.length);
+ WritableMemory dstMem2 =
+ WritableMemory.allocate(refByteArr.length + 1).writableRegion(1,
refByteArr.length);
+
+ // Test with Memory objects, where base offset != 0
+ assertRoundTrips(refStr, refSubCharLen, offsetBytes, utf8LengthBytes,
refByteArr, refMem, dstMem);
+ assertRoundTrips(refStr, refSubCharLen, offsetBytes, utf8LengthBytes,
refByteArr, refMem, dstMem2);
+ assertRoundTrips(refStr, refSubCharLen, offsetBytes, utf8LengthBytes,
refByteArr, refReg, dstMem);
+ assertRoundTrips(refStr, refSubCharLen, offsetBytes, utf8LengthBytes,
refByteArr, refReg, dstMem2);
+ }
+
+ private static void assertRoundTrips(String refStr, int refSubCharLen, int
offsetBytes,
+ int utf8LengthBytes, byte[] refByteArr, Memory refMem, WritableMemory
dstMem)
+ throws IOException {
+ StringBuilder sb = new StringBuilder();
+
+ int charPos = refMem.getCharsFromUtf8(offsetBytes, utf8LengthBytes, sb);
+ checkStrings(sb.toString(), new String(refByteArr, offsetBytes,
utf8LengthBytes, UTF_8));
+ assertEquals(charPos, refSubCharLen);
+
+ CharBuffer cb = CharBuffer.allocate(refByteArr.length + 1);
+ cb.position(1);
+ // Make CharBuffer 1-based, to check correct offset handling
+ cb = cb.slice();
+ refMem.getCharsFromUtf8(offsetBytes, utf8LengthBytes, cb);
+ cb.flip();
+ checkStrings(cb.toString(), new String(refByteArr, offsetBytes,
utf8LengthBytes, UTF_8));
+
+ long encodedUtf8Bytes = dstMem.putCharsToUtf8(0, refStr); //encodes entire
refStr
+ assertEquals(encodedUtf8Bytes, refByteArr.length); //compares bytes length
+ //compare the actual bytes encoded
+ assertEquals(0, dstMem.compareTo(0, refByteArr.length, refMem, 0,
refByteArr.length));
+
+ // Test write overflow
+ WritableMemory writeMem2 = WritableMemory.allocate(refByteArr.length - 1);
+ try {
+ writeMem2.putCharsToUtf8(0, refStr);
+ fail();
+ } catch (Utf8CodingException e) {
+ // Expected.
+ }
+ }
+
+ private static boolean isSurrogateCodePoint(final int cp) {
+ return (cp >= Character.MIN_SURROGATE) && (cp <= Character.MAX_SURROGATE);
+ }
+
+ private static void checkStrings(String actual, String expected) {
+ if (!expected.equals(actual)) {
+ fail("Failure: Expected (" + codepoints(expected) + ") Actual (" +
codepoints(actual) + ")");
+ }
+ }
+
+ private static List<String> codepoints(String str) {
+ List<String> codepoints = new ArrayList<>();
+ for (int i = 0; i < str.length(); i++) {
+ codepoints.add(Long.toHexString(str.charAt(i)));
+ }
+ return codepoints;
+ }
+
+ @Test
+ public void printlnTest() {
+ println("PRINTING: "+this.getClass().getName());
+ }
+
+ /**
+ * @param s value to print
+ */
+ static void println(String s) {
+ //System.out.println(s); //disable here
+ }
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/Utf8Test.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * Note: Lincoln's Gettysburg Address is in the public domain. See LICENSE.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import static org.apache.datasketches.memory.internal.Util.characterPad;
+import static org.apache.datasketches.memory.internal.Util.getResourceBytes;
+import static org.apache.datasketches.memory.internal.Util.getResourceFile;
+import static org.apache.datasketches.memory.internal.Util.negativeCheck;
+import static org.apache.datasketches.memory.internal.Util.nullCheck;
+import static org.apache.datasketches.memory.internal.Util.zeroCheck;
+import static org.apache.datasketches.memory.internal.Util.zeroPad;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertTrue;
+import static org.testng.Assert.fail;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.LinkOption;
+import java.nio.file.attribute.PosixFileAttributeView;
+import java.nio.file.attribute.PosixFileAttributes;
+import java.nio.file.attribute.PosixFilePermissions;
+
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class UtilTest {
+ private static final String LS = System.getProperty("line.separator");
+
+ //Binary Search
+ @Test
+ public void checkBinarySearch() {
+ int k = 1024; //longs
+ WritableMemory wMem = WritableMemory.allocate(k << 3); //1024 longs
+ for (int i = 0; i < k; i++) { wMem.putLong(i << 3, i); }
+ long idx = Util.binarySearchLongs(wMem, 0, k - 1, k / 2);
+ long val = wMem.getLong(idx << 3);
+ assertEquals(idx, k/2);
+ assertEquals(val, k/2);
+
+ idx = Util.binarySearchLongs(wMem, 0, k - 1, k);
+ assertEquals(idx, -1024);
+ }
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void checkBoundsTest() {
+ UnsafeUtil.checkBounds(999, 2, 1000);
+ }
+
+ @Test
+ public void checkPadding() {
+ String s = "123";
+ String t = zeroPad(s, 4);
+ assertTrue(t.startsWith("0"));
+
+ t = characterPad(s, 4, '0', true);
+ assertTrue(t.endsWith("0"));
+
+ t = characterPad(s, 3, '0', false);
+ assertEquals(s, t);
+ }
+
+ @Test
+ public void checkNullZeroNegativeChecks() {
+ Object obj = null;
+ try {
+ nullCheck(obj, "Test Object");
+ fail();
+ } catch (IllegalArgumentException e) {
+ //OK
+ }
+ try {
+ zeroCheck(0, "Test Long");
+ fail();
+ } catch (IllegalArgumentException e) {
+ //OK
+ }
+ try {
+ negativeCheck(-1L, "Test Long");
+ fail();
+ } catch (IllegalArgumentException e) {
+ //OK
+ }
+ }
+
+ @Test
+ public void checkCodePointArr() {
+ final Util.RandomCodePoints rvcp = new Util.RandomCodePoints(true);
+ final int n = 1000;
+ final int[] cpArr = new int[n];
+ rvcp.fillCodePointArray(cpArr);
+ for (int i = 0; i < n; i++) {
+ int cp = cpArr[i];
+ if ((cp >= Character.MIN_SURROGATE) && (cp <= Character.MAX_SURROGATE)) {
+ fail();
+ }
+ }
+ }
+
+ @Test
+ public void checkCodePoint() {
+ final Util.RandomCodePoints rvcp = new Util.RandomCodePoints(true);
+ final int n = 1000;
+ for (int i = 0; i < n; i++) {
+ int cp = rvcp.getCodePoint();
+ if ((cp >= Character.MIN_SURROGATE) && (cp <= Character.MAX_SURROGATE)) {
+ fail();
+ }
+ }
+ }
+
+ static final String getFileAttributes(File file) {
+ try {
+ PosixFileAttributes attrs = Files.getFileAttributeView(
+ file.toPath(), PosixFileAttributeView.class, new
LinkOption[0]).readAttributes();
+ String s = String.format("%s: %s %s %s%n",
+ file.getPath(),
+ attrs.owner().getName(),
+ attrs.group().getName(),
+ PosixFilePermissions.toString(attrs.permissions()));
+ return s;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ static final void setGettysburgAddressFileToReadOnly() {
+ File file = getResourceFile("GettysburgAddress.txt");
+ try {
+ Files.setPosixFilePermissions(file.toPath(),
PosixFilePermissions.fromString("r--r--r--"));
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ //Resources
+
+ @Test
+ public void resourceFileExits() {
+ final String shortFileName = "GettysburgAddress.txt";
+ final File file = getResourceFile(shortFileName);
+ assertTrue(file.exists());
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void resourceFileNotFound() {
+ final String shortFileName = "GettysburgAddress.txt";
+ getResourceFile(shortFileName + "123");
+ }
+
+ @Test
+ public void resourceBytesCorrect() {
+ final String shortFileName = "GettysburgAddress.txt";
+ final byte[] bytes = getResourceBytes(shortFileName);
+ assertTrue(bytes.length == 1541);
+ }
+
+ @Test(expectedExceptions = NullPointerException.class)
+ public void resourceBytesFileNotFound() {
+ final String shortFileName = "GettysburgAddress.txt";
+ getResourceBytes(shortFileName + "123");
+ }
+
+ @Test
+ public void printlnTest() {
+ println("PRINTING: "+this.getClass().getName());
+ }
+
+ static void println(final Object o) {
+ if (o == null) { print(LS); }
+ else { print(o.toString() + LS); }
+ }
+
+ /**
+ * @param o value to print
+ */
+ static void print(final Object o) {
+ if (o != null) {
+ //System.out.print(o.toString()); //disable here
+ }
+ }
+
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/UtilTest.java
------------------------------------------------------------------------------
svn:executable = *
Added:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
==============================================================================
---
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
(added)
+++
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.datasketches.memory.internal;
+
+import org.apache.datasketches.memory.internal.VirtualMachineMemory;
+import org.testng.annotations.Test;
+
+@SuppressWarnings({"unused"})
+public class VirtualMachineMemoryTest {
+
+ @Test
+ public void maxDirectBufferMemory() {
+ assert(VirtualMachineMemory.getMaxDBBMemory() >= 0);
+ }
+
+ @Test
+ public void inertPageAlignment() {
+ boolean result = VirtualMachineMemory.getIsPageAligned();
+ //System.out.println("VM page alignment:" + result);
+ assert(true); //no exception was thrown
+ }
+}
Propchange:
dev/datasketches/memory/2.2.0-RC1/apache-datasketches-memory-2.2.0-src/datasketches-memory-java8/src/test/java/org/apache/datasketches/memory/internal/VirtualMachineMemoryTest.java
------------------------------------------------------------------------------
svn:executable = *
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]