1 /* 2 * Copyright (C) 2007 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.truth.Truth.assertThat; 20 import static java.util.Arrays.asList; 21 22 import com.google.common.annotations.GwtCompatible; 23 import com.google.common.annotations.GwtIncompatible; 24 import com.google.common.annotations.J2ktIncompatible; 25 import com.google.common.collect.testing.features.CollectionFeature; 26 import com.google.common.collect.testing.features.CollectionSize; 27 import com.google.common.collect.testing.google.MultisetFeature; 28 import com.google.common.collect.testing.google.MultisetTestSuiteBuilder; 29 import com.google.common.collect.testing.google.TestStringMultisetGenerator; 30 import java.util.Arrays; 31 import java.util.List; 32 import junit.framework.Test; 33 import junit.framework.TestCase; 34 import junit.framework.TestSuite; 35 36 /** 37 * Unit test for {@link LinkedHashMultiset}. 38 * 39 * @author Kevin Bourrillion 40 */ 41 @GwtCompatible(emulated = true) 42 @ElementTypesAreNonnullByDefault 43 public class LinkedHashMultisetTest extends TestCase { 44 45 @J2ktIncompatible 46 @GwtIncompatible // suite suite()47 public static Test suite() { 48 TestSuite suite = new TestSuite(); 49 suite.addTest( 50 MultisetTestSuiteBuilder.using(linkedHashMultisetGenerator()) 51 .named("LinkedHashMultiset") 52 .withFeatures( 53 CollectionSize.ANY, 54 CollectionFeature.KNOWN_ORDER, 55 CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, 56 CollectionFeature.ALLOWS_NULL_VALUES, 57 CollectionFeature.SERIALIZABLE, 58 CollectionFeature.GENERAL_PURPOSE, 59 MultisetFeature.ENTRIES_ARE_VIEWS) 60 .createTestSuite()); 61 suite.addTestSuite(LinkedHashMultisetTest.class); 62 return suite; 63 } 64 linkedHashMultisetGenerator()65 private static TestStringMultisetGenerator linkedHashMultisetGenerator() { 66 return new TestStringMultisetGenerator() { 67 @Override 68 protected Multiset<String> create(String[] elements) { 69 return LinkedHashMultiset.create(asList(elements)); 70 } 71 72 @Override 73 public List<String> order(List<String> insertionOrder) { 74 List<String> order = Lists.newArrayList(); 75 for (String s : insertionOrder) { 76 int index = order.indexOf(s); 77 if (index == -1) { 78 order.add(s); 79 } else { 80 order.add(index, s); 81 } 82 } 83 return order; 84 } 85 }; 86 } 87 88 public void testCreate() { 89 Multiset<String> multiset = LinkedHashMultiset.create(); 90 multiset.add("foo", 2); 91 multiset.add("bar"); 92 assertEquals(3, multiset.size()); 93 assertEquals(2, multiset.count("foo")); 94 assertEquals("[foo x 2, bar]", multiset.toString()); 95 } 96 97 public void testCreateWithSize() { 98 Multiset<String> multiset = LinkedHashMultiset.create(50); 99 multiset.add("foo", 2); 100 multiset.add("bar"); 101 assertEquals(3, multiset.size()); 102 assertEquals(2, multiset.count("foo")); 103 assertEquals("[foo x 2, bar]", multiset.toString()); 104 } 105 106 public void testCreateFromIterable() { 107 Multiset<String> multiset = LinkedHashMultiset.create(Arrays.asList("foo", "bar", "foo")); 108 assertEquals(3, multiset.size()); 109 assertEquals(2, multiset.count("foo")); 110 assertEquals("[foo x 2, bar]", multiset.toString()); 111 } 112 113 public void testToString() { 114 Multiset<String> ms = LinkedHashMultiset.create(); 115 ms.add("a", 3); 116 ms.add("c", 1); 117 ms.add("b", 2); 118 119 assertEquals("[a x 3, c, b x 2]", ms.toString()); 120 } 121 122 public void testLosesPlaceInLine() throws Exception { 123 Multiset<String> ms = LinkedHashMultiset.create(); 124 ms.add("a"); 125 ms.add("b", 2); 126 ms.add("c"); 127 assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); 128 ms.remove("b"); 129 assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); 130 ms.add("b"); 131 assertThat(ms.elementSet()).containsExactly("a", "b", "c").inOrder(); 132 ms.remove("b", 2); 133 ms.add("b"); 134 assertThat(ms.elementSet()).containsExactly("a", "c", "b").inOrder(); 135 } 136 } 137