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

import java.util.NoSuchElementException;
import net.ranides.assira.collection.arrays.IntArrays;
import net.ranides.assira.collection.lists.IntList;
import net.ranides.assira.collection.sets.IntHashSet;
import net.ranides.assira.collection.sets.IntSet;
import static net.ranides.assira.junit.NewAssert.assertThrows;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class IntCollectionUtilsTest {
	
	@Test
    public void testFirst() {
		IntList list8 = IntArrays.asList(1, 2, 13, 4, 15, 6, 17, 8);
		IntList list1 = IntArrays.asList(1);
		IntList list0 = IntArrays.asList();
		
        assertEquals(1, IntCollectionUtils.first(list8));
        assertEquals(1, IntCollectionUtils.first(list1));
		
        assertThrows(NoSuchElementException.class, ()-> {
			IntCollectionUtils.first(list0);
		});
		assertThrows(NoSuchElementException.class, ()-> {
			IntCollectionUtils.first(list8, v -> false);
		});
		
		assertEquals(1, IntCollectionUtils.first(list8, v -> true));
		assertEquals(13, IntCollectionUtils.first(list8, v -> v>10));
    }
	
	@Test
    public void testLast() {
		IntList list8 = IntArrays.asList(1, 2, 13, 4, 15, 6, 17, 8);
		IntList list1 = IntArrays.asList(1);
		IntList list0 = IntArrays.asList();
		
        assertEquals(8, IntCollectionUtils.last(list8));
        assertEquals(1, IntCollectionUtils.last(list1));
		
        assertThrows(NoSuchElementException.class, ()-> {
			IntCollectionUtils.last(list0);
		});
		assertThrows(NoSuchElementException.class, ()-> {
			IntCollectionUtils.last(list8, v -> false);
		});
		
		assertEquals(8, IntCollectionUtils.last(list8, v -> true));
		assertEquals(17, IntCollectionUtils.last(list8, v -> v>10));
    }
	
	@Test
	public void testFilter() {
		IntCollection c = IntCollectionUtils.filter(IntArrays.asList(1,2,3,4,5,6), v -> 0==v%2);
		assertTrue("not expected: "+c, IntCollectionUtils.equivalent(IntArrays.asList(2,4,6), c));
		assertTrue(c.contains(2));
		assertFalse(c.contains(8));
		assertFalse(c.contains(3));
	}
	
	@Test
	public void testEquivalent() {
		IntList a = IntArrays.asList(1,2,3,4,5);
		IntList b = IntArrays.asList(1,5,4,3,2);
		IntList c = IntArrays.asList(1,5,4,3,8);
		IntList d = IntArrays.asList(1,5,4,3);
		
		IntSet e = new IntHashSet(a);
		
		assertTrue( IntCollectionUtils.equivalent(a,b) );
		assertFalse( IntCollectionUtils.equivalent(a,c) );
		assertFalse( IntCollectionUtils.equivalent(a,d) );
		
		assertTrue( IntCollectionUtils.equivalent(a,e) );
		assertTrue( IntCollectionUtils.equivalent(e,a) );
		assertFalse( IntCollectionUtils.equivalent(c,e) );
		assertFalse( IntCollectionUtils.equivalent(e,c) );
	}
	
}
