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 java.util.Arrays.asList;
     20 
     21 /**
     22  * Unit test for {@link ConcurrentHashMultiset}.
     23  *
     24  * @author Kevin Bourrillion
     25  * @author Jared Levy
     26  */
     27 public abstract class AbstractConcurrentHashMultisetTest
     28     extends AbstractMultisetTest {
     29   // we don't support null
     30   @Override public void testToStringNull() {}
     31 
     32   // our entries are snapshots, not live views. at least for now.
     33 
     34   @Override public void testEntryAfterRemove() {}
     35   @Override public void testEntryAfterClear() {}
     36   @Override public void testEntryAfterEntrySetClear() {}
     37   @Override public void testEntryAfterEntrySetIteratorRemove() {}
     38   @Override public void testEntryAfterElementSetIteratorRemove() {}
     39 
     40   public void testCopyConstructor() {
     41     ms = ConcurrentHashMultiset.create(asList("a", "b", "a", "c"));
     42     assertEquals(4, ms.size());
     43     assertEquals(2, ms.count("a"));
     44     assertEquals(1, ms.count("b"));
     45     assertEquals(1, ms.count("c"));
     46   }
     47 
     48   public void testSetCount() {
     49     ConcurrentHashMultiset<String> cms = ConcurrentHashMultiset.create();
     50     cms.add("a", 2);
     51     cms.add("b", 3);
     52 
     53     try {
     54       cms.setCount("a", -1);
     55       fail();
     56     } catch (IllegalArgumentException expected) {}
     57     assertEquals(2, cms.count("a"));
     58 
     59     assertEquals(2, cms.setCount("a", 0));
     60     assertEquals(0, cms.count("a"));
     61     assertEquals(3, cms.setCount("b", 4));
     62     assertEquals(4, cms.count("b"));
     63     assertEquals(0, cms.setCount("c", 5));
     64     assertEquals(5, cms.count("c"));
     65   }
     66 
     67   public void testSetCountConditional() {
     68     ConcurrentHashMultiset<String> cms = ConcurrentHashMultiset.create();
     69     cms.add("a", 2);
     70     cms.add("b", 3);
     71 
     72     try {
     73       cms.setCount("a", -1, 1);
     74       fail();
     75     } catch (IllegalArgumentException expected) {}
     76     try {
     77       cms.setCount("a", 1, -1);
     78       fail();
     79     } catch (IllegalArgumentException expected) {}
     80     assertEquals(2, cms.count("a"));
     81 
     82     assertTrue(cms.setCount("c", 0, 0));
     83     assertEquals(0, cms.count("c"));
     84     assertFalse(cms.setCount("c", 1, 0));
     85     assertEquals(0, cms.count("c"));
     86     assertFalse(cms.setCount("a", 0, 0));
     87     assertEquals(2, cms.count("a"));
     88     assertFalse(cms.setCount("a", 1, 0));
     89     assertEquals(2, cms.count("a"));
     90     assertTrue(cms.setCount("a", 2, 0));
     91     assertEquals(0, cms.count("a"));
     92 
     93     assertTrue(cms.setCount("d", 0, 4));
     94     assertEquals(4, cms.count("d"));
     95     assertFalse(cms.setCount("b", 0, 5));
     96     assertEquals(3, cms.count("b"));
     97     assertFalse(cms.setCount("b", 1, 5));
     98     assertEquals(3, cms.count("b"));
     99     assertTrue(cms.setCount("b", 3, 5));
    100     assertEquals(5, cms.count("b"));
    101   }
    102 
    103   public void testRemoveExactly() {
    104     ConcurrentHashMultiset<String> cms = ConcurrentHashMultiset.create();
    105     cms.add("a", 2);
    106     cms.add("b", 3);
    107 
    108     try {
    109       cms.removeExactly("a", -2);
    110     } catch (IllegalArgumentException expected) {}
    111 
    112     assertTrue(cms.removeExactly("a", 0));
    113     assertEquals(2, cms.count("a"));
    114     assertTrue(cms.removeExactly("c", 0));
    115     assertEquals(0, cms.count("c"));
    116 
    117     assertFalse(cms.removeExactly("a", 4));
    118     assertEquals(2, cms.count("a"));
    119     assertTrue(cms.removeExactly("a", 2));
    120     assertEquals(0, cms.count("a"));
    121     assertTrue(cms.removeExactly("b", 2));
    122     assertEquals(1, cms.count("b"));
    123   }
    124 }
    125