1 /* 2 * Copyright (C) 2008 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.collect.testing.MinimalCollection; 24 import com.google.common.collect.testing.google.UnmodifiableCollectionTests; 25 import com.google.common.testing.EqualsTester; 26 27 import junit.framework.TestCase; 28 29 import java.util.Collection; 30 import java.util.Iterator; 31 import java.util.List; 32 33 /** 34 * Tests for {@link ImmutableMultiset}. 35 * 36 * @author Jared Levy 37 */ 38 @GwtCompatible(emulated = true) 39 public class ImmutableMultisetTest extends TestCase { 40 41 public void testCreation_noArgs() { 42 Multiset<String> multiset = ImmutableMultiset.of(); 43 assertTrue(multiset.isEmpty()); 44 } 45 46 public void testCreation_oneElement() { 47 Multiset<String> multiset = ImmutableMultiset.of("a"); 48 assertEquals(HashMultiset.create(asList("a")), multiset); 49 } 50 51 public void testCreation_twoElements() { 52 Multiset<String> multiset = ImmutableMultiset.of("a", "b"); 53 assertEquals(HashMultiset.create(asList("a", "b")), multiset); 54 } 55 56 public void testCreation_threeElements() { 57 Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c"); 58 assertEquals(HashMultiset.create(asList("a", "b", "c")), multiset); 59 } 60 61 public void testCreation_fourElements() { 62 Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d"); 63 assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset); 64 } 65 66 public void testCreation_fiveElements() { 67 Multiset<String> multiset = ImmutableMultiset.of("a", "b", "c", "d", "e"); 68 assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e")), 69 multiset); 70 } 71 72 public void testCreation_sixElements() { 73 Multiset<String> multiset = ImmutableMultiset.of( 74 "a", "b", "c", "d", "e", "f"); 75 assertEquals(HashMultiset.create(asList("a", "b", "c", "d", "e", "f")), 76 multiset); 77 } 78 79 public void testCreation_sevenElements() { 80 Multiset<String> multiset = ImmutableMultiset.of( 81 "a", "b", "c", "d", "e", "f", "g"); 82 assertEquals( 83 HashMultiset.create(asList("a", "b", "c", "d", "e", "f", "g")), 84 multiset); 85 } 86 87 public void testCreation_emptyArray() { 88 String[] array = new String[0]; 89 Multiset<String> multiset = ImmutableMultiset.copyOf(array); 90 assertTrue(multiset.isEmpty()); 91 } 92 93 public void testCreation_arrayOfOneElement() { 94 String[] array = new String[] { "a" }; 95 Multiset<String> multiset = ImmutableMultiset.copyOf(array); 96 assertEquals(HashMultiset.create(asList("a")), multiset); 97 } 98 99 public void testCreation_arrayOfArray() { 100 String[] array = new String[] { "a" }; 101 Multiset<String[]> multiset = ImmutableMultiset.<String[]>of(array); 102 Multiset<String[]> expected = HashMultiset.create(); 103 expected.add(array); 104 assertEquals(expected, multiset); 105 } 106 107 public void testCreation_arrayContainingOnlyNull() { 108 String[] array = new String[] { null }; 109 try { 110 ImmutableMultiset.copyOf(array); 111 fail(); 112 } catch (NullPointerException expected) {} 113 } 114 115 public void testCopyOf_collection_empty() { 116 // "<String>" is required to work around a javac 1.5 bug. 117 Collection<String> c = MinimalCollection.<String>of(); 118 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 119 assertTrue(multiset.isEmpty()); 120 } 121 122 public void testCopyOf_collection_oneElement() { 123 Collection<String> c = MinimalCollection.of("a"); 124 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 125 assertEquals(HashMultiset.create(asList("a")), multiset); 126 } 127 128 public void testCopyOf_collection_general() { 129 Collection<String> c = MinimalCollection.of("a", "b", "a"); 130 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 131 assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); 132 } 133 134 public void testCopyOf_collectionContainingNull() { 135 Collection<String> c = MinimalCollection.of("a", null, "b"); 136 try { 137 ImmutableMultiset.copyOf(c); 138 fail(); 139 } catch (NullPointerException expected) {} 140 } 141 142 public void testCopyOf_multiset_empty() { 143 Multiset<String> c = HashMultiset.create(); 144 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 145 assertTrue(multiset.isEmpty()); 146 } 147 148 public void testCopyOf_multiset_oneElement() { 149 Multiset<String> c = HashMultiset.create(asList("a")); 150 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 151 assertEquals(HashMultiset.create(asList("a")), multiset); 152 } 153 154 public void testCopyOf_multiset_general() { 155 Multiset<String> c = HashMultiset.create(asList("a", "b", "a")); 156 Multiset<String> multiset = ImmutableMultiset.copyOf(c); 157 assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); 158 } 159 160 public void testCopyOf_multisetContainingNull() { 161 Multiset<String> c = HashMultiset.create(asList("a", null, "b")); 162 try { 163 ImmutableMultiset.copyOf(c); 164 fail(); 165 } catch (NullPointerException expected) {} 166 } 167 168 public void testCopyOf_iterator_empty() { 169 Iterator<String> iterator = Iterators.emptyIterator(); 170 Multiset<String> multiset = ImmutableMultiset.copyOf(iterator); 171 assertTrue(multiset.isEmpty()); 172 } 173 174 public void testCopyOf_iterator_oneElement() { 175 Iterator<String> iterator = Iterators.singletonIterator("a"); 176 Multiset<String> multiset = ImmutableMultiset.copyOf(iterator); 177 assertEquals(HashMultiset.create(asList("a")), multiset); 178 } 179 180 public void testCopyOf_iterator_general() { 181 Iterator<String> iterator = asList("a", "b", "a").iterator(); 182 Multiset<String> multiset = ImmutableMultiset.copyOf(iterator); 183 assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); 184 } 185 186 public void testCopyOf_iteratorContainingNull() { 187 Iterator<String> iterator = asList("a", null, "b").iterator(); 188 try { 189 ImmutableMultiset.copyOf(iterator); 190 fail(); 191 } catch (NullPointerException expected) {} 192 } 193 194 private static class CountingIterable implements Iterable<String> { 195 int count = 0; 196 @Override 197 public Iterator<String> iterator() { 198 count++; 199 return asList("a", "b", "a").iterator(); 200 } 201 } 202 203 public void testCopyOf_plainIterable() { 204 CountingIterable iterable = new CountingIterable(); 205 Multiset<String> multiset = ImmutableMultiset.copyOf(iterable); 206 assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); 207 assertEquals(1, iterable.count); 208 } 209 210 public void testCopyOf_shortcut_empty() { 211 Collection<String> c = ImmutableMultiset.of(); 212 assertSame(c, ImmutableMultiset.copyOf(c)); 213 } 214 215 public void testCopyOf_shortcut_singleton() { 216 Collection<String> c = ImmutableMultiset.of("a"); 217 assertSame(c, ImmutableMultiset.copyOf(c)); 218 } 219 220 public void testCopyOf_shortcut_immutableMultiset() { 221 Collection<String> c = ImmutableMultiset.of("a", "b", "c"); 222 assertSame(c, ImmutableMultiset.copyOf(c)); 223 } 224 225 public void testBuilderAdd() { 226 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 227 .add("a") 228 .add("b") 229 .add("a") 230 .add("c") 231 .build(); 232 assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset); 233 } 234 235 public void testBuilderAddAll() { 236 List<String> a = asList("a", "b"); 237 List<String> b = asList("c", "d"); 238 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 239 .addAll(a) 240 .addAll(b) 241 .build(); 242 assertEquals(HashMultiset.create(asList("a", "b", "c", "d")), multiset); 243 } 244 245 public void testBuilderAddAllMultiset() { 246 Multiset<String> a = HashMultiset.create(asList("a", "b", "b")); 247 Multiset<String> b = HashMultiset.create(asList("c", "b")); 248 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 249 .addAll(a) 250 .addAll(b) 251 .build(); 252 assertEquals( 253 HashMultiset.create(asList("a", "b", "b", "b", "c")), multiset); 254 } 255 256 public void testBuilderAddAllIterator() { 257 Iterator<String> iterator = asList("a", "b", "a", "c").iterator(); 258 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 259 .addAll(iterator) 260 .build(); 261 assertEquals(HashMultiset.create(asList("a", "b", "a", "c")), multiset); 262 } 263 264 public void testBuilderAddCopies() { 265 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 266 .addCopies("a", 2) 267 .addCopies("b", 3) 268 .addCopies("c", 0) 269 .build(); 270 assertEquals( 271 HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset); 272 } 273 274 public void testBuilderSetCount() { 275 ImmutableMultiset<String> multiset = new ImmutableMultiset.Builder<String>() 276 .add("a") 277 .setCount("a", 2) 278 .setCount("b", 3) 279 .build(); 280 assertEquals( 281 HashMultiset.create(asList("a", "a", "b", "b", "b")), multiset); 282 } 283 284 public void testBuilderAddHandlesNullsCorrectly() { 285 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 286 try { 287 builder.add((String) null); 288 fail("expected NullPointerException"); 289 } catch (NullPointerException expected) {} 290 } 291 292 public void testBuilderAddAllHandlesNullsCorrectly() { 293 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 294 try { 295 builder.addAll((Collection<String>) null); 296 fail("expected NullPointerException"); 297 } catch (NullPointerException expected) {} 298 299 builder = ImmutableMultiset.builder(); 300 List<String> listWithNulls = asList("a", null, "b"); 301 try { 302 builder.addAll(listWithNulls); 303 fail("expected NullPointerException"); 304 } catch (NullPointerException expected) {} 305 306 builder = ImmutableMultiset.builder(); 307 Multiset<String> multisetWithNull 308 = LinkedHashMultiset.create(asList("a", null, "b")); 309 try { 310 builder.addAll(multisetWithNull); 311 fail("expected NullPointerException"); 312 } catch (NullPointerException expected) {} 313 } 314 315 public void testBuilderAddCopiesHandlesNullsCorrectly() { 316 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 317 try { 318 builder.addCopies(null, 2); 319 fail("expected NullPointerException"); 320 } catch (NullPointerException expected) {} 321 } 322 323 public void testBuilderAddCopiesIllegal() { 324 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 325 try { 326 builder.addCopies("a", -2); 327 fail("expected IllegalArgumentException"); 328 } catch (IllegalArgumentException expected) {} 329 } 330 331 public void testBuilderSetCountHandlesNullsCorrectly() { 332 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 333 try { 334 builder.setCount(null, 2); 335 fail("expected NullPointerException"); 336 } catch (NullPointerException expected) {} 337 } 338 339 public void testBuilderSetCountIllegal() { 340 ImmutableMultiset.Builder<String> builder = ImmutableMultiset.builder(); 341 try { 342 builder.setCount("a", -2); 343 fail("expected IllegalArgumentException"); 344 } catch (IllegalArgumentException expected) {} 345 } 346 347 public void testEquals_immutableMultiset() { 348 Collection<String> c = ImmutableMultiset.of("a", "b", "a"); 349 assertEquals(c, ImmutableMultiset.of("a", "b", "a")); 350 assertEquals(c, ImmutableMultiset.of("a", "a", "b")); 351 assertThat(c).isNotEqualTo(ImmutableMultiset.of("a", "b")); 352 assertThat(c).isNotEqualTo(ImmutableMultiset.of("a", "b", "c", "d")); 353 } 354 355 public void testIterationOrder() { 356 Collection<String> c = ImmutableMultiset.of("a", "b", "a"); 357 assertThat(c).has().exactly("a", "a", "b").inOrder(); 358 } 359 360 public void testMultisetWrites() { 361 Multiset<String> multiset = ImmutableMultiset.of("a", "b", "a"); 362 UnmodifiableCollectionTests.assertMultisetIsUnmodifiable(multiset, "test"); 363 } 364 365 public void testAsList() { 366 ImmutableMultiset<String> multiset 367 = ImmutableMultiset.of("a", "a", "b", "b", "b"); 368 ImmutableList<String> list = multiset.asList(); 369 assertEquals(ImmutableList.of("a", "a", "b", "b", "b"), list); 370 assertEquals(2, list.indexOf("b")); 371 assertEquals(4, list.lastIndexOf("b")); 372 } 373 374 public void testEquals() { 375 new EqualsTester() 376 .addEqualityGroup(ImmutableMultiset.of(), ImmutableMultiset.of()) 377 .addEqualityGroup(ImmutableMultiset.of(1), ImmutableMultiset.of(1)) 378 .addEqualityGroup(ImmutableMultiset.of(1, 1), ImmutableMultiset.of(1, 1)) 379 .addEqualityGroup(ImmutableMultiset.of(1, 2, 1), ImmutableMultiset.of(2, 1, 1)) 380 .testEquals(); 381 } 382 } 383 384