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.collect.Lists.newArrayList;
     20 import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE;
     21 import static org.junit.contrib.truth.Truth.ASSERT;
     22 
     23 import com.google.common.annotations.GwtCompatible;
     24 import com.google.common.annotations.GwtIncompatible;
     25 import com.google.common.collect.testing.IteratorTester;
     26 
     27 import java.util.Arrays;
     28 import java.util.ConcurrentModificationException;
     29 import java.util.Iterator;
     30 
     31 /**
     32  * Unit test for {@link LinkedHashMultiset}.
     33  *
     34  * @author Kevin Bourrillion
     35  */
     36 @GwtCompatible(emulated = true)
     37 public class LinkedHashMultisetTest extends AbstractMultisetTest {
     38   @Override protected <E> Multiset<E> create() {
     39     return LinkedHashMultiset.create();
     40   }
     41 
     42   public void testCreate() {
     43     Multiset<String> multiset = LinkedHashMultiset.create();
     44     multiset.add("foo", 2);
     45     multiset.add("bar");
     46     assertEquals(3, multiset.size());
     47     assertEquals(2, multiset.count("foo"));
     48     assertEquals("[foo x 2, bar]", multiset.toString());
     49   }
     50 
     51   public void testCreateWithSize() {
     52     Multiset<String> multiset = LinkedHashMultiset.create(50);
     53     multiset.add("foo", 2);
     54     multiset.add("bar");
     55     assertEquals(3, multiset.size());
     56     assertEquals(2, multiset.count("foo"));
     57     assertEquals("[foo x 2, bar]", multiset.toString());
     58   }
     59 
     60   public void testCreateFromIterable() {
     61     Multiset<String> multiset
     62         = LinkedHashMultiset.create(Arrays.asList("foo", "bar", "foo"));
     63     assertEquals(3, multiset.size());
     64     assertEquals(2, multiset.count("foo"));
     65     assertEquals("[foo x 2, bar]", multiset.toString());
     66   }
     67 
     68   @GwtIncompatible("unreasonable slow")
     69   public void testIteratorBashing() {
     70     ms = createSample();
     71     IteratorTester<String> tester =
     72         new IteratorTester<String>(6, MODIFIABLE, newArrayList(ms),
     73             IteratorTester.KnownOrder.KNOWN_ORDER) {
     74           @Override protected Iterator<String> newTargetIterator() {
     75             return createSample().iterator();
     76           }
     77         };
     78     tester.test();
     79   }
     80 
     81   @GwtIncompatible("slow (~30s)")
     82   public void testElementSetIteratorBashing() {
     83     IteratorTester<String> tester =
     84         new IteratorTester<String>(5, MODIFIABLE, newArrayList("a", "c", "b"),
     85             IteratorTester.KnownOrder.KNOWN_ORDER) {
     86           @Override protected Iterator<String> newTargetIterator() {
     87             Multiset<String> multiset = create();
     88             multiset.add("a", 3);
     89             multiset.add("c", 1);
     90             multiset.add("b", 2);
     91             return multiset.elementSet().iterator();
     92           }
     93         };
     94     tester.test();
     95   }
     96 
     97   public void testToString() {
     98     ms.add("a", 3);
     99     ms.add("c", 1);
    100     ms.add("b", 2);
    101 
    102     assertEquals("[a x 3, c, b x 2]", ms.toString());
    103   }
    104 
    105   public void testLosesPlaceInLine() throws Exception {
    106     ms.add("a");
    107     ms.add("b", 2);
    108     ms.add("c");
    109     ASSERT.that(ms.elementSet()).hasContentsInOrder("a", "b", "c");
    110     ms.remove("b");
    111     ASSERT.that(ms.elementSet()).hasContentsInOrder("a", "b", "c");
    112     ms.add("b");
    113     ASSERT.that(ms.elementSet()).hasContentsInOrder("a", "b", "c");
    114     ms.remove("b", 2);
    115     ms.add("b");
    116     ASSERT.that(ms.elementSet()).hasContentsInOrder("a", "c", "b");
    117   }
    118 
    119   public void testIteratorRemoveConcurrentModification() {
    120     ms.add("a");
    121     ms.add("b");
    122     Iterator<String> iterator = ms.iterator();
    123     iterator.next();
    124     ms.remove("a");
    125     assertEquals(1, ms.size());
    126     assertTrue(ms.contains("b"));
    127     try {
    128       iterator.remove();
    129       fail();
    130     } catch (ConcurrentModificationException expected) {}
    131     assertEquals(1, ms.size());
    132     assertTrue(ms.contains("b"));
    133   }
    134 }
    135