/*
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.collection.maps;

import java.io.Serializable;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;
import net.ranides.test.ContractTesters;
import net.ranides.test.data.TMaps;
import net.ranides.test.data.TPoint;
import net.ranides.test.data.TMap;
import net.ranides.test.data.TWrapper;
import org.junit.Assert;
import org.junit.Test;

/**
 *
 * @author Ranides Atterwim {@literal <ranides@gmail.com>}
 */
public class MapUtilsTest {

    private final TMap<TPoint, Integer> $map = TMaps.MAP_PI;

    @Test
    public void testSynchronized() {
        ContractTesters.runner()
                .param("map!", $map)
                .ignore("MapTester.basicEquals_HC")
                .function(new int[0], array -> $map.list(array).into(MapUtils.synchronizedMultiMap(new OpenMultiMap<>())))
                .run();
    }

    @Test
    public void view() {
        Map<Object,Number> a = new OpenMap<>();
        Map<Object,?> b = a;
        Map<?,?> c = a;

        Map<String, Number> ma = MapUtils.keyMap(a, String::valueOf);
        Map<String, Object> mb = MapUtils.keyMap(b, String::valueOf);
        Map<String, Object> mc = MapUtils.keyMap(c, String::valueOf);

        Assert.assertNotNull(ma);
        Assert.assertNotNull(mb);
        Assert.assertNotNull(mc);
    }

    @Test
    public void testMap_Projection() {
        TMap<TPoint, Integer> tmap = TMaps.MAP_PI;
        
        Function<int[], Map<TPoint, Integer>> gf = (Function<int[], Map<TPoint, Integer>> & Serializable)(int[] array) -> {
            Map<TPoint, TWrapper<Integer>> mmap = new OpenMap<>();
            TWrapper.into(mmap, tmap.list(array));
            return MapUtils.valueProjection(mmap, TWrapper.projection());
        };
        
        ContractTesters.runner()
            .param("map!", tmap)
            .function(new int[0], gf)
			.run();
        
    }
    
    @Test
    public void testMap_Function() {
        TMap<TPoint, Integer> tmap = TMaps.MAP_PI;
        
        Function<int[], Map<TPoint, Integer>> gf = (Function<int[], Map<TPoint, Integer>> & Serializable)(int[] array) -> {
            Map<TPoint, TWrapper<Integer>> mmap = new OpenMap<>();
            TWrapper.into(mmap, tmap.list(array));
            return MapUtils.valueAdapter(mmap, TWrapper.unwrap());
        };
        
        ContractTesters.runner()
            .ignore(Pattern.compile(".*_HC"))
            .ignore("MapTester.basicEntrySet_Iterator_SetValue")
            .ignore("MapTester.basicPut")
            .ignore("MapTester.basicPutRandom")
            .ignore("MapTester.basicPutReplace")
            .param("map!", tmap)
            .function(new int[0], gf)
			.run();
        
    }
       
}
