Ejemplo n.º 1
0
  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();
  }
Ejemplo n.º 2
0
  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();
  }
Ejemplo n.º 3
0
  // 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());
  }
Ejemplo n.º 4
0
  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);
  }
Ejemplo n.º 5
0
  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);
  }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
  @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();
  }
Ejemplo n.º 8
0
 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());
 }
Ejemplo n.º 9
0
  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);
  }
Ejemplo n.º 10
0
 /**
  * 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));
 }
Ejemplo n.º 11
0
 void testCompareTo() {
   Helpers.testComparator(ordering, strictlyOrderedList);
 }
Ejemplo n.º 12
0
 public void testCompound_instance() {
   Comparator<String> comparator = byCharAt(1).compound(byCharAt(0));
   Helpers.testComparator(
       comparator,
       ImmutableList.of("red", "yellow", "violet", "blue", "indigo", "green", "orange"));
 }
Ejemplo n.º 13
0
 public void testUsingToString() {
   Ordering<Object> ordering = Ordering.usingToString();
   Helpers.testComparator(ordering, 1, 12, 124, 2);
   assertEquals("Ordering.usingToString()", ordering.toString());
   assertSame(ordering, reserialize(ordering));
 }