/*
 * @author Ranides Atterwim {@literal <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.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 CustomLookupTest {
    
    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 CustomLookup<>($map.comparator())))
			.run();
    }
    
    @Test
    public void testConstruct() {
        assertNotNull( new CustomLookup<>(32, 0.75f, $map.comparator()) );
        assertNotNull( new CustomLookup<>(32, $map.comparator()) );
        assertNotNull( new CustomLookup<>($map.comparator()) );

        assertThrows(IllegalArgumentException.class, () -> 
            new CustomLookup<>(32, 0.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomLookup<>(32, -1.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomLookup<>(32, 1.1f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomLookup<>(32, 2.0f, $map.comparator())
        );
        assertThrows(IllegalArgumentException.class, () -> 
            new CustomLookup<>(-1, 0.5f, $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);
		
		CustomLookup<TPoint> smap1 = new CustomLookup<>(items.keys(), items.valuesInt(), cmp1);
		CustomLookup<TPoint> smap2 = new CustomLookup<>(items.keys(), items.valuesInt(), cmp2);
		CustomLookup<TPoint> smap3 = new CustomLookup<>(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 CustomLookup<>(imap, cmp1).size());
        assertEquals(180, new CustomLookup<>((SortedMap<TPoint,Integer>)imap, cmp1).size());
        
        assertEquals(180, new CustomLookup<>(imap, cmp1).size());
        assertEquals(120, new CustomLookup<>(imap, cmp2).size());
        
        assertEquals(180, new CustomLookup<>((Lookup<TPoint>)smap1, cmp1).size());
        assertEquals(120, new CustomLookup<>((Lookup<TPoint>)smap1, cmp2).size());
        assertEquals(12, new CustomLookup<>((Lookup<TPoint>)smap1, cmp3).size());
        
        assertEquals(180, new CustomLookup<>(items.keys(), items.valuesInt(), cmp1).size());
        assertEquals(120, new CustomLookup<>(items.keys(), items.valuesInt(), cmp2).size());
        assertEquals(12, new CustomLookup<>(items.keys(), items.valuesInt(), cmp3).size());

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