aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/io/trygvis/btree/HeapPageTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/io/trygvis/btree/HeapPageTest.java')
-rw-r--r--src/test/java/io/trygvis/btree/HeapPageTest.java51
1 files changed, 33 insertions, 18 deletions
diff --git a/src/test/java/io/trygvis/btree/HeapPageTest.java b/src/test/java/io/trygvis/btree/HeapPageTest.java
index e845a16..b158880 100644
--- a/src/test/java/io/trygvis/btree/HeapPageTest.java
+++ b/src/test/java/io/trygvis/btree/HeapPageTest.java
@@ -3,7 +3,9 @@ package io.trygvis.btree;
import org.junit.Test;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
import java.util.Iterator;
+import java.util.List;
import static io.trygvis.btree.HeapPage.*;
import static org.junit.Assert.*;
@@ -19,30 +21,43 @@ public class HeapPageTest {
HeapPage page = HeapPage.blankHeapPage(buffer);
assertEquals(pageSize - headerSize, page.bytesFree());
- int itemCount = 6;
- byte[] object = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
+ List<byte[]> objects = new ArrayList<>();
+ byte[] appendObject = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf};
+ int objectSize = 16;
- for (int i = 0; i < itemCount; i++) {
- page.append(object);
- assertEquals("i=" + i, pageSize - headerSize - (object.length + itemSize) * (i + 1), page.bytesFree());
+ for (int i = 0; i < 5; i++) {
+ page.append(appendObject);
+ objects.add(appendObject);
+ assertEquals("i=" + i, pageSize - headerSize - (appendObject.length + itemSize) * (i + 1), page.bytesFree());
}
- try {
- page.append(object);
- fail("Expected PageOverflowException");
- } catch (PageOverflowException e) {
- assertEquals(pageSize - headerSize - itemCount * (object.length + itemSize), e.bytesFree);
- assertEquals(object.length, e.bytesRequested);
+ byte[] prependObject = new byte[]{0xf, 0xe, 0xd, 0xc, 0xb, 0xa, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
+ page.prepend(prependObject);
+ objects.add(0, prependObject);
+
+// try {
+// page.append(object);
+// fail("Expected PageOverflowException");
+// } catch (PageOverflowException e) {
+// assertEquals(pageSize - headerSize - objects.size() * (object.length + objects.size()), e.bytesFree);
+// assertEquals(object.length, e.bytesRequested);
+// }
+
+ for (byte[] object : objects) {
+ for (byte b : object) {
+ System.out.print(Integer.toHexString(b));
+ }
+ System.out.println();
}
{
ByteBuffer expectedBuffer = ByteBuffer.allocate(pageSize);
- int position = pageSize - itemCount * (object.length + itemSize);
+ int position = pageSize - objects.size() * (objectSize + itemSize);
expectedBuffer.putInt(position);
expectedBuffer.position(position);
- for (int i = 0; i < itemCount; i++) {
- expectedBuffer.put(object);
- expectedBuffer.putInt(object.length);
+ for (byte[] o : objects) {
+ expectedBuffer.put(o);
+ expectedBuffer.putInt(objectSize);
}
assertEquals(pageSize, expectedBuffer.position());
@@ -52,11 +67,11 @@ public class HeapPageTest {
}
{
- Iterator<ByteBuffer> it = page.bufferIterator().iterator();
- for (int i = 0; i < itemCount; i++) {
+ Iterator<ByteBuffer> it = page.items().iterator();
+ for (byte[] o : objects) {
assertTrue(it.hasNext());
ByteBuffer bb = it.next();
- assertArrayEquals(object, toArray(bb));
+ assertArrayEquals(o, toArray(bb));
}
assertFalse(it.hasNext());
}