package net.ranides.assira.collection.maps;

import net.ranides.test.ContractTesters;
import net.ranides.test.data.TMaps;
import org.junit.Test;

import net.ranides.assira.collection.sets.OpenSet;
import net.ranides.assira.collection.utils.CollectionSort;
import net.ranides.test.data.TCollection;
import net.ranides.test.data.TMap;

import java.util.List;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;

public class IntArrayMapTest {

    private final TMap<Integer, String> $map = TMaps.MAP_IS;


    @Test
    public void testBasics() throws Exception {
        TCollection.TItems<Integer> keys = $map.keys().range(32).shuffle(5);
        TCollection.TItems<String> values = $map.values().range(32).shuffle(5);


        IntArrayMap<String> map = new IntArrayMap<>(keys.valuesInt(), values.values());

        List<Integer> ekeys = keys.intoList();
        List<String> evalues = values.intoList();

        CollectionSort.sortArrays($map.comparator(), ekeys, evalues);

        assertEquals("[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31]", ekeys.toString());
        assertEquals("[S0.0, S1.0, S2.0, S3.0, S4.0, S5.0, S6.0, S7.0, S8.0, S9.0, S10.0, S11.0, S12.0, S13.0, S14.0, S15.0, S16.0, S17.0, S18.0, S19.0, S20.0, S21.0, S22.0, S23.0, S24.0, S25.0, S26.0, S27.0, S28.0, S29.0, S30.0, S31.0]",evalues.toString());

        assertEquals(new OpenSet<>(ekeys), map.keySet());
        assertEquals(evalues, map.values());
    }

    @Test
    public void testSuite() {
        ContractTesters.runner()
                .param("map!", $map)
                .function(new int[0], array -> new IntArrayMap<>($map.keys().list(array).valuesInt(), $map.values().list(array).values()))
                .ignore(Pattern.compile(".*_HC"))
                .run();
    }

    @Test
    public void testMap() {
        IntArrayMap<String> map = new IntArrayMap<>(
                new int[]{1,2,3,4,5},
                new String[]{"R","Z","c","d","A"}
        );

        assertEquals("Z", map.get(2));
        assertEquals("A", map.get(5));
        assertEquals("R", map.get(1));
        assertEquals(null, map.get(7));
        assertEquals(null, map.get("r"));
    }

}