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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.ranides.assira.ContractTesters;
import net.ranides.assira.collection.mockup.TMaps;
import net.ranides.assira.collection.sets.IntAVLTreeSet;
import net.ranides.assira.collection.sets.IntSet;
import static net.ranides.assira.junit.NewAssert.*;
import net.ranides.assira.test.TCollection;
import org.junit.Test;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IntArrayListTest {
    
    private final TCollection<Integer> $var = TMaps.MAP_IS.keys();

    @Test
    public void testSuite() {
        ContractTesters.runner()
			.param("collection!", $var)
			.function(new int[0], array -> new IntArrayList($var.list(array).valuesInt()))
			.run();
    }

    @Test
    public void testConstruct() {
        assertNotNull(new IntArrayList(32));
        assertThrows(IllegalArgumentException.class, ()->{
            new IntArrayList(-1).size();
        });
    }
    
    @Test
    public void testConstructCopy() {
        IntRange r = new IntRange(0, 10);
        List<Integer> exp = Arrays.asList(0,1,2,3,4,5,6,7,8,9);
        
        assertEquals(10, new IntArrayList(r).size());
        assertEquals(exp, new IntArrayList(r));
        assertEquals(exp, new IntArrayList(new IntArrayList(r)));
        assertEquals(new IntArrayList(r), new IntArrayList(new IntArrayList(r)));
        assertEquals(Arrays.asList(1,2,3,8), new IntArrayList(new int[]{1,2,3,8}));
        assertEquals(Arrays.asList(2,3,8), new IntArrayList(new int[]{1,2,3,8,10,11},1,3));
        
        assertEquals(exp, new IntArrayList(r.iterator()));
        assertEquals(exp, new IntArrayList(new ArrayList<>(r).iterator()));
        
		IntSet set = new IntAVLTreeSet(new int[]{1,3,5,7});
		assertEquals(Arrays.asList(1,3,5,7), new IntArrayList(set));
    }
    
    @Test
    public void testResize() {
        IntArrayList list = new IntArrayList();
        list.add(1);
        list.resize(5);
        assertEquals(5, list.size());
        assertEquals(Arrays.asList(1,0,0,0,0), list);
        
        list.set(1,4);
        list.set(2,5);
        list.set(3,6);
        list.add(8);
        
        assertEquals(Arrays.asList(1,4,5,6,0,8), list);
        
        list.resize(3);
        assertEquals(3, list.size());
        assertEquals(Arrays.asList(1,4,5), list);
        
        list.resize(20);
        assertEquals(20, list.size());
    }
        
    @Test
    public void testCompare() {
        IntArrayList ilist1 = new IntArrayList(new int[]{1,3,5,7});
        IntArrayList ilist2 = new IntArrayList(new int[]{1,3,5,7});
        IntArrayList ilist3 = new IntArrayList(new int[]{1,3,5});
        IntArrayList ilist4 = new IntArrayList(new int[]{1,2,5,7});
        IntArrayList ilist5 = new IntArrayList(new int[]{1,4,5,7});
        
        assertTrue( ilist1.compareTo(ilist1) == 0 );
        assertTrue( ilist1.compareTo(ilist2) == 0 );
        assertTrue( ilist1.compareTo(ilist3) > 0 );
        assertTrue( ilist1.compareTo(ilist4) > 0 );
        assertTrue( ilist1.compareTo(ilist5) < 0 );
    }
    
    @Test
    public void testCompare_List() {
        IntArrayList list0 = new IntArrayList(new int[]{1,3,5,7});
        
        List<Integer> ilist0 = list0;
        List<Integer> ilist1 = new IntArrayList(new int[]{1,3,5,7});
        List<Integer> ilist2 = new IntArrayList(new int[]{1,3,5,7});
        List<Integer> ilist3 = new IntArrayList(new int[]{1,3,5});
        List<Integer> ilist4 = new IntArrayList(new int[]{1,2,5,7});
        List<Integer> ilist5 = new IntArrayList(new int[]{1,4,5,7});
        List<Integer> ilist6 = new IntArrayList(new int[]{1,4,5,7,9});
        
        List<Integer> list1 = Arrays.asList(1,3,5,7);
        List<Integer> list2 = Arrays.asList(1,3,5,7);
        List<Integer> list3 = Arrays.asList(1,3,5);
        List<Integer> list4 = Arrays.asList(1,2,5,7);
        List<Integer> list5 = Arrays.asList(1,4,5,7);
        List<Integer> list6 = Arrays.asList(1,4,5,7,9);
        
        assertTrue( list0.compareTo(ilist0) == 0 );
        
        assertTrue( list0.compareTo(ilist1) == 0 );
        assertTrue( list0.compareTo(ilist2) == 0 );
        assertTrue( list0.compareTo(ilist3) > 0 );
        assertTrue( list0.compareTo(ilist4) > 0 );
        assertTrue( list0.compareTo(ilist5) < 0 );
        assertTrue( list0.compareTo(ilist6) < 0 );
        
        assertTrue( list0.compareTo(list1) == 0 );
        assertTrue( list0.compareTo(list2) == 0 );
        assertTrue( list0.compareTo(list3) > 0 );
        assertTrue( list0.compareTo(list4) > 0 );
        assertTrue( list0.compareTo(list5) < 0 );
        assertTrue( list0.compareTo(list6) < 0 );
    }
	
    
}
