This is an automated email from the ASF dual-hosted git repository. leerho pushed a commit to branch 6.0.X_cherrypick_tgt2 in repository https://gitbox.apache.org/repos/asf/datasketches-memory.git
commit 45b1a5b88b138da0812f5cd4d4495046c93bef91 Author: Lee Rhodes <[email protected]> AuthorDate: Mon Feb 3 12:28:42 2025 -0800 Better test coverage AND actual checking for the @pawel-weijacha bug for all the primitives. --- .../internal/NativeWritableBufferImplTest.java | 315 +++++++++------------ .../internal/NativeWritableMemoryImplTest.java | 303 ++++++++------------ .../internal/NonNativeWritableBufferImplTest.java | 132 ++++----- .../internal/NonNativeWritableMemoryImplTest.java | 101 +++---- 4 files changed, 354 insertions(+), 497 deletions(-) diff --git a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java index c1fbc36a..b72f0da5 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableBufferImplTest.java @@ -20,7 +20,14 @@ package org.apache.datasketches.memory.internal; import java.lang.foreign.Arena; -import static org.apache.datasketches.memory.internal.ResourceImpl.NON_NATIVE_BYTE_ORDER; +import static org.apache.datasketches.memory.internal.UtilForTest.CB; +import static org.apache.datasketches.memory.internal.UtilForTest.DB; +import static org.apache.datasketches.memory.internal.UtilForTest.FB; +import static org.apache.datasketches.memory.internal.UtilForTest.IB; +import static org.apache.datasketches.memory.internal.UtilForTest.LB; +import static org.apache.datasketches.memory.internal.UtilForTest.NBO; +import static org.apache.datasketches.memory.internal.UtilForTest.NNBO; +import static org.apache.datasketches.memory.internal.UtilForTest.SB; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertNotNull; @@ -38,6 +45,9 @@ import org.apache.datasketches.memory.WritableMemory; import org.testng.Assert; import org.testng.annotations.Test; +/** + * @author Lee Rhodes + */ public class NativeWritableBufferImplTest { private static final MemoryRequestServer memReqSvr = Resource.defaultMemReqSvr; @@ -55,239 +65,170 @@ public class NativeWritableBufferImplTest { try { wmem.getArena().close(); } catch (IllegalStateException e) { } } - //Simple Heap arrays + //Check primitives @Test - public void checkGetByteArray() { - byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetBooleans() { + boolean[] srcArray = {true, false, true, false, false, true, false, true}; final int len = srcArray.length; final int half = len / 2; - byte[] dstArray = new byte[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getByteArray(dstArray, 0, half); - buf.getByteArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getByteArray(dstArray, 0, half); - wbuf.getByteArray(dstArray, half, half); - assertEquals(dstArray, srcArray); + WritableBuffer wbuf = WritableMemory.allocate(len, NBO).asWritableBuffer(); + //put + for (int i = 0; i < half; i++) { wbuf.putBoolean(srcArray[i]); } //put*(value) + for (int i = half; i < len; i++) { wbuf.putBoolean(i, srcArray[i]); } //put*(add, value) + wbuf.resetPosition(); + //get + boolean[] dstArray = new boolean[len]; + for (int i = 0; i < half; i++) { dstArray[i] = wbuf.getBoolean(); } //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getBoolean(i); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutByteArray() { + public void checkPutGetBytes() { byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; final int len = srcArray.length; final int half = len / 2; - byte[] dstArray = new byte[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Byte.BYTES).asWritableBuffer(); - wbuf.putByteArray(srcArray, 0, half); - wbuf.putByteArray(srcArray, half, half); + WritableBuffer wbuf = WritableMemory.allocate(len, NBO).asWritableBuffer(); + //put + wbuf.putByte(srcArray[0]); //put*(value) + wbuf.putByteArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putByte(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putByte(i, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); - wbuf.getByteArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); + //get + byte[] dstArray = new byte[len]; + dstArray[0] = wbuf.getByte(); //get*() + wbuf.getByteArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getByte(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getByte(i); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkGetCharArray() { - char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + public void checkPutGetNativeCharacters() { + char[] srcArray = { 'a','b','c','d','e','f','g','h' }; final int len = srcArray.length; final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * CB, NBO).asWritableBuffer(); + //put + wbuf.putChar(srcArray[0]); //put*(value) + wbuf.putCharArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putChar(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putChar(i * CB, srcArray[i]); } //put*(add, value) + wbuf.resetPosition(); + //get char[] dstArray = new char[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getCharArray(dstArray, 0, half); - buf.getCharArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getCharArray(dstArray, 0, half); - wbuf.getCharArray(dstArray, half, half); - assertEquals(dstArray, srcArray); + dstArray[0] = wbuf.getChar(); //get*() + wbuf.getCharArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getChar(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getChar(i * CB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutCharArray() { - char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + public void checkPutGetNativeDoubles() { + double[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - char[] dstArray = new char[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Character.BYTES).asWritableBuffer(); - wbuf.putCharArray(srcArray, 0, half); - wbuf.putCharArray(srcArray, half, half); + WritableBuffer wbuf = WritableMemory.allocate(len * DB, NBO).asWritableBuffer(); + //put + wbuf.putDouble(srcArray[0]); //put*(value) + wbuf.putDoubleArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putDouble(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putDouble(i * DB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); - wbuf.getCharArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetShortArray() { - short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - short[] dstArray = new short[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getShortArray(dstArray, 0, half); - buf.getShortArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getShortArray(dstArray, 0, half); - wbuf.getShortArray(dstArray, half, half); - assertEquals(dstArray, srcArray); + //get + double[] dstArray = new double[len]; + dstArray[0] = wbuf.getDouble(); //get*() + wbuf.getDoubleArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getDouble(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getDouble(i * DB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutShortArray() { - short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeFloats() { + float[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - short[] dstArray = new short[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Short.BYTES).asWritableBuffer(); - wbuf.putShortArray(srcArray, 0, half); - wbuf.putShortArray(srcArray, half, half); + WritableBuffer wbuf = WritableMemory.allocate(len * FB, NBO).asWritableBuffer(); + //put + wbuf.putFloat(srcArray[0]); //put*(value) + wbuf.putFloatArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putFloat(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putFloat(i * FB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); - wbuf.getShortArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); + //get + float[] dstArray = new float[len]; + dstArray[0] = wbuf.getFloat(); //get*() + wbuf.getFloatArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getFloat(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getFloat(i * FB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkGetIntArray() { - int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeInts() { + int[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; + WritableBuffer wbuf = WritableMemory.allocate(len * IB, NBO).asWritableBuffer(); + //put + wbuf.putInt(srcArray[0]); //put*(value) + wbuf.putIntArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putInt(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putInt(i * IB, srcArray[i]); } //put*(add, value) + wbuf.resetPosition(); + //get int[] dstArray = new int[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getIntArray(dstArray, 0, half); - buf.getIntArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getIntArray(dstArray, 0, half); - wbuf.getIntArray(dstArray, half, half); - assertEquals(dstArray, srcArray); + dstArray[0] = wbuf.getInt(); //get*() + wbuf.getIntArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getInt(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getInt(i * IB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutIntArray() { - int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeLongs() { + long[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - int[] dstArray = new int[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Integer.BYTES).asWritableBuffer(); - wbuf.putIntArray(srcArray, 0, half); - wbuf.putIntArray(srcArray, half, half); + WritableBuffer wbuf = WritableMemory.allocate(len * LB, NNBO).asWritableBuffer(); + //put + wbuf.putLong(srcArray[0]); //put*(value) + wbuf.putLongArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putLong(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putLong(i * LB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); - wbuf.getIntArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetLongArray() { - long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - long[] dstArray = new long[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getLongArray(dstArray, 0, half); - buf.getLongArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getLongArray(dstArray, 0, half); - wbuf.getLongArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkPutLongArray() { - long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; + //get long[] dstArray = new long[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Long.BYTES).asWritableBuffer(); - wbuf.putLongArray(srcArray, 0, half); - wbuf.putLongArray(srcArray, half, half); - wbuf.resetPosition(); - wbuf.getLongArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetFloatArray() { - float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - float[] dstArray = new float[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getFloatArray(dstArray, 0, half); - buf.getFloatArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getFloatArray(dstArray, 0, half); - wbuf.getFloatArray(dstArray, half, half); - assertEquals(dstArray, srcArray); + dstArray[0] = wbuf.getLong(); //get*() + wbuf.getLongArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getLong(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getLong(i * LB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutFloatArray() { - float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeShorts() { + short[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - float[] dstArray = new float[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Float.BYTES).asWritableBuffer(); - wbuf.putFloatArray(srcArray, 0, half); - wbuf.putFloatArray(srcArray, half, half); + WritableBuffer wbuf = WritableMemory.allocate(len * SB, NBO).asWritableBuffer(); + //put + wbuf.putShort(srcArray[0]); //put*(value) + wbuf.putShortArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putShort(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putShort(i * SB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); - wbuf.getFloatArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetDoubleArray() { - double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - double[] dstArray = new double[len]; - - Buffer buf = Memory.wrap(srcArray).asBuffer(); - buf.getDoubleArray(dstArray, 0, half); - buf.getDoubleArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - - - WritableBuffer wbuf = WritableMemory.writableWrap(srcArray).asWritableBuffer(); - wbuf.getDoubleArray(dstArray, 0, half); - wbuf.getDoubleArray(dstArray, half, half); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkPutDoubleArray() { - double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - double[] dstArray = new double[len]; - - WritableBuffer wbuf = WritableMemory.allocate(len * Double.BYTES).asWritableBuffer(); - wbuf.putDoubleArray(srcArray, 0, half); - wbuf.putDoubleArray(srcArray, half, half); - wbuf.resetPosition(); - wbuf.getDoubleArray(dstArray, 0, len); - assertEquals(dstArray, srcArray); + //get + short[] dstArray = new short[len]; + dstArray[0] = wbuf.getShort(); //get*() + wbuf.getShortArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getShort(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getShort(i * SB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test @@ -571,7 +512,7 @@ public class NativeWritableBufferImplTest { public void checkDuplicateNonNative() { WritableMemory wmem = WritableMemory.allocate(64); wmem.putShort(0, (short) 1); - Buffer buf = wmem.asWritableBuffer().duplicate(NON_NATIVE_BYTE_ORDER); + Buffer buf = wmem.asWritableBuffer().duplicate(NNBO); assertEquals(buf.getShort(0), 256); } diff --git a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java index c58c6691..07726dd3 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NativeWritableMemoryImplTest.java @@ -19,7 +19,14 @@ package org.apache.datasketches.memory.internal; -import static org.apache.datasketches.memory.internal.ResourceImpl.NON_NATIVE_BYTE_ORDER; +import static org.apache.datasketches.memory.internal.UtilForTest.CB; +import static org.apache.datasketches.memory.internal.UtilForTest.DB; +import static org.apache.datasketches.memory.internal.UtilForTest.FB; +import static org.apache.datasketches.memory.internal.UtilForTest.IB; +import static org.apache.datasketches.memory.internal.UtilForTest.LB; +import static org.apache.datasketches.memory.internal.UtilForTest.NBO; +import static org.apache.datasketches.memory.internal.UtilForTest.NNBO; +import static org.apache.datasketches.memory.internal.UtilForTest.SB; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -58,231 +65,159 @@ public class NativeWritableMemoryImplTest { try { wmem.getArena().close(); } catch (IllegalStateException e) { } } - //Simple Heap arrays + //Check primitives @Test - public void checkGetByteArray() { - byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetBooleans() { + boolean[] srcArray = {true, false, true, false, false, true, false, true}; final int len = srcArray.length; - final int half = len / 2; - byte[] dstArray = new byte[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getByteArray(0, dstArray, 0, half); - mem.getByteArray(half, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getByteArray(0, dstArray, 0, half); - wmem.getByteArray(half, dstArray, half, half); - assertEquals(dstArray, srcArray); + WritableMemory wmem = WritableMemory.allocate(len, NBO); + //put + for (int i = 0; i < len; i++) { wmem.putBoolean(i, srcArray[i]); } //put*(add, value) + //get + boolean[] dstArray = new boolean[len]; + for (int i = 0; i < len; i++) { dstArray[i] = wmem.getBoolean(i); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutByteArray() { + public void checkPutGetBytes() { byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; final int len = srcArray.length; final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len, NBO); + //put + wmem.putByte(0, srcArray[0]); //put*(add, value) + wmem.putByteArray(1, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putByte(3, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putByte(i, srcArray[i]); } //put*(add, value) + //get byte[] dstArray = new byte[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Byte.BYTES); - wmem.putByteArray(0, srcArray, 0, half); - wmem.putByteArray(half * Byte.BYTES, srcArray, half, half); - wmem.getByteArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); + dstArray[0] = wmem.getByte(0); //get*(add) + wmem.getByteArray(1, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getByte(3); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getByte(i); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkGetCharArray() { - char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; + public void checkPutGetNativeCharacters() { + char[] srcArray = { 'a','b','c','d','e','f','g','h' }; final int len = srcArray.length; final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * CB, NBO); + //put + wmem.putChar(0, srcArray[0]); //put*(add, value) + wmem.putCharArray(1 * CB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putChar(3 * CB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putChar(i * CB, srcArray[i]); } //put*(add, value) + //get char[] dstArray = new char[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getCharArray(0, dstArray, 0, half); - mem.getCharArray(half * Character.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getCharArray(0, dstArray, 0, half); - wmem.getCharArray(half * Character.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkPutCharArray() { - char[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; - final int len = srcArray.length; - final int half = len / 2; - char[] dstArray = new char[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Character.BYTES); - wmem.putCharArray(0, srcArray, 0, half); - wmem.putCharArray(half * Character.BYTES, srcArray, half, half); - wmem.getCharArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetShortArray() { - short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - short[] dstArray = new short[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getShortArray(0, dstArray, 0, half); - mem.getShortArray(half * Short.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getShortArray(0, dstArray, 0, half); - wmem.getShortArray(half * Short.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); + dstArray[0] = wmem.getChar(0); //get*(add) + wmem.getCharArray(1 * CB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getChar(3 * CB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getChar(i * CB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutShortArray() { - short[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeDoubles() { + double[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - short[] dstArray = new short[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Short.BYTES); - wmem.putShortArray(0, srcArray, 0, half); - wmem.putShortArray(half * Short.BYTES, srcArray, half, half); - wmem.getShortArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); + WritableMemory wmem = WritableMemory.allocate(len * DB, NBO); + //put + wmem.putDouble(0, srcArray[0]); //put*(add, value) + wmem.putDoubleArray(1 * DB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putDouble(3 * DB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putDouble(i * DB, srcArray[i]); } //put*(add, value) + //get + double[] dstArray = new double[len]; + dstArray[0] = wmem.getDouble(0); //get*(add) + wmem.getDoubleArray(1 * DB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getDouble(3 * DB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getDouble(i * DB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkGetIntArray() { - int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeFloats() { + float[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - int[] dstArray = new int[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getIntArray(0, dstArray, 0, half); - mem.getIntArray(half * Integer.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getIntArray(0, dstArray, 0, half); - wmem.getIntArray(half * Integer.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); + WritableMemory wmem = WritableMemory.allocate(len * FB, NBO); + //put + wmem.putFloat(0, srcArray[0]); //put*(add, value) + wmem.putFloatArray(1 * FB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putFloat(3 * FB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putFloat(i * FB, srcArray[i]); } //put*(add, value) + //get + float[] dstArray = new float[len]; + dstArray[0] = wmem.getFloat(0); //get*(add) + wmem.getFloatArray(1 * FB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getFloat(3 * FB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getFloat(i * FB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutIntArray() { - int[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeInts() { + int[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * IB, NBO); + //put + wmem.putInt(0, srcArray[0]); //put*(add, value) + wmem.putIntArray(1 * IB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putInt(3 * IB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putInt(i * IB, srcArray[i]); } //put*(add, value) + //get int[] dstArray = new int[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Integer.BYTES); - wmem.putIntArray(0, srcArray, 0, half); - wmem.putIntArray(half * Integer.BYTES, srcArray, half, half); - wmem.getIntArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); + dstArray[0] = wmem.getInt(0); //get*(add) + wmem.getIntArray(1 * IB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getInt(3 * IB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getInt(i * IB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkGetLongArray() { - long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeLongs() { + long[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; + WritableMemory wmem = WritableMemory.allocate(len * LB, NBO); + //put + wmem.putLong(0, srcArray[0]); //put*(add, value) + wmem.putLongArray(1 * LB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putLong(3 * LB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putLong(i * LB, srcArray[i]); } //put*(add, value) + //get long[] dstArray = new long[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getLongArray(0, dstArray, 0, half); - mem.getLongArray(half * Long.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getLongArray(0, dstArray, 0, half); - wmem.getLongArray(half * Long.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkPutLongArray() { - long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - long[] dstArray = new long[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Long.BYTES); - wmem.putLongArray(0, srcArray, 0, half); - wmem.putLongArray(half * Long.BYTES, srcArray, half, half); - wmem.getLongArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - - @Test - public void checkGetFloatArray() { - float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - float[] dstArray = new float[len]; - - Memory mem = Memory.wrap(srcArray); - mem.getFloatArray(0, dstArray, 0, half); - mem.getFloatArray(half * Float.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getFloatArray(0, dstArray, 0, half); - wmem.getFloatArray(half * Float.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); + dstArray[0] = wmem.getLong(0); //get*(add) + wmem.getLongArray(1 * LB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getLong(3 * LB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getLong(i * LB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test - public void checkPutFloatArray() { - float[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; + public void checkPutGetNativeShorts() { + short[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - float[] dstArray = new float[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Float.BYTES); - wmem.putFloatArray(0, srcArray, 0, half); - wmem.putFloatArray(half * Float.BYTES, srcArray, half, half); - wmem.getFloatArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkGetDoubleArray() { - double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - double[] dstArray = new double[8]; - - Memory mem = Memory.wrap(srcArray); - mem.getDoubleArray(0, dstArray, 0, half); - mem.getDoubleArray(half * Double.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - - WritableMemory wmem = WritableMemory.writableWrap(srcArray); - wmem.getDoubleArray(0, dstArray, 0, half); - wmem.getDoubleArray(half * Double.BYTES, dstArray, half, half); - assertEquals(dstArray, srcArray); - } - - @Test - public void checkPutDoubleArray() { - double[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 }; - final int len = srcArray.length; - final int half = len / 2; - double[] dstArray = new double[len]; - - WritableMemory wmem = WritableMemory.allocate(len * Double.BYTES); - wmem.putDoubleArray(0, srcArray, 0, half); - wmem.putDoubleArray(half * Double.BYTES, srcArray, half, half); - wmem.getDoubleArray(0, dstArray, 0, len); - assertEquals(dstArray, srcArray); + WritableMemory wmem = WritableMemory.allocate(len * SB, NBO); + //put + wmem.putShort(0, srcArray[0]); //put*(add, value) + wmem.putShortArray(1 * SB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putShort(3 * SB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putShort(i * SB, srcArray[i]); } //put*(add, value) + //get + short[] dstArray = new short[len]; + dstArray[0] = wmem.getShort(0); //get*(add) + wmem.getShortArray(1 * SB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getShort(3 * SB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getShort(i * SB); } //get*(add) + assertEquals(srcArray, dstArray); } @Test @@ -695,7 +630,7 @@ public class NativeWritableMemoryImplTest { public void checkAsBufferNonNative() { WritableMemory wmem = WritableMemory.allocate(64); wmem.putShort(0, (short) 1); - Buffer buf = wmem.asBuffer(NON_NATIVE_BYTE_ORDER); + Buffer buf = wmem.asBuffer(NNBO); assertEquals(buf.getShort(0), 256); } diff --git a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java index 316f1853..a10a9fd2 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableBufferImplTest.java @@ -48,31 +48,27 @@ public class NonNativeWritableBufferImplTest { char[] srcArray = { 'a','b','c','d','e','f','g','h' }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * CB, NNBO).asWritableBuffer(); //put - wbuf.putCharArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putChar(qtr * CB, srcArray[qtr]); //put*(add, value) - wbuf.putChar((qtr + 1) * CB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * CB); - for (int i = half; i < len; i++) { wbuf.putChar(srcArray[i]); } //put*(value) + wbuf.putChar(srcArray[0]); //put*(value) + wbuf.putCharArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putChar(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putChar(i * CB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * CB, NBO).asWritableBuffer(); for (int i = 0; i < len * CB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == Character.reverseBytes(wbuf2.getChar())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); char[] dstArray = new char[len]; - wbuf.getCharArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getChar(qtr * CB); //get*(add) - dstArray[qtr + 1] = wbuf.getChar((qtr + 1) * CB); //get*(add) - wbuf.setPosition(half * CB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getChar(); } //get*() + dstArray[0] = wbuf.getChar(); //get*() + wbuf.getCharArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getChar(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getChar(i * CB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -81,31 +77,27 @@ public class NonNativeWritableBufferImplTest { double[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * DB, NNBO).asWritableBuffer(); //put - wbuf.putDoubleArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putDouble(qtr * DB, srcArray[qtr]); //put*(add, value) - wbuf.putDouble((qtr + 1) * DB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * DB); - for (int i = half; i < len; i++) { wbuf.putDouble(srcArray[i]); } //put*(value) + wbuf.putDouble(srcArray[0]); //put*(value) + wbuf.putDoubleArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putDouble(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putDouble(i * DB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * DB, NBO).asWritableBuffer(); for (int i = 0; i < len * DB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == UtilForTest.doubleReverseBytes(wbuf2.getDouble())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); double[] dstArray = new double[len]; - wbuf.getDoubleArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getDouble(qtr * DB); //get*(add) - dstArray[qtr + 1] = wbuf.getDouble((qtr + 1) * DB); //get*(add) - wbuf.setPosition(half * DB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getDouble(); } //get*() + dstArray[0] = wbuf.getDouble(); //get*() + wbuf.getDoubleArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getDouble(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getDouble(i * DB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -114,31 +106,27 @@ public class NonNativeWritableBufferImplTest { float[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * FB, NNBO).asWritableBuffer(); //put - wbuf.putFloatArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putFloat(qtr * FB, srcArray[qtr]); //put*(add, value) - wbuf.putFloat((qtr + 1) * FB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * FB); - for (int i = half; i < len; i++) { wbuf.putFloat(srcArray[i]); } //put*(value) + wbuf.putFloat(srcArray[0]); //put*(value) + wbuf.putFloatArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putFloat(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putFloat(i * FB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * FB, NBO).asWritableBuffer(); for (int i = 0; i < len * FB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == UtilForTest.floatReverseBytes(wbuf2.getFloat())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); float[] dstArray = new float[len]; - wbuf.getFloatArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getFloat(qtr * FB); //get*(add) - dstArray[qtr + 1] = wbuf.getFloat((qtr + 1) * FB); //get*(add) - wbuf.setPosition(half * FB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getFloat(); } //get*() + dstArray[0] = wbuf.getFloat(); //get*() + wbuf.getFloatArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getFloat(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getFloat(i * FB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -147,31 +135,27 @@ public class NonNativeWritableBufferImplTest { int[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * IB, NNBO).asWritableBuffer(); //put - wbuf.putIntArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putInt(qtr * IB, srcArray[qtr]); //put*(add, value) - wbuf.putInt((qtr + 1) * IB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * IB); - for (int i = half; i < len; i++) { wbuf.putInt(srcArray[i]); } //put*(value) + wbuf.putInt(srcArray[0]); //put*(value) + wbuf.putIntArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putInt(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putInt(i * IB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * IB, NBO).asWritableBuffer(); for (int i = 0; i < len * IB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == Integer.reverseBytes(wbuf2.getInt())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); int[] dstArray = new int[len]; - wbuf.getIntArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getInt(qtr * IB); //get*(add) - dstArray[qtr + 1] = wbuf.getInt((qtr + 1) * IB); //get*(add) - wbuf.setPosition(half * IB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getInt(); } //get*() + dstArray[0] = wbuf.getInt(); //get*() + wbuf.getIntArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getInt(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getInt(i * IB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -180,31 +164,27 @@ public class NonNativeWritableBufferImplTest { long[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * LB, NNBO).asWritableBuffer(); //put - wbuf.putLongArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putLong(qtr * LB, srcArray[qtr]); //put*(add, value) - wbuf.putLong((qtr + 1) * LB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * LB); - for (int i = half; i < len; i++) { wbuf.putLong(srcArray[i]); } //put*(value) + wbuf.putLong(srcArray[0]); //put*(value) + wbuf.putLongArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putLong(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putLong(i * LB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * LB, NBO).asWritableBuffer(); for (int i = 0; i < len * LB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == Long.reverseBytes(wbuf2.getLong())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); long[] dstArray = new long[len]; - wbuf.getLongArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getLong(qtr * LB); //get*(add) - dstArray[qtr + 1] = wbuf.getLong((qtr + 1) * LB); //get*(add) - wbuf.setPosition(half * LB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getLong(); } //get*() + dstArray[0] = wbuf.getLong(); //get*() + wbuf.getLongArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getLong(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getLong(i * LB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -213,31 +193,27 @@ public class NonNativeWritableBufferImplTest { short[] srcArray = { 1, 2, 3, 4, 5, 6, 7, 8 }; final int len = srcArray.length; final int half = len / 2; - final int qtr = len / 4; WritableBuffer wbuf = WritableMemory.allocate(len * SB, NNBO).asWritableBuffer(); //put - wbuf.putShortArray(srcArray, 0, qtr); //put*Array(src[], srcOff, len) - wbuf.putShort(qtr * SB, srcArray[qtr]); //put*(add, value) - wbuf.putShort((qtr + 1) * SB, srcArray[qtr + 1]); //put*(add, value) - wbuf.setPosition(half * SB); - for (int i = half; i < len; i++) { wbuf.putShort(srcArray[i]); } //put*(value) + wbuf.putShort(srcArray[0]); //put*(value) + wbuf.putShortArray(srcArray, 1, 2); //put*Array(src[], srcOff, len) + wbuf.putShort(srcArray[3]); //put*(value) + for (int i = half; i < len; i++) { wbuf.putShort(i * SB, srcArray[i]); } //put*(add, value) wbuf.resetPosition(); //confirm WritableBuffer wbuf2 = WritableMemory.allocate(len * SB, NBO).asWritableBuffer(); for (int i = 0; i < len * SB; i++) { wbuf2.putByte(wbuf.getByte()); } - wbuf.resetPosition(); wbuf2.resetPosition(); for (int i = 0; i < len; i++) { assertTrue(srcArray[i] == Short.reverseBytes(wbuf2.getShort())); } //get - wbuf2.resetPosition(); + wbuf.resetPosition(); short[] dstArray = new short[len]; - wbuf.getShortArray(dstArray, 0, qtr); //get*Array(dst[], dstOff, len) - dstArray[qtr] = wbuf.getShort(qtr * SB); //get*(add) - dstArray[qtr + 1] = wbuf.getShort((qtr + 1) * SB); //get*(add) - wbuf.setPosition(half * SB); - for (int i = half; i < len; i++) { dstArray[i] = wbuf.getShort(); } //get*() + dstArray[0] = wbuf.getShort(); //get*() + wbuf.getShortArray(dstArray, 1, 2); //get*Array(dst[], dstOff, len) + dstArray[3] = wbuf.getShort(); //get*() + for (int i = half; i < len; i++) { dstArray[i] = wbuf.getShort(i * SB); } //get*(add) assertEquals(srcArray, dstArray); } diff --git a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java index 5dc6d928..f296d430 100644 --- a/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java +++ b/src/test/java/org/apache/datasketches/memory/internal/NonNativeWritableMemoryImplTest.java @@ -52,10 +52,10 @@ public class NonNativeWritableMemoryImplTest { final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * CB, NNBO); //put - wmem.putCharArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putChar((half + i) * CB, srcArray[half + i]); //put*(add, value) - } + wmem.putChar(0, srcArray[0]); //put*(add, value) + wmem.putCharArray(1 * CB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putChar(3 * CB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putChar(i * CB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * CB, NBO); wmem.copyTo(0, wmem2, 0, len * CB); @@ -64,10 +64,10 @@ public class NonNativeWritableMemoryImplTest { } //get char[] dstArray = new char[len]; - wmem.getCharArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getChar(i * CB); //get*(add) - } + dstArray[0] = wmem.getChar(0); //get*(add) + wmem.getCharArray(1 * CB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getChar(3 * CB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getChar(i * CB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -77,10 +77,11 @@ public class NonNativeWritableMemoryImplTest { final int len = srcArray.length; final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * DB, NNBO); - wmem.putDoubleArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putDouble((half + i) * DB, srcArray[half + i]); //put*(add, value) - } + //put + wmem.putDouble(0, srcArray[0]); //put*(add, value) + wmem.putDoubleArray(1 * DB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putDouble(3 * DB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putDouble(i * DB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * DB, NBO); wmem.copyTo(0, wmem2, 0, len * DB); @@ -89,10 +90,10 @@ public class NonNativeWritableMemoryImplTest { } //get double[] dstArray = new double[len]; - wmem.getDoubleArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getDouble(i * DB); //get*(add) - } + dstArray[0] = wmem.getDouble(0); //get*(add) + wmem.getDoubleArray(1 * DB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getDouble(3 * DB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getDouble(i * DB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -102,10 +103,11 @@ public class NonNativeWritableMemoryImplTest { final int len = srcArray.length; final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * FB, NNBO); - wmem.putFloatArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putFloat((half + i) * FB, srcArray[half + i]); //put*(add, value) - } + //put + wmem.putFloat(0, srcArray[0]); //put*(add, value) + wmem.putFloatArray(1 * FB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putFloat(3 * FB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putFloat(i * FB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * FB, NBO); wmem.copyTo(0, wmem2, 0, len * FB); @@ -114,10 +116,10 @@ public class NonNativeWritableMemoryImplTest { } //get float[] dstArray = new float[len]; - wmem.getFloatArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getFloat(i * FB); //get*(add) - } + dstArray[0] = wmem.getFloat(0); //get*(add) + wmem.getFloatArray(1 * FB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getFloat(3 * FB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getFloat(i * FB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -127,10 +129,11 @@ public class NonNativeWritableMemoryImplTest { final int len = srcArray.length; final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * IB, NNBO); - wmem.putIntArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putInt((half + i) * IB, srcArray[half + i]); //put*(add, value) - } + //put + wmem.putInt(0, srcArray[0]); //put*(add, value) + wmem.putIntArray(1 * IB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putInt(3 * IB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putInt(i * IB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * IB, NBO); wmem.copyTo(0, wmem2, 0, len * IB); @@ -139,10 +142,10 @@ public class NonNativeWritableMemoryImplTest { } //get int[] dstArray = new int[len]; - wmem.getIntArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getInt(i * IB); //get*(add) - } + dstArray[0] = wmem.getInt(0); //get*(add) + wmem.getIntArray(1 * IB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getInt(3 * IB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getInt(i * IB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -152,10 +155,11 @@ public class NonNativeWritableMemoryImplTest { final int len = srcArray.length; final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * LB, NNBO); - wmem.putLongArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putLong((half + i) * LB, srcArray[half + i]); //put*(add, value) - } + //put + wmem.putLong(0, srcArray[0]); //put*(add, value) + wmem.putLongArray(1 * LB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putLong(3 * LB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putLong(i * LB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * LB, NBO); wmem.copyTo(0, wmem2, 0, len * LB); @@ -164,10 +168,10 @@ public class NonNativeWritableMemoryImplTest { } //get long[] dstArray = new long[len]; - wmem.getLongArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getLong(i *LB); //get*(add) - } + dstArray[0] = wmem.getLong(0); //get*(add) + wmem.getLongArray(1 * LB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getLong(3 * LB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getLong(i * LB); } //get*(add) assertEquals(srcArray, dstArray); } @@ -177,10 +181,11 @@ public class NonNativeWritableMemoryImplTest { final int len = srcArray.length; final int half = len / 2; WritableMemory wmem = WritableMemory.allocate(len * SB, NNBO); - wmem.putShortArray(0, srcArray, 0, half); //put*Array(add, src[], srcOff, len) - for (int i = 0; i < half; i++) { - wmem.putShort((half + i) * SB, srcArray[half + i]); //put*(add, value) - } + //put + wmem.putShort(0, srcArray[0]); //put*(add, value) + wmem.putShortArray(1 * SB, srcArray, 1, 2); //put*Array(add, src[], srcOff, len) + wmem.putShort(3 * SB, srcArray[3]); //put*(add, value) + for (int i = half; i < len; i++) { wmem.putShort(i * SB, srcArray[i]); } //put*(add, value) //confirm WritableMemory wmem2 = WritableMemory.allocate(len * SB, NBO); wmem.copyTo(0, wmem2, 0, len * SB); @@ -189,10 +194,10 @@ public class NonNativeWritableMemoryImplTest { } //get short[] dstArray = new short[len]; - wmem.getShortArray(0, dstArray, 0, half); //get*Array(add, dst[], dstOff, len) - for (int i = half; i < len; i++) { - dstArray[i] = wmem.getShort(i *SB); //get*(add) - } + dstArray[0] = wmem.getShort(0); //get*(add) + wmem.getShortArray(1 * SB, dstArray, 1, 2); //get*Array(add, dst[], dstOff, len) + dstArray[3] = wmem.getShort(3 * SB); //get*(add) + for (int i = half; i < len; i++) { dstArray[i] = wmem.getShort(i * SB); } //get*(add) assertEquals(srcArray, dstArray); } --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
