package chunk;

import org.junit.Assert;
import org.junit.Test;
import org.mwdb.Constants;
import org.mwdb.chunk.*;

public class LongLongArrayMapTest implements KChunkListener {

    private int dirtyCount = 0;

    @Test
    public void arrayHeapTest() {
        test(new org.mwdb.chunk.heap.ArrayLongLongArrayMap(this, Constants.MAP_INITIAL_CAPACITY, null));
    }

    @Test
    public void arrayOffHeapTest() {
        test(new org.mwdb.chunk.offheap.ArrayLongLongArrayMap(this, Constants.MAP_INITIAL_CAPACITY, -1));
    }

    private void test(KLongLongArrayMap map) {
        dirtyCount = 0;

        map.put(10, 10);
        Assert.assertTrue(map.size() == 1);
        Assert.assertTrue(map.get(10).length == 1);
        Assert.assertTrue(map.get(10)[0] == 10);

        map.put(10, 100);
        Assert.assertTrue(map.size() == 2);
        Assert.assertTrue(map.get(10).length == 2);
        Assert.assertTrue(map.get(10)[0] == 100);
        Assert.assertTrue(map.get(10)[1] == 10);


        map.put(10, 100);
        Assert.assertTrue(map.size() == 2);
        Assert.assertTrue(map.get(10).length == 2);


        //force reHash
        for (int i = 0; i < Constants.MAP_INITIAL_CAPACITY; i++) {
            map.put(Constants.BEGINNING_OF_TIME, i);
        }
        Assert.assertTrue(map.size() == Constants.MAP_INITIAL_CAPACITY + 2);

        long[] getRes = map.get(Constants.BEGINNING_OF_TIME);
        Assert.assertTrue(getRes.length == Constants.MAP_INITIAL_CAPACITY);
        for (int i = 0; i < Constants.MAP_INITIAL_CAPACITY; i++) {
            Assert.assertTrue(getRes[i] == (Constants.MAP_INITIAL_CAPACITY - i - 1));
        }

        Assert.assertTrue(dirtyCount == 18);

    }

    @Override
    public void declareDirty(KChunk chunk) {
        dirtyCount++;
    }
}

@@ -108,16 +108,212 @@
 chunk.*;
+%0Aimport org.mwdb.chunk.offheap.OffHeapByteArray;%0Aimport org.mwdb.chunk.offheap.OffHeapDoubleArray;%0Aimport org.mwdb.chunk.offheap.OffHeapLongArray;%0Aimport org.mwdb.chunk.offheap.OffHeapStringArray;
 %0A%0Apublic
@@ -617,105 +617,691 @@
     
-test(new org.mwdb.chunk.offheap.ArrayLongLongArrayMap(this, Constants.MAP_INITIAL_CAPACITY, -1));
+OffHeapByteArray.alloc_counter = 0;%0A        OffHeapDoubleArray.alloc_counter = 0;%0A        OffHeapLongArray.alloc_counter = 0;%0A        OffHeapStringArray.alloc_counter = 0;%0A%0A        org.mwdb.chunk.offheap.ArrayLongLongArrayMap map = new org.mwdb.chunk.offheap.ArrayLongLongArrayMap(this, Constants.MAP_INITIAL_CAPACITY, -1);%0A%0A        test(map);%0A        org.mwdb.chunk.offheap.ArrayLongLongArrayMap.free(map.rootAddress());%0A%0A        Assert.assertTrue(OffHeapByteArray.alloc_counter == 0);%0A        Assert.assertTrue(OffHeapDoubleArray.alloc_counter == 0);%0A        Assert.assertTrue(OffHeapLongArray.alloc_counter == 0);%0A        Assert.assertTrue(OffHeapStringArray.alloc_counter == 0);%0A
 %0A   

add test