/*
 * @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.io.Serializable;
import java.util.function.IntSupplier;
import java.util.function.ToIntFunction;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.collection.mockup.TMaps;
import net.ranides.assira.collection.mockup.TPoint;
import net.ranides.assira.test.TMap;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class LazyLookupTest {
    
    
	private final TMap<TPoint, Integer> $map = TMaps.MAP_PI;
    
    @Test
    public void testContract() {
		IntSupplier sup1 = (Serializable & IntSupplier) () -> -12;
		ContractTesters.runner()
            .param("map!", $map)
            .param("map.default!", -12)
            .param("map.null!", -12)
            .param("lookup.default!", -12)
            .function(new int[0], array -> $map.list(array).into(new LazyLookup<>(new RBTreeLookup<>($map.comparator()), sup1)))   
			.run();
		assertTrue(true);
    }
    
    @Test
    public void testIfPresent() {
        // tak skonfigurowany lookup łamie częściowo kontrakt (związany z default)
        ToIntFunction<String> sup2 = (Serializable & ToIntFunction<String>) (v) -> null == v ? -2 : Integer.valueOf(v);
        LazyLookup<String> map = new LazyLookup<>(new HashLookup<>(), sup2);
        
        map.put("14", 1444);
        
        assertEquals(1444, map.getIntIfPresent("14"));
        assertEquals(1444, map.getInt("14"));
        assertEquals(1444, map.getIfPresent("14").intValue());
        assertEquals(1444, map.get("14").intValue());

        assertEquals(-2, map.getIntIfPresent("15"));
        assertEquals(-2, map.getIfPresent("16").intValue());
        
        assertEquals(18, map.getInt("18"));
        assertEquals(19, map.get("19").intValue());
       
    }
    
}
