• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import static com.google.common.base.Preconditions.checkArgument;
20 import static com.google.common.truth.Truth.assertThat;
21 import static java.util.Arrays.asList;
22 
23 import com.google.common.annotations.GwtCompatible;
24 import com.google.common.annotations.GwtIncompatible;
25 import com.google.common.collect.testing.ListTestSuiteBuilder;
26 import com.google.common.collect.testing.MinimalCollection;
27 import com.google.common.collect.testing.SetTestSuiteBuilder;
28 import com.google.common.collect.testing.TestStringListGenerator;
29 import com.google.common.collect.testing.TestStringSetGenerator;
30 import com.google.common.collect.testing.features.CollectionFeature;
31 import com.google.common.collect.testing.features.CollectionSize;
32 import com.google.common.collect.testing.google.MultisetTestSuiteBuilder;
33 import com.google.common.collect.testing.google.TestStringMultisetGenerator;
34 import com.google.common.collect.testing.google.UnmodifiableCollectionTests;
35 import com.google.common.testing.CollectorTester;
36 import com.google.common.testing.EqualsTester;
37 import com.google.common.testing.NullPointerTester;
38 import com.google.common.testing.SerializableTester;
39 import java.util.ArrayList;
40 import java.util.Collection;
41 import java.util.HashSet;
42 import java.util.Iterator;
43 import java.util.List;
44 import java.util.Set;
45 import java.util.function.BiPredicate;
46 import java.util.stream.Collector;
47 import junit.framework.Test;
48 import junit.framework.TestCase;
49 import junit.framework.TestSuite;
50 import org.checkerframework.checker.nullness.qual.Nullable;
51 
52 /**
53  * Tests for {@link ImmutableMultiset}.
54  *
55  * @author Jared Levy
56  */
57 @GwtCompatible(emulated = true)
58 public class ImmutableMultisetTest extends TestCase {
59 
60   @GwtIncompatible // suite // TODO(cpovirk): add to collect/gwt/suites
suite()61   public static Test suite() {
62     TestSuite suite = new TestSuite();
63     suite.addTestSuite(ImmutableMultisetTest.class);
64 
65     suite.addTest(
66         MultisetTestSuiteBuilder.using(
67                 new TestStringMultisetGenerator() {
68                   @Override
69                   protected Multiset<String> create(String[] elements) {
70                     return ImmutableMultiset.copyOf(elements);
71                   }
72                 })
73             .named("ImmutableMultiset")
74             .withFeatures(
75                 CollectionSize.ANY,
76                 CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
77                 CollectionFeature.ALLOWS_NULL_QUERIES)
78             .createTestSuite());
79 
80     suite.addTest(
81         MultisetTestSuiteBuilder.using(
82                 new TestStringMultisetGenerator() {
83                   @Override
84                   protected Multiset<String> create(String[] elements) {
85                     return ImmutableMultiset.<String>builder().add(elements).buildJdkBacked();
86                   }
87                 })
88             .named("ImmutableMultiset [JDK backed]")
89             .withFeatures(
90                 CollectionSize.ANY,
91                 CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS,
92                 CollectionFeature.ALLOWS_NULL_QUERIES)
93             .createTestSuite());
94 
95     suite.addTest(
96         SetTestSuiteBuilder.using(
97                 new TestStringSetGenerator() {
98                   @Override
99                   protected Set<String> create(String[] elements) {
100                     return ImmutableMultiset.copyOf(elements).elementSet();
101                   }
102                 })
103             .named("ImmutableMultiset, element set")
104             .withFeatures(
105                 CollectionSize.ANY,
106                 CollectionFeature.SERIALIZABLE,
107                 CollectionFeature.ALLOWS_NULL_QUERIES)
108             .createTestSuite());
109 
110     suite.addTest(
111         ListTestSuiteBuilder.using(
112                 new TestStringListGenerator() {
113                   @Override
114                   protected List<String> create(String[] elements) {
115                     return ImmutableMultiset.copyOf(elements).asList();
116                   }
117 
118                   @Override
119                   public List<String> order(List<String> insertionOrder) {
120                     List<String> order = new ArrayList<>();
121                     for (String s : insertionOrder) {
122                       int index = order.indexOf(s);
123                       if (index == -1) {
124                         order.add(s);
125                       } else {
126                         order.add(index, s);
127                       }
128                     }
129                     return order;
130                   }
131                 })
132             .named("ImmutableMultiset.asList")
133             .withFeatures(
134                 CollectionSize.ANY,
135                 CollectionFeature.SERIALIZABLE,
136                 CollectionFeature.ALLOWS_NULL_QUERIES)
137             .createTestSuite());
138 
139     suite.addTest(
140         ListTestSuiteBuilder.using(
141                 new TestStringListGenerator() {
142                   @Override
143                   protected List<String> create(String[] elements) {
144                     Set<String> set = new HashSet<>();
145                     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
146                     for (String s : elements) {
147                       checkArgument(set.add(s));
148                       builder.addCopies(s, 2);
149                     }
150                     ImmutableSet<String> elementSet =
151                         (ImmutableSet<String>) builder.build().elementSet();
152                     return elementSet.asList();
153                   }
154                 })
155             .named("ImmutableMultiset.elementSet.asList")
156             .withFeatures(
157                 CollectionSize.ANY,
158                 CollectionFeature.REJECTS_DUPLICATES_AT_CREATION,
159                 CollectionFeature.SERIALIZABLE,
160                 CollectionFeature.ALLOWS_NULL_QUERIES)
161             .createTestSuite());
162 
163     return suite;
164   }
165 
testCreation_noArgs()166   public void testCreation_noArgs() {
167     Multiset<String> multiset = ImmutableMultiset.of();
168     assertTrue(multiset.isEmpty());
169   }
170 
testCreation_oneElement()171   public void testCreation_oneElement() {
172     Multiset<String> multiset = ImmutableMultiset.of("a");
173     assertEquals(HashMultiset.create(asList("a")), multiset);
174   }
175 
testCreation_twoElements()176   public void testCreation_twoElements() {
177     Multiset<String> multiset = ImmutableMultiset.of("a", "b");
178     assertEquals(HashMultiset.create(asList("a", "b")), multiset);
179   }
180 
testCreation_threeElements()181   public void testCreation_threeElements() {
182     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c");
183     assertEquals(HashMultiset.create(asList("a", "b", "c")), multiset);
184   }
185 
testCreation_fourElements()186   public void testCreation_fourElements() {
187     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d");
188     assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset);
189   }
190 
testCreation_fiveElements()191   public void testCreation_fiveElements() {
192     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d", "e");
193     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e")), multiset);
194   }
195 
testCreation_sixElements()196   public void testCreation_sixElements() {
197     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d", "e", "f");
198     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e", "f")), multiset);
199   }
200 
testCreation_sevenElements()201   public void testCreation_sevenElements() {
202     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d", "e", "f", "g");
203     assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e", "f", "g")), multiset);
204   }
205 
testCreation_emptyArray()206   public void testCreation_emptyArray() {
207     String[] array = new String[0];
208     Multiset<String> multiset = ImmutableMultiset.copyOf(array);
209     assertTrue(multiset.isEmpty());
210   }
211 
testCreation_arrayOfOneElement()212   public void testCreation_arrayOfOneElement() {
213     String[] array = new String[] {"a"};
214     Multiset<String> multiset = ImmutableMultiset.copyOf(array);
215     assertEquals(HashMultiset.create(asList("a")), multiset);
216   }
217 
testCreation_arrayOfArray()218   public void testCreation_arrayOfArray() {
219     String[] array = new String[] {"a"};
220     Multiset<String[]> multiset = ImmutableMultiset.<String[]>of(array);
221     Multiset<String[]> expected = HashMultiset.create();
222     expected.add(array);
223     assertEquals(expected, multiset);
224   }
225 
testCreation_arrayContainingOnlyNull()226   public void testCreation_arrayContainingOnlyNull() {
227     String[] array = new String[] {null};
228     try {
229       ImmutableMultiset.copyOf(array);
230       fail();
231     } catch (NullPointerException expected) {
232     }
233   }
234 
testCopyOf_collection_empty()235   public void testCopyOf_collection_empty() {
236     // "<String>" is required to work around a javac 1.5 bug.
237     Collection<String> c = MinimalCollection.<String>of();
238     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
239     assertTrue(multiset.isEmpty());
240   }
241 
testCopyOf_collection_oneElement()242   public void testCopyOf_collection_oneElement() {
243     Collection<String> c = MinimalCollection.of("a");
244     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
245     assertEquals(HashMultiset.create(asList("a")), multiset);
246   }
247 
testCopyOf_collection_general()248   public void testCopyOf_collection_general() {
249     Collection<String> c = MinimalCollection.of("a", "b", "a");
250     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
251     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
252   }
253 
testCopyOf_collectionContainingNull()254   public void testCopyOf_collectionContainingNull() {
255     Collection<String> c = MinimalCollection.of("a", null, "b");
256     try {
257       ImmutableMultiset.copyOf(c);
258       fail();
259     } catch (NullPointerException expected) {
260     }
261   }
262 
testCopyOf_multiset_empty()263   public void testCopyOf_multiset_empty() {
264     Multiset<String> c = HashMultiset.create();
265     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
266     assertTrue(multiset.isEmpty());
267   }
268 
testCopyOf_multiset_oneElement()269   public void testCopyOf_multiset_oneElement() {
270     Multiset<String> c = HashMultiset.create(asList("a"));
271     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
272     assertEquals(HashMultiset.create(asList("a")), multiset);
273   }
274 
testCopyOf_multiset_general()275   public void testCopyOf_multiset_general() {
276     Multiset<String> c = HashMultiset.create(asList("a", "b", "a"));
277     Multiset<String> multiset = ImmutableMultiset.copyOf(c);
278     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
279   }
280 
testCopyOf_multisetContainingNull()281   public void testCopyOf_multisetContainingNull() {
282     Multiset<String> c = HashMultiset.create(asList("a", null, "b"));
283     try {
284       ImmutableMultiset.copyOf(c);
285       fail();
286     } catch (NullPointerException expected) {
287     }
288   }
289 
testCopyOf_iterator_empty()290   public void testCopyOf_iterator_empty() {
291     Iterator<String> iterator = Iterators.emptyIterator();
292     Multiset<String> multiset = ImmutableMultiset.copyOf(iterator);
293     assertTrue(multiset.isEmpty());
294   }
295 
testCopyOf_iterator_oneElement()296   public void testCopyOf_iterator_oneElement() {
297     Iterator<String> iterator = Iterators.singletonIterator("a");
298     Multiset<String> multiset = ImmutableMultiset.copyOf(iterator);
299     assertEquals(HashMultiset.create(asList("a")), multiset);
300   }
301 
testCopyOf_iterator_general()302   public void testCopyOf_iterator_general() {
303     Iterator<String> iterator = asList("a", "b", "a").iterator();
304     Multiset<String> multiset = ImmutableMultiset.copyOf(iterator);
305     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
306   }
307 
testCopyOf_iteratorContainingNull()308   public void testCopyOf_iteratorContainingNull() {
309     Iterator<String> iterator = asList("a", null, "b").iterator();
310     try {
311       ImmutableMultiset.copyOf(iterator);
312       fail();
313     } catch (NullPointerException expected) {
314     }
315   }
316 
testToImmutableMultiset()317   public void testToImmutableMultiset() {
318     BiPredicate<ImmutableMultiset<String>, ImmutableMultiset<String>> equivalence =
319         (ms1, ms2) -> ms1.equals(ms2) && ms1.entrySet().asList().equals(ms2.entrySet().asList());
320     CollectorTester.of(ImmutableMultiset.<String>toImmutableMultiset(), equivalence)
321         .expectCollects(ImmutableMultiset.of())
322         .expectCollects(
323             ImmutableMultiset.of("a", "a", "b", "c", "c", "c"), "a", "a", "b", "c", "c", "c");
324   }
325 
testToImmutableMultisetCountFunction()326   public void testToImmutableMultisetCountFunction() {
327     BiPredicate<ImmutableMultiset<String>, ImmutableMultiset<String>> equivalence =
328         (ms1, ms2) -> ms1.equals(ms2) && ms1.entrySet().asList().equals(ms2.entrySet().asList());
329     CollectorTester.of(
330             ImmutableMultiset.<Multiset.Entry<String>, String>toImmutableMultiset(
331                 Multiset.Entry::getElement, Multiset.Entry::getCount),
332             equivalence)
333         .expectCollects(ImmutableMultiset.of())
334         .expectCollects(
335             ImmutableMultiset.of("a", "a", "b", "c", "c", "c"),
336             Multisets.immutableEntry("a", 1),
337             Multisets.immutableEntry("b", 1),
338             Multisets.immutableEntry("a", 1),
339             Multisets.immutableEntry("c", 3));
340   }
341 
testToImmutableMultiset_duplicates()342   public void testToImmutableMultiset_duplicates() {
343     class TypeWithDuplicates {
344       final int a;
345       final int b;
346 
347       TypeWithDuplicates(int a, int b) {
348         this.a = a;
349         this.b = b;
350       }
351 
352       @Override
353       public int hashCode() {
354         return a;
355       }
356 
357       @Override
358       public boolean equals(@Nullable Object obj) {
359         return obj instanceof TypeWithDuplicates && ((TypeWithDuplicates) obj).a == a;
360       }
361 
362       public boolean fullEquals(@Nullable TypeWithDuplicates other) {
363         return other != null && a == other.a && b == other.b;
364       }
365     }
366 
367     Collector<TypeWithDuplicates, ?, ImmutableMultiset<TypeWithDuplicates>> collector =
368         ImmutableMultiset.toImmutableMultiset();
369     BiPredicate<ImmutableMultiset<TypeWithDuplicates>, ImmutableMultiset<TypeWithDuplicates>>
370         equivalence =
371             (ms1, ms2) -> {
372               if (!ms1.equals(ms2)) {
373                 return false;
374               }
375               List<TypeWithDuplicates> elements1 = ImmutableList.copyOf(ms1.elementSet());
376               List<TypeWithDuplicates> elements2 = ImmutableList.copyOf(ms2.elementSet());
377               for (int i = 0; i < ms1.elementSet().size(); i++) {
378                 if (!elements1.get(i).fullEquals(elements2.get(i))) {
379                   return false;
380                 }
381               }
382               return true;
383             };
384     TypeWithDuplicates a = new TypeWithDuplicates(1, 1);
385     TypeWithDuplicates b1 = new TypeWithDuplicates(2, 1);
386     TypeWithDuplicates b2 = new TypeWithDuplicates(2, 2);
387     TypeWithDuplicates c = new TypeWithDuplicates(3, 1);
388     CollectorTester.of(collector, equivalence)
389         .expectCollects(
390             ImmutableMultiset.<TypeWithDuplicates>builder().add(a).addCopies(b1, 2).add(c).build(),
391             a,
392             b1,
393             c,
394             b2);
395     collector = ImmutableMultiset.toImmutableMultiset(e -> e, e -> 1);
396     CollectorTester.of(collector, equivalence)
397         .expectCollects(
398             ImmutableMultiset.<TypeWithDuplicates>builder().add(a).addCopies(b1, 2).add(c).build(),
399             a,
400             b1,
401             c,
402             b2);
403   }
404 
405   private static class CountingIterable implements Iterable<String> {
406     int count = 0;
407 
408     @Override
iterator()409     public Iterator<String> iterator() {
410       count++;
411       return asList("a", "b", "a").iterator();
412     }
413   }
414 
testCopyOf_plainIterable()415   public void testCopyOf_plainIterable() {
416     CountingIterable iterable = new CountingIterable();
417     Multiset<String> multiset = ImmutableMultiset.copyOf(iterable);
418     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
419     assertEquals(1, iterable.count);
420   }
421 
testCopyOf_hashMultiset()422   public void testCopyOf_hashMultiset() {
423     Multiset<String> iterable = HashMultiset.create(asList("a", "b", "a"));
424     Multiset<String> multiset = ImmutableMultiset.copyOf(iterable);
425     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
426   }
427 
testCopyOf_treeMultiset()428   public void testCopyOf_treeMultiset() {
429     Multiset<String> iterable = TreeMultiset.create(asList("a", "b", "a"));
430     Multiset<String> multiset = ImmutableMultiset.copyOf(iterable);
431     assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset);
432   }
433 
testCopyOf_shortcut_empty()434   public void testCopyOf_shortcut_empty() {
435     Collection<String> c = ImmutableMultiset.of();
436     assertSame(c, ImmutableMultiset.copyOf(c));
437   }
438 
testCopyOf_shortcut_singleton()439   public void testCopyOf_shortcut_singleton() {
440     Collection<String> c = ImmutableMultiset.of("a");
441     assertSame(c, ImmutableMultiset.copyOf(c));
442   }
443 
testCopyOf_shortcut_immutableMultiset()444   public void testCopyOf_shortcut_immutableMultiset() {
445     Collection<String> c = ImmutableMultiset.of("a", "b", "c");
446     assertSame(c, ImmutableMultiset.copyOf(c));
447   }
448 
testBuilderAdd()449   public void testBuilderAdd() {
450     ImmutableMultiset<String> multiset =
451         new ImmutableMultiset.Builder<String>().add("a").add("b").add("a").add("c").build();
452     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset);
453   }
454 
testBuilderAddAll()455   public void testBuilderAddAll() {
456     List<String> a = asList("a", "b");
457     List<String> b = asList("c", "d");
458     ImmutableMultiset<String> multiset =
459         new ImmutableMultiset.Builder<String>().addAll(a).addAll(b).build();
460     assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset);
461   }
462 
testBuilderAddAllHashMultiset()463   public void testBuilderAddAllHashMultiset() {
464     Multiset<String> a = HashMultiset.create(asList("a", "b", "b"));
465     Multiset<String> b = HashMultiset.create(asList("c", "b"));
466     ImmutableMultiset<String> multiset =
467         new ImmutableMultiset.Builder<String>().addAll(a).addAll(b).build();
468     assertEquals(HashMultiset.create(asList("a", "b", "b", "b", "c")), multiset);
469   }
470 
testBuilderAddAllImmutableMultiset()471   public void testBuilderAddAllImmutableMultiset() {
472     Multiset<String> a = ImmutableMultiset.of("a", "b", "b");
473     Multiset<String> b = ImmutableMultiset.of("c", "b");
474     ImmutableMultiset<String> multiset =
475         new ImmutableMultiset.Builder<String>().addAll(a).addAll(b).build();
476     assertEquals(HashMultiset.create(asList("a", "b", "b", "b", "c")), multiset);
477   }
478 
testBuilderAddAllTreeMultiset()479   public void testBuilderAddAllTreeMultiset() {
480     Multiset<String> a = TreeMultiset.create(asList("a", "b", "b"));
481     Multiset<String> b = TreeMultiset.create(asList("c", "b"));
482     ImmutableMultiset<String> multiset =
483         new ImmutableMultiset.Builder<String>().addAll(a).addAll(b).build();
484     assertEquals(HashMultiset.create(asList("a", "b", "b", "b", "c")), multiset);
485   }
486 
testBuilderAddAllIterator()487   public void testBuilderAddAllIterator() {
488     Iterator<String> iterator = asList("a", "b", "a", "c").iterator();
489     ImmutableMultiset<String> multiset =
490         new ImmutableMultiset.Builder<String>().addAll(iterator).build();
491     assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset);
492   }
493 
testBuilderAddCopies()494   public void testBuilderAddCopies() {
495     ImmutableMultiset<String> multiset =
496         new ImmutableMultiset.Builder<String>()
497             .addCopies("a", 2)
498             .addCopies("b", 3)
499             .addCopies("c", 0)
500             .build();
501     assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset);
502   }
503 
testBuilderSetCount()504   public void testBuilderSetCount() {
505     ImmutableMultiset<String> multiset =
506         new ImmutableMultiset.Builder<String>().add("a").setCount("a", 2).setCount("b", 3).build();
507     assertEquals(HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset);
508   }
509 
testBuilderAddHandlesNullsCorrectly()510   public void testBuilderAddHandlesNullsCorrectly() {
511     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
512     try {
513       builder.add((String) null);
514       fail("expected NullPointerException");
515     } catch (NullPointerException expected) {
516     }
517   }
518 
testBuilderAddAllHandlesNullsCorrectly()519   public void testBuilderAddAllHandlesNullsCorrectly() {
520     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
521     try {
522       builder.addAll((Collection<String>) null);
523       fail("expected NullPointerException");
524     } catch (NullPointerException expected) {
525     }
526 
527     builder = ImmutableMultiset.builder();
528     List<String> listWithNulls = asList("a", null, "b");
529     try {
530       builder.addAll(listWithNulls);
531       fail("expected NullPointerException");
532     } catch (NullPointerException expected) {
533     }
534 
535     builder = ImmutableMultiset.builder();
536     Multiset<String> multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b"));
537     try {
538       builder.addAll(multisetWithNull);
539       fail("expected NullPointerException");
540     } catch (NullPointerException expected) {
541     }
542   }
543 
testBuilderAddCopiesHandlesNullsCorrectly()544   public void testBuilderAddCopiesHandlesNullsCorrectly() {
545     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
546     try {
547       builder.addCopies(null, 2);
548       fail("expected NullPointerException");
549     } catch (NullPointerException expected) {
550     }
551   }
552 
testBuilderAddCopiesIllegal()553   public void testBuilderAddCopiesIllegal() {
554     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
555     try {
556       builder.addCopies("a", -2);
557       fail("expected IllegalArgumentException");
558     } catch (IllegalArgumentException expected) {
559     }
560   }
561 
testBuilderSetCountHandlesNullsCorrectly()562   public void testBuilderSetCountHandlesNullsCorrectly() {
563     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
564     try {
565       builder.setCount(null, 2);
566       fail("expected NullPointerException");
567     } catch (NullPointerException expected) {
568     }
569   }
570 
testBuilderSetCountIllegal()571   public void testBuilderSetCountIllegal() {
572     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
573     try {
574       builder.setCount("a", -2);
575       fail("expected IllegalArgumentException");
576     } catch (IllegalArgumentException expected) {
577     }
578   }
579 
580   @GwtIncompatible // NullPointerTester
581   @AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
testNullPointers()582   public void testNullPointers() {
583     NullPointerTester tester = new NullPointerTester();
584     tester.testAllPublicStaticMethods(ImmutableMultiset.class);
585   }
586 
587   @GwtIncompatible // SerializableTester
testSerialization_empty()588   public void testSerialization_empty() {
589     Collection<String> c = ImmutableMultiset.of();
590     assertSame(c, SerializableTester.reserialize(c));
591   }
592 
593   @GwtIncompatible // SerializableTester
testSerialization_multiple()594   public void testSerialization_multiple() {
595     Collection<String> c = ImmutableMultiset.of("a", "b", "a");
596     Collection<String> copy = SerializableTester.reserializeAndAssert(c);
597     assertThat(copy).containsExactly("a", "a", "b").inOrder();
598   }
599 
600   @GwtIncompatible // SerializableTester
testSerialization_elementSet()601   public void testSerialization_elementSet() {
602     Multiset<String> c = ImmutableMultiset.of("a", "b", "a");
603     Collection<String> copy = LenientSerializableTester.reserializeAndAssertLenient(c.elementSet());
604     assertThat(copy).containsExactly("a", "b").inOrder();
605   }
606 
607   @GwtIncompatible // SerializableTester
testSerialization_entrySet()608   public void testSerialization_entrySet() {
609     Multiset<String> c = ImmutableMultiset.of("a", "b", "c");
610     SerializableTester.reserializeAndAssert(c.entrySet());
611   }
612 
testEquals_immutableMultiset()613   public void testEquals_immutableMultiset() {
614     Collection<String> c = ImmutableMultiset.of("a", "b", "a");
615     assertEquals(c, ImmutableMultiset.of("a", "b", "a"));
616     assertEquals(c, ImmutableMultiset.of("a", "a", "b"));
617     assertThat(c).isNotEqualTo(ImmutableMultiset.of("a", "b"));
618     assertThat(c).isNotEqualTo(ImmutableMultiset.of("a", "b", "c", "d"));
619   }
620 
testIterationOrder()621   public void testIterationOrder() {
622     Collection<String> c = ImmutableMultiset.of("a", "b", "a");
623     assertThat(c).containsExactly("a", "a", "b").inOrder();
624     assertThat(ImmutableMultiset.of("c", "b", "a", "c").elementSet())
625         .containsExactly("c", "b", "a")
626         .inOrder();
627   }
628 
testMultisetWrites()629   public void testMultisetWrites() {
630     Multiset<String> multiset = ImmutableMultiset.of("a", "b", "a");
631     UnmodifiableCollectionTests.assertMultisetIsUnmodifiable(multiset, "test");
632   }
633 
testAsList()634   public void testAsList() {
635     ImmutableMultiset<String> multiset = ImmutableMultiset.of("a", "a", "b", "b", "b");
636     ImmutableList<String> list = multiset.asList();
637     assertEquals(ImmutableList.of("a", "a", "b", "b", "b"), list);
638     assertEquals(2, list.indexOf("b"));
639     assertEquals(4, list.lastIndexOf("b"));
640   }
641 
642   @GwtIncompatible // SerializableTester
testSerialization_asList()643   public void testSerialization_asList() {
644     ImmutableMultiset<String> multiset = ImmutableMultiset.of("a", "a", "b", "b", "b");
645     SerializableTester.reserializeAndAssert(multiset.asList());
646   }
647 
testEquals()648   public void testEquals() {
649     new EqualsTester()
650         .addEqualityGroup(ImmutableMultiset.of(), ImmutableMultiset.of())
651         .addEqualityGroup(ImmutableMultiset.of(1), ImmutableMultiset.of(1))
652         .addEqualityGroup(ImmutableMultiset.of(1, 1), ImmutableMultiset.of(1, 1))
653         .addEqualityGroup(ImmutableMultiset.of(1, 2, 1), ImmutableMultiset.of(2, 1, 1))
654         .testEquals();
655   }
656 
testIterationOrderThroughBuilderRemovals()657   public void testIterationOrderThroughBuilderRemovals() {
658     ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder();
659     builder.addCopies("a", 2);
660     builder.add("b");
661     builder.add("c");
662     builder.setCount("b", 0);
663     ImmutableMultiset<String> multiset = builder.build();
664     assertThat(multiset.elementSet()).containsExactly("a", "c").inOrder();
665     builder.add("b");
666     assertThat(builder.build().elementSet()).containsExactly("a", "c", "b").inOrder();
667     assertThat(multiset.elementSet()).containsExactly("a", "c").inOrder();
668   }
669 }
670