package ch.ethz.globis.distindex.mapping;

import ch.ethz.globis.distindex.mapping.bst.BSTMapping;
import ch.ethz.globis.distindex.mapping.bst.LongArrayKeyConverter;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

public class BSTMappingTest {

    @Test
    public void testMapping() {
        KeyMapping<long[]> mapping = new BSTMapping<>(new LongArrayKeyConverter(), new String[]{});
        String[] hosts = { "one", "two", "three", "four", "five" };
        for (int i = 0; i < hosts.length; i++) {
            mapping.add(hosts[i]);
            assertEquals(Arrays.asList(hosts).subList(0, i + 1), mapping.getHostIds());
        }
    }

    @Test
    public void testTreeCreation() {
        int size = 16;
        String[] keys = new String[size];
        for (int i = 0; i < size; i++) {
            keys[i] = String.valueOf(i);
        }
        KeyMapping<long[]> mapping = new BSTMapping<>(new LongArrayKeyConverter(), keys);
        System.out.println(mapping.getHosts());
    }

    @Test
    public void testRangeQuery() {
        String[] hosts = { "one", "two", "three", "four" };
        KeyMapping<long[]> mapping = new BSTMapping<>(new LongArrayKeyConverter(), hosts);
        Map<String, String> directMapping = mapping.getHosts();
        long[] start = {1, 1};
        long[] end = {2, 2};
        List<String> hostIds = mapping.getHostIds(start, end);
        assertEqualsListVararg(hostIds, directMapping.get("00"));

        start = new long[] {-1, -1};
        end = new long[] {-5, -5};
        hostIds = mapping.getHostIds(start, end);
        assertEqualsListVararg(hostIds, directMapping.get("11"));

        start = new long[] {-1, -1};
        end = new long[] {5, 5};
        hostIds = mapping.getHostIds(start, end);
        assertEqualsListVararg(hostIds, directMapping.get("00"), directMapping.get("01"),
                directMapping.get("10"), directMapping.get("11"));
    }

    private void assertEqualsListVararg(List<String> current, String... expected) {
        List<String> expectedList = Arrays.asList(expected);
        assertEquals(expectedList, current);
    }
}

@@ -1714,24 +1714,398 @@
 et(%2211%22));%0A%0A
+        start = new long%5B%5D %7B-1, 1%7D;%0A        end = new long%5B%5D %7B-5, 5%7D;%0A        hostIds = mapping.getHostIds(start, end);%0A        assertEqualsListVararg(hostIds, directMapping.get(%2210%22));%0A%0A        start = new long%5B%5D %7B1, -1%7D;%0A        end = new long%5B%5D %7B5, -5%7D;%0A        hostIds = mapping.getHostIds(start, end);%0A        assertEqualsListVararg(hostIds, directMapping.get(%2201%22));%0A%0A
         star

Index distribution Additional test cases for range search results.