/*
 * @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 net.ranides.test.ContractTesters;
import net.ranides.test.data.TMaps;
import net.ranides.test.data.TPoint;
import org.junit.Test;

import net.ranides.test.data.TMap;

import java.io.Serializable;
import java.util.function.IntSupplier;
import java.util.function.ToIntFunction;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 *
 * @author Ranides Atterwim {@literal <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 OpenLookup<>(), 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());
       
    }
    
}
