/*
 * @author Ranides Atterwim <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.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.*;

/**
 * 
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class CustomMapTest {
    
    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 CustomMap<>($map.comparator())))
			.run();
    }
    
    @Test
    public void testConstruct() {
        assertNotNull( new CustomMap<>(32, 0.75f, $map.comparator()) );
        assertNotNull( new CustomMap<>(32, $map.comparator()) );
        assertNotNull( new CustomMap<>($map.comparator()) );

        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>(32, 0.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>(32, -1.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>(32, 1.1f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>(32, 2.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>(-1, 0.5f, $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);
        Map<TPoint, Integer> imap = items.into(new TreeMap<>(cmp1));
        
        assertEquals(180, new CustomMap<>(imap, $map.comparator()).size());
        
        assertEquals(180, new CustomMap<>(new CustomMap<>(imap, cmp1), cmp1).size());
        assertEquals(120, new CustomMap<>(new CustomMap<>(imap, cmp1), cmp2).size());
        assertEquals(12, new CustomMap<>(new CustomMap<>(imap, cmp1), cmp3).size());
        assertEquals(120, new CustomMap<>(new CustomMap<>(imap, cmp2), cmp1).size());
        assertEquals(120, new CustomMap<>(new CustomMap<>(imap, cmp2), cmp2).size());
        assertEquals(12, new CustomMap<>(new CustomMap<>(imap, cmp2), cmp3).size());
        assertEquals(12, new CustomMap<>(new CustomMap<>(imap, cmp3), cmp1).size());
        assertEquals(12, new CustomMap<>(new CustomMap<>(imap, cmp3), cmp2).size());
        assertEquals(12, new CustomMap<>(new CustomMap<>(imap, cmp3), cmp3).size());
        
        
        assertEquals(120, new CustomMap<>(items.keys(), items.values(), cmp2).size());
        
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomMap<>($map.range(60).keys(), $map.range(40).values(), cmp1)
        );
    }
    
}
