package com.github.davidmoten.geo.mem;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.UUID;

import org.junit.Test;

import com.google.common.collect.Lists;

public class GeomemTest {

    private static final double topLeftLat = -5;
    private static final double topLeftLong = 100;
    private static final double bottomRightLat = -45;
    private static final double bottomRightLong = 170;
    private static final double PRECISION = 0.00001;

    @Test
    public void testGeomemFindWhenNoData() {
        Geomem<String, String> g = new Geomem<String, String>();
        g.find(topLeftLat, topLeftLong, bottomRightLat, bottomRightLong, 0,
                1000);
    }

    @Test
    public void testGeomemFindWhenOneEntryInsideRegion() {
        Geomem<String, String> g = new Geomem<String, String>();
        g.add(-15, 120, 500, "A1", "a1");
        List<Info<String, String>> list = Lists.newArrayList(g.find(topLeftLat,
                topLeftLong, bottomRightLat, bottomRightLong, 0, 1000));
        assertEquals(1, list.size());
        assertEquals(-15, list.get(0).lat(), PRECISION);
        assertEquals(120, list.get(0).lon(), PRECISION);
        assertEquals(500L, list.get(0).time());
        assertEquals("A1", list.get(0).value());
        assertEquals("a1", list.get(0).id().get());
    }

    @Test
    public void testGeomemFindWhenOneEntryOutsideRegion() {
        Geomem<String, String> g = new Geomem<String, String>();
        g.add(15, 120, 500, "A1", "a1");
        List<Info<String, String>> list = Lists.newArrayList(g.find(topLeftLat,
                topLeftLong, bottomRightLat, bottomRightLong, 0, 1000));
        assertTrue(list.isEmpty());
    }

    @Test
    public void testGeomemManyEntries() {
        System.out.println("maxMemory=" + Runtime.getRuntime().maxMemory()
                / 1048576 + "MB");
        Geomem<String, String> g = new Geomem<String, String>();
        for (int i = 0; i < 10000; i++) {
            if (i % 1000 == 0)
                System.out.println("count="
                        + i
                        + " memUsed="
                        + (Runtime.getRuntime().totalMemory() - Runtime
                                .getRuntime().freeMemory()) / 1048576 + "MB");
            addRandomEntry(g);
        }
        List<Info<String, String>> list = Lists.newArrayList(g.find(topLeftLat,
                topLeftLong, bottomRightLat, bottomRightLong, 0, 1000));
        System.out.println(list.size());
    }

    private void addRandomEntry(Geomem<String, String> g) {
        double lat = GeomemTest.topLeftLat + 5 - Math.random() * 40;
        double lon = GeomemTest.topLeftLong - 5 + Math.random() * 80;
        long t = Math.round(Math.random() * 1200);
        String id = UUID.randomUUID().toString().substring(0, 2);
        g.add(lat, lon, t, id, id);
    }
}

@@ -1751,32 +1751,185 @@
 mpty());%0A    %7D%0A%0A
+    /**%0A     * Indicates 4.5MB per 1000 records. Thus one million entries needs 4500%0A     * entries.%0A     * %0A     * @throws InterruptedException%0A     */%0A
     @Test%0A    pu
@@ -1961,16 +1961,44 @@
 ntries()
+ throws InterruptedException
  %7B%0A     
@@ -2159,32 +2159,110 @@
 ing, String%3E();%0A
+        System.gc();%0A        Thread.sleep(1000);%0A        reportMemoryUsage();%0A
         for (int
@@ -2317,16 +2317,18 @@
 00 == 0)
+ %7B
 %0A       
@@ -2367,38 +2367,15 @@
 nt=%22
-%0A                       
  + i
+);
 %0A
- 
     
@@ -2390,28 +2390,28 @@
     
-       + %22 memUsed=%22
+reportMemoryUsage();
 %0A   
@@ -2411,32 +2411,34 @@
 ();%0A            
+%7D%0A
             + (R
@@ -2437,55 +2437,26 @@
     
-+ (Runtime.getRuntime().totalMemory() - Runtime
+addRandomEntry(g);
 %0A   
@@ -2456,32 +2456,34 @@
 g);%0A        
+%7D%0A
         
             
@@ -2474,111 +2474,106 @@
     
-                .getRuntime().freeMemory()) / 1048576 + %22MB%22);%0A            addRandomEntry(g);%0A        %7D
+reportMemoryUsage();%0A        System.gc();%0A        Thread.sleep(1000);%0A        reportMemoryUsage();
 %0A   
@@ -2758,24 +2758,244 @@
 ist.size());
+%0A%0A    %7D%0A%0A    private void reportMemoryUsage() %7B%0A        System.out.println(%22memUsed=%22%0A                + (Runtime.getRuntime().totalMemory() - Runtime.getRuntime()%0A                        .freeMemory()) / 1048576 + %22MB%22);
 %0A    %7D%0A%0A    

add test for geomem