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

import net.ranides.assira.collection.ByteArray.ByteIterator;
import net.ranides.assira.text.LexicalCast;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class ByteArrayTest {
    
    public ByteArrayTest() {
    }

    @Test
    public void testByte() {
        byte[] input = new byte[]{1,2,3,4,5,6};
        ByteArray array = new ByteArray(input);
        assertEquals(6, array.size());
        assertEquals(3, array.get(2));
        assertEquals(6, array.get(5));
        
        array.set(2, (byte)30);
        array.set(5, (byte)60);
        
        assertArrayEquals(new byte[]{1,2,30,4,5,60}, input);
    }
    
    @Test
    public void testByteIterator() {
        byte[] input = new byte[]{1,2,3,4,5,6};
        ByteIterator iterator = new ByteArray(input).iterator();
        int sum = 0;
        while(iterator.hasNext()) {
            sum += iterator.next();
        }
        assertEquals(21, sum);
        
        
        ByteIterator iterator2 = new ByteArray(input).listIterator(2);
        int sum2 = 0;
        while(iterator2.hasNext()) {
            sum2 += iterator2.next();
        }
        assertEquals(18, sum2);
    }
    
    @Test
    public void testByteIterator2() {
        byte[] input = new byte[]{1,2,3,4,5,6};
        ByteIterator iterator2 = new ByteArray(input).listIterator(2);
        while(iterator2.hasNext()) {
            assertEquals(2, iterator2.nextIndex() - iterator2.previousIndex() );
            iterator2.set( (byte)(iterator2.next()+10) );
        }
        assertArrayEquals(new byte[]{1,2,13,14,15,16}, input);
        
        ByteIterator iterator3 = new ByteArray(input).listIterator(input.length);
        StringBuilder sb = new StringBuilder();
        while(iterator3.hasPrevious()) {
            sb.append( iterator3.previous() ).append(' ');
        }
        assertEquals("16 15 14 13 2 1 ", sb.toString());
    }
    
    @Test
    public void testByteArray() {
        byte[] input = new byte[]{1,2,3,4,5,6};
        byte[] out1 = new ByteArray(input).toArray();
        
        assertArrayEquals(input, out1);
        assertNotSame(input, out1);
        
        byte[] out2 = new ByteArray(input).toArray(1, 4);
        assertArrayEquals(new byte[]{2,3,4}, out2);
    }
    
    @Test
    public void testShort() {
        short[] input = new short[]{0x0102, 0x0304, 0x0506, 0x0708, 0x090A, 0x0B0C};
        ByteArray array = new ByteArray(input);
        assertEquals(12, array.size());
        assertEquals(0x04, array.get(2));
        assertEquals(0x03, array.get(3));
        
        assertEquals(0x06, array.get(4));
        assertEquals(0x05, array.get(5));
        
        array.set(2, (byte)0x7F);
        array.set(5, (byte)0x7E);
        
        assertArrayEquals(
            new short[]{ 0x0102, 0x037F, 0x7E06, 0x0708, 0x090A, 0x0B0C}, 
            input
        );
    }
    
}
