/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.test;

import java.util.TreeMap;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class TComparatorTest {
    
    
    private static final TMap<String, String> SSMAP = new TMap<String, String>(){

        @Override
        protected String key(int index, int n) {
            assert n==0;
            return "K"+index;
        }
        
        @Override
        protected String value(int index, int n) {
            assert n==0;
            return "V"+index;
        }

        @Override
        protected int cmp(String a, String b) {
            return a.compareTo(b);
        }

        @Override
        protected int hash(String key) {
            return key.hashCode();
        }
    };
        
    @Test
    public void testF1() {
        TComparator<String> cmp = new TComparator<>(SSMAP.range(8));
        TreeMap<String, String> map = new TreeMap<>(cmp);
        SSMAP.range(8).into(map);
        assertEquals(8, map.size());
        assertEquals("{K0=V0, K1=V1, K2=V2, K3=V3, K4=V4, K5=V5, K6=V6, K7=V7}", map.toString());
    }
    

    @Test
    public void testF2() {
        TComparator<String> cmp = new TComparator<>(SSMAP.range(8));
        cmp.hash(1,2).order(7,6,5,4);
        /*
            value 0 1 2 3 4 5 6 7
            hash  1 1 1 1 2 2 2 2
            order 7 7 6 6 5 5 4 4
        */
        
        assertEquals(1, cmp.hashCode(SSMAP.item(0).key()) );
        assertEquals(1, cmp.hashCode(SSMAP.item(1).key()) );
        assertEquals(1, cmp.hashCode(SSMAP.item(2).key()) );
        assertEquals(1, cmp.hashCode(SSMAP.item(3).key()) );
        assertEquals(2, cmp.hashCode(SSMAP.item(4).key()) );
        assertEquals(2, cmp.hashCode(SSMAP.item(5).key()) );
        assertEquals(2, cmp.hashCode(SSMAP.item(6).key()) );
        assertEquals(2, cmp.hashCode(SSMAP.item(7).key()) );
        
        assertArrayEquals(new int[]{1,1,1,1,2,2,2,2}, cmp.hashCodes(SSMAP.range(8)));
        
        TreeMap<String, String> map = new TreeMap<>(cmp);
        SSMAP.range(8).into(map);
        assertEquals(4, map.size());
        assertEquals("[V7, V5, V3, V1]", map.values().toString());
    }
    
}
