/*
 * @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.Locale;
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 HashComparatorTest {
	
	private static final HashComparator<String> CMP = new HashComparator<String>() {

		@Override
		public int hashCode(String object) {
			return object.toUpperCase(Locale.ROOT).hashCode();
		}

		@Override
		public int compare(String o1, String o2) {
			return o1.toUpperCase(Locale.ROOT).compareTo(o2.toUpperCase(Locale.ROOT));
		}
		
	};
	
	private static final HashComparator<String> RCMP = CMP.reversed();

	@Test
	public void testCompare() {
		Set<String> set1 = new TreeSet<>(CMP);
		set1.addAll(Arrays.asList("a", "B", "c", "D"));
		
		Set<String> set2 = new TreeSet<>(RCMP);
		set2.addAll(Arrays.asList("a", "B", "c", "D"));
		
		Set<String> set3 = new TreeSet<>();
		set3.addAll(Arrays.asList("a", "B", "c", "D"));

		assertEquals("[a, B, c, D]", set1.toString());
		assertEquals("[D, c, B, a]", set2.toString());
		assertEquals("[B, D, a, c]", set3.toString());
	}
	
	@Test
	public void testHash() {
		assertTrue(CMP.hashCode("A") == CMP.hashCode("a"));
		assertTrue(CMP.equals("A","a"));
		
		assertFalse(CMP.hashCode("B") == CMP.hashCode("a"));
		assertFalse(CMP.equals("B","a"));
		
		assertTrue(RCMP.hashCode("A") == RCMP.hashCode("a"));
		assertTrue(RCMP.equals("A","a"));
		
		assertFalse(RCMP.hashCode("B") == RCMP.hashCode("a"));
		assertFalse(RCMP.equals("B","a"));
	}
	
}
