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

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import net.ranides.assira.collection.mockup.TMaps;
import net.ranides.assira.collection.mockup.TPoint;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.test.TComparator;
import net.ranides.assira.test.TMap;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;

public class RBTreeLookupTest {
    
    private final TMap<TPoint, Integer> $map = TMaps.MAP_PI;
    
    @Test
    public void testSuite() {
        ContractTesters.runner()
            .param("map!", $map)
            .function(new int[0], array -> $map.list(array).into(new RBTreeLookup<>($map.comparator())))
			.run();
    }

    @Test
    public void testConstruct() {
        assertNotNull( new RBTreeLookup<>() );
        assertNotNull( new RBTreeLookup<>($map.comparator()) );
    }
    
    @Test
    public void testConstructCopy() {
        TMap.TItems<TPoint, Integer> items = $map.range(180);
        
		TComparator<TPoint> cmp1 = new TComparator<>(items);
		TComparator<TPoint> cmp2 = new TComparator<>(items).uorder();
		TComparator<TPoint> cmp3 = new TComparator<>(items).hash(1,2,3,4).order(1,2,3,4,5,6,7,8,9,10,11,12);
		items.shuffle(777);
		
		RBTreeLookup<TPoint> smap1 = new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp1);
		RBTreeLookup<TPoint> smap2 = new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp2);
		RBTreeLookup<TPoint> smap3 = new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp3);

		assertEquals(180, smap1.size());
		assertEquals(120, smap2.size());
		assertEquals(12, smap3.size());
		
		Map<TPoint, Integer> imap = items.into(new TreeMap<>(cmp1));
		
        assertEquals(180, new RBTreeLookup<>(imap).size());
        assertEquals(180, new RBTreeLookup<>((SortedMap<TPoint,Integer>)imap).size());
        
        assertEquals(180, new RBTreeLookup<>(smap1).size());
        assertEquals(120, new RBTreeLookup<>(smap2).size());
        
        assertEquals(180, new RBTreeLookup<>((Lookup<TPoint>)smap1).size());
        assertEquals(120, new RBTreeLookup<>((Lookup<TPoint>)smap2).size());
        
        assertEquals(180, new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp1).size());
        assertEquals(120, new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp2).size());
        assertEquals(12, new RBTreeLookup<>(items.keys(), items.valuesInt(), cmp3).size());
        
		assertEquals(180, new RBTreeLookup<>(items.keys(), items.valuesInt()).size());

        assertThrows(IllegalArgumentException.class, () -> 
            new RBTreeLookup<>($map.range(60).keys(), $map.range(40).valuesInt(), cmp1)
        );
    }
    
}
