public void testExplicit_withDuplicates() { try { Ordering.explicit(1, 2, 3, 4, 2); fail(); } catch (IllegalArgumentException expected) { } }
public void testExplicit_sortingExample() { Comparator<Integer> c = Ordering.explicit(2, 8, 6, 1, 7, 5, 3, 4, 0, 9); List<Integer> list = Arrays.asList(0, 3, 5, 6, 7, 8, 9); Collections.sort(list, c); assertThat(list).containsExactly(8, 6, 7, 5, 3, 0, 9).inOrder(); reserializeAndAssert(c); }
public void testExplicit_two() { Comparator<Integer> c = Ordering.explicit(42, 5); assertEquals(0, c.compare(5, 5)); assertTrue(c.compare(5, 42) > 0); assertTrue(c.compare(42, 5) < 0); try { c.compare(5, 666); fail(); } catch (IncomparableValueException expected) { assertEquals(666, expected.value); } new EqualsTester() .addEqualityGroup(c, Ordering.explicit(42, 5)) .addEqualityGroup(Ordering.explicit(5, 42)) .addEqualityGroup(Ordering.explicit(42)) .testEquals(); reserializeAndAssert(c); }
public void testExplicit_none() { Comparator<Integer> c = Ordering.explicit(Collections.<Integer>emptyList()); try { c.compare(0, 0); fail(); } catch (IncomparableValueException expected) { assertEquals(0, expected.value); } reserializeAndAssert(c); }
private static Ordering<String> createExplicitComparator(String[] elements) { // Collapse equal elements, which Ordering.explicit() doesn't support, while // maintaining the ordering by first occurrence. Set<String> elementsPlus = Sets.newLinkedHashSet(); elementsPlus.add(BEFORE_FIRST); elementsPlus.add(BEFORE_FIRST_2); elementsPlus.addAll(Arrays.asList(elements)); elementsPlus.add(AFTER_LAST); elementsPlus.add(AFTER_LAST_2); return Ordering.explicit(Lists.newArrayList(elementsPlus)); }
public void testExplicit_one() { Comparator<Integer> c = Ordering.explicit(0); assertEquals(0, c.compare(0, 0)); try { c.compare(0, 1); fail(); } catch (IncomparableValueException expected) { assertEquals(1, expected.value); } reserializeAndAssert(c); assertEquals("Ordering.explicit([0])", c.toString()); }
public void testCompound_instance_generics() { Ordering<Object> objects = Ordering.explicit((Object) 1); Ordering<Number> numbers = Ordering.explicit((Number) 1); Ordering<Integer> integers = Ordering.explicit(1); // Like by like equals like Ordering<Number> a = numbers.compound(numbers); // The compound takes the more specific type of the two, regardless of order Ordering<Number> b = numbers.compound(objects); Ordering<Number> c = objects.compound(numbers); Ordering<Integer> d = numbers.compound(integers); Ordering<Integer> e = integers.compound(numbers); // This works with three levels too (IDEA falsely reports errors as noted // below. Both javac and eclipse handle these cases correctly.) Ordering<Number> f = numbers.compound(objects).compound(objects); // bad IDEA Ordering<Number> g = objects.compound(numbers).compound(objects); Ordering<Number> h = objects.compound(objects).compound(numbers); Ordering<Number> i = numbers.compound(objects.compound(objects)); Ordering<Number> j = objects.compound(numbers.compound(objects)); // bad IDEA Ordering<Number> k = objects.compound(objects.compound(numbers)); // You can also arbitrarily assign a more restricted type - not an intended // feature, exactly, but unavoidable (I think) and harmless Ordering<Integer> l = objects.compound(numbers); // This correctly doesn't work: // Ordering<Object> m = numbers.compound(objects); // Sadly, the following works in javac 1.6, but at least it fails for // eclipse, and is *correctly* highlighted red in IDEA. // Ordering<Object> n = objects.compound(numbers); }
@Test(groups = "ordering") public void orderingExplicit() { // Which is better? Phantom Menace or A New Hope? assertEquals( Ordering.explicit( phantomMenace, attackOfTheClones, revengeOfTheSith, returnOfTheJedi, aNewHope, empireStrikesBack) .max(revengeOfTheSith, aNewHope), aNewHope); }
@Test(groups = "ordering") public void orderingExplicitAnotherTest() { StarWarsEpisode planetOfTheApes = new StarWarsEpisode("Planet of the Apes", 12, 1965); // Which is better? Planet of the Apes or A New Hope? try { Ordering.explicit( phantomMenace, attackOfTheClones, revengeOfTheSith, returnOfTheJedi, aNewHope, empireStrikesBack) .max(planetOfTheApes, aNewHope); } catch (Exception ive) { assertTrue(true); } }
@GwtIncompatible("too slow") public void testCombinationsExhaustively_startingFromExplicit() { testExhaustively(Ordering.explicit("a", "b", "c", "d"), "a", "b", "d"); }