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