/*
 * @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.Set;
import java.util.TreeSet;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public class HashIntComparatorTest {
	
	private static final HashIntComparator CMP = new HashIntComparator() {

		@Override
		public int hashCode(int object) {
			return object % 10;
		}

		@Override
		public int compare(int a, int b) {
			return Integer.compare(a%10, b%10);
		}
		
	};
	
	private static final HashIntComparator RCMP = CMP.reversed();

	@Test
	public void testCompare() {
		Set<Integer> set1 = new TreeSet<>(CMP);
		set1.addAll(Arrays.asList(21, 12, 23, 14));
		
		Set<Integer> set2 = new TreeSet<>(RCMP);
		set2.addAll(Arrays.asList(21, 12, 23, 14));
		
		Set<Integer> set3 = new TreeSet<>();
		set3.addAll(Arrays.asList(21, 12, 23, 14));

		assertEquals("[21, 12, 23, 14]", set1.toString());
		assertEquals("[14, 23, 12, 21]", set2.toString());
		assertEquals("[12, 14, 21, 23]", set3.toString());
	}
	
	@Test
	public void testHash() {
		assertTrue(CMP.hashCode((Integer)23) == CMP.hashCode((Integer)33));
		assertTrue(CMP.equals(23,33));
		
		assertFalse(CMP.hashCode((Integer)23) == CMP.hashCode((Integer)24));
		assertFalse(CMP.equals(23,24));
		
		assertTrue(RCMP.hashCode((Integer)23) == RCMP.hashCode((Integer)33));
		assertTrue(RCMP.equals(23,33));
		
		assertFalse(RCMP.hashCode((Integer)23) == RCMP.hashCode((Integer)24));
		assertFalse(RCMP.equals(23,24));
		
	}
	
}
