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

import java.util.AbstractMap;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.collection.mockup.TMaps;
import net.ranides.assira.collection.mockup.TPoint;
import net.ranides.assira.junit.NewAssert;
import net.ranides.assira.test.TMap;
import org.junit.Assert;
import org.junit.Test;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class RandomAccessMapTest {
    
    private final TMap<TPoint, Integer> $map = TMaps.MAP_PI;
    
    @Test
    public void testSuite() {
        ContractTesters.runner()
            .param("map!", $map)
            .function(new int[0], array -> $map.list(array).into(new RandomAccessMap<>()))
			.run();
    }
    
    @Test
    public void testSetValue() {
        RandomAccessMap<String, String> map = new RandomAccessMap<>();
        map.put("a", "1");
        map.put("c", "3");
        map.put("b", "2");
        
        map.values().set(1, "33");
        Assert.assertEquals("{a=>1, c=>33, b=>2}", map.toString());
    }
    
    @Test
    public void testSetKey() {
        RandomAccessMap<String, String> map = new RandomAccessMap<>();
        map.put("a", "1");
        map.put("c", "3");
        map.put("b", "2");
        
        map.keySet().set(1, "d");
        Assert.assertEquals("{a=>1, d=>3, b=>2}", map.toString());
        
        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            map.keySet().set(2, "a");
        });
    }
    
    @Test
    public void testAddEntry() {
        RandomAccessMap<String, String> map = new RandomAccessMap<>();
        map.put("a", "1");
        map.put("c", "3");
        map.put("b", "2");
        
        map.entrySet().add(new AbstractMap.SimpleEntry<>("e","4"));
        Assert.assertEquals("{a=>1, c=>3, b=>2, e=>4}", map.toString());
        
        map.entrySet().add(new AbstractMap.SimpleEntry<>("a","7"));
        Assert.assertEquals("{a=>7, c=>3, b=>2, e=>4}", map.toString());
        
        map.entrySet().set(2, new AbstractMap.SimpleEntry<>("b","9"));
        Assert.assertEquals("{a=>7, c=>3, b=>9, e=>4}", map.toString());

        map.entrySet().set(2, new AbstractMap.SimpleEntry<>("q","10"));
        Assert.assertEquals("{a=>7, c=>3, q=>10, e=>4}", map.toString());

        NewAssert.assertThrows(IllegalArgumentException.class, ()->{
            map.entrySet().set(1, new AbstractMap.SimpleEntry<>("a","2"));
        });
    }
    
}
