/*
 * @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.Function;
import java.util.function.IntUnaryOperator;
import java.util.regex.Pattern;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.collection.mockup.TMaps;
import net.ranides.assira.collection.mockup.TPoint;
import net.ranides.assira.functional.IntCoFunction;
import net.ranides.assira.functional.IntProjectionFunction;
import net.ranides.assira.test.TMap;
import org.junit.Test;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class LookupUtilsTest {
    
    @Test
    public void testMap_Projection() {
        TMap<TPoint, Integer> tmap = TMaps.MAP_PI;
        
        IntUnaryOperator fget = (IntUnaryOperator & Serializable)v -> v / 100;
        IntCoFunction fset = (IntCoFunction & Serializable)v -> 100 * v;

        Function<int[], Lookup<TPoint>> gf = (Function<int[], Lookup<TPoint>> & Serializable)(int[] array) -> {
            Lookup<TPoint> mmap = new HashLookup<>();
            for(int v : array) {
                mmap.put(tmap.item(v).key(), fset.invert(tmap.item(v).value()));
            }
            return LookupUtils.map(mmap, IntProjectionFunction.produce(fget, fset));
        };
        
        ContractTesters.runner()
            .param("map!", tmap)
            .function(new int[0], gf)
			.run();
        
    }
    
    @Test
    public void testMap_Function() {
        TMap<TPoint, Integer> tmap = TMaps.MAP_PI;
        
        IntUnaryOperator fget = (IntUnaryOperator & Serializable)v -> v / 100;
        IntCoFunction fset = (IntCoFunction & Serializable)v -> 100 * v;
        
        Function<int[], Lookup<TPoint>> gf = (Function<int[], Lookup<TPoint>> & Serializable)(int[] array) -> {
            Lookup<TPoint> mmap = new HashLookup<>();
            for(int v : array) {
                mmap.put(tmap.item(v).key(), fset.invert(tmap.item(v).value()));
            }
            return LookupUtils.map(mmap, fget);
        };
        
        ContractTesters.runner()
            .ignore(Pattern.compile(".*_HC"))
            .ignore("LookupTester.basicEntrySet_Iterator_SetValue")
            .ignore("LookupTester.logicPut")
            .ignore("LookupTester.basicPut")
            .ignore("LookupTester.basicPutRandom")
            .ignore("LookupTester.basicPutReplace")
                
            .ignore("MapTester.basicPut")
            .ignore("MapTester.basicPutRandom")
            .ignore("MapTester.basicPutReplace")
            .ignore("MapTester.basicEntrySet_Iterator_SetValue")

            .param("map!", tmap)
            .function(new int[0], gf)
			.run();
        
    }
    
}
