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

import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import net.ranides.test.data.TMapItems;
import net.ranides.test.data.TMaps;
import net.ranides.test.data.TPoint;
import net.ranides.test.ContractTesters;
import net.ranides.test.data.TComparator;
import net.ranides.test.data.TMap;
import org.junit.Test;
import static net.ranides.assira.junit.NewAssert.*;

/**
 * 
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
public class RBTreeMapTest {
    
    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 RBTreeMap<>($map.comparator())))
			.run();
    }
    
    @Test
    public void testConstruct() {
        assertNotNull( new RBTreeMap<>() );
        assertNotNull( new RBTreeMap<>($map.comparator()) );
    }
    
    @Test
    public void testConstructCopy() {
        TMapItems<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);
		
		RBTreeMap<TPoint, Integer> smap1 = new RBTreeMap<>(items.keys(), items.values(), cmp1);
		RBTreeMap<TPoint, Integer> smap2 = new RBTreeMap<>(items.keys(), items.values(), cmp2);
		RBTreeMap<TPoint, Integer> smap3 = new RBTreeMap<>(items.keys(), items.values(), 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 RBTreeMap<>(imap).size());
        assertEquals(180, new RBTreeMap<>((SortedMap<TPoint,Integer>)imap).size());
        
        assertEquals(180, new RBTreeMap<>(smap1).size());
        assertEquals(120, new RBTreeMap<>(smap2).size());
        
        assertEquals(180, new RBTreeMap<>(items.keys(), items.values(), cmp1).size());
        assertEquals(120, new RBTreeMap<>(items.keys(), items.values(), cmp2).size());
        assertEquals(12, new RBTreeMap<>(items.keys(), items.values(), cmp3).size());
        
		assertEquals(180, new RBTreeMap<>(items.keys(), items.values()).size());

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