@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 testGetMaxInt() { int test1 = 1; int test2 = 2; int output = test.getMax(test1, test2); assertEquals(2, output); }
@Test public void testGetMaxString() { String test1 = "1"; String test2 = "2"; String output = test.getMax(test1, test2); assertEquals("2", output); }
@Test public void testGetMaxDouble() { double test1 = 1; double test2 = 2; double output = test.getMax(test1, test2); assertEquals(2.0, output, 1); }
private static <C> void assertListsEqual( List<C> expectedList, List<C> actualList, Comparator<C> comparator, String name) { List<C> notFoundExpected = new ArrayList<C>(); List<C> notFoundActual = new ArrayList<C>(); for (C expected : expectedList) { boolean found = false; for (C actual : actualList) { if (comparator.compare(actual, expected) == 0) { found = true; break; } } if (!found) { notFoundExpected.add(expected); } } for (C actual : actualList) { boolean found = false; for (C expected : expectedList) { if (comparator.compare(actual, expected) == 0) { found = true; break; } } if (!found) { notFoundActual.add(actual); } } if (!notFoundExpected.isEmpty()) { fail("Not found expected " + name + " " + Arrays.toString(notFoundExpected.toArray())); } if (!notFoundActual.isEmpty()) { fail("Not expected " + name + " " + Arrays.toString(notFoundActual.toArray())); } }
@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); }