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

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author ranides
 */
public class SwitchMapTest {

    public SwitchMapTest() {
    }

    @SuppressWarnings("PMD.AvoidReassigningParameters")
    private static String convert(long value, Map<Integer, Character> digits) {
        char[] buffer = new char[65];
        int i = 64;
        while(value > 0) {
            buffer[ i-- ] = digits.get( (int)(value & 0xF) );
            value >>= 4;
        }
        return new String(buffer, i+1, (64 - i));
    }

    @Test
    public void testSimple() {
        SwitchMap<Integer, Character> digits = new SwitchMap<>(
            new Integer[]{ 10, 11, 12, 13, 15, 14, 1, 0, 2, 3, 4, 5, 6, 7, 8, 9  },
            new Character[]{ 'a', 'b', 'c', 'd', 'f', 'e', '1', '0', '2', '3', '4', '5', '6', '7', '8', '9' }
        );

        assertEquals(Character.valueOf('b'), digits.get(11) );
        assertEquals(Character.valueOf('7'), digits.get(7) );
        assertEquals(Character.valueOf('0'), digits.get(0) );

        assertEquals("12345ace", convert(0x12345ace, digits) );
        assertEquals("1", convert(0x1, digits) );
        assertEquals("7fffffff", convert(0x7fffffff, digits) );
        assertEquals("fffffff", convert(0xfffffff, digits) );
    }

    @Test
    public void testArrayConstruction() {
        final String[] NAMES = new String[]{"zero", "one", "two", "three", "four" };
        SwitchMap<Integer, String> numbers = new SwitchMap<>(new Integer[]{0, 1, 2, 3, 4});
        numbers.put(3, NAMES[3]);
        numbers.put(2, NAMES[2]);
        numbers.put(1, NAMES[1]);
        numbers.put(4, NAMES[4]);

        assertEquals( NAMES[1], numbers.get(1) );
        assertEquals( NAMES[2], numbers.get(2) );
        assertEquals( NAMES[3], numbers.get(3) );
        assertEquals( null, numbers.get(0) );
    }

    @Test
    public void testCollectConstruction() {
        Map<Integer, String> source = new HashMap<Integer, String>(){{
            put(0, "zero");
            put(1, "one");
            put(2, "two");
            put(3, "three");
            put(4, "four");
        }};
        SwitchMap<Integer, String> numbers = new SwitchMap<>(source.keySet());
        for(Entry<Integer, String> entry : source.entrySet() ) {
            numbers.put( entry.getKey(), entry.getValue() );
        }

        assertEquals( source.get(1), numbers.get(1) );
        assertEquals( source.get(2), numbers.get(2) );
        assertEquals( source.get(3), numbers.get(3) );
        assertEquals( source.get(0), numbers.get(0) );

    }

}
