aboutsummaryrefslogtreecommitdiff
path: root/src/test/java
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java')
-rw-r--r--src/test/java/io/trygvis/btree/BtreeMapTest.java45
-rw-r--r--src/test/java/io/trygvis/btree/HeapPageTest.java51
-rw-r--r--src/test/java/io/trygvis/btree/HeapTest.java26
3 files changed, 103 insertions, 19 deletions
diff --git a/src/test/java/io/trygvis/btree/BtreeMapTest.java b/src/test/java/io/trygvis/btree/BtreeMapTest.java
new file mode 100644
index 0000000..3c771a8
--- /dev/null
+++ b/src/test/java/io/trygvis/btree/BtreeMapTest.java
@@ -0,0 +1,45 @@
+package io.trygvis.btree;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.nio.ByteBuffer;
+
+import static io.trygvis.btree.Bits.intFromBytes;
+import static io.trygvis.btree.HeapTest.Person.randomPerson;
+import static io.trygvis.btree.TestUtils.deleteFile;
+
+public class BtreeMapTest {
+
+ @Test
+ public void testBasic() throws Exception {
+ File f = new File("target/btreemap");
+ BtreeMap<Integer, HeapTest.Person> map = new BtreeMap<>(new BtreeFile(new HeapFile(128, deleteFile(f)), 4), new IntegerSerializer(), new PersonSerializer());
+
+ map.put(1, randomPerson());
+ }
+
+ private static class IntegerSerializer implements BtreeMap.Serializer<Integer> {
+ @Override
+ public byte[] toBytes(Integer integer) {
+ return Bits.toBytes(integer);
+ }
+
+ @Override
+ public Integer fromBytes(byte[] bytes) {
+ return intFromBytes(bytes);
+ }
+ }
+
+ private static class PersonSerializer implements BtreeMap.Serializer<HeapTest.Person> {
+ @Override
+ public byte[] toBytes(HeapTest.Person person) {
+ return person.toBytes();
+ }
+
+ @Override
+ public HeapTest.Person fromBytes(byte[] bytes) {
+ return HeapTest.Person.fromBytes(ByteBuffer.wrap(bytes));
+ }
+ }
+}
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());
}
diff --git a/src/test/java/io/trygvis/btree/HeapTest.java b/src/test/java/io/trygvis/btree/HeapTest.java
index 1d724f4..48a1463 100644
--- a/src/test/java/io/trygvis/btree/HeapTest.java
+++ b/src/test/java/io/trygvis/btree/HeapTest.java
@@ -4,6 +4,8 @@ import org.junit.Test;
import java.io.File;
import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.List;
import java.util.Random;
import static io.trygvis.btree.HeapTest.Person.randomPerson;
@@ -14,6 +16,8 @@ public class HeapTest {
@Test
public void testHeap() throws Exception {
+ List<Person> persons = new ArrayList<Person>();
+
File f = new File("target/heapTest");
HeapFile file = new HeapFile(128, deleteFile(f));
@@ -23,6 +27,7 @@ public class HeapTest {
int pageNo = 0;
for (int i = 0; i < 10; i++) {
Person p = randomPerson();
+ persons.add(p);
byte[] bytes = p.toBytes();
@@ -47,13 +52,15 @@ public class HeapTest {
System.out.println("file.pageCount() = " + file.pageCount());
+ int counter = 0;
for (int i = 0; i < file.pageCount(); i++) {
System.out.println("page #" + i);
page = file.loadPage(i);
- for (ByteBuffer buffer : page.bufferIterator()) {
+ for (ByteBuffer buffer : page.items()) {
Person p = Person.fromBytes(buffer);
System.out.println("p = " + p);
+ assertEquals(persons.get(counter++), p);
}
}
}
@@ -91,6 +98,23 @@ public class HeapTest {
'}';
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof Person)) return false;
+
+ Person person = (Person) o;
+
+ return age == person.age && name.equals(person.name);
+ }
+
+ @Override
+ public int hashCode() {
+ int result = name.hashCode();
+ result = 31 * result + age;
+ return result;
+ }
+
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",