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

import net.ranides.assira.collection.ArrayUtils;
import java.util.List;
import org.junit.Test;
import static org.junit.Assert.*;

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

    public NativeArrayListTest() {
    }

    @Test
    public void testWrappingArray() {
        short[] array = new short[]{ 0x3333, 0x3333, 0x3333, 0x3333, 0x3333, 0x3333, 0x3333, 0x3333 };

        List<Short> list = new NativeArrayList<>(array);

        List<Byte> bytes = new NativeArrayList<>(byte[].class, array);

        List<Long> longs = new NativeArrayList<>(long[].class, array);

        longs.set(1, 0x7172737475767778L);

        bytes.set(1, (byte)0x51 );
        bytes.set(2, (byte)0x52 );
        bytes.set(3, (byte)0x53 );
        bytes.set(4, (byte)0x54 );

        assertArrayEquals(
            new short[]{0x5133,0x5352,0x3354,0x3333,0x7778,0x7576,0x7374,0x7172},
            array
        );
        assertEquals(
            ArrayUtils.asList((short)0x5133,(short)0x5352,(short)0x3354,(short)0x3333,(short)0x7778,(short)0x7576,(short)0x7374,(short)0x7172),
            list
        );
        assertEquals(
            ArrayUtils.asList((byte)0x33,(byte)0x51,(byte)0x52,(byte)0x53,(byte)0x54,(byte)0x33,(byte)0x33,(byte)0x33,(byte)0x78,(byte)0x77,(byte)0x76,(byte)0x75,(byte)0x74,(byte)0x73,(byte)0x72,(byte)0x71),
            bytes
        );
    }


    @Test
    public void testResizing() {
        int[] array1 = new int[]{0,1,2,3,4,5,6};
        short[] array2 = new short[]{0,1,2,3,4,5,6};

        List<Integer> list1 = new NativeArrayList<>(array1, 0);
        List<Short> list2 = new NativeArrayList<>(array2, 0);

        for(int i=0; i<=6; i++) {
            list1.add(10 * i);
            list2.add( (short)(10 * i) ); // super, kompilator wymagał rzutowania
        }
        assertArrayEquals(new int[]{0,10,20,30,40,50,60}, array1);
        assertArrayEquals(new short[]{0,10,20,30,40,50,60}, array2);

        try {
            list1.add(99);
            fail("exception expected");
        } catch(IndexOutOfBoundsException ex) {
            assertTrue(true);
        }

        try {
            list1.clear();
            list1.set(15,99);
            fail("exception expected");
        } catch(IndexOutOfBoundsException ex) {
            assertTrue(true);
        }
    }

}
