public void testSortedCopy() { List<Integer> unsortedInts = Collections.unmodifiableList(Arrays.asList(5, 0, 3, null, 0, 9)); List<Integer> sortedInts = numberOrdering.nullsLast().sortedCopy(unsortedInts); assertEquals(Arrays.asList(0, 0, 3, 5, 9, null), sortedInts); assertEquals( Collections.emptyList(), numberOrdering.sortedCopy(Collections.<Integer>emptyList())); }
public void testLeastOfIterable_ties() { Integer foo = new Integer(Integer.MAX_VALUE - 10); Integer bar = new Integer(Integer.MAX_VALUE - 10); assertNotSame(foo, bar); assertEquals(foo, bar); List<Integer> list = Arrays.asList(3, foo, bar, -1); List<Integer> result = numberOrdering.leastOf(list, list.size()); assertEquals(ImmutableList.of(-1, 3, foo, bar), result); }
public void testLeastOfIterator_ties() { Integer foo = new Integer(Integer.MAX_VALUE - 10); Integer bar = new Integer(Integer.MAX_VALUE - 10); assertNotSame(foo, bar); assertEquals(foo, bar); List<Integer> list = Arrays.asList(3, foo, bar, -1); List<Integer> result = numberOrdering.leastOf(list.iterator(), list.size()); assertEquals(ImmutableList.of(-1, 3, foo, bar), result); }
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 testAllEqual() { Ordering<Object> comparator = Ordering.allEqual(); assertSame(comparator, comparator.reverse()); assertEquals(0, comparator.compare(null, null)); assertEquals(0, comparator.compare(new Object(), new Object())); assertEquals(0, comparator.compare("apples", "oranges")); assertSame(comparator, reserialize(comparator)); assertEquals("Ordering.allEqual()", comparator.toString()); List<String> strings = ImmutableList.of("b", "a", "d", "c"); assertEquals(strings, comparator.sortedCopy(strings)); assertEquals(strings, comparator.immutableSortedCopy(strings)); }
public void testIteratorMaxExhaustsIterator() { List<Integer> ints = Lists.newArrayList(9, 0, 3, 5); Iterator<Integer> iterator = ints.iterator(); assertEquals(9, (int) numberOrdering.max(iterator)); assertFalse(iterator.hasNext()); }
public void testLeastOfIterator_simple_nPlusOne() { List<Integer> list = Arrays.asList(3, 4, 5, -1); List<Integer> result = numberOrdering.leastOf(list.iterator(), list.size() + 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 4, 5), result); }
public void testLeastOfIterator_simple_n() { List<Integer> list = Arrays.asList(3, 4, 5, -1); List<Integer> result = numberOrdering.leastOf(list.iterator(), list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 4, 5), result); }
public void testLeastOfIterable_simple_nMinusOne() { List<Integer> list = Arrays.asList(3, 4, 5, -1); List<Integer> result = numberOrdering.leastOf(list, list.size() - 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 4), result); }
public void testLeastOfIterable_simple_nPlusOne() { List<Integer> list = Arrays.asList(3, 4, 5, -1); List<Integer> result = numberOrdering.leastOf(list, list.size() + 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 4, 5), result); }
public void testLeastOfIterable_simple_0() { List<Integer> result = numberOrdering.leastOf(Arrays.asList(3, 4, 5, -1), 0); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.<Integer>of(), result); }
public void testLeastOfIterable_simple_1() { List<Integer> result = numberOrdering.leastOf(Arrays.asList(3, 4, 5, -1), 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1), result); }
public void testIterableMinAndMax() { List<Integer> ints = Lists.newArrayList(5, 3, 0, 9); assertEquals(9, (int) numberOrdering.max(ints)); assertEquals(0, (int) numberOrdering.min(ints)); // when the values are the same, the first argument should be returned Integer a = new Integer(4); Integer b = new Integer(4); ints = Lists.newArrayList(a, b, b); assertSame(a, numberOrdering.max(ints)); assertSame(a, numberOrdering.min(ints)); }
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); }
public void testLeastOfIterable_simple_nMinusOne_withNullElement() { List<Integer> list = Arrays.asList(3, null, 5, -1); List<Integer> result = Ordering.natural().nullsLast().leastOf(list, list.size() - 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); }
public void testLeastOfIterable_simple_n_withNullElement() { List<Integer> list = Arrays.asList(3, 4, 5, null, -1); List<Integer> result = Ordering.natural().nullsLast().leastOf(list, list.size()); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(Arrays.asList(-1, 3, 4, 5, null), result); }
public void testLeastOfIterator_singleton_0() { List<Integer> result = numberOrdering.leastOf(Iterators.singletonIterator(3), 0); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.<Integer>of(), result); }
public void testLeastOfIterator_simple_0() { List<Integer> result = numberOrdering.leastOf(Iterators.forArray(3, 4, 5, -1), 0); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.<Integer>of(), result); }
public void testLeastOfIterator_simple_1() { List<Integer> result = numberOrdering.leastOf(Iterators.forArray(3, 4, 5, -1), 1); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1), result); }
public void testLeastOfIterator_simple_nMinusOne_withNullElement() { Iterator<Integer> itr = Iterators.forArray(3, null, 5, -1); List<Integer> result = Ordering.natural().nullsLast().leastOf(itr, 3); assertTrue(result instanceof RandomAccess); assertListImmutable(result); assertEquals(ImmutableList.of(-1, 3, 5), result); }