From 34248e29da79722bfce0e82c7fcd56c559d0cd2e Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Thu, 22 Jan 2015 16:30:26 +0000 Subject: [PATCH] added eg.OffHeapByteArrayExampleTest --- .../java/eg/OffHeapByteArrayExampleTest.java | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/test/java/eg/OffHeapByteArrayExampleTest.java diff --git a/src/test/java/eg/OffHeapByteArrayExampleTest.java b/src/test/java/eg/OffHeapByteArrayExampleTest.java new file mode 100644 index 000000000..bdf3d660c --- /dev/null +++ b/src/test/java/eg/OffHeapByteArrayExampleTest.java @@ -0,0 +1,69 @@ +package eg; + +import net.openhft.chronicle.map.ChronicleMap; +import net.openhft.chronicle.map.ChronicleMapBuilder; +import net.openhft.lang.model.DataValueClasses; +import net.openhft.lang.model.constraints.MaxSize; +import net.openhft.lang.values.ByteValue; +import net.openhft.lang.values.LongValue; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +public class OffHeapByteArrayExampleTest { + + public static final char EXPECTED = 'b'; + + interface ByteArray { + void setByteValueAt(@MaxSize(7) int index, ByteValue value); + + ByteValue getByteValueAt(int index); + } + + private static ChronicleMap chm; + + @BeforeClass + public static void beforeClass() { + chm = ChronicleMapBuilder + .of(LongValue.class, ByteArray.class) + .entries(1000) + .create(); + } + + @AfterClass + public static void afterClass() { + if (chm != null) + chm.close(); + } + + @Test + public void test() { + + // this objects will be reused + ByteValue byteValue = DataValueClasses.newDirectInstance(ByteValue.class); + ByteArray value = chm.newValueInstance(); + LongValue key = chm.newKeyInstance(); + + + key.setValue(1); + + // this is kind of like byteValue[1] = 'b' + byteValue.setValue((byte) EXPECTED); + value.setByteValueAt(1, byteValue); + + chm.put(key, value); + + // clear the byteValue just to prove it works when we read it back + byteValue.setValue((byte) '0'); + + chm.getUsing(key, value); + + + byte actual = value.getByteValueAt(1).getValue(); + Assert.assertEquals(EXPECTED, actual); + + Assert.assertEquals(0, value.getByteValueAt(2).getValue()); + Assert.assertEquals(0, value.getByteValueAt(3).getValue()); + } +}