package ch.ethz.globis.distindex.mapping.zorder;

import org.junit.Ignore;
import org.junit.Test;

import java.math.BigInteger;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertEquals;

public class ZMappingTest {

    @Test
    public void testRangeQuerySimple() {
        int dim = 2;
        int depth = 64;
        ZMapping mapping = new ZMapping(dim, depth);
        mapping.add("one");
        mapping.add("two");
        mapping.add("three");
        mapping.add("four");
        String host;
        host = mapping.get(new long[] {1L, 1L});
        System.out.println(host);
        host = mapping.get(new long[] {-1L, 1L});
        System.out.println(host);
        host = mapping.get(new long[] {1L, -1L});
        System.out.println(host);
        host = mapping.get(new long[] {-1L, -1L});
        System.out.println(host);

        List<String> hosts;
        hosts = mapping.get(new long[]{1L, 1L}, new long[]{100L, 100L});
        System.out.println(hosts);
        assertEquals(1, hosts.size());

        hosts = mapping.get(new long[]{-1L, 1L}, new long[]{-100L, 100L});
        System.out.println(hosts);
        assertEquals(1, hosts.size());

        hosts = mapping.get(new long[]{1L, -1L}, new long[]{100L, -100L});
        System.out.println(hosts);
        assertEquals(1, hosts.size());

        hosts = mapping.get(new long[]{-1L, -1L}, new long[]{-100L, -100L});
        System.out.println(hosts);
        assertEquals(1, hosts.size());

        hosts = mapping.get(new long[]{-1L, -1L}, new long[] {-1L, 1L});
        System.out.println(hosts);
        assertEquals(2, hosts.size());

        hosts = mapping.get(new long[]{-1L, -1L}, new long[] {1L, -1L});
        System.out.println(hosts);
        assertEquals(2, hosts.size());

        hosts = mapping.get(new long[]{-1L, -1L}, new long[] {1L, 1L});
        System.out.println(hosts);
        assertEquals(4, hosts.size());
    }

    @Test
    @Ignore
    public void testReversedArguments() {
        /*
            Both bounds represent the same square, but the second one is not
             represented in lower-left, upper right manner.
         */
        //ToDo handle wrong square bounds
        int dim = 2;
        int depth = 64;
        ZMapping mapping = new ZMapping(dim, depth);
        mapping.add("one");
        mapping.add("two");
        mapping.add("three");
        mapping.add("four");
        List<String> hosts;
        hosts = mapping.get(new long[] {-1L, -1L}, new long[]{1L, 1L} );
        System.out.println(hosts);
        assertEquals(4, hosts.size());

        hosts = mapping.get(new long[] {-1L, 1L}, new long[]{1L, -1L} );
        System.out.println(hosts);
        assertEquals(4, hosts.size());
    }

    @Test
    public void serializeDeserializeTest() {
        int dim = 3;
        int depth = 64;
        ZMapping mapping = new ZMapping(dim, depth);

        //create the mapping
        String hostId;
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            hostId = new BigInteger(32, random).toString();
            mapping.add(hostId);
        }

        //test that the serialization and de-serialization work
        byte[] serializedBytes = mapping.serialize();
        ZMapping deSerializedMapping = ZMapping.deserialize(serializedBytes);
        assertEquals("The decoded mapping is not equal to the original mapping", mapping, deSerializedMapping);
    }
}

@@ -121,16 +121,41 @@
 nteger;%0A
+import java.util.Arrays;%0A
 import j
@@ -239,16 +239,62 @@
 tEquals;
+%0Aimport static org.junit.Assert.assertNotNull;
 %0A%0Apublic
@@ -3524,11 +3524,698 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testPointQuery() %7B%0A        int dim = 2;%0A        int depth = 64;%0A        String%5B%5D hosts = %7B %22one%22, %22two%22, %22three%22, %22four%22 %7D;%0A        ZMapping mapping = new ZMapping(dim, depth);%0A        mapping.add(Arrays.asList(hosts));%0A%0A        assertHostForPoint(mapping, %22one%22, 1L, 1L);%0A        assertHostForPoint(mapping, %22two%22, 1L, -1L);%0A        assertHostForPoint(mapping, %22three%22, -1L, 1L);%0A        assertHostForPoint(mapping, %22four%22, -1L, -1L);%0A    %7D%0A%0A    private void assertHostForPoint(ZMapping mapping, String expectedHostId, long... key) %7B%0A        String hostId = mapping.get(key);%0A        assertNotNull(hostId);%0A        assertEquals(expectedHostId, hostId);%0A    %7D
 %0A%7D%0A

Index distribution Added new test for the ZMapping.