diff options
Diffstat (limited to 'src/test/java/io/trygvis')
-rw-r--r-- | src/test/java/io/trygvis/btree/HeapFileTest.java | 11 | ||||
-rw-r--r-- | src/test/java/io/trygvis/btree/HeapTest.java | 103 | ||||
-rw-r--r-- | src/test/java/io/trygvis/btree/TestUtils.java | 15 |
3 files changed, 122 insertions, 7 deletions
diff --git a/src/test/java/io/trygvis/btree/HeapFileTest.java b/src/test/java/io/trygvis/btree/HeapFileTest.java index a9105bb..558f192 100644 --- a/src/test/java/io/trygvis/btree/HeapFileTest.java +++ b/src/test/java/io/trygvis/btree/HeapFileTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import java.io.File; +import static io.trygvis.btree.TestUtils.deleteFile; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -12,16 +13,12 @@ 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); + HeapFile file = new HeapFile(pageSize, deleteFile(new File("target/heap"))); assertEquals(0, file.pageCount()); HeapPage page = file.blankPage(); - file.writePage(page); + long pageNumber = file.writePage(page); + assertEquals(0, pageNumber); assertEquals(1, file.pageCount()); } } diff --git a/src/test/java/io/trygvis/btree/HeapTest.java b/src/test/java/io/trygvis/btree/HeapTest.java new file mode 100644 index 0000000..1d724f4 --- /dev/null +++ b/src/test/java/io/trygvis/btree/HeapTest.java @@ -0,0 +1,103 @@ +package io.trygvis.btree; + +import org.junit.Test; + +import java.io.File; +import java.nio.ByteBuffer; +import java.util.Random; + +import static io.trygvis.btree.HeapTest.Person.randomPerson; +import static io.trygvis.btree.TestUtils.deleteFile; +import static org.junit.Assert.assertEquals; + +public class HeapTest { + + @Test + public void testHeap() throws Exception { + File f = new File("target/heapTest"); + + HeapFile file = new HeapFile(128, deleteFile(f)); + + HeapPage page = file.blankPage(); + + int pageNo = 0; + for (int i = 0; i < 10; i++) { + Person p = randomPerson(); + + byte[] bytes = p.toBytes(); + + System.out.println("p = " + p + ", size = " + bytes.length); + + if (page.bytesFree() < bytes.length) { + System.out.println("Flushing page"); + long actualPageNo = file.writePage(page); + assertEquals(pageNo++, actualPageNo); + page = file.blankPage(); + } + + page.append(bytes); + } + + long actualPageNo = file.writePage(page); + assertEquals(pageNo, actualPageNo); + file.close(); + + System.out.println("-------------------------------------------------------"); + file = new HeapFile(128, f); + + System.out.println("file.pageCount() = " + file.pageCount()); + + for (int i = 0; i < file.pageCount(); i++) { + System.out.println("page #" + i); + page = file.loadPage(i); + + for (ByteBuffer buffer : page.bufferIterator()) { + Person p = Person.fromBytes(buffer); + System.out.println("p = " + p); + } + } + } + + public static class Person { + private final String name; + private final int age; + + public Person(String name, int age) { + this.name = name; + this.age = age; + } + + public byte[] toBytes() { + byte[] s = name.getBytes(); + ByteBuffer buffer = ByteBuffer.allocate(s.length + 8); + buffer.putInt(s.length); + buffer.put(s); + buffer.putInt(age); + return buffer.array(); + } + + public static Person fromBytes(ByteBuffer buffer) { + int stringSize = buffer.getInt(); + byte[] s = new byte[stringSize]; + buffer.get(s); + return new Person(new String(s), buffer.getInt()); + } + + @Override + public String toString() { + return "Person{" + + "name='" + name + '\'' + + ", age=" + age + + '}'; + } + + private final static Random r = new Random(0); + private final static String[] names = new String[]{ + "Lukas", "Emil", "Mathias", "Jonas", "Aleksander", "William", "Oskar", "Magnus", "Markus", "Oliver", + "Nora", "Emma", "Sofie", "Linnea", "Sara", "Emilie", "Ingrid", "Thea", "Leah", "Sofia"}; + + public static Person randomPerson() { + return new Person(names[r.nextInt(names.length)], r.nextInt(80) + 20); + } + } +} diff --git a/src/test/java/io/trygvis/btree/TestUtils.java b/src/test/java/io/trygvis/btree/TestUtils.java new file mode 100644 index 0000000..92565e5 --- /dev/null +++ b/src/test/java/io/trygvis/btree/TestUtils.java @@ -0,0 +1,15 @@ +package io.trygvis.btree; + +import java.io.File; + +import static org.junit.Assert.assertTrue; + +public class TestUtils { + public static File deleteFile(File file) { + if (file.exists()) { + assertTrue(file.delete()); + } + + return file; + } +} |