@Test public void testReverseOrderRandomIntegers() { Comparator<Integer> naturalOrder = new NaturalOrder<Integer>(); Comparator<Integer> reverse = CollectionUtil.reverseOrder(naturalOrder); Random random = new Random(243249878l); // Stable "random" sequence for (int i = 0; i < 65536; i++) { // Verified to be ~ 50/50 lt/gt int integer = random.nextInt(); int integerToo = random.nextInt(); assertEquals(0, reverse.compare(integer, integer)); assertEquals(0, reverse.compare(integerToo, integerToo)); int natural = naturalOrder.compare(integer, integerToo); if (natural == 0) { // Actually never hits, but eq case is tested above assertEquals(0, reverse.compare(integer, integerToo)); } else if (natural < 0) { assertTrue(reverse.compare(integer, integerToo) > 0); } else { assertTrue(reverse.compare(integer, integerToo) < 0); } } }
@Test public void testReverseOrder() { Comparator<String> naturalOrder = new NaturalOrder<String>(); Comparator<String> reverse = CollectionUtil.reverseOrder(naturalOrder); assertNotNull(reverse); assertEquals(0, naturalOrder.compare("foo", "foo")); assertEquals(0, reverse.compare("foo", "foo")); assertTrue(naturalOrder.compare("bar", "baz") < 0); assertTrue(reverse.compare("bar", "baz") > 0); assertTrue(naturalOrder.compare("baz", "bar") > 0); assertTrue(reverse.compare("baz", "bar") < 0); }
@Test(expected = IllegalArgumentException.class) public void testReverseOrderNull() { CollectionUtil.reverseOrder(null); }