/** Test a TreeMultiset with a comparator that can return 0 when comparing unequal values. */ public void testDegenerateComparator() throws Exception { TreeMultiset<String> ms = TreeMultiset.create(DEGENERATE_COMPARATOR); ms.add("foo"); ms.add("a"); ms.add("bar"); ms.add("b"); ms.add("c"); assertEquals(2, ms.count("bar")); assertEquals(3, ms.count("b")); Multiset<String> ms2 = TreeMultiset.create(DEGENERATE_COMPARATOR); ms2.add("cat", 2); ms2.add("x", 3); assertEquals(ms, ms2); assertEquals(ms2, ms); SortedSet<String> elementSet = ms.elementSet(); assertEquals("a", elementSet.first()); assertEquals("foo", elementSet.last()); assertEquals(DEGENERATE_COMPARATOR, elementSet.comparator()); }
public void testElementSetSortedSetMethods() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("c", 1); ms.add("a", 3); ms.add("b", 2); SortedSet<String> elementSet = ms.elementSet(); assertEquals("a", elementSet.first()); assertEquals("c", elementSet.last()); assertEquals(Ordering.natural(), elementSet.comparator()); ASSERT.that(elementSet.headSet("b")).has().exactly("a").inOrder(); ASSERT.that(elementSet.tailSet("b")).has().exactly("b", "c").inOrder(); ASSERT.that(elementSet.subSet("a", "c")).has().exactly("a", "b").inOrder(); }
public void testNullAcceptingComparator() throws Exception { Comparator<String> comparator = Ordering.<String>natural().nullsFirst(); TreeMultiset<String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add(null); ms.add("a"); ms.add("b"); ms.add(null, 2); ASSERT.that(ms).has().exactly(null, null, null, "a", "b", "b").inOrder(); assertEquals(3, ms.count(null)); SortedSet<String> elementSet = ms.elementSet(); assertEquals(null, elementSet.first()); assertEquals("b", elementSet.last()); assertEquals(comparator, elementSet.comparator()); }
public void testElementSetSubsetClear() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); subset.clear(); ASSERT.that(elementSet).has().exactly("a", "f").inOrder(); ASSERT.that(subset).isEmpty(); assertEquals(3, ms.size()); }
public void testElementSetSubsetRetainAll() { TreeMultiset<String> ms = TreeMultiset.create(); ms.add("a", 1); ms.add("b", 3); ms.add("c", 2); ms.add("d", 1); ms.add("e", 3); ms.add("f", 2); SortedSet<String> elementSet = ms.elementSet(); ASSERT.that(elementSet).has().exactly("a", "b", "c", "d", "e", "f").inOrder(); SortedSet<String> subset = elementSet.subSet("b", "f"); ASSERT.that(subset).has().exactly("b", "c", "d", "e").inOrder(); assertTrue(subset.retainAll(Arrays.asList("a", "c"))); ASSERT.that(elementSet).has().exactly("a", "c", "f").inOrder(); ASSERT.that(subset).has().exactly("c").inOrder(); assertEquals(5, ms.size()); }
public void testCustomComparator() throws Exception { Comparator<String> comparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }; TreeMultiset<String> ms = TreeMultiset.create(comparator); ms.add("b"); ms.add("c"); ms.add("a"); ms.add("b"); ms.add("d"); ASSERT.that(ms).has().exactly("d", "c", "b", "b", "a").inOrder(); SortedSet<String> elementSet = ms.elementSet(); assertEquals("d", elementSet.first()); assertEquals("a", elementSet.last()); assertEquals(comparator, elementSet.comparator()); }