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/LeafImplTest.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/LeafImplTest.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/LeafImplTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,276 @@
+/*
+ * 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.apache.datasketches.memory.internal.Util.NON_NATIVE_BYTE_ORDER;
+import static org.apache.datasketches.memory.internal.Util.otherByteOrder;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+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.MemoryRequestServer;
+import org.apache.datasketches.memory.WritableBuffer;
+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 LeafImplTest {
+ private static final ByteOrder NBO = ByteOrder.nativeOrder();
+ private static final ByteOrder NNBO = NON_NATIVE_BYTE_ORDER;
+ private static final MemoryRequestServer dummyMemReqSvr = new
DummyMemoryRequestServer();
+
+ static class DummyMemoryRequestServer implements MemoryRequestServer {
+ @Override
+ public WritableMemory request(WritableMemory currentWMem, long
capacityBytes) { return null; }
+ @Override
+ public void requestClose(WritableMemory memToClose, WritableMemory
newMemory) { }
+ }
+
+ @Test
+ public void checkDirectLeafs() throws Exception {
+ long off = 0;
+ long cap = 128;
+ // Off Heap, Native order, No ByteBuffer, has MemReqSvr
+ try (WritableHandle wdh = WritableMemory.allocateDirect(cap, NBO,
dummyMemReqSvr)) {
+ WritableMemory memNO = wdh.getWritable();
+ memNO.putShort(0, (short) 1);
+ assertNull(((BaseStateImpl)memNO).getUnsafeObject());
+ assertTrue(memNO.isDirect());
+ checkCombinations(memNO, off, cap, memNO.isDirect(), NBO, false, true);
+ }
+ // Off Heap, Non Native order, No ByteBuffer, has MemReqSvr
+ try (WritableHandle wdh = WritableMemory.allocateDirect(cap, NNBO,
dummyMemReqSvr)) {
+ WritableMemory memNNO = wdh.getWritable();
+ memNNO.putShort(0, (short) 1);
+ assertNull(((BaseStateImpl)memNNO).getUnsafeObject());
+ assertTrue(memNNO.isDirect());
+ checkCombinations(memNNO, off, cap, memNNO.isDirect(), NNBO, false,
true);
+ }
+ }
+
+ @Test
+ public void checkByteBufferLeafs() {
+ long off = 0;
+ long cap = 128;
+ //BB on heap, native order, has ByteBuffer, has MemReqSvr
+ ByteBuffer bb = ByteBuffer.allocate((int)cap);
+ bb.order(NBO);
+ bb.putShort(0, (short) 1);
+ WritableMemory mem = WritableMemory.writableWrap(bb, NBO, dummyMemReqSvr);
+ assertEquals(bb.isDirect(), mem.isDirect());
+ assertNotNull(((BaseStateImpl)mem).getUnsafeObject());
+ checkCombinations(mem, off, cap, mem.isDirect(), mem.getTypeByteOrder(),
true, true);
+
+ //BB off heap, native order, has ByteBuffer, has MemReqSvr
+ ByteBuffer dbb = ByteBuffer.allocateDirect((int)cap);
+ dbb.order(NBO);
+ dbb.putShort(0, (short) 1);
+ mem = WritableMemory.writableWrap(dbb, NBO, dummyMemReqSvr);
+ assertEquals(dbb.isDirect(), mem.isDirect());
+ assertNull(((BaseStateImpl)mem).getUnsafeObject());
+ checkCombinations(mem, off, cap, mem.isDirect(), mem.getTypeByteOrder(),
true, true);
+
+ //BB on heap, non native order, has ByteBuffer, has MemReqSvr
+ bb = ByteBuffer.allocate((int)cap);
+ bb.order(NNBO);
+ bb.putShort(0, (short) 1);
+ mem = WritableMemory.writableWrap(bb, NNBO, dummyMemReqSvr);
+ assertEquals(bb.isDirect(), mem.isDirect());
+ assertNotNull(((BaseStateImpl)mem).getUnsafeObject());
+ checkCombinations(mem, off, cap, mem.isDirect(), mem.getTypeByteOrder(),
true, true);
+
+ //BB off heap, non native order, has ByteBuffer, has MemReqSvr
+ dbb = ByteBuffer.allocateDirect((int)cap);
+ dbb.order(NNBO);
+ dbb.putShort(0, (short) 1);
+ mem = WritableMemory.writableWrap(dbb, NNBO, dummyMemReqSvr);
+ assertEquals(dbb.isDirect(), mem.isDirect());
+ assertNull(((BaseStateImpl)mem).getUnsafeObject());
+ checkCombinations(mem, off, cap, mem.isDirect(), mem.getTypeByteOrder(),
true, true);
+ }
+
+ @Test
+ public void checkMapLeafs() throws Exception {
+ long off = 0;
+ long cap = 128;
+ 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.
+ // Off Heap, Native order, No ByteBuffer, No MemReqSvr
+ try (WritableMapHandle wmh = WritableMemory.writableMap(file, off, cap,
NBO)) {
+ WritableMemory memNO = wmh.getWritable();
+ memNO.putShort(0, (short) 1);
+ assertNull(((BaseStateImpl)memNO).getUnsafeObject());
+ assertTrue(memNO.isDirect());
+ checkCombinations(memNO, off, cap, memNO.isDirect(), NBO, false, false);
+ }
+ // Off heap, Non Native order, No ByteBuffer, no MemReqSvr
+ try (WritableMapHandle wmh = WritableMemory.writableMap(file, off, cap,
NNBO)) {
+ WritableMemory memNNO = wmh.getWritable();
+ memNNO.putShort(0, (short) 1);
+ assertNull(((BaseStateImpl)memNNO).getUnsafeObject());
+ assertTrue(memNNO.isDirect());
+ checkCombinations(memNNO, off, cap, memNNO.isDirect(), NNBO, false,
false);
+ }
+ }
+
+ @Test
+ public void checkHeapLeafs() {
+ long off = 0;
+ long cap = 128;
+ // On Heap, Native order, No ByteBuffer, No MemReqSvr
+ WritableMemory memNO = WritableMemory.allocate((int)cap); //assumes NBO
+ memNO.putShort(0, (short) 1);
+ assertNotNull(((BaseStateImpl)memNO).getUnsafeObject());
+ assertFalse(memNO.isDirect());
+ checkCombinations(memNO, off, cap, memNO.isDirect(), NBO, false, false);
+ // On Heap, Non-native order, No ByteBuffer, No MemReqSvr
+ WritableMemory memNNO = WritableMemory.allocate((int)cap, NNBO);
+ memNNO.putShort(0, (short) 1);
+ assertNotNull(((BaseStateImpl)memNNO).getUnsafeObject());
+ assertFalse(memNNO.isDirect());
+ checkCombinations(memNNO, off, cap, memNNO.isDirect(), NNBO, false, false);
+ }
+
+ private static void checkCombinations(WritableMemory mem, long off, long cap,
+ boolean direct, ByteOrder bo, boolean hasByteBuffer, boolean
hasMemReqSvr) {
+ ByteOrder oo = otherByteOrder(bo);
+
+ assertEquals(mem.writableRegion(off, cap, bo).getShort(0), 1);
+ assertEquals(mem.writableRegion(off, cap, oo).getShort(0), 256);
+
+ assertEquals(mem.asWritableBuffer(bo).getShort(0), 1);
+ assertEquals(mem.asWritableBuffer(oo).getShort(0), 256);
+
+ ByteBuffer bb = mem.getByteBuffer();
+ assertTrue( hasByteBuffer ? bb != null : bb == null);
+
+ assertTrue(mem.getTypeByteOrder() == bo);
+
+ if (hasMemReqSvr) { assertTrue(mem.getMemoryRequestServer() instanceof
DummyMemoryRequestServer); }
+
+ Object obj = ((BaseStateImpl)mem).getUnsafeObject();
+ if (direct) {
+ assertTrue(mem.isDirect());
+ assertNull(obj);
+ } else {
+ assertFalse(mem.isDirect());
+ assertNotNull(obj);
+ }
+
+ assertTrue(mem.isValid() == true);
+
+ WritableBuffer buf = mem.asWritableBuffer();
+
+ assertEquals(buf.writableRegion(off, cap, bo).getShort(0), 1);
+ assertEquals(buf.writableRegion(off, cap, oo).getShort(0), 256);
+ assertEquals(buf.writableDuplicate(bo).getShort(0), 1);
+ assertEquals(buf.writableDuplicate(oo).getShort(0), 256);
+
+ bb = buf.getByteBuffer();
+ assertTrue(hasByteBuffer ? bb != null : bb == null);
+
+ assertTrue(buf.getTypeByteOrder() == bo);
+
+ if (hasMemReqSvr) { assertTrue(buf.getMemoryRequestServer() instanceof
DummyMemoryRequestServer); }
+
+ obj = ((BaseStateImpl)buf).getUnsafeObject();
+ if (direct) {
+ assertTrue(buf.isDirect());
+ assertNull(obj);
+ } else {
+ assertFalse(buf.isDirect());
+ assertNotNull(obj);
+ }
+
+ assertTrue(buf.isValid() == true);
+
+ WritableMemory nnMem = mem.writableRegion(off, cap, oo);
+
+ assertEquals(nnMem.writableRegion(off, cap, bo).getShort(0), 1);
+ assertEquals(nnMem.writableRegion(off, cap, oo).getShort(0), 256);
+ assertEquals(nnMem.asWritableBuffer(bo).getShort(0), 1);
+ assertEquals(nnMem.asWritableBuffer(oo).getShort(0), 256);
+
+ bb = nnMem.getByteBuffer();
+ assertTrue( hasByteBuffer ? bb != null : bb == null);
+
+ assertTrue(nnMem.getTypeByteOrder() == oo);
+
+ if (hasMemReqSvr) { assertTrue(nnMem.getMemoryRequestServer() instanceof
DummyMemoryRequestServer); }
+
+ obj = ((BaseStateImpl)nnMem).getUnsafeObject();
+ if (direct) {
+ assertTrue(nnMem.isDirect());
+ assertNull(obj);
+ } else {
+ assertFalse(nnMem.isDirect());
+ assertNotNull(obj);
+ }
+
+ assertTrue(nnMem.isValid() == true);
+
+ WritableBuffer nnBuf = mem.asWritableBuffer(oo);
+
+ assertEquals(nnBuf.writableRegion(off, cap, bo).getShort(0), 1);
+ assertEquals(nnBuf.writableRegion(off, cap, oo).getShort(0), 256);
+ assertEquals(nnBuf.writableDuplicate(bo).getShort(0), 1);
+ assertEquals(nnBuf.writableDuplicate(oo).getShort(0), 256);
+
+ bb = nnBuf.getByteBuffer();
+ assertTrue( hasByteBuffer ? bb != null : bb == null);
+
+ assertTrue(nnBuf.getTypeByteOrder() == oo);
+
+ if (hasMemReqSvr) { assertTrue(nnBuf.getMemoryRequestServer() instanceof
DummyMemoryRequestServer); }
+
+ obj = ((BaseStateImpl)nnBuf).getUnsafeObject();
+ if (direct) {
+ assertTrue(nnBuf.isDirect());
+ assertNull(obj);
+ } else {
+ assertFalse(nnBuf.isDirect());
+ assertNotNull(obj);
+ }
+
+ assertTrue(nnBuf.isValid() == true);
+ }
+
+}
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/LeafImplTest.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/MemoryBoundaryCheckTest.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/MemoryBoundaryCheckTest.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/MemoryBoundaryCheckTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,205 @@
+/*
+ * 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.WritableBuffer;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class MemoryBoundaryCheckTest {
+
+ private final WritableBuffer writableBuffer =
WritableMemory.allocate(8).asWritableBuffer();
+
+ @Test
+ public void testGetBoolean() {
+ writableBuffer.getBoolean(7);
+ try {
+ writableBuffer.getBoolean(8);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutBoolean() {
+ writableBuffer.putBoolean(7, true);
+ try {
+ writableBuffer.putBoolean(8, true);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetByte() {
+ writableBuffer.getByte(7);
+ try {
+ writableBuffer.getByte(8);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutByte() {
+ writableBuffer.putByte(7, (byte) 1);
+ try {
+ writableBuffer.putByte(8, (byte) 1);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetChar() {
+ writableBuffer.getChar(6);
+ try {
+ writableBuffer.getChar(7);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutChar() {
+ writableBuffer.putChar(6, 'a');
+ try {
+ writableBuffer.putChar(7, 'a');
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetShort() {
+ writableBuffer.getShort(6);
+ try {
+ writableBuffer.getShort(7);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutShort() {
+ writableBuffer.putShort(6, (short) 1);
+ try {
+ writableBuffer.putShort(7, (short) 1);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetInt() {
+ writableBuffer.getInt(4);
+ try {
+ writableBuffer.getInt(5);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutInt() {
+ writableBuffer.putInt(4, 1);
+ try {
+ writableBuffer.putInt(5, 1);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetFloat() {
+ writableBuffer.getFloat(4);
+ try {
+ writableBuffer.getFloat(5);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutFloat() {
+ writableBuffer.putFloat(4, 1f);
+ try {
+ writableBuffer.putFloat(5, 1f);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetLong() {
+ writableBuffer.getLong(0);
+ try {
+ writableBuffer.getLong(1);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutLong() {
+ writableBuffer.putLong(0, 1L);
+ try {
+ writableBuffer.putLong(1, 1L);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testGetDouble() {
+ writableBuffer.getDouble(0);
+ try {
+ writableBuffer.getDouble(1);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void testPutDouble() {
+ writableBuffer.putDouble(0, 1d);
+ try {
+ writableBuffer.putDouble(1, 1d);
+ throw new RuntimeException("Expected AssertionError");
+ } catch (final AssertionError expected) {
+ // ignore
+ }
+ }
+}
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/MemoryBoundaryCheckTest.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/MemoryCleanerTest.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/MemoryCleanerTest.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/MemoryCleanerTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,62 @@
+/*
+ * 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.util.concurrent.atomic.AtomicBoolean;
+
+import org.testng.annotations.Test;
+
+public class MemoryCleanerTest {
+
+ @Test
+ public void cleanerDeallocates() {
+ SimpleDeallocator deallocator = new SimpleDeallocator();
+ MemoryCleaner cleaner = new MemoryCleaner(this, deallocator);
+ cleaner.clean();
+ assertTrue(SimpleDeallocator.getHasRun());
+ }
+
+ @Test
+ public void noDeallocation() {
+ SimpleDeallocator deallocator = new SimpleDeallocator();
+ new MemoryCleaner(this, deallocator);
+ assertFalse(SimpleDeallocator.getHasRun());
+ }
+
+ static final class SimpleDeallocator implements Runnable {
+ static final AtomicBoolean hasRun = new AtomicBoolean();
+
+ SimpleDeallocator() {
+ hasRun.set(false);
+ }
+
+ @Override
+ public void run() {
+ hasRun.compareAndSet(false, true);
+ }
+
+ public static Boolean getHasRun() {
+ return hasRun.get();
+ }
+ }
+}
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/MemoryCleanerTest.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/MemoryCloseExceptionTest.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/MemoryCloseExceptionTest.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/MemoryCloseExceptionTest.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.MemoryCloseException;
+import org.testng.annotations.Test;
+
+public class MemoryCloseExceptionTest {
+
+ @Test
+ public void checkNoArgs() {
+ try {
+ throw new MemoryCloseException();
+ } catch (final MemoryCloseException e) {}
+
+ try {
+ throw new MemoryCloseException("Test Exception");
+ } catch (final MemoryCloseException e) {}
+ }
+}
+
+
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/MemoryCloseExceptionTest.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/MemoryReadWriteSafetyTest.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/MemoryReadWriteSafetyTest.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/MemoryReadWriteSafetyTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,227 @@
+/*
+ * 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 java.io.File;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+import org.apache.datasketches.memory.MapHandle;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.ReadOnlyException;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.Test;
+
+public class MemoryReadWriteSafetyTest {
+
+ // Test various operations with read-only Memory
+
+ final WritableMemory mem = (WritableMemory) Memory.wrap(new byte[8]);
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutByte() {
+ mem.putByte(0, (byte) 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutBoolean() {
+ mem.putBoolean(0, true);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutShort() {
+ mem.putShort(0, (short) 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutChar() {
+ mem.putChar(0, (char) 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutInt() {
+ mem.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutLong() {
+ mem.putLong(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutFloat() {
+ mem.putFloat(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testPutDouble() {
+ mem.putDouble(0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutByteArray() {
+ mem.putByteArray(0, new byte[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutBooleanArray() {
+ mem.putBooleanArray(0, new boolean[] {true}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutShortArray() {
+ mem.putShortArray(0, new short[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutCharArray() {
+ mem.putCharArray(0, new char[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutIntArray() {
+ mem.putIntArray(0, new int[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutLongArray() {
+ mem.putLongArray(0, new long[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testPutFloatArray() {
+ mem.putFloatArray(0, new float[] {1}, 0, 1);
+ }
+
+ @Test(expectedExceptions = ReadOnlyException.class)
+ public void testDoubleByteArray() {
+ mem.putDoubleArray(0, new double[] {1}, 0, 1);
+ }
+
+ // Now, test that various ways to obtain a read-only memory produce a
read-only memory indeed
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testWritableMemoryRegion() {
+ WritableMemory mem1 = (WritableMemory)
WritableMemory.allocate(8).region(0, 8);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testByteArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new byte[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testByteArrayWrapWithBO() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new byte[8],
ByteOrder.nativeOrder());
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testByteArrayWrapWithOffsetsAndBO() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new byte[8], 0, 4,
ByteOrder.nativeOrder());
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testBooleanArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new boolean[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testShortArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new short[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testCharArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new char[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testIntArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new int[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testLongArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new long[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testFloatArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new float[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testDoubleArrayWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(new double[8]);
+ mem1.putInt(0, 1);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void testByteBufferWrap() {
+ WritableMemory mem1 = (WritableMemory) Memory.wrap(ByteBuffer.allocate(8));
+ mem1.putInt(0, 1);
+ }
+
+ //@SuppressWarnings("resource")
+ @Test(expectedExceptions = AssertionError.class)
+ public void testMapFile() throws Exception {
+ File tempFile = File.createTempFile("test", null);
+ tempFile.deleteOnExit();
+ try (RandomAccessFile raf = new RandomAccessFile(tempFile, "rw")) {
+ raf.setLength(8);
+ //System.out.println(UtilTest.getFileAttributes(tempFile));
+ try (MapHandle h = Memory.map(tempFile)) {
+ ((WritableMemory) h.get()).putInt(0, 1);
+ }
+ }
+ }
+
+ @SuppressWarnings("resource")
+ @Test(expectedExceptions = AssertionError.class)
+ public void testMapFileWithOffsetsAndBO() throws Exception {
+ File tempFile = File.createTempFile("test", "test");
+ tempFile.deleteOnExit();
+ new RandomAccessFile(tempFile, "rw").setLength(8);
+ try (MapHandle h = Memory.map(tempFile, 0, 4, ByteOrder.nativeOrder())) {
+ ((WritableMemory) h.get()).putInt(0, 1);
+ }
+ }
+
+ @SuppressWarnings("resource")
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void testMapFileBeyondTheFileSize() throws Exception {
+ File tempFile = File.createTempFile("test", "test");
+ tempFile.deleteOnExit();
+ new RandomAccessFile(tempFile, "rw").setLength(8);
+ try (MapHandle unused = Memory.map(tempFile, 0, 16,
ByteOrder.nativeOrder())) {
+ }
+ }
+}
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/MemoryReadWriteSafetyTest.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/MemoryTest.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/MemoryTest.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/MemoryTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,479 @@
+/*
+ * 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.getResourceFile;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import java.io.File;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+import java.util.List;
+
+import org.apache.datasketches.memory.BaseState;
+import org.apache.datasketches.memory.MapHandle;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+import org.testng.collections.Lists;
+
+public class MemoryTest {
+ private static final String LS = System.getProperty("line.separator");
+
+ @BeforeClass
+ public void setReadOnly() {
+ UtilTest.setGettysburgAddressFileToReadOnly();
+ }
+
+ @Test
+ public void checkDirectRoundTrip() throws Exception {
+ int n = 1024; //longs
+ try (WritableHandle wh = WritableMemory.allocateDirect(n * 8)) {
+ WritableMemory mem = wh.getWritable();
+ for (int i = 0; i < n; i++) {
+ mem.putLong(i * 8, i);
+ }
+ for (int i = 0; i < n; i++) {
+ long v = mem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ }
+ }
+
+ @Test
+ public void checkAutoHeapRoundTrip() {
+ int n = 1024; //longs
+ WritableMemory wmem = WritableMemory.allocate(n * 8);
+ for (int i = 0; i < n; i++) {
+ wmem.putLong(i * 8, i);
+ }
+ for (int i = 0; i < n; i++) {
+ long v = wmem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ }
+
+ @Test
+ public void checkArrayWrap() {
+ int n = 1024; //longs
+ byte[] arr = new byte[n * 8];
+ WritableMemory wmem = WritableMemory.writableWrap(arr);
+ for (int i = 0; i < n; i++) {
+ wmem.putLong(i * 8, i);
+ }
+ for (int i = 0; i < n; i++) {
+ long v = wmem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ Memory mem = Memory.wrap(arr, ByteOrder.nativeOrder());
+ for (int i = 0; i < n; i++) {
+ long v = mem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ // check 0 length array wraps
+ Memory memZeroLengthArrayBoolean = WritableMemory.writableWrap(new
boolean[0]);
+ Memory memZeroLengthArrayByte = WritableMemory.writableWrap(new byte[0]);
+ Memory memZeroLengthArrayChar = WritableMemory.writableWrap(new char[0]);
+ Memory memZeroLengthArrayShort = WritableMemory.writableWrap(new short[0]);
+ Memory memZeroLengthArrayInt = WritableMemory.writableWrap(new int[0]);
+ Memory memZeroLengthArrayLong = WritableMemory.writableWrap(new long[0]);
+ Memory memZeroLengthArrayFloat = WritableMemory.writableWrap(new float[0]);
+ Memory memZeroLengthArrayDouble = WritableMemory.writableWrap(new
double[0]);
+ assertEquals(memZeroLengthArrayBoolean.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayByte.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayChar.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayShort.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayInt.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayLong.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayFloat.getCapacity(), 0);
+ assertEquals(memZeroLengthArrayDouble.getCapacity(), 0);
+
+ // check 0 length array wraps
+ List<Memory> memoryToCheck = Lists.newArrayList();
+ memoryToCheck.add(WritableMemory.allocate(0));
+ memoryToCheck.add(WritableMemory.writableWrap(ByteBuffer.allocate(0)));
+ memoryToCheck.add(WritableMemory.writableWrap(new boolean[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new byte[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new char[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new short[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new int[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new long[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new float[0]));
+ memoryToCheck.add(WritableMemory.writableWrap(new double[0]));
+ memoryToCheck.add(Memory.wrap(ByteBuffer.allocate(0)));
+ memoryToCheck.add(Memory.wrap(new boolean[0]));
+ memoryToCheck.add(Memory.wrap(new byte[0]));
+ memoryToCheck.add(Memory.wrap(new char[0]));
+ memoryToCheck.add(Memory.wrap(new short[0]));
+ memoryToCheck.add(Memory.wrap(new int[0]));
+ memoryToCheck.add(Memory.wrap(new long[0]));
+ memoryToCheck.add(Memory.wrap(new float[0]));
+ memoryToCheck.add(Memory.wrap(new double[0]));
+ //Check the Memory lengths
+ for (Memory memory : memoryToCheck) {
+ assertEquals(memory.getCapacity(), 0);
+ }
+ }
+
+ @Test
+ public void checkByteBufHeap() {
+ int n = 1024; //longs
+ byte[] arr = new byte[n * 8];
+ ByteBuffer bb = ByteBuffer.wrap(arr);
+ bb.order(ByteOrder.nativeOrder());
+ WritableMemory wmem = WritableMemory.writableWrap(bb);
+ for (int i = 0; i < n; i++) { //write to wmem
+ wmem.putLong(i * 8, i);
+ }
+ for (int i = 0; i < n; i++) { //read from wmem
+ long v = wmem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ for (int i = 0; i < n; i++) { //read from BB
+ long v = bb.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ Memory mem1 = Memory.wrap(arr);
+ for (int i = 0; i < n; i++) { //read from wrapped arr
+ long v = mem1.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ //convert to RO
+ Memory mem = wmem;
+ for (int i = 0; i < n; i++) {
+ long v = mem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ }
+
+ @Test
+ public void checkByteBufDirect() {
+ int n = 1024; //longs
+ ByteBuffer bb = ByteBuffer.allocateDirect(n * 8);
+ bb.order(ByteOrder.nativeOrder());
+ WritableMemory wmem = WritableMemory.writableWrap(bb);
+ for (int i = 0; i < n; i++) { //write to wmem
+ wmem.putLong(i * 8, i);
+ }
+ for (int i = 0; i < n; i++) { //read from wmem
+ long v = wmem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ for (int i = 0; i < n; i++) { //read from BB
+ long v = bb.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ Memory mem1 = Memory.wrap(bb);
+ for (int i = 0; i < n; i++) { //read from wrapped bb RO
+ long v = mem1.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ //convert to RO
+ Memory mem = wmem;
+ for (int i = 0; i < n; i++) {
+ long v = mem.getLong(i * 8);
+ assertEquals(v, i);
+ }
+ }
+
+ @Test
+ public void checkByteBufWrongOrder() {
+ int n = 1024; //longs
+ ByteBuffer bb = ByteBuffer.allocate(n * 8);
+ bb.order(ByteOrder.BIG_ENDIAN);
+ Memory mem = Memory.wrap(bb);
+ assertFalse(mem.getTypeByteOrder() == ByteOrder.nativeOrder());
+ assertEquals(mem.getTypeByteOrder(), ByteOrder.BIG_ENDIAN);
+ }
+
+ @Test
+ public void checkReadOnlyHeapByteBuffer() {
+ ByteBuffer bb = ByteBuffer.allocate(128);
+ bb.order(ByteOrder.nativeOrder());
+ for (int i = 0; i < 128; i++) { bb.put(i, (byte)i); }
+ bb.position(64);
+ ByteBuffer slice = bb.slice().asReadOnlyBuffer();
+ slice.order(ByteOrder.nativeOrder());
+ Memory mem = Memory.wrap(slice);
+ for (int i = 0; i < 64; i++) {
+ assertEquals(mem.getByte(i), 64 + i);
+ }
+ mem.toHexString("slice", 0, slice.capacity());
+ //println(s);
+ }
+
+ @Test
+ public void checkPutGetArraysHeap() {
+ int n = 1024; //longs
+ long[] arr = new long[n];
+ for (int i = 0; i < n; i++) { arr[i] = i; }
+ WritableMemory wmem = WritableMemory.allocate(n * 8);
+ wmem.putLongArray(0, arr, 0, n);
+ long[] arr2 = new long[n];
+ wmem.getLongArray(0, arr2, 0, n);
+ for (int i = 0; i < n; i++) {
+ assertEquals(arr2[i], i);
+ }
+ }
+
+ @Test
+ public void checkRORegions() {
+ int n = 16;
+ int n2 = n / 2;
+ long[] arr = new long[n];
+ for (int i = 0; i < n; i++) { arr[i] = i; }
+ Memory mem = Memory.wrap(arr);
+ Memory reg = mem.region(n2 * 8, n2 * 8); //top half
+ for (int i = 0; i < n2; i++) {
+ long v = reg.getLong(i * 8);
+ long e = i + n2;
+ assertEquals(v, e);
+ }
+ }
+
+ @Test
+ public void checkRORegionsReverseBO() {
+ int n = 16;
+ int n2 = n / 2;
+ long[] arr = new long[n];
+ for (int i = 0; i < n; i++) { arr[i] = i; }
+ Memory mem = Memory.wrap(arr);
+ Memory reg = mem.region(n2 * 8, n2 * 8, Util.NON_NATIVE_BYTE_ORDER); //top
half
+ for (int i = 0; i < n2; i++) {
+ long v = Long.reverseBytes(reg.getLong(i * 8));
+ long e = i + n2;
+ assertEquals(v, e);
+ }
+ }
+
+ @Test
+ public void checkWRegions() {
+ int n = 16;
+ int n2 = n / 2;
+ long[] arr = new long[n];
+ for (int i = 0; i < n; i++) { arr[i] = i; }
+ WritableMemory wmem = WritableMemory.writableWrap(arr);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem.getLong(i * 8), i);
+ //println("" + wmem.getLong(i * 8));
+ }
+ //println("");
+ WritableMemory reg = wmem.writableRegion(n2 * 8, n2 * 8);
+ for (int i = 0; i < n2; i++) { reg.putLong(i * 8, i); }
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem.getLong(i * 8), i % 8);
+ //println("" + wmem.getLong(i * 8));
+ }
+ }
+
+ @Test
+ public void checkWRegionsReverseBO() {
+ int n = 16;
+ int n2 = n / 2;
+ long[] arr = new long[n];
+ for (int i = 0; i < n; i++) { arr[i] = i; }
+ WritableMemory wmem = WritableMemory.writableWrap(arr);
+ for (int i = 0; i < n; i++) {
+ assertEquals(wmem.getLong(i * 8), i);
+ //println("" + wmem.getLong(i * 8));
+ }
+ //println("");
+ WritableMemory reg = wmem.writableRegion(n2 * 8, n2 * 8,
Util.NON_NATIVE_BYTE_ORDER);
+ for (int i = 0; i < n2; i++) { reg.putLong(i * 8, i); }
+ for (int i = 0; i < n; i++) {
+ long v = wmem.getLong(i * 8);
+ if (i < n2) {
+ assertEquals(v, i % 8);
+ } else {
+ assertEquals(Long.reverseBytes(v), i % 8);
+ }
+ //println("" + wmem.getLong(i * 8));
+ }
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void checkParentUseAfterFree() throws Exception {
+ int bytes = 64 * 8;
+ WritableHandle wh = WritableMemory.allocateDirect(bytes);
+ WritableMemory wmem = wh.getWritable();
+ wh.close();
+ //with -ea assert: Memory not valid.
+ //with -da sometimes segfaults, sometimes passes!
+ wmem.getLong(0);
+ }
+
+ @Test(expectedExceptions = AssertionError.class)
+ public void checkRegionUseAfterFree() throws Exception {
+ int bytes = 64;
+ WritableHandle wh = WritableMemory.allocateDirect(bytes);
+ Memory wmem = wh.get();
+ Memory region = wmem.region(0L, bytes);
+ wh.close();
+ //with -ea assert: Memory not valid.
+ //with -da sometimes segfaults, sometimes passes!
+ region.getByte(0);
+ }
+
+ @Test
+ public void checkMonitorDirectStats() throws Exception {
+ int bytes = 1024;
+ long curAllocations = BaseState.getCurrentDirectMemoryAllocations();
+ long curAllocated = BaseState.getCurrentDirectMemoryAllocated();
+ if (curAllocations != 0) { System.err.println(curAllocations + " should be
zero!"); }
+ WritableHandle wh1 = WritableMemory.allocateDirect(bytes);
+ WritableHandle wh2 = WritableMemory.allocateDirect(bytes);
+ assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 2L +
curAllocations);
+ assertEquals(BaseState.getCurrentDirectMemoryAllocated(), 2 * bytes +
curAllocated);
+
+ wh1.close();
+ assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 1L +
curAllocations);
+ assertEquals(BaseState.getCurrentDirectMemoryAllocated(), bytes +
curAllocated);
+
+ wh2.close();
+ wh2.close(); //check that it doesn't go negative.
+ //even though the handles are closed, these methods are static access
+ assertEquals(BaseState.getCurrentDirectMemoryAllocations(), 0L +
curAllocations);
+ assertEquals(BaseState.getCurrentDirectMemoryAllocated(), 0L +
curAllocated);
+ }
+
+ @Test
+ public void checkMonitorDirectMapStats() throws Exception {
+ File file = getResourceFile("GettysburgAddress.txt");
+ long bytes = file.length();
+
+ MapHandle mmh1 = Memory.map(file);
+ MapHandle mmh2 = Memory.map(file);
+
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 2L);
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), 2 * bytes);
+
+ mmh1.close();
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 1L);
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), bytes);
+
+ mmh2.close();
+ mmh2.close(); //check that it doesn't go negative.
+ //even though the handles are closed, these methods are static access
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocations(), 0L);
+ assertEquals(BaseState.getCurrentDirectMemoryMapAllocated(), 0L);
+ }
+
+ @Test
+ public void checkMemReqSvr() throws Exception {
+ WritableMemory wmem;
+ WritableBuffer wbuf;
+ if (BaseState.defaultMemReqSvr == null) { //This is a policy choice
+ //ON HEAP
+ wmem = WritableMemory.writableWrap(new byte[16]);
+ assertNull(wmem.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNull(wbuf.getMemoryRequestServer());
+ //OFF HEAP
+ try (WritableHandle wdh = WritableMemory.allocateDirect(16)) { //OFF HEAP
+ wmem = wdh.getWritable();
+ assertNull(wmem.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNull(wbuf.getMemoryRequestServer());
+ }
+ //ByteBuffer
+ ByteBuffer bb = ByteBuffer.allocate(16);
+ wmem = WritableMemory.writableWrap(bb);
+ assertNull(wmem.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNull(wbuf.getMemoryRequestServer());
+ } else {
+ //ON HEAP
+ wmem = WritableMemory.writableWrap(new byte[16]);
+ assertNotNull(wmem.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNotNull(wbuf.getMemoryRequestServer());
+ //OFF HEAP
+ try (WritableHandle wdh = WritableMemory.allocateDirect(16)) {
+ WritableMemory wmem2 = wdh.getWritable();
+ assertNotNull(wmem2.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNotNull(wbuf.getMemoryRequestServer());
+ }
+ //ByteBuffer
+ ByteBuffer bb = ByteBuffer.allocate(16);
+ wmem = WritableMemory.writableWrap(bb);
+ assertNotNull(wmem.getMemoryRequestServer());
+ wbuf = wmem.asWritableBuffer();
+ assertNotNull(wbuf.getMemoryRequestServer());
+ }
+ }
+
+ @Test
+ public void checkHashCode() {
+ WritableMemory wmem = WritableMemory.allocate(32 + 7);
+ int hc = wmem.hashCode();
+ assertEquals(hc, -1895166923);
+ }
+
+ @Test
+ public void checkSelfEqualsToAndCompareTo() {
+ int len = 64;
+ WritableMemory wmem = WritableMemory.allocate(len);
+ for (int i = 0; i < len; i++) { wmem.putByte(i, (byte) i); }
+ assertTrue(wmem.equalTo(0, wmem, 0, len));
+ assertFalse(wmem.equalTo(0, wmem, len/2, len/2));
+ assertEquals(wmem.compareTo(0, len, wmem, 0, len), 0);
+ assertTrue(wmem.compareTo(0, 0, wmem, len/2, len/2) < 0);
+ }
+
+ @Test
+ public void wrapBigEndianAsLittle() {
+ ByteBuffer bb = ByteBuffer.allocate(64);
+ bb.putChar(0, (char)1); //as NNO
+ Memory mem = Memory.wrap(bb, ByteOrder.LITTLE_ENDIAN);
+ assertEquals(mem.getChar(0), 256);
+ }
+
+ @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/MemoryTest.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/MemoryWriteToTest.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/MemoryWriteToTest.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/MemoryWriteToTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,96 @@
+/*
+ * 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.apache.datasketches.memory.internal.Util.UNSAFE_COPY_THRESHOLD_BYTES;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.channels.Channels;
+import java.nio.channels.WritableByteChannel;
+import java.util.concurrent.ThreadLocalRandom;
+
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class MemoryWriteToTest {
+
+ @Test
+ public void testOnHeap() throws IOException {
+ testWriteTo(createRandomBytesMemory(0));
+ testWriteTo(createRandomBytesMemory(7));
+ testWriteTo(createRandomBytesMemory(1023));
+ testWriteTo(createRandomBytesMemory(10_000));
+ testWriteTo(createRandomBytesMemory(UNSAFE_COPY_THRESHOLD_BYTES * 5));
+ testWriteTo(createRandomBytesMemory((UNSAFE_COPY_THRESHOLD_BYTES * 5) +
10));
+ }
+
+ @Test
+ public void testOnHeapInts() throws IOException {
+ testWriteTo(createRandomIntsMemory(0));
+ testWriteTo(createRandomIntsMemory(7));
+ testWriteTo(createRandomIntsMemory(1023));
+ testWriteTo(createRandomIntsMemory(10_000));
+ testWriteTo(createRandomIntsMemory(UNSAFE_COPY_THRESHOLD_BYTES * 5));
+ testWriteTo(createRandomIntsMemory((UNSAFE_COPY_THRESHOLD_BYTES * 5) +
10));
+ }
+
+ @Test
+ public void testOffHeap() throws Exception {
+ try (WritableHandle handle =
+ WritableMemory.allocateDirect((UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10))
{
+ WritableMemory mem = handle.getWritable();
+ testWriteTo(mem.region(0, 0));
+ testOffHeap(mem, 7);
+ testOffHeap(mem, 1023);
+ testOffHeap(mem, 10_000);
+ testOffHeap(mem, UNSAFE_COPY_THRESHOLD_BYTES * 5);
+ testOffHeap(mem, (UNSAFE_COPY_THRESHOLD_BYTES * 5) + 10);
+ }
+ }
+
+ private static void testOffHeap(WritableMemory mem, int size) throws
IOException {
+ createRandomBytesMemory(size).copyTo(0, mem, 0, size);
+ testWriteTo(mem.region(0, size));
+ }
+
+ private static Memory createRandomBytesMemory(int size) {
+ byte[] bytes = new byte[size];
+ ThreadLocalRandom.current().nextBytes(bytes);
+ return Memory.wrap(bytes);
+ }
+
+ private static Memory createRandomIntsMemory(int size) {
+ int[] ints = ThreadLocalRandom.current().ints(size).toArray();
+ return Memory.wrap(ints);
+ }
+
+ private static void testWriteTo(Memory mem) throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ try (WritableByteChannel out = Channels.newChannel(baos)) {
+ mem.writeTo(0, mem.getCapacity(), out);
+ }
+ byte[] result = baos.toByteArray();
+ Assert.assertTrue(mem.equals(Memory.wrap(result)));
+ }
+}
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/MemoryWriteToTest.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/MurmurHash3v2Test.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/MurmurHash3v2Test.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/MurmurHash3v2Test.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,401 @@
+/*
+ * 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.apache.datasketches.memory.MurmurHash3v2.hash;
+import static org.testng.Assert.fail;
+
+import org.apache.datasketches.memory.Memory;
+import org.apache.datasketches.memory.MurmurHash3v2;
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+/**
+ * Tests the MurmurHash3 against specific, known hash results given known
+ * inputs obtained from the public domain C++ version 150.
+ *
+ * @author Lee Rhodes
+ */
+public class MurmurHash3v2Test {
+
+ @Test
+ public void checkByteArrRemainderGT8() { //byte[], remainder > 8
+ String keyStr = "The quick brown fox jumps over the lazy dog";
+ byte[] key = keyStr.getBytes(UTF_8);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xe34bbc7bbc071b6cL;
+ long h2 = 0x7a433ca9c49a9347L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ @Test
+ public void checkByteArrChange1bit() { //byte[], change one bit
+ String keyStr = "The quick brown fox jumps over the lazy eog";
+ byte[] key = keyStr.getBytes(UTF_8);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0x362108102c62d1c9L;
+ long h2 = 0x3285cd100292b305L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ @Test
+ public void checkByteArrRemainderLt8() { //byte[], test a remainder < 8
+ String keyStr = "The quick brown fox jumps over the lazy dogdogdog";
+ byte[] key = keyStr.getBytes(UTF_8);
+ long[] result = hash(key, 0);
+ //Should be;
+ long h1 = 0x9c8205300e612fc4L;
+ long h2 = 0xcbc0af6136aa3df9L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ @Test
+ public void checkByteArrReaminderEQ8() { //byte[], test a remainder = 8
+ String keyStr = "The quick brown fox jumps over the lazy1";
+ byte[] key = keyStr.getBytes(UTF_8);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xe3301a827e5cdfe3L;
+ long h2 = 0xbdbf05f8da0f0392L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+
+ }
+
+ /**
+ * This test should have the exact same output as Test4
+ */
+ @Test
+ public void checkLongArrRemainderEQ8() { //long[], test a remainder = 8
+ String keyStr = "The quick brown fox jumps over the lazy1";
+ long[] key = stringToLongs(keyStr);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xe3301a827e5cdfe3L;
+ long h2 = 0xbdbf05f8da0f0392L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+
+ }
+
+ /**
+ * This test should have the exact same output as Test4
+ */
+ @Test
+ public void checkIntArrRemainderEQ8() { //int[], test a remainder = 8
+ String keyStr = "The quick brown fox jumps over the lazy1"; //40B
+ int[] key = stringToInts(keyStr);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xe3301a827e5cdfe3L;
+ long h2 = 0xbdbf05f8da0f0392L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ @Test
+ public void checkIntArrRemainderEQ0() { //int[], test a remainder = 0
+ String keyStr = "The quick brown fox jumps over t"; //32B
+ int[] key = stringToInts(keyStr);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xdf6af91bb29bdacfL;
+ long h2 = 0x91a341c58df1f3a6L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+
+ /**
+ * Tests an odd remainder of int[].
+ */
+ @Test
+ public void checkIntArrOddRemainder() { //int[], odd remainder
+ String keyStr = "The quick brown fox jumps over the lazy dog"; //43B
+ int[] key = stringToInts(keyStr);
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0x1eb232b0087543f5L;
+ long h2 = 0xfc4c1383c3ace40fL;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+
+ /**
+ * Tests an odd remainder of int[].
+ */
+ @Test
+ public void checkCharArrOddRemainder() { //char[], odd remainder
+ String keyStr = "The quick brown fox jumps over the lazy dog.."; //45B
+ char[] key = keyStr.toCharArray();
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0xca77b498ea9ed953L;
+ long h2 = 0x8b8f8ec3a8f4657eL;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ /**
+ * Tests an odd remainder of int[].
+ */
+ @Test
+ public void checkCharArrRemainderEQ0() { //char[], remainder of 0
+ String keyStr = "The quick brown fox jumps over the lazy "; //40B
+ char[] key = keyStr.toCharArray();
+ long[] result = hash(key, 0);
+ //Should be:
+ long h1 = 0x51b15e9d0887f9f1L;
+ long h2 = 0x8106d226786511ebL;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ @Test
+ public void checkByteArrAllOnesZeros() { //byte[], test a ones byte and a
zeros byte
+ byte[] key = {
+ 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b, 0x20, 0x62, 0x72,
0x6f, 0x77, 0x6e,
+ 0x20, 0x66, 0x6f, 0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
0x76, 0x65,
+ 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c, 0x61, 0x7a, 0x79, 0x20, 0x64,
0x6f, 0x67,
+ (byte) 0xff, 0x64, 0x6f, 0x67, 0x00
+ };
+ long[] result = MurmurHash3v2.hash(key, 0);
+
+ //Should be:
+ long h1 = 0xe88abda785929c9eL;
+ long h2 = 0x96b98587cacc83d6L;
+ Assert.assertEquals(result[0], h1);
+ Assert.assertEquals(result[1], h2);
+ }
+
+ /**
+ * This test demonstrates that the hash of byte[], char[], int[], or long[]
will produce the
+ * same hash result if, and only if, all the arrays have the same exact
length in bytes, and if
+ * the contents of the values in the arrays have the same byte endianness
and overall order.
+ */
+ @Test
+ public void checkCrossTypeHashConsistency() {
+ long[] out;
+ println("Bytes");
+ byte[] bArr = {1,2,3,4,5,6,7,8, 9,10,11,12,13,14,15,16,
17,18,19,20,21,22,23,24};
+ long[] out1 = hash(bArr, 0L);
+ println(longToHexBytes(out1[0]));
+ println(longToHexBytes(out1[1]));
+
+ println("Chars");
+ char[] cArr = {0X0201, 0X0403, 0X0605, 0X0807, 0X0a09, 0X0c0b, 0X0e0d,
0X100f,
+ 0X1211, 0X1413, 0X1615, 0X1817};
+ out = hash(cArr, 0L);
+ Assert.assertEquals(out, out1);
+ println(longToHexBytes(out[0]));
+ println(longToHexBytes(out[1]));
+
+ println("Ints");
+ int[] iArr = {0X04030201, 0X08070605, 0X0c0b0a09, 0X100f0e0d,
0X14131211, 0X18171615};
+ out = hash(iArr, 0L);
+ Assert.assertEquals(out, out1);
+ println(longToHexBytes(out[0]));
+ println(longToHexBytes(out[1]));
+
+ println("Longs");
+ long[] lArr = {0X0807060504030201L, 0X100f0e0d0c0b0a09L,
0X1817161514131211L};
+ out = hash(lArr, 0L);
+ Assert.assertEquals(out, out1);
+ println(longToHexBytes(out[0]));
+ println(longToHexBytes(out[1]));
+ }
+
+ @Test
+ public void checkEmptyOrNullExceptions() {
+ try {
+ long[] arr = null; hash(arr, 1L); fail();
+ } catch (final IllegalArgumentException e) { }
+ try {
+ int[] arr = null; hash(arr, 1L); fail();
+ } catch (final IllegalArgumentException e) { }
+ try {
+ char[] arr = null; hash(arr, 1L); fail();
+ } catch (final IllegalArgumentException e) { }
+ try {
+ byte[] arr = null; hash(arr, 1L); fail();
+ } catch (final IllegalArgumentException e) { }
+ try {
+ long[] out = new long[2];
+ String in = null; hash(in, 1L, out); fail();
+ } catch (final IllegalArgumentException e) { }
+ try {
+ long[] out = new long[2];
+ Memory mem = Memory.wrap(new byte[0]);
+ out = hash(mem, 0L, 4L, 1L, out);
+ } catch (final IllegalArgumentException e) { }
+ try (WritableHandle wh = WritableMemory.allocateDirect(8)) {
+ long[] out = new long[2];
+ Memory mem = wh.get();
+ out = hash(mem, 0L, 4L, 1L, out);
+ } catch (Exception ee) {}
+ }
+
+ @Test
+ public void checkHashTails() {
+ long[] out = new long[2];
+ WritableMemory mem = WritableMemory.allocate(32);
+ mem.fill((byte)85);
+
+ for (int i = 16; i <= 32; i++) {
+ out = hash(mem, 0, i, 1L, out);
+ }
+ }
+
+ @Test
+ public void checkSinglePrimitives() {
+ long[] out = new long[2];
+ out = hash(1L, 1L, out);
+ out = hash(0.0, 1L, out);
+ out = hash("123", 1L, out);
+ }
+
+ //Helper methods
+
+ private static long[] stringToLongs(String in) {
+ byte[] bArr = in.getBytes(UTF_8);
+ int inLen = bArr.length;
+ int outLen = (inLen / 8) + (((inLen % 8) != 0) ? 1 : 0);
+ long[] out = new long[outLen];
+
+ for (int i = 0; i < (outLen - 1); i++ ) {
+ for (int j = 0; j < 8; j++ ) {
+ out[i] |= ((bArr[(i * 8) + j] & 0xFFL) << (j * 8));
+ }
+ }
+ int inTail = 8 * (outLen - 1);
+ int rem = inLen - inTail;
+ for (int j = 0; j < rem; j++ ) {
+ out[outLen - 1] |= ((bArr[inTail + j] & 0xFFL) << (j * 8));
+ }
+ return out;
+ }
+
+ private static int[] stringToInts(String in) {
+ byte[] bArr = in.getBytes(UTF_8);
+ int inLen = bArr.length;
+ int outLen = (inLen / 4) + (((inLen % 4) != 0) ? 1 : 0);
+ int[] out = new int[outLen];
+
+ for (int i = 0; i < (outLen - 1); i++ ) {
+ for (int j = 0; j < 4; j++ ) {
+ out[i] |= ((bArr[(i * 4) + j] & 0xFFL) << (j * 8));
+ }
+ }
+ int inTail = 4 * (outLen - 1);
+ int rem = inLen - inTail;
+ for (int j = 0; j < rem; j++ ) {
+ out[outLen - 1] |= ((bArr[inTail + j] & 0xFFL) << (j * 8));
+ }
+ return out;
+ }
+
+ /**
+ * Returns a string of spaced hex bytes in Big-Endian order.
+ * @param v the given long
+ * @return string of spaced hex bytes in Big-Endian order.
+ */
+ private static String longToHexBytes(final long v) {
+ final long mask = 0XFFL;
+ final StringBuilder sb = new StringBuilder();
+ for (int i = 8; i-- > 0; ) {
+ final String s = Long.toHexString((v >>> (i * 8)) & mask);
+ sb.append(zeroPad(s, 2)).append(" ");
+ }
+ return sb.toString();
+ }
+
+ /**
+ * Prepend the given string with zeros. If the given string is equal or
greater than the given
+ * field length, it will be returned without modification.
+ * @param s the given string
+ * @param fieldLength desired total field length including the given string
+ * @return the given string prepended with zeros.
+ */
+ private static final String zeroPad(final String s, final int fieldLength) {
+ return characterPad(s, fieldLength, '0', false);
+ }
+
+ /**
+ * Prepend or postpend the given string with the given character to fill the
given field length.
+ * If the given string is equal or greater than the given field length, it
will be returned
+ * without modification.
+ * @param s the given string
+ * @param fieldLength the desired field length
+ * @param padChar the desired pad character
+ * @param postpend if true append the padCharacters to the end of the string.
+ * @return prepended or postpended given string with the given character to
fill the given field
+ * length.
+ */
+ private static final String characterPad(final String s, final int
fieldLength, final char padChar,
+ final boolean postpend) {
+ final char[] chArr = s.toCharArray();
+ final int sLen = chArr.length;
+ if (sLen < fieldLength) {
+ final char[] out = new char[fieldLength];
+ final int blanks = fieldLength - sLen;
+
+ if (postpend) {
+ for (int i = 0; i < sLen; i++) {
+ out[i] = chArr[i];
+ }
+ for (int i = sLen; i < fieldLength; i++) {
+ out[i] = padChar;
+ }
+ } else { //prepend
+ for (int i = 0; i < blanks; i++) {
+ out[i] = padChar;
+ }
+ for (int i = blanks; i < fieldLength; i++) {
+ out[i] = chArr[i - blanks];
+ }
+ }
+
+ return String.valueOf(out);
+ }
+ return s;
+ }
+
+ @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/MurmurHash3v2Test.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/NativeWritableBufferImplTest.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/NativeWritableBufferImplTest.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/NativeWritableBufferImplTest.java
Tue May 21 21:11:49 2024
@@ -0,0 +1,600 @@
+/*
+ * 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.assertNotNull;
+import static org.testng.Assert.assertTrue;
+
+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.ReadOnlyException;
+import org.apache.datasketches.memory.WritableBuffer;
+import org.apache.datasketches.memory.WritableHandle;
+import org.apache.datasketches.memory.WritableMemory;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+
+public class NativeWritableBufferImplTest {
+
+ //Simple Native direct
+
+ @Test
+ public void checkNativeCapacityAndClose() throws Exception {
+ int memCapacity = 64;
+ WritableHandle wmh = WritableMemory.allocateDirect(memCapacity);
+ WritableMemory wmem = wmh.getWritable();
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ assertEquals(wbuf.getCapacity(), memCapacity);
+
+ wmh.close(); //intentional
+ assertFalse(wbuf.isValid());
+
+ wmh.close(); //intentional, nothing to free
+ }
+
+ //Simple Heap arrays
+
+ @Test
+ public void checkBooleanArray() {
+ boolean[] srcArray = { true, false, true, false, false, true, true, false
};
+ boolean[] dstArray = new boolean[8];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getBooleanArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getBooleanArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+ assertTrue(buf.hasArray());
+ }
+
+ @Test
+ public void checkByteArray() {
+ byte[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+ byte[] dstArray = new byte[8];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getByteArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getByteArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getCharArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getCharArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getShortArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getShortArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getIntArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getIntArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getLongArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getLongArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getFloatArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getFloatArray(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];
+
+ Buffer buf = Memory.wrap(srcArray).asBuffer();
+ buf.getDoubleArray(dstArray, 0, 8);
+ for (int i=0; i<8; i++) {
+ assertEquals(dstArray[i], srcArray[i]);
+ }
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ wbuf.getDoubleArray(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 wmem = wrh.getWritable();
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ wbuf.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 wmem = wrh.getWritable();
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ byte[] srcArray = { 1, -2, 3, -4 };
+ wbuf.putByteArray(srcArray, 0, 5); //wrong!
+ } catch (IllegalArgumentException e) {
+ //pass
+ }
+ }
+
+
+ @Test(expectedExceptions = IllegalArgumentException.class)
+ public void checkRegionBounds() throws Exception {
+ int memCapacity = 64;
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory wmem = wrh.getWritable();
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ wbuf.writableRegion(1, 64, wbuf.getTypeByteOrder()); //wrong!
+ }
+ }
+
+ @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);
+ }
+
+ WritableBuffer wbuf = WritableBuffer.writableWrap(byteBuf);
+
+ for (int i=0; i<memCapacity; i++) {
+ assertEquals(wbuf.getByte(), byteBuf.get(i));
+ }
+
+ assertTrue(wbuf.hasByteBuffer());
+ ByteBuffer byteBuf2 = wbuf.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);
+ }
+
+ Buffer buf = WritableBuffer.writableWrap(byteBuf);
+
+ for (int i = 0; i < memCapacity; i++) {
+ assertEquals(buf.getByte(), 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();
+ byteBufRO.order(ByteOrder.nativeOrder());
+ assertTrue(true);
+ WritableBuffer wbuf = WritableBuffer.writableWrap(byteBufRO);
+ assertTrue(wbuf.isReadOnly());
+ }
+
+ @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());
+
+ Buffer buf = Buffer.wrap(byteBufRO);
+
+ for (int i = 0; i < memCapacity; i++) {
+ assertEquals(buf.getByte(), 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());
+
+ WritableBuffer.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);
+ }
+
+ Buffer buf = Buffer.wrap(byteBuf);
+
+ for (int i=0; i<memCapacity; i++) {
+ assertEquals(buf.getByte(), byteBuf.get(i));
+ }
+
+ //println( mem.toHexString("HeapBB", 0, memCapacity));
+ }
+
+ @Test
+ public void checkIsDirect() throws Exception {
+ int memCapacity = 64;
+ WritableBuffer mem =
WritableMemory.allocate(memCapacity).asWritableBuffer();
+ assertFalse(mem.isDirect());
+ try (WritableHandle wrh = WritableMemory.allocateDirect(memCapacity)) {
+ WritableMemory mem2 = wrh.getWritable();
+ WritableBuffer wbuf = mem2.asWritableBuffer();
+ assertTrue(wbuf.isDirect());
+ wrh.close(); //immediate close
+ }
+ }
+
+ @Test
+ public void checkIsReadOnly() {
+ long[] srcArray = { 1, -2, 3, -4, 5, -6, 7, -8 };
+
+ WritableBuffer wbuf =
WritableMemory.writableWrap(srcArray).asWritableBuffer();
+ assertFalse(wbuf.isReadOnly());
+
+ Buffer buf = wbuf;
+ assertFalse(buf.isReadOnly());
+
+ for (int i = 0; i < srcArray.length; i++) {
+ assertEquals(buf.getLong(), srcArray[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};
+
+ Buffer buf1 = Memory.wrap(arr1).asBuffer();
+ Buffer buf2 = Memory.wrap(arr2).asBuffer();
+ Buffer buf3 = Memory.wrap(arr3).asBuffer();
+
+ int comp = buf1.compareTo(0, 3, buf2, 0, 3);
+ assertEquals(comp, 0);
+ comp = buf1.compareTo(0, 4, buf2, 0, 4);
+ assertEquals(comp, -1);
+ comp = buf2.compareTo(0, 4, buf1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = buf1.compareTo(0, 4, buf3, 0, 5);
+ assertEquals(comp, -1);
+ comp = buf3.compareTo(0, 5, buf1, 0, 4);
+ assertEquals(comp, 1);
+ }
+
+ @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);
+
+ Buffer buf1 = mem1.asBuffer();
+ Buffer buf2 = mem2.asBuffer();
+ Buffer buf3 = mem3.asBuffer();
+
+ int comp = buf1.compareTo(0, 3, buf2, 0, 3);
+ assertEquals(comp, 0);
+ comp = buf1.compareTo(0, 4, buf2, 0, 4);
+ assertEquals(comp, -1);
+ comp = buf2.compareTo(0, 4, buf1, 0, 4);
+ assertEquals(comp, 1);
+ //different lengths
+ comp = buf1.compareTo(0, 4, buf3, 0, 5);
+ assertEquals(comp, -1);
+ comp = buf3.compareTo(0, 5, buf1, 0, 4);
+ assertEquals(comp, 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 checkDuplicate() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ for (int i = 0; i < 64; i++) { wmem.putByte(i, (byte)i); }
+
+ WritableBuffer wbuf = wmem.asWritableBuffer().writableDuplicate();
+ wbuf.checkValidAndBounds(0, 64);
+ for (int i = 0; i < 64; i++) {
+ assertEquals(wbuf.getByte(), i);
+ }
+ Buffer buf = wmem.asBuffer().duplicate();
+ for (int i = 0; i < 64; i++) {
+ assertEquals(buf.getByte(), i);
+ }
+
+ WritableMemory wmem2 = wbuf.asWritableMemory();
+ for (int i = 0; i < 64; i++) {
+ assertEquals(wmem2.getByte(i), i);
+ }
+ WritableMemory wmem3 = wbuf.asWritableMemory();
+ wmem3.checkValidAndBounds(0, 64);
+ }
+
+ @Test
+ public void checkCumAndRegionOffset() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ WritableMemory reg = wmem.writableRegion(32, 32);
+ WritableBuffer buf = reg.asWritableBuffer();
+ assertEquals(buf.getRegionOffset(), 32);
+ assertEquals(buf.getRegionOffset(0), 32);
+ assertEquals(buf.getCumulativeOffset(), 32 + 16);
+ assertEquals(buf.getCumulativeOffset(0), 32 + 16);
+ }
+
+ @Test
+ public void checkIsSameResource() {
+ byte[] byteArr = new byte[64];
+ WritableBuffer wbuf1 =
WritableMemory.writableWrap(byteArr).asWritableBuffer();
+ WritableBuffer wbuf2 =
WritableMemory.writableWrap(byteArr).asWritableBuffer();
+ assertTrue(wbuf1.isSameResource(wbuf2));
+ }
+
+ @Test
+ public void checkDegenerateRegionReturn() {
+ Memory mem = Memory.wrap(new byte[0]);
+ Buffer buf = mem.asBuffer();
+ Buffer reg = buf.region();
+ assertEquals(reg.getCapacity(), 0);
+ }
+
+ @Test
+ public void checkAsWritableMemoryRO() {
+ ByteBuffer bb = ByteBuffer.allocate(64);
+ WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
+ WritableMemory wmem = wbuf.asWritableMemory(); //OK
+ assertNotNull(wmem);
+
+ try {
+ Buffer buf = Buffer.wrap(bb.asReadOnlyBuffer());
+ wbuf = (WritableBuffer) buf;
+ wmem = wbuf.asWritableMemory();
+ Assert.fail();
+ } catch (ReadOnlyException expected) {
+ // expected
+ }
+ }
+
+ @Test
+ public void checkWritableDuplicateRO() {
+ ByteBuffer bb = ByteBuffer.allocate(64);
+ WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
+ @SuppressWarnings("unused")
+ WritableBuffer wdup = wbuf.writableDuplicate();
+
+ try {
+ Buffer buf = Buffer.wrap(bb);
+ wbuf = (WritableBuffer) buf;
+ @SuppressWarnings("unused")
+ WritableBuffer wdup2 = wbuf.writableDuplicate();
+ Assert.fail();
+ } catch (ReadOnlyException expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void checkWritableRegionRO() {
+ ByteBuffer bb = ByteBuffer.allocate(64);
+ WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
+ @SuppressWarnings("unused")
+ WritableBuffer wreg = wbuf.writableRegion();
+
+ try {
+ Buffer buf = Buffer.wrap(bb);
+ wbuf = (WritableBuffer) buf;
+ @SuppressWarnings("unused")
+ WritableBuffer wreg2 = wbuf.writableRegion();
+ Assert.fail();
+ } catch (ReadOnlyException expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void checkWritableRegionWithParamsRO() {
+ ByteBuffer bb = ByteBuffer.allocate(64);
+ WritableBuffer wbuf = WritableBuffer.writableWrap(bb);
+ @SuppressWarnings("unused")
+ WritableBuffer wreg = wbuf.writableRegion(0, 1, wbuf.getTypeByteOrder());
+
+ try {
+ Buffer buf = Buffer.wrap(bb);
+ wbuf = (WritableBuffer) buf;
+ @SuppressWarnings("unused")
+ WritableBuffer wreg2 = wbuf.writableRegion(0, 1,
wbuf.getTypeByteOrder());
+ Assert.fail();
+ } catch (ReadOnlyException expected) {
+ // ignore
+ }
+ }
+
+ @Test
+ public void checkZeroBuffer() {
+ WritableMemory wmem = WritableMemory.allocate(8);
+ WritableBuffer wbuf = wmem.asWritableBuffer();
+ WritableBuffer reg = wbuf.writableRegion(0, 0, wbuf.getTypeByteOrder());
+ assertEquals(reg.getCapacity(), 0);
+ }
+
+ @Test
+ public void checkDuplicateNonNative() {
+ WritableMemory wmem = WritableMemory.allocate(64);
+ wmem.putShort(0, (short) 1);
+ Buffer buf = wmem.asWritableBuffer().duplicate(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/NativeWritableBufferImplTest.java
------------------------------------------------------------------------------
svn:executable = *
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]