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 import com.google.common.annotations.GwtCompatible; 22 import com.google.common.collect.testing.Helpers; 23 import com.google.common.collect.testing.MinimalCollection; 24 import com.google.common.collect.testing.MinimalIterable; 25 26 import junit.framework.TestCase; 27 28 import java.util.Arrays; 29 import java.util.Collection; 30 import java.util.Collections; 31 import java.util.Iterator; 32 import java.util.List; 33 34 /** 35 * Unit test for {@link ImmutableList}. 36 * 37 * @author Kevin Bourrillion 38 * @author George van den Driessche 39 * @author Jared Levy 40 */ 41 @GwtCompatible(emulated = true) 42 public class ImmutableListTest extends TestCase { 43 44 public static class CreationTests extends TestCase { testCreation_noArgs()45 public void testCreation_noArgs() { 46 List<String> list = ImmutableList.of(); 47 assertEquals(Collections.emptyList(), list); 48 } 49 testCreation_oneElement()50 public void testCreation_oneElement() { 51 List<String> list = ImmutableList.of("a"); 52 assertEquals(Collections.singletonList("a"), list); 53 } 54 testCreation_twoElements()55 public void testCreation_twoElements() { 56 List<String> list = ImmutableList.of("a", "b"); 57 assertEquals(Lists.newArrayList("a", "b"), list); 58 } 59 testCreation_threeElements()60 public void testCreation_threeElements() { 61 List<String> list = ImmutableList.of("a", "b", "c"); 62 assertEquals(Lists.newArrayList("a", "b", "c"), list); 63 } 64 testCreation_fourElements()65 public void testCreation_fourElements() { 66 List<String> list = ImmutableList.of("a", "b", "c", "d"); 67 assertEquals(Lists.newArrayList("a", "b", "c", "d"), list); 68 } 69 testCreation_fiveElements()70 public void testCreation_fiveElements() { 71 List<String> list = ImmutableList.of("a", "b", "c", "d", "e"); 72 assertEquals(Lists.newArrayList("a", "b", "c", "d", "e"), list); 73 } 74 testCreation_sixElements()75 public void testCreation_sixElements() { 76 List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f"); 77 assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f"), list); 78 } 79 testCreation_sevenElements()80 public void testCreation_sevenElements() { 81 List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g"); 82 assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g"), list); 83 } 84 testCreation_eightElements()85 public void testCreation_eightElements() { 86 List<String> list = ImmutableList.of( 87 "a", "b", "c", "d", "e", "f", "g", "h"); 88 assertEquals(Lists.newArrayList( 89 "a", "b", "c", "d", "e", "f", "g", "h"), list); 90 } 91 testCreation_nineElements()92 public void testCreation_nineElements() { 93 List<String> list = ImmutableList.of( 94 "a", "b", "c", "d", "e", "f", "g", "h", "i"); 95 assertEquals(Lists.newArrayList( 96 "a", "b", "c", "d", "e", "f", "g", "h", "i"), list); 97 } 98 testCreation_tenElements()99 public void testCreation_tenElements() { 100 List<String> list = ImmutableList.of( 101 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"); 102 assertEquals(Lists.newArrayList( 103 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), list); 104 } 105 testCreation_elevenElements()106 public void testCreation_elevenElements() { 107 List<String> list = ImmutableList.of( 108 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"); 109 assertEquals(Lists.newArrayList( 110 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), list); 111 } 112 113 // Varargs versions 114 testCreation_twelveElements()115 public void testCreation_twelveElements() { 116 List<String> list = ImmutableList.of( 117 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"); 118 assertEquals(Lists.newArrayList( 119 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"), list); 120 } 121 testCreation_thirteenElements()122 public void testCreation_thirteenElements() { 123 List<String> list = ImmutableList.of( 124 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"); 125 assertEquals(Lists.newArrayList( 126 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"), 127 list); 128 } 129 testCreation_fourteenElements()130 public void testCreation_fourteenElements() { 131 List<String> list = ImmutableList.of( 132 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"); 133 assertEquals(Lists.newArrayList( 134 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"), 135 list); 136 } 137 testCreation_singletonNull()138 public void testCreation_singletonNull() { 139 try { 140 ImmutableList.of((String) null); 141 fail(); 142 } catch (NullPointerException expected) { 143 } 144 } 145 testCreation_withNull()146 public void testCreation_withNull() { 147 try { 148 ImmutableList.of("a", null, "b"); 149 fail(); 150 } catch (NullPointerException expected) { 151 } 152 } 153 testCreation_generic()154 public void testCreation_generic() { 155 List<String> a = ImmutableList.of("a"); 156 // only verify that there is no compile warning 157 ImmutableList.of(a, a); 158 } 159 testCreation_arrayOfArray()160 public void testCreation_arrayOfArray() { 161 String[] array = new String[] { "a" }; 162 List<String[]> list = ImmutableList.<String[]>of(array); 163 assertEquals(Collections.singletonList(array), list); 164 } 165 testCopyOf_emptyArray()166 public void testCopyOf_emptyArray() { 167 String[] array = new String[0]; 168 List<String> list = ImmutableList.copyOf(array); 169 assertEquals(Collections.emptyList(), list); 170 } 171 testCopyOf_arrayOfOneElement()172 public void testCopyOf_arrayOfOneElement() { 173 String[] array = new String[] { "a" }; 174 List<String> list = ImmutableList.copyOf(array); 175 assertEquals(Collections.singletonList("a"), list); 176 } 177 testCopyOf_nullArray()178 public void testCopyOf_nullArray() { 179 try { 180 ImmutableList.copyOf((String[]) null); 181 fail(); 182 } catch (NullPointerException expected) { 183 } 184 } 185 testCopyOf_arrayContainingOnlyNull()186 public void testCopyOf_arrayContainingOnlyNull() { 187 String[] array = new String[] { null }; 188 try { 189 ImmutableList.copyOf(array); 190 fail(); 191 } catch (NullPointerException expected) { 192 } 193 } 194 testCopyOf_collection_empty()195 public void testCopyOf_collection_empty() { 196 // "<String>" is required to work around a javac 1.5 bug. 197 Collection<String> c = MinimalCollection.<String>of(); 198 List<String> list = ImmutableList.copyOf(c); 199 assertEquals(Collections.emptyList(), list); 200 } 201 testCopyOf_collection_oneElement()202 public void testCopyOf_collection_oneElement() { 203 Collection<String> c = MinimalCollection.of("a"); 204 List<String> list = ImmutableList.copyOf(c); 205 assertEquals(Collections.singletonList("a"), list); 206 } 207 testCopyOf_collection_general()208 public void testCopyOf_collection_general() { 209 Collection<String> c = MinimalCollection.of("a", "b", "a"); 210 List<String> list = ImmutableList.copyOf(c); 211 assertEquals(asList("a", "b", "a"), list); 212 List<String> mutableList = asList("a", "b"); 213 list = ImmutableList.copyOf(mutableList); 214 mutableList.set(0, "c"); 215 assertEquals(asList("a", "b"), list); 216 } 217 testCopyOf_collectionContainingNull()218 public void testCopyOf_collectionContainingNull() { 219 Collection<String> c = MinimalCollection.of("a", null, "b"); 220 try { 221 ImmutableList.copyOf(c); 222 fail(); 223 } catch (NullPointerException expected) { 224 } 225 } 226 testCopyOf_iterator_empty()227 public void testCopyOf_iterator_empty() { 228 Iterator<String> iterator = Iterators.emptyIterator(); 229 List<String> list = ImmutableList.copyOf(iterator); 230 assertEquals(Collections.emptyList(), list); 231 } 232 testCopyOf_iterator_oneElement()233 public void testCopyOf_iterator_oneElement() { 234 Iterator<String> iterator = Iterators.singletonIterator("a"); 235 List<String> list = ImmutableList.copyOf(iterator); 236 assertEquals(Collections.singletonList("a"), list); 237 } 238 testCopyOf_iterator_general()239 public void testCopyOf_iterator_general() { 240 Iterator<String> iterator = asList("a", "b", "a").iterator(); 241 List<String> list = ImmutableList.copyOf(iterator); 242 assertEquals(asList("a", "b", "a"), list); 243 } 244 testCopyOf_iteratorContainingNull()245 public void testCopyOf_iteratorContainingNull() { 246 Iterator<String> iterator = asList("a", null, "b").iterator(); 247 try { 248 ImmutableList.copyOf(iterator); 249 fail(); 250 } catch (NullPointerException expected) { 251 } 252 } 253 testCopyOf_iteratorNull()254 public void testCopyOf_iteratorNull() { 255 try { 256 ImmutableList.copyOf((Iterator<String>) null); 257 fail(); 258 } catch (NullPointerException expected) { 259 } 260 } 261 testCopyOf_concurrentlyMutating()262 public void testCopyOf_concurrentlyMutating() { 263 List<String> sample = Lists.newArrayList("a", "b", "c"); 264 for (int delta : new int[] {-1, 0, 1}) { 265 for (int i = 0; i < sample.size(); i++) { 266 Collection<String> misleading = 267 Helpers.misleadingSizeCollection(delta); 268 List<String> expected = sample.subList(0, i); 269 misleading.addAll(expected); 270 assertEquals(expected, ImmutableList.copyOf(misleading)); 271 assertEquals(expected, 272 ImmutableList.copyOf((Iterable<String>) misleading)); 273 } 274 } 275 } 276 277 private static class CountingIterable implements Iterable<String> { 278 int count = 0; 279 @Override iterator()280 public Iterator<String> iterator() { 281 count++; 282 return asList("a", "b", "a").iterator(); 283 } 284 } 285 testCopyOf_plainIterable()286 public void testCopyOf_plainIterable() { 287 CountingIterable iterable = new CountingIterable(); 288 List<String> list = ImmutableList.copyOf(iterable); 289 assertEquals(asList("a", "b", "a"), list); 290 } 291 testCopyOf_plainIterable_iteratesOnce()292 public void testCopyOf_plainIterable_iteratesOnce() { 293 CountingIterable iterable = new CountingIterable(); 294 ImmutableList.copyOf(iterable); 295 assertEquals(1, iterable.count); 296 } 297 testCopyOf_shortcut_empty()298 public void testCopyOf_shortcut_empty() { 299 Collection<String> c = ImmutableList.of(); 300 assertSame(c, ImmutableList.copyOf(c)); 301 } 302 testCopyOf_shortcut_singleton()303 public void testCopyOf_shortcut_singleton() { 304 Collection<String> c = ImmutableList.of("a"); 305 assertSame(c, ImmutableList.copyOf(c)); 306 } 307 testCopyOf_shortcut_immutableList()308 public void testCopyOf_shortcut_immutableList() { 309 Collection<String> c = ImmutableList.of("a", "b", "c"); 310 assertSame(c, ImmutableList.copyOf(c)); 311 } 312 testBuilderAddArrayHandlesNulls()313 public void testBuilderAddArrayHandlesNulls() { 314 String[] elements = {"a", null, "b"}; 315 ImmutableList.Builder<String> builder = ImmutableList.builder(); 316 try { 317 builder.add(elements); 318 fail ("Expected NullPointerException"); 319 } catch (NullPointerException expected) { 320 } 321 ImmutableList<String> result = builder.build(); 322 323 /* 324 * Maybe it rejects all elements, or maybe it adds "a" before failing. 325 * Either way is fine with us. 326 */ 327 if (result.isEmpty()) { 328 return; 329 } 330 assertTrue(ImmutableList.of("a").equals(result)); 331 assertEquals(1, result.size()); 332 } 333 testBuilderAddCollectionHandlesNulls()334 public void testBuilderAddCollectionHandlesNulls() { 335 List<String> elements = Arrays.asList("a", null, "b"); 336 ImmutableList.Builder<String> builder = ImmutableList.builder(); 337 try { 338 builder.addAll(elements); 339 fail ("Expected NullPointerException"); 340 } catch (NullPointerException expected) { 341 } 342 ImmutableList<String> result = builder.build(); 343 assertEquals(ImmutableList.of("a"), result); 344 assertEquals(1, result.size()); 345 } 346 } 347 348 public static class BasicTests extends TestCase { 349 testEquals_immutableList()350 public void testEquals_immutableList() { 351 Collection<String> c = ImmutableList.of("a", "b", "c"); 352 assertTrue(c.equals(ImmutableList.of("a", "b", "c"))); 353 assertFalse(c.equals(ImmutableList.of("a", "c", "b"))); 354 assertFalse(c.equals(ImmutableList.of("a", "b"))); 355 assertFalse(c.equals(ImmutableList.of("a", "b", "c", "d"))); 356 } 357 testBuilderAdd()358 public void testBuilderAdd() { 359 ImmutableList<String> list = new ImmutableList.Builder<String>() 360 .add("a") 361 .add("b") 362 .add("a") 363 .add("c") 364 .build(); 365 assertEquals(asList("a", "b", "a", "c"), list); 366 } 367 testBuilderAdd_varargs()368 public void testBuilderAdd_varargs() { 369 ImmutableList<String> list = new ImmutableList.Builder<String>() 370 .add("a", "b", "a", "c") 371 .build(); 372 assertEquals(asList("a", "b", "a", "c"), list); 373 } 374 testBuilderAddAll_iterable()375 public void testBuilderAddAll_iterable() { 376 List<String> a = asList("a", "b"); 377 List<String> b = asList("c", "d"); 378 ImmutableList<String> list = new ImmutableList.Builder<String>() 379 .addAll(a) 380 .addAll(b) 381 .build(); 382 assertEquals(asList( "a", "b", "c", "d"), list); 383 b.set(0, "f"); 384 assertEquals(asList( "a", "b", "c", "d"), list); 385 } 386 testBuilderAddAll_iterator()387 public void testBuilderAddAll_iterator() { 388 List<String> a = asList("a", "b"); 389 List<String> b = asList("c", "d"); 390 ImmutableList<String> list = new ImmutableList.Builder<String>() 391 .addAll(a.iterator()) 392 .addAll(b.iterator()) 393 .build(); 394 assertEquals(asList( "a", "b", "c", "d"), list); 395 b.set(0, "f"); 396 assertEquals(asList( "a", "b", "c", "d"), list); 397 } 398 testComplexBuilder()399 public void testComplexBuilder() { 400 List<Integer> colorElem = asList(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF); 401 ImmutableList.Builder<Integer> webSafeColorsBuilder 402 = ImmutableList.builder(); 403 for (Integer red : colorElem) { 404 for (Integer green : colorElem) { 405 for (Integer blue : colorElem) { 406 webSafeColorsBuilder.add((red << 16) + (green << 8) + blue); 407 } 408 } 409 } 410 ImmutableList<Integer> webSafeColors = webSafeColorsBuilder.build(); 411 assertEquals(216, webSafeColors.size()); 412 Integer[] webSafeColorArray = 413 webSafeColors.toArray(new Integer[webSafeColors.size()]); 414 assertEquals(0x000000, (int) webSafeColorArray[0]); 415 assertEquals(0x000033, (int) webSafeColorArray[1]); 416 assertEquals(0x000066, (int) webSafeColorArray[2]); 417 assertEquals(0x003300, (int) webSafeColorArray[6]); 418 assertEquals(0x330000, (int) webSafeColorArray[36]); 419 assertEquals(0x000066, (int) webSafeColors.get(2)); 420 assertEquals(0x003300, (int) webSafeColors.get(6)); 421 ImmutableList<Integer> addedColor 422 = webSafeColorsBuilder.add(0x00BFFF).build(); 423 assertEquals("Modifying the builder should not have changed any already" 424 + " built sets", 216, webSafeColors.size()); 425 assertEquals("the new array should be one bigger than webSafeColors", 426 217, addedColor.size()); 427 Integer[] appendColorArray = 428 addedColor.toArray(new Integer[addedColor.size()]); 429 assertEquals(0x00BFFF, (int) appendColorArray[216]); 430 } 431 testBuilderAddHandlesNullsCorrectly()432 public void testBuilderAddHandlesNullsCorrectly() { 433 ImmutableList.Builder<String> builder = ImmutableList.builder(); 434 try { 435 builder.add((String) null); 436 fail("expected NullPointerException"); 437 } catch (NullPointerException expected) { 438 } 439 440 try { 441 builder.add((String[]) null); 442 fail("expected NullPointerException"); 443 } catch (NullPointerException expected) { 444 } 445 446 try { 447 builder.add("a", null, "b"); 448 fail("expected NullPointerException"); 449 } catch (NullPointerException expected) { 450 } 451 } 452 testBuilderAddAllHandlesNullsCorrectly()453 public void testBuilderAddAllHandlesNullsCorrectly() { 454 ImmutableList.Builder<String> builder = ImmutableList.builder(); 455 try { 456 builder.addAll((Iterable<String>) null); 457 fail("expected NullPointerException"); 458 } catch (NullPointerException expected) { 459 } 460 461 try { 462 builder.addAll((Iterator<String>) null); 463 fail("expected NullPointerException"); 464 } catch (NullPointerException expected) { 465 } 466 467 builder = ImmutableList.builder(); 468 List<String> listWithNulls = asList("a", null, "b"); 469 try { 470 builder.addAll(listWithNulls); 471 fail("expected NullPointerException"); 472 } catch (NullPointerException expected) { 473 } 474 475 builder = ImmutableList.builder(); 476 Iterator<String> iteratorWithNulls = asList("a", null, "b").iterator(); 477 try { 478 builder.addAll(iteratorWithNulls); 479 fail("expected NullPointerException"); 480 } catch (NullPointerException expected) { 481 } 482 483 Iterable<String> iterableWithNulls = MinimalIterable.of("a", null, "b"); 484 try { 485 builder.addAll(iterableWithNulls); 486 fail("expected NullPointerException"); 487 } catch (NullPointerException expected) { 488 } 489 } 490 testAsList()491 public void testAsList() { 492 ImmutableList<String> list = ImmutableList.of("a", "b"); 493 assertSame(list, list.asList()); 494 } 495 } 496 } 497 498