• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import static com.google.common.base.Preconditions.checkArgument;
18 import static com.google.common.truth.Truth.assertThat;
19 import static java.util.Arrays.asList;
20 
21 import com.google.common.base.Function;
22 import com.google.common.collect.ImmutableSortedMultiset.Builder;
23 import com.google.common.collect.testing.ListTestSuiteBuilder;
24 import com.google.common.collect.testing.MinimalCollection;
25 import com.google.common.collect.testing.TestStringListGenerator;
26 import com.google.common.collect.testing.features.CollectionFeature;
27 import com.google.common.collect.testing.features.CollectionSize;
28 import com.google.common.collect.testing.google.SortedMultisetTestSuiteBuilder;
29 import com.google.common.collect.testing.google.TestStringMultisetGenerator;
30 import com.google.common.collect.testing.google.UnmodifiableCollectionTests;
31 import com.google.common.testing.NullPointerTester;
32 import com.google.common.testing.SerializableTester;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Comparator;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Set;
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42 
43 /**
44  * Tests for {@link ImmutableSortedMultiset}.
45  *
46  * @author Louis Wasserman
47  */
48 public class ImmutableSortedMultisetTest extends TestCase {
suite()49   public static Test suite() {
50     TestSuite suite = new TestSuite();
51     suite.addTestSuite(ImmutableSortedMultisetTest.class);
52 
53     suite.addTest(
54         SortedMultisetTestSuiteBuilder.using(
55                 new TestStringMultisetGenerator() {
56                   @Override
57                   protected Multiset<String> create(String[] elements) {
58                     return ImmutableSortedMultiset.copyOf(elements);
59                   }
60 
61                   @Override
62                   public List<String> order(List<String> insertionOrder) {
63                     return Ordering.natural().sortedCopy(insertionOrder);
64                   }
65                 })
66             .named("ImmutableSortedMultiset")
67             .withFeatures(
68                 CollectionSize.ANY,
69                 CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
70                 CollectionFeature.ALLOWS_NULL_QUERIES)
71             .createTestSuite());
72 
73     suite.addTest(
74         ListTestSuiteBuilder.using(
75                 new TestStringListGenerator() {
76                   @Override
77                   protected List<String> create(String[] elements) {
78                     return ImmutableSortedMultiset.copyOf(elements).asList();
79                   }
80 
81                   @Override
82                   public List<String> order(List<String> insertionOrder) {
83                     return Ordering.natural().sortedCopy(insertionOrder);
84                   }
85                 })
86             .named("ImmutableSortedMultiset.asList")
87             .withFeatures(
88                 CollectionSize.ANY,
89                 CollectionFeature.SERIALIZABLE,
90                 CollectionFeature.ALLOWS_NULL_QUERIES)
91             .createTestSuite());
92 
93     suite.addTest(
94         ListTestSuiteBuilder.using(
95                 new TestStringListGenerator() {
96                   @Override
97                   protected List<String> create(String[] elements) {
98                     Set<String> set = Sets.newHashSet();
99                     ImmutableSortedMultiset.Builder<String> builder =
100                         ImmutableSortedMultiset.naturalOrder();
101                     for (String s : elements) {
102                       checkArgument(set.add(s));
103                       builder.addCopies(s, 2);
104                     }
105                     return builder.build().elementSet().asList();
106                   }
107 
108                   @Override
109                   public List<String> order(List<String> insertionOrder) {
110                     return Ordering.natural().sortedCopy(insertionOrder);
111                   }
112                 })
113             .named("ImmutableSortedMultiset.elementSet.asList")
114             .withFeatures(
115                 CollectionSize.ANY,
116                 CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
117                 CollectionFeature.SERIALIZABLE,
118                 CollectionFeature.ALLOWS_NULL_QUERIES)
119             .createTestSuite());
120 
121     return suite;
122   }
123 
testCreation_noArgs()124   public void testCreation_noArgs() {
125     Multiset<String> multiset = ImmutableSortedMultiset.of();
126     assertTrue(multiset.isEmpty());
127   }
128 
testCreation_oneElement()129   public void testCreation_oneElement() {
130     Multiset<String> multiset = ImmutableSortedMultiset.of("a");
131     assertEquals(HashMultiset.create(asList("a")), multiset);
132   }
133 
testCreation_twoElements()134   public void testCreation_twoElements() {
135     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b");
136     assertEquals(HashMultiset.create(asList("a", "b")), multiset);
137   }
138 
testCreation_threeElements()139   public void testCreation_threeElements() {
140     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "c");
141     assertEquals(HashMultiset.create(asList("a", "b", "c")), multiset);
142   }
143 
testCreation_fourElements()144   public void testCreation_fourElements() {
145     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "c", "d");
146     assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset);
147   }
148 
testCreation_fiveElements()149   public void testCreation_fiveElements() {
150     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "c", "d", "e");
151     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e")), multiset);
152   }
153 
testCreation_sixElements()154   public void testCreation_sixElements() {
155     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "c", "d", "e", "f");
156     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e", "f")), multiset);
157   }
158 
testCreation_sevenElements()159   public void testCreation_sevenElements() {
160     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "c", "d", "e", "f", "g");
161     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e", "f", "g")), multiset);
162   }
163 
testCreation_emptyArray()164   public void testCreation_emptyArray() {
165     String[] array = new String[0];
166     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array);
167     assertTrue(multiset.isEmpty());
168   }
169 
testCreation_arrayOfOneElement()170   public void testCreation_arrayOfOneElement() {
171     String[] array = new String[] {"a"};
172     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array);
173     assertEquals(HashMultiset.create(asList("a")), multiset);
174   }
175 
testCreation_arrayOfArray()176   public void testCreation_arrayOfArray() {
177     Comparator<String[]> comparator =
178         Ordering.natural()
179             .lexicographical()
180             .onResultOf(
181                 new Function<String[], Iterable<Comparable>>() {
182                   @Override
183                   public Iterable<Comparable> apply(String[] input) {
184                     return Arrays.<Comparable>asList(input);
185                   }
186                 });
187     String[] array = new String[] {"a"};
188     Multiset<String[]> multiset = ImmutableSortedMultiset.orderedBy(comparator).add(array).build();
189     Multiset<String[]> expected = HashMultiset.create();
190     expected.add(array);
191     assertEquals(expected, multiset);
192   }
193 
testCreation_arrayContainingOnlyNull()194   public void testCreation_arrayContainingOnlyNull() {
195     String[] array = new String[] {null};
196     try {
197       ImmutableSortedMultiset.copyOf(array);
198       fail();
199     } catch (NullPointerException expected) {
200     }
201   }
202 
testCopyOf_collection_empty()203   public void testCopyOf_collection_empty() {
204     // "<String>" is required to work around a javac 1.5 bug.
205     Collection<String> c = MinimalCollection.<String>of();
206     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
207     assertTrue(multiset.isEmpty());
208   }
209 
testCopyOf_collection_oneElement()210   public void testCopyOf_collection_oneElement() {
211     Collection<String> c = MinimalCollection.of("a");
212     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
213     assertEquals(HashMultiset.create(asList("a")), multiset);
214   }
215 
testCopyOf_collection_general()216   public void testCopyOf_collection_general() {
217     Collection<String> c = MinimalCollection.of("a", "b", "a");
218     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
219     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
220   }
221 
testCopyOf_collectionContainingNull()222   public void testCopyOf_collectionContainingNull() {
223     Collection<String> c = MinimalCollection.of("a", null, "b");
224     try {
225       ImmutableSortedMultiset.copyOf(c);
226       fail();
227     } catch (NullPointerException expected) {
228     }
229   }
230 
testCopyOf_multiset_empty()231   public void testCopyOf_multiset_empty() {
232     Multiset<String> c = HashMultiset.create();
233     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
234     assertTrue(multiset.isEmpty());
235   }
236 
testCopyOf_multiset_oneElement()237   public void testCopyOf_multiset_oneElement() {
238     Multiset<String> c = HashMultiset.create(asList("a"));
239     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
240     assertEquals(HashMultiset.create(asList("a")), multiset);
241   }
242 
testCopyOf_multiset_general()243   public void testCopyOf_multiset_general() {
244     Multiset<String> c = HashMultiset.create(asList("a", "b", "a"));
245     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c);
246     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
247   }
248 
testCopyOf_multisetContainingNull()249   public void testCopyOf_multisetContainingNull() {
250     Multiset<String> c = HashMultiset.create(asList("a", null, "b"));
251     try {
252       ImmutableSortedMultiset.copyOf(c);
253       fail();
254     } catch (NullPointerException expected) {
255     }
256   }
257 
testCopyOf_iterator_empty()258   public void testCopyOf_iterator_empty() {
259     Iterator<String> iterator = Iterators.emptyIterator();
260     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator);
261     assertTrue(multiset.isEmpty());
262   }
263 
testCopyOf_iterator_oneElement()264   public void testCopyOf_iterator_oneElement() {
265     Iterator<String> iterator = Iterators.singletonIterator("a");
266     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator);
267     assertEquals(HashMultiset.create(asList("a")), multiset);
268   }
269 
testCopyOf_iterator_general()270   public void testCopyOf_iterator_general() {
271     Iterator<String> iterator = asList("a", "b", "a").iterator();
272     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator);
273     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
274   }
275 
testCopyOf_iteratorContainingNull()276   public void testCopyOf_iteratorContainingNull() {
277     Iterator<String> iterator = asList("a", null, "b").iterator();
278     try {
279       ImmutableSortedMultiset.copyOf(iterator);
280       fail();
281     } catch (NullPointerException expected) {
282     }
283   }
284 
285   private static class CountingIterable implements Iterable<String> {
286     int count = 0;
287 
288     @Override
iterator()289     public Iterator<String> iterator() {
290       count++;
291       return asList("a", "b", "a").iterator();
292     }
293   }
294 
testCopyOf_plainIterable()295   public void testCopyOf_plainIterable() {
296     CountingIterable iterable = new CountingIterable();
297     Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterable);
298     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
299     assertEquals(1, iterable.count);
300   }
301 
testCopyOf_shortcut_empty()302   public void testCopyOf_shortcut_empty() {
303     Collection<String> c = ImmutableSortedMultiset.of();
304     assertSame(c, ImmutableSortedMultiset.copyOf(c));
305   }
306 
testCopyOf_shortcut_singleton()307   public void testCopyOf_shortcut_singleton() {
308     Collection<String> c = ImmutableSortedMultiset.of("a");
309     assertSame(c, ImmutableSortedMultiset.copyOf(c));
310   }
311 
testCopyOf_shortcut_immutableMultiset()312   public void testCopyOf_shortcut_immutableMultiset() {
313     Collection<String> c = ImmutableSortedMultiset.of("a", "b", "c");
314     assertSame(c, ImmutableSortedMultiset.copyOf(c));
315   }
316 
testBuilderAdd()317   public void testBuilderAdd() {
318     ImmutableSortedMultiset<String> multiset =
319         ImmutableSortedMultiset.<String>naturalOrder().add("a").add("b").add("a").add("c").build();
320     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset);
321   }
322 
testReuseBuilder()323   public void testReuseBuilder() {
324     Builder<String> builder =
325         ImmutableSortedMultiset.<String>naturalOrder().add("a").add("b").add("a").add("c");
326     ImmutableSortedMultiset<String> multiset1 = builder.build();
327     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset1);
328     ImmutableSortedMultiset<String> multiset2 = builder.add("c").build();
329     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset1);
330     assertEquals(HashMultiset.create(asList("a", "b", "a", "c", "c")), multiset2);
331     assertTrue(
332         ((RegularImmutableList<String>)
333                     ((RegularImmutableSortedMultiset<String>) multiset1).elementSet.elements)
334                 .array
335             != builder.elements);
336   }
337 
testBuilderAddAll()338   public void testBuilderAddAll() {
339     List<String> a = asList("a", "b");
340     List<String> b = asList("c", "d");
341     ImmutableSortedMultiset<String> multiset =
342         ImmutableSortedMultiset.<String>naturalOrder().addAll(a).addAll(b).build();
343     assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset);
344   }
345 
testBuilderAddAllMultiset()346   public void testBuilderAddAllMultiset() {
347     Multiset<String> a = HashMultiset.create(asList("a", "b", "b"));
348     Multiset<String> b = HashMultiset.create(asList("c", "b"));
349     ImmutableSortedMultiset<String> multiset =
350         ImmutableSortedMultiset.<String>naturalOrder().addAll(a).addAll(b).build();
351     assertEquals(HashMultiset.create(asList("a", "b", "b", "b", "c")), multiset);
352   }
353 
testBuilderAddAllIterator()354   public void testBuilderAddAllIterator() {
355     Iterator<String> iterator = asList("a", "b", "a", "c").iterator();
356     ImmutableSortedMultiset<String> multiset =
357         ImmutableSortedMultiset.<String>naturalOrder().addAll(iterator).build();
358     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset);
359   }
360 
testBuilderAddCopies()361   public void testBuilderAddCopies() {
362     ImmutableSortedMultiset<String> multiset =
363         ImmutableSortedMultiset.<String>naturalOrder()
364             .addCopies("a", 2)
365             .addCopies("b", 3)
366             .addCopies("c", 0)
367             .build();
368     assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset);
369   }
370 
testBuilderSetCount()371   public void testBuilderSetCount() {
372     ImmutableSortedMultiset<String> multiset =
373         ImmutableSortedMultiset.<String>naturalOrder()
374             .add("a")
375             .setCount("a", 2)
376             .setCount("b", 3)
377             .build();
378     assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset);
379   }
380 
testBuilderSetCountZero()381   public void testBuilderSetCountZero() {
382     ImmutableSortedMultiset<String> multiset =
383         ImmutableSortedMultiset.<String>naturalOrder()
384             .add("a")
385             .setCount("a", 2)
386             .setCount("b", 3)
387             .setCount("a", 0)
388             .build();
389     assertEquals(HashMultiset.create(asList("b", "b", "b")), multiset);
390   }
391 
testBuilderSetCountThenAdd()392   public void testBuilderSetCountThenAdd() {
393     ImmutableSortedMultiset<String> multiset =
394         ImmutableSortedMultiset.<String>naturalOrder()
395             .add("a")
396             .setCount("a", 2)
397             .setCount("b", 3)
398             .setCount("a", 1)
399             .add("a")
400             .build();
401     assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset);
402   }
403 
testBuilderAddHandlesNullsCorrectly()404   public void testBuilderAddHandlesNullsCorrectly() {
405     ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder();
406     try {
407       builder.add((String) null);
408       fail("expected NullPointerException");
409     } catch (NullPointerException expected) {
410     }
411   }
412 
testBuilderAddAllHandlesNullsCorrectly()413   public void testBuilderAddAllHandlesNullsCorrectly() {
414     ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder();
415     try {
416       builder.addAll((Collection<String>) null);
417       fail("expected NullPointerException");
418     } catch (NullPointerException expected) {
419     }
420 
421     builder = ImmutableSortedMultiset.naturalOrder();
422     List<String> listWithNulls = asList("a", null, "b");
423     try {
424       builder.addAll(listWithNulls);
425       fail("expected NullPointerException");
426     } catch (NullPointerException expected) {
427     }
428 
429     builder = ImmutableSortedMultiset.naturalOrder();
430     Multiset<String> multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b"));
431     try {
432       builder.addAll(multisetWithNull);
433       fail("expected NullPointerException");
434     } catch (NullPointerException expected) {
435     }
436   }
437 
testBuilderAddCopiesHandlesNullsCorrectly()438   public void testBuilderAddCopiesHandlesNullsCorrectly() {
439     ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder();
440     try {
441       builder.addCopies(null, 2);
442       fail("expected NullPointerException");
443     } catch (NullPointerException expected) {
444     }
445   }
446 
testBuilderAddCopiesIllegal()447   public void testBuilderAddCopiesIllegal() {
448     ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder();
449     try {
450       builder.addCopies("a", -2);
451       fail("expected IllegalArgumentException");
452     } catch (IllegalArgumentException expected) {
453     }
454   }
455 
testBuilderSetCountHandlesNullsCorrectly()456   public void testBuilderSetCountHandlesNullsCorrectly() {
457     ImmutableSortedMultiset.Builder<String> builder =
458         new ImmutableSortedMultiset.Builder<>(Ordering.natural().nullsFirst());
459     try {
460       builder.setCount(null, 2);
461       fail("expected NullPointerException");
462     } catch (NullPointerException expected) {
463     }
464   }
465 
testBuilderSetCountIllegal()466   public void testBuilderSetCountIllegal() {
467     ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder();
468     try {
469       builder.setCount("a", -2);
470       fail("expected IllegalArgumentException");
471     } catch (IllegalArgumentException expected) {
472     }
473   }
474 
testNullPointers()475   public void testNullPointers() {
476     new NullPointerTester().testAllPublicStaticMethods(ImmutableSortedMultiset.class);
477   }
478 
testSerialization_empty()479   public void testSerialization_empty() {
480     Collection<String> c = ImmutableSortedMultiset.of();
481     assertSame(c, SerializableTester.reserialize(c));
482   }
483 
testSerialization_multiple()484   public void testSerialization_multiple() {
485     Collection<String> c = ImmutableSortedMultiset.of("a", "b", "a");
486     Collection<String> copy = SerializableTester.reserializeAndAssert(c);
487     assertThat(copy).containsExactly("a", "a", "b").inOrder();
488   }
489 
testSerialization_elementSet()490   public void testSerialization_elementSet() {
491     Multiset<String> c = ImmutableSortedMultiset.of("a", "b", "a");
492     Collection<String> copy = SerializableTester.reserializeAndAssert(c.elementSet());
493     assertThat(copy).containsExactly("a", "b").inOrder();
494   }
495 
testSerialization_entrySet()496   public void testSerialization_entrySet() {
497     Multiset<String> c = ImmutableSortedMultiset.of("a", "b", "c");
498     SerializableTester.reserializeAndAssert(c.entrySet());
499   }
500 
testEquals_immutableMultiset()501   public void testEquals_immutableMultiset() {
502     Collection<String> c = ImmutableSortedMultiset.of("a", "b", "a");
503     assertEquals(c, ImmutableSortedMultiset.of("a", "b", "a"));
504     assertEquals(c, ImmutableSortedMultiset.of("a", "a", "b"));
505     assertThat(c).isNotEqualTo(ImmutableSortedMultiset.of("a", "b"));
506     assertThat(c).isNotEqualTo(ImmutableSortedMultiset.of("a", "b", "c", "d"));
507   }
508 
testIterationOrder()509   public void testIterationOrder() {
510     Collection<String> c = ImmutableSortedMultiset.of("a", "b", "a");
511     assertThat(c).containsExactly("a", "a", "b").inOrder();
512   }
513 
testMultisetWrites()514   public void testMultisetWrites() {
515     Multiset<String> multiset = ImmutableSortedMultiset.of("a", "b", "a");
516     UnmodifiableCollectionTests.assertMultisetIsUnmodifiable(multiset, "test");
517   }
518 
testAsList()519   public void testAsList() {
520     ImmutableSortedMultiset<String> multiset = ImmutableSortedMultiset.of("a", "a", "b", "b", "b");
521     ImmutableList<String> list = multiset.asList();
522     assertEquals(ImmutableList.of("a", "a", "b", "b", "b"), list);
523     SerializableTester.reserializeAndAssert(list);
524     assertEquals(2, list.indexOf("b"));
525     assertEquals(4, list.lastIndexOf("b"));
526   }
527 
528   private static class IntegerDiv10 implements Comparable<IntegerDiv10> {
529     final int value;
530 
IntegerDiv10(int value)531     IntegerDiv10(int value) {
532       this.value = value;
533     }
534 
535     @Override
compareTo(IntegerDiv10 o)536     public int compareTo(IntegerDiv10 o) {
537       return value / 10 - o.value / 10;
538     }
539 
540     @Override
toString()541     public String toString() {
542       return Integer.toString(value);
543     }
544   }
545 
testCopyOfDuplicateInconsistentWithEquals()546   public void testCopyOfDuplicateInconsistentWithEquals() {
547     IntegerDiv10 three = new IntegerDiv10(3);
548     IntegerDiv10 eleven = new IntegerDiv10(11);
549     IntegerDiv10 twelve = new IntegerDiv10(12);
550     IntegerDiv10 twenty = new IntegerDiv10(20);
551 
552     List<IntegerDiv10> original = ImmutableList.of(three, eleven, twelve, twenty);
553 
554     Multiset<IntegerDiv10> copy = ImmutableSortedMultiset.copyOf(original);
555     assertTrue(copy.contains(eleven));
556     assertTrue(copy.contains(twelve));
557   }
558 }
559