Home | History | Annotate | Download | only in collect
      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 
     30 import junit.framework.Test;
     31 import junit.framework.TestCase;
     32 import junit.framework.TestSuite;
     33 
     34 import java.util.Arrays;
     35 import java.util.List;
     36 
     37 /**
     38  * Unit test for {@link LinkedHashMultiset}.
     39  *
     40  * @author Kevin Bourrillion
     41  */
     42 @GwtCompatible(emulated = true)
     43 public class LinkedHashMultisetTest extends TestCase {
     44 
     45   @GwtIncompatible("suite")
     46   public static Test suite() {
     47     TestSuite suite = new TestSuite();
     48     suite.addTest(MultisetTestSuiteBuilder.using(linkedHashMultisetGenerator())
     49         .named("LinkedHashMultiset")
     50         .withFeatures(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 
     62   private static TestStringMultisetGenerator linkedHashMultisetGenerator() {
     63     return new TestStringMultisetGenerator() {
     64       @Override protected Multiset<String> create(String[] elements) {
     65         return LinkedHashMultiset.create(asList(elements));
     66       }
     67 
     68       @Override
     69       public List<String> order(List<String> insertionOrder) {
     70         List<String> order = Lists.newArrayList();
     71         for (String s : insertionOrder) {
     72           int index = order.indexOf(s);
     73           if (index == -1) {
     74             order.add(s);
     75           } else {
     76             order.add(index, s);
     77           }
     78         }
     79         return order;
     80       }
     81     };
     82   }
     83 
     84   public void testCreate() {
     85     Multiset<String> multiset = LinkedHashMultiset.create();
     86     multiset.add("foo", 2);
     87     multiset.add("bar");
     88     assertEquals(3, multiset.size());
     89     assertEquals(2, multiset.count("foo"));
     90     assertEquals("[foo x 2, bar]", multiset.toString());
     91   }
     92 
     93   public void testCreateWithSize() {
     94     Multiset<String> multiset = LinkedHashMultiset.create(50);
     95     multiset.add("foo", 2);
     96     multiset.add("bar");
     97     assertEquals(3, multiset.size());
     98     assertEquals(2, multiset.count("foo"));
     99     assertEquals("[foo x 2, bar]", multiset.toString());
    100   }
    101 
    102   public void testCreateFromIterable() {
    103     Multiset<String> multiset
    104         = 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()).has().exactly("a", "b", "c").inOrder();
    125     ms.remove("b");
    126     assertThat(ms.elementSet()).has().exactly("a", "b", "c").inOrder();
    127     ms.add("b");
    128     assertThat(ms.elementSet()).has().exactly("a", "b", "c").inOrder();
    129     ms.remove("b", 2);
    130     ms.add("b");
    131     assertThat(ms.elementSet()).has().exactly("a", "c", "b").inOrder();
    132   }
    133 }
    134