/*
 * @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.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import net.ranides.assira.collection.sets.HashSet;
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 CollectionUtilsTest {

	@Test
    public void testFirst() {
		List<String> list8= Arrays.asList("a", "b", "cc", "d", "ee", "f", "gg", "h");
		List<String> list1 = Arrays.asList("a");
		List<String> list0 = Arrays.asList();
		
        assertEquals("a", CollectionUtils.first(list8).get());
        assertEquals("a", CollectionUtils.first(list1).get());
		
        assertThrows(NoSuchElementException.class, ()-> {
			CollectionUtils.first(list0).get();
		});
		assertThrows(NoSuchElementException.class, ()-> {
			CollectionUtils.first(list8, v -> false).get();
		});
		
		assertEquals("a", CollectionUtils.first(list8, v -> true).get());
		assertEquals("cc", CollectionUtils.first(list8, v -> v.length()>1).get());
    }
	
	@Test
    public void testLast() {
		List<String> list8= Arrays.asList("a", "b", "cc", "d", "ee", "f", "gg", "h");
		List<String> list1 = Arrays.asList("a");
		List<String> list0 = Arrays.asList();
		
        assertEquals("h", CollectionUtils.last(list8).get());
        assertEquals("a", CollectionUtils.last(list1).get());
		
        assertThrows(NoSuchElementException.class, ()-> {
			CollectionUtils.last(list0).get();
		});
		assertThrows(NoSuchElementException.class, ()-> {
			CollectionUtils.last(list8, v -> false).get();
		});
		
		assertEquals("h", CollectionUtils.last(list8, v -> true).get());
		assertEquals("gg", CollectionUtils.last(list8, v -> v.length()>1).get());
    }
	
	@Test
	public void testFilter() {
		Collection<Integer> c = CollectionUtils.filter(Arrays.asList(1,2,3,4,5,6), v -> 0==v%2);
		assertTrue("not expected: "+c, CollectionUtils.equivalent(Arrays.asList(2,4,6), c));
		assertTrue(c.contains(2));
		assertFalse(c.contains(8));
		assertFalse(c.contains(3));
	}
	
	@Test
	public void testEquivalent() {
		List<Integer> a = Arrays.asList(1,2,3,4,5);
		List<Integer> b = Arrays.asList(1,5,4,3,2);
		List<Integer> c = Arrays.asList(1,5,4,3,8);
		List<Integer> d = Arrays.asList(1,5,4,3);
		
		Set<Integer> e = new HashSet<Integer>(a);
		
		assertTrue( CollectionUtils.equivalent(a,b) );
		assertFalse( CollectionUtils.equivalent(a,c) );
		assertFalse( CollectionUtils.equivalent(a,d) );
		
		assertTrue( CollectionUtils.equivalent(a,e) );
		assertTrue( CollectionUtils.equivalent(e,a) );
		assertFalse( CollectionUtils.equivalent(c,e) );
		assertFalse( CollectionUtils.equivalent(e,c) );
	}
	
}
