diff options
Diffstat (limited to 'src/test/java/io/trygvis/btree')
-rw-r--r-- | src/test/java/io/trygvis/btree/HeapFileTest.java | 27 | ||||
-rw-r--r-- | src/test/java/io/trygvis/btree/HeapPageTest.java | 70 |
2 files changed, 97 insertions, 0 deletions
diff --git a/src/test/java/io/trygvis/btree/HeapFileTest.java b/src/test/java/io/trygvis/btree/HeapFileTest.java new file mode 100644 index 0000000..a9105bb --- /dev/null +++ b/src/test/java/io/trygvis/btree/HeapFileTest.java @@ -0,0 +1,27 @@ +package io.trygvis.btree; + +import org.junit.Test; + +import java.io.File; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class HeapFileTest { + + @Test + public void testBasic() throws Exception { + int pageSize = 128; + File f = new File("target/heap"); + if (f.exists()) { + assertTrue(f.delete()); + } + + HeapFile file = new HeapFile(pageSize, f); + + assertEquals(0, file.pageCount()); + HeapPage page = file.blankPage(); + file.writePage(page); + assertEquals(1, file.pageCount()); + } +} diff --git a/src/test/java/io/trygvis/btree/HeapPageTest.java b/src/test/java/io/trygvis/btree/HeapPageTest.java new file mode 100644 index 0000000..e845a16 --- /dev/null +++ b/src/test/java/io/trygvis/btree/HeapPageTest.java @@ -0,0 +1,70 @@ +package io.trygvis.btree; + +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.Iterator; + +import static io.trygvis.btree.HeapPage.*; +import static org.junit.Assert.*; + +public class HeapPageTest { + + int pageSize = 128; + + @Test + public void testBasic() { + ByteBuffer buffer = ByteBuffer.allocate(pageSize); + + 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}; + + for (int i = 0; i < itemCount; i++) { + page.append(object); + assertEquals("i=" + i, pageSize - headerSize - (object.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); + } + + { + ByteBuffer expectedBuffer = ByteBuffer.allocate(pageSize); + int position = pageSize - itemCount * (object.length + itemSize); + expectedBuffer.putInt(position); + expectedBuffer.position(position); + for (int i = 0; i < itemCount; i++) { + expectedBuffer.put(object); + expectedBuffer.putInt(object.length); + } + assertEquals(pageSize, expectedBuffer.position()); + + byte[] expectedArray = expectedBuffer.array(); + byte[] actualArray = buffer.array(); + assertArrayEquals(expectedArray, actualArray); + } + + { + Iterator<ByteBuffer> it = page.bufferIterator().iterator(); + for (int i = 0; i < itemCount; i++) { + assertTrue(it.hasNext()); + ByteBuffer bb = it.next(); + assertArrayEquals(object, toArray(bb)); + } + assertFalse(it.hasNext()); + } + } + + private byte[] toArray(ByteBuffer bb) { + byte[] bytes = new byte[bb.limit()]; + bb.get(bytes); + return bytes; + } +} |