public void testNullsLast() { Ordering<Integer> ordering = Ordering.natural().nullsLast(); Helpers.testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsLast()) .addEqualityGroup(numberOrdering.nullsLast()) .addEqualityGroup(Ordering.natural()) .testEquals(); }
public void testReverse() { Ordering<Number> reverseOrder = numberOrdering.reverse(); Helpers.testComparator(reverseOrder, Integer.MAX_VALUE, 1, 0, -1, Integer.MIN_VALUE); new EqualsTester() .addEqualityGroup(reverseOrder, numberOrdering.reverse()) .addEqualityGroup(Ordering.natural().reverse()) .addEqualityGroup(Collections.reverseOrder()) .testEquals(); }
// A more limited test than the one that follows, but this one uses the // actual public API. public void testArbitrary_withoutCollisions() { List<Object> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(new Object()); } Ordering<Object> arbitrary = Ordering.arbitrary(); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); assertEquals("Ordering.arbitrary()", arbitrary.toString()); }
public void testLexicographicalComparator() { List<float[]> ordered = Arrays.asList( new float[] {}, new float[] {LEAST}, new float[] {LEAST, LEAST}, new float[] {LEAST, (float) 1}, new float[] {(float) 1}, new float[] {(float) 1, LEAST}, new float[] {GREATEST, Float.MAX_VALUE}, new float[] {GREATEST, GREATEST}, new float[] {GREATEST, GREATEST, GREATEST}); Comparator<float[]> comparator = Floats.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); }
public void testLexicographicalComparator() { List<int[]> ordered = Arrays.asList( new int[] {}, new int[] {LEAST}, new int[] {LEAST, LEAST}, new int[] {LEAST, (int) 1L}, new int[] {(int) 1L}, new int[] {(int) 1L, LEAST}, new int[] {GREATEST, (GREATEST - (int) 1L)}, new int[] {GREATEST, GREATEST}, new int[] {GREATEST, GREATEST, GREATEST}); Comparator<int[]> comparator = UnsignedInts.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); }
public void testCompound_static() { Comparator<String> comparator = Ordering.compound( ImmutableList.of( byCharAt(0), byCharAt(1), byCharAt(2), byCharAt(3), byCharAt(4), byCharAt(5))); Helpers.testComparator( comparator, ImmutableList.of( "applesauce", "apricot", "artichoke", "banality", "banana", "banquet", "tangelo", "tangerine")); reserializeAndAssert(comparator); }
@SuppressWarnings("unchecked") // dang varargs public void testLexicographical() { Ordering<String> ordering = Ordering.natural(); Ordering<Iterable<String>> lexy = ordering.lexicographical(); ImmutableList<String> empty = ImmutableList.of(); ImmutableList<String> a = ImmutableList.of("a"); ImmutableList<String> aa = ImmutableList.of("a", "a"); ImmutableList<String> ab = ImmutableList.of("a", "b"); ImmutableList<String> b = ImmutableList.of("b"); Helpers.testComparator(lexy, empty, a, aa, ab, b); new EqualsTester() .addEqualityGroup(lexy, ordering.lexicographical()) .addEqualityGroup(numberOrdering.lexicographical()) .addEqualityGroup(Ordering.natural()) .testEquals(); }
public void testNatural() { Ordering<Integer> comparator = Ordering.natural(); Helpers.testComparator(comparator, Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE); try { comparator.compare(1, null); fail(); } catch (NullPointerException expected) { } try { comparator.compare(null, 2); fail(); } catch (NullPointerException expected) { } try { comparator.compare(null, null); fail(); } catch (NullPointerException expected) { } assertSame(comparator, reserialize(comparator)); assertEquals("Ordering.natural()", comparator.toString()); }
public void testArbitrary_withCollisions() { List<Integer> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(i); } Ordering<Object> arbitrary = new ArbitraryOrdering() { @Override int identityHashCode(Object object) { return ((Integer) object) % 5; // fake tons of collisions! } }; // Don't let the elements be in such a predictable order list = shuffledCopy(list, new Random(1)); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); }
/** * Asserts that all pairs of {@code T} values within {@code valuesInExpectedOrder} are ordered * consistently between their order within {@code valuesInExpectedOrder} and the order implied by * the given {@code comparator}. * * @see #testComparator(Comparator, List) */ public static <T> void testComparator( Comparator<? super T> comparator, T... valuesInExpectedOrder) { testComparator(comparator, Arrays.asList(valuesInExpectedOrder)); }
void testCompareTo() { Helpers.testComparator(ordering, strictlyOrderedList); }
public void testCompound_instance() { Comparator<String> comparator = byCharAt(1).compound(byCharAt(0)); Helpers.testComparator( comparator, ImmutableList.of("red", "yellow", "violet", "blue", "indigo", "green", "orange")); }
public void testUsingToString() { Ordering<Object> ordering = Ordering.usingToString(); Helpers.testComparator(ordering, 1, 12, 124, 2); assertEquals("Ordering.usingToString()", ordering.toString()); assertSame(ordering, reserialize(ordering)); }