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

import java.util.Comparator;
import net.ranides.assira.collection.IntComparator;
import org.junit.Test;
import static org.junit.Assert.*;

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

	@Test
	public void testEqualsObject() {
		assertTrue( CompareUtils.equals("a", "a"));
		assertFalse( CompareUtils.equals("#", "a"));
		assertFalse( CompareUtils.equals(null, "a"));
		assertFalse( CompareUtils.equals("a", null));
		assertTrue( CompareUtils.equals(null, null));
	}
	
	@Test
	public void testEqualsObject_precision() {
		assertTrue( CompareUtils.equals("a", "a", 1));
		assertFalse( CompareUtils.equals("#", "a", 1));
		assertFalse( CompareUtils.equals(null, "a", 1));
		assertFalse( CompareUtils.equals("a", null, 1));
		assertTrue( CompareUtils.equals((Object)null, (Object)null, 1));
		
		assertTrue( CompareUtils.equals((Object)10.0, (Object)9.5, 1));
		assertFalse( CompareUtils.equals((Object)10.0, (Object)8.5, 1));
		assertFalse( CompareUtils.equals(null, (Object)8.5, 1));
		assertFalse( CompareUtils.equals((Object)8.5, null, 1));
	}
	
	@Test
	public void testEqualsNumber() {
		assertTrue( CompareUtils.equals((Number)10.0, (Number)9.5, 1));
		assertFalse( CompareUtils.equals((Number)10.0, (Number)8.5, 1));
		assertFalse( CompareUtils.equals((Number)null, (Number)8.5, 1));
		assertFalse( CompareUtils.equals((Number)8.5, (Number)null, 1));
		assertTrue( CompareUtils.equals((Number)null, (Number)null, 1));
	}
	
	@Test
	public void testCmp() {
		assertTrue( CompareUtils.cmp(1,3) < 0);
		assertTrue( CompareUtils.cmp(5,3) > 0);
		assertTrue( CompareUtils.cmp(5,5) == 0);
	}
	
	@Test
	public void testComparator() {
		assertTrue(CompareUtils.<Integer>comparator(int.class) instanceof IntComparator);
		assertTrue(CompareUtils.<Integer>comparator(Integer.class) instanceof IntComparator);
		assertFalse(CompareUtils.<Integer>comparator(float.class) instanceof IntComparator);
	}
	
	@Test
	public void testReverse() {
		Comparator<Integer> nc = CompareUtils.comparator();
		Comparator<Integer> rc = CompareUtils.reversed(nc);
		assertTrue( rc.compare(1,3) > 0);
		assertTrue( rc.compare(5,3) < 0);
		assertTrue( rc.compare(5,5) == 0);
	}
	
}
