package com.github.davidmoten.rtree;

import static org.junit.Assert.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.junit.Test;

import com.github.davidmoten.rtree.fbs.Serializer;
import com.github.davidmoten.rtree.geometry.Geometries;
import com.github.davidmoten.rtree.geometry.Point;

public class SerializersTest {

    @Test
    public void testStringSerialization() throws IOException {
        Serializer<String, Point> serializer = Serializers.flatBuffers().utf8();
        RTree<String, Point> tree = RTree.create();
        tree = tree.add("hello", Geometries.point(1, 2));
        tree = tree.add("there", Geometries.point(3, 4));
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        serializer.serialize(tree, bytes);
        bytes.close();
        {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes.toByteArray());
            RTree<String, Point> tree2 = serializer.deserialize(bytes.size(), input,
                    InternalStructure.DEFAULT);
            assertEquals(2, tree2.size());
        }
        {
            ByteArrayInputStream input = new ByteArrayInputStream(bytes.toByteArray());
            RTree<String, Point> tree2 = serializer.deserialize(bytes.size(), input,
                    InternalStructure.SINGLE_ARRAY);
            assertEquals(2, tree2.size());
        }
    }

}

@@ -205,16 +205,61 @@
 .Test;%0A%0A
+import com.github.davidmoten.guavamini.Sets;%0A
 import c
@@ -591,24 +591,408 @@
 s().utf8();%0A
+        checkRoundTrip(serializer);%0A    %7D%0A    %0A    @Test%0A    public void testJavaIoSerialization() throws IOException %7B%0A        Serializer%3CString, Point%3E serializer = Serializers.flatBuffers().javaIo();%0A        checkRoundTrip(serializer);%0A    %7D%0A    %0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private static void checkRoundTrip(Serializer%3CString, Point%3E serializer) throws IOException %7B%0A
         RTre
@@ -1039,31 +1039,54 @@
         
-tree = tree.add
+Entry%3CString, Point%3E a = Entries.entry
 (%22hello%22
@@ -1124,55 +1124,113 @@
     
-tree = tree.add(%22there%22, Geometries.point(3, 4)
+Entry%3CString, Point%3E b = Entries.entry(%22there%22, Geometries.point(3, 4));%0A        tree = tree.add(a).add(b
 );%0A 
@@ -1627,32 +1627,166 @@
  tree2.size());%0A
+            assertEquals(Sets.newHashSet(a, b),%0A                    Sets.newHashSet(tree2.entries().toList().toBlocking().single()));%0A
         %7D%0A      
@@ -2062,23 +2062,165 @@
 ));%0A        
-%7D
+    assertEquals(Sets.newHashSet(a, b),%0A                    Sets.newHashSet(tree2.entries().toList().toBlocking().single()));%0A        %7D        
 %0A    %7D%0A%0A%7D%0A

add serialization tests