aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/collect/ImmutableListTest.java
blob: c881e0bf41937d1de5091c3364e0c292a013cd71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
/*
 * Copyright (C) 2007 The Guava Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.common.collect;

import static com.google.common.collect.testing.features.CollectionFeature.ALLOWS_NULL_QUERIES;
import static com.google.common.collect.testing.features.CollectionFeature.SERIALIZABLE;
import static java.util.Arrays.asList;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.testing.Helpers;
import com.google.common.collect.testing.ListTestSuiteBuilder;
import com.google.common.collect.testing.MinimalCollection;
import com.google.common.collect.testing.MinimalIterable;
import com.google.common.collect.testing.features.CollectionSize;
import com.google.common.collect.testing.google.ListGenerators.BuilderAddAllListGenerator;
import com.google.common.collect.testing.google.ListGenerators.BuilderReversedListGenerator;
import com.google.common.collect.testing.google.ListGenerators.ImmutableListHeadSubListGenerator;
import com.google.common.collect.testing.google.ListGenerators.ImmutableListMiddleSubListGenerator;
import com.google.common.collect.testing.google.ListGenerators.ImmutableListOfGenerator;
import com.google.common.collect.testing.google.ListGenerators.ImmutableListTailSubListGenerator;
import com.google.common.collect.testing.google.ListGenerators.UnhashableElementsImmutableListGenerator;
import com.google.common.collect.testing.testers.ListHashCodeTester;
import com.google.common.testing.CollectorTester;
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit test for {@link ImmutableList}.
 *
 * @author Kevin Bourrillion
 * @author George van den Driessche
 * @author Jared Levy
 */
@GwtCompatible(emulated = true)
public class ImmutableListTest extends TestCase {

  @GwtIncompatible // suite
  public static Test suite() {
    TestSuite suite = new TestSuite();
    suite.addTest(
        ListTestSuiteBuilder.using(new ImmutableListOfGenerator())
            .named("ImmutableList")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new BuilderAddAllListGenerator())
            .named("ImmutableList, built with Builder.add")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new BuilderAddAllListGenerator())
            .named("ImmutableList, built with Builder.addAll")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new BuilderReversedListGenerator())
            .named("ImmutableList, reversed")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new ImmutableListHeadSubListGenerator())
            .named("ImmutableList, head subList")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new ImmutableListTailSubListGenerator())
            .named("ImmutableList, tail subList")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new ImmutableListMiddleSubListGenerator())
            .named("ImmutableList, middle subList")
            .withFeatures(CollectionSize.ANY, SERIALIZABLE, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    suite.addTest(
        ListTestSuiteBuilder.using(new UnhashableElementsImmutableListGenerator())
            .suppressing(ListHashCodeTester.getHashCodeMethod())
            .named("ImmutableList, unhashable values")
            .withFeatures(CollectionSize.ANY, ALLOWS_NULL_QUERIES)
            .createTestSuite());
    return suite;
  }

  // Creation tests

  public void testCreation_noArgs() {
    List<String> list = ImmutableList.of();
    assertEquals(Collections.emptyList(), list);
  }

  public void testCreation_oneElement() {
    List<String> list = ImmutableList.of("a");
    assertEquals(Collections.singletonList("a"), list);
  }

  public void testCreation_twoElements() {
    List<String> list = ImmutableList.of("a", "b");
    assertEquals(Lists.newArrayList("a", "b"), list);
  }

  public void testCreation_threeElements() {
    List<String> list = ImmutableList.of("a", "b", "c");
    assertEquals(Lists.newArrayList("a", "b", "c"), list);
  }

  public void testCreation_fourElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d");
    assertEquals(Lists.newArrayList("a", "b", "c", "d"), list);
  }

  public void testCreation_fiveElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e"), list);
  }

  public void testCreation_sixElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f"), list);
  }

  public void testCreation_sevenElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g"), list);
  }

  public void testCreation_eightElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h"), list);
  }

  public void testCreation_nineElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i"), list);
  }

  public void testCreation_tenElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j"), list);
  }

  public void testCreation_elevenElements() {
    List<String> list = ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k");
    assertEquals(Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"), list);
  }

  // Varargs versions

  public void testCreation_twelveElements() {
    List<String> list =
        ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l");
    assertEquals(
        Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"), list);
  }

  public void testCreation_thirteenElements() {
    List<String> list =
        ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m");
    assertEquals(
        Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"), list);
  }

  public void testCreation_fourteenElements() {
    List<String> list =
        ImmutableList.of("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n");
    assertEquals(
        Lists.newArrayList("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n"),
        list);
  }

  public void testCreation_singletonNull() {
    try {
      ImmutableList.of((String) null);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCreation_withNull() {
    try {
      ImmutableList.of("a", null, "b");
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCreation_generic() {
    List<String> a = ImmutableList.of("a");
    // only verify that there is no compile warning
    ImmutableList<List<String>> unused = ImmutableList.of(a, a);
  }

  public void testCreation_arrayOfArray() {
    String[] array = new String[] {"a"};
    List<String[]> list = ImmutableList.<String[]>of(array);
    assertEquals(Collections.singletonList(array), list);
  }

  public void testCopyOf_emptyArray() {
    String[] array = new String[0];
    List<String> list = ImmutableList.copyOf(array);
    assertEquals(Collections.emptyList(), list);
  }

  public void testCopyOf_arrayOfOneElement() {
    String[] array = new String[] {"a"};
    List<String> list = ImmutableList.copyOf(array);
    assertEquals(Collections.singletonList("a"), list);
  }

  public void testCopyOf_nullArray() {
    try {
      ImmutableList.copyOf((String[]) null);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCopyOf_arrayContainingOnlyNull() {
    String[] array = new String[] {null};
    try {
      ImmutableList.copyOf(array);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCopyOf_collection_empty() {
    // "<String>" is required to work around a javac 1.5 bug.
    Collection<String> c = MinimalCollection.<String>of();
    List<String> list = ImmutableList.copyOf(c);
    assertEquals(Collections.emptyList(), list);
  }

  public void testCopyOf_collection_oneElement() {
    Collection<String> c = MinimalCollection.of("a");
    List<String> list = ImmutableList.copyOf(c);
    assertEquals(Collections.singletonList("a"), list);
  }

  public void testCopyOf_collection_general() {
    Collection<String> c = MinimalCollection.of("a", "b", "a");
    List<String> list = ImmutableList.copyOf(c);
    assertEquals(asList("a", "b", "a"), list);
    List<String> mutableList = asList("a", "b");
    list = ImmutableList.copyOf(mutableList);
    mutableList.set(0, "c");
    assertEquals(asList("a", "b"), list);
  }

  public void testCopyOf_collectionContainingNull() {
    Collection<String> c = MinimalCollection.of("a", null, "b");
    try {
      ImmutableList.copyOf(c);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCopyOf_iterator_empty() {
    Iterator<String> iterator = Iterators.emptyIterator();
    List<String> list = ImmutableList.copyOf(iterator);
    assertEquals(Collections.emptyList(), list);
  }

  public void testCopyOf_iterator_oneElement() {
    Iterator<String> iterator = Iterators.singletonIterator("a");
    List<String> list = ImmutableList.copyOf(iterator);
    assertEquals(Collections.singletonList("a"), list);
  }

  public void testCopyOf_iterator_general() {
    Iterator<String> iterator = asList("a", "b", "a").iterator();
    List<String> list = ImmutableList.copyOf(iterator);
    assertEquals(asList("a", "b", "a"), list);
  }

  public void testCopyOf_iteratorContainingNull() {
    Iterator<String> iterator = asList("a", null, "b").iterator();
    try {
      ImmutableList.copyOf(iterator);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCopyOf_iteratorNull() {
    try {
      ImmutableList.copyOf((Iterator<String>) null);
      fail();
    } catch (NullPointerException expected) {
    }
  }

  public void testCopyOf_concurrentlyMutating() {
    List<String> sample = Lists.newArrayList("a", "b", "c");
    for (int delta : new int[] {-1, 0, 1}) {
      for (int i = 0; i < sample.size(); i++) {
        Collection<String> misleading = Helpers.misleadingSizeCollection(delta);
        List<String> expected = sample.subList(0, i);
        misleading.addAll(expected);
        assertEquals(expected, ImmutableList.copyOf(misleading));
        assertEquals(expected, ImmutableList.copyOf((Iterable<String>) misleading));
      }
    }
  }

  private static class CountingIterable implements Iterable<String> {
    int count = 0;

    @Override
    public Iterator<String> iterator() {
      count++;
      return asList("a", "b", "a").iterator();
    }
  }

  public void testCopyOf_plainIterable() {
    CountingIterable iterable = new CountingIterable();
    List<String> list = ImmutableList.copyOf(iterable);
    assertEquals(asList("a", "b", "a"), list);
  }

  public void testCopyOf_plainIterable_iteratesOnce() {
    CountingIterable iterable = new CountingIterable();
    ImmutableList<String> unused = ImmutableList.copyOf(iterable);
    assertEquals(1, iterable.count);
  }

  public void testCopyOf_shortcut_empty() {
    Collection<String> c = ImmutableList.of();
    assertSame(c, ImmutableList.copyOf(c));
  }

  public void testCopyOf_shortcut_singleton() {
    Collection<String> c = ImmutableList.of("a");
    assertSame(c, ImmutableList.copyOf(c));
  }

  public void testCopyOf_shortcut_immutableList() {
    Collection<String> c = ImmutableList.of("a", "b", "c");
    assertSame(c, ImmutableList.copyOf(c));
  }

  public void testBuilderAddArrayHandlesNulls() {
    String[] elements = {"a", null, "b"};
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    try {
      builder.add(elements);
      fail("Expected NullPointerException");
    } catch (NullPointerException expected) {
    }
    ImmutableList<String> result = builder.build();

    /*
     * Maybe it rejects all elements, or maybe it adds "a" before failing.
     * Either way is fine with us.
     */
    if (result.isEmpty()) {
      return;
    }
    assertTrue(ImmutableList.of("a").equals(result));
    assertEquals(1, result.size());
  }

  public void testBuilderAddCollectionHandlesNulls() {
    List<String> elements = Arrays.asList("a", null, "b");
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    try {
      builder.addAll(elements);
      fail("Expected NullPointerException");
    } catch (NullPointerException expected) {
    }
    ImmutableList<String> result = builder.build();
    assertEquals(ImmutableList.of("a"), result);
    assertEquals(1, result.size());
  }

  public void testSortedCopyOf_natural() {
    Collection<Integer> c = MinimalCollection.of(4, 16, 10, -1, 5);
    ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c);
    assertEquals(asList(-1, 4, 5, 10, 16), list);
  }

  public void testSortedCopyOf_natural_empty() {
    Collection<Integer> c = MinimalCollection.of();
    ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c);
    assertEquals(asList(), list);
  }

  public void testSortedCopyOf_natural_singleton() {
    Collection<Integer> c = MinimalCollection.of(100);
    ImmutableList<Integer> list = ImmutableList.sortedCopyOf(c);
    assertEquals(asList(100), list);
  }

  public void testSortedCopyOf_natural_containsNull() {
    Collection<Integer> c = MinimalCollection.of(1, 3, null, 2);
    try {
      ImmutableList.sortedCopyOf(c);
      fail("Expected NPE");
    } catch (NullPointerException expected) {
    }
  }

  public void testSortedCopyOf() {
    Collection<String> c = MinimalCollection.of("a", "b", "A", "c");
    List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c);
    assertEquals(asList("a", "A", "b", "c"), list);
  }

  public void testSortedCopyOf_empty() {
    Collection<String> c = MinimalCollection.of();
    List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c);
    assertEquals(asList(), list);
  }

  public void testSortedCopyOf_singleton() {
    Collection<String> c = MinimalCollection.of("a");
    List<String> list = ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c);
    assertEquals(asList("a"), list);
  }

  public void testSortedCopyOf_containsNull() {
    Collection<String> c = MinimalCollection.of("a", "b", "A", null, "c");
    try {
      ImmutableList.sortedCopyOf(String.CASE_INSENSITIVE_ORDER, c);
      fail("Expected NPE");
    } catch (NullPointerException expected) {
    }
  }

  public void testToImmutableList() {
    CollectorTester.of(ImmutableList.<String>toImmutableList())
        .expectCollects(ImmutableList.of("a", "b", "c", "d"), "a", "b", "c", "d");
  }

  // Basic tests

  @GwtIncompatible // NullPointerTester
  @AndroidIncompatible // see ImmutableTableTest.testNullPointerInstance
  public void testNullPointers() {
    NullPointerTester tester = new NullPointerTester();
    tester.testAllPublicStaticMethods(ImmutableList.class);
    tester.testAllPublicInstanceMethods(ImmutableList.of(1, 2, 3));
  }

  @GwtIncompatible // SerializableTester
  public void testSerialization_empty() {
    Collection<String> c = ImmutableList.of();
    assertSame(c, SerializableTester.reserialize(c));
  }

  @GwtIncompatible // SerializableTester
  public void testSerialization_singleton() {
    Collection<String> c = ImmutableList.of("a");
    SerializableTester.reserializeAndAssert(c);
  }

  @GwtIncompatible // SerializableTester
  public void testSerialization_multiple() {
    Collection<String> c = ImmutableList.of("a", "b", "c");
    SerializableTester.reserializeAndAssert(c);
  }

  public void testEquals_immutableList() {
    Collection<String> c = ImmutableList.of("a", "b", "c");
    assertTrue(c.equals(ImmutableList.of("a", "b", "c")));
    assertFalse(c.equals(ImmutableList.of("a", "c", "b")));
    assertFalse(c.equals(ImmutableList.of("a", "b")));
    assertFalse(c.equals(ImmutableList.of("a", "b", "c", "d")));
  }

  public void testBuilderAdd() {
    ImmutableList<String> list =
        new ImmutableList.Builder<String>().add("a").add("b").add("a").add("c").build();
    assertEquals(asList("a", "b", "a", "c"), list);
  }

  @GwtIncompatible("Builder impl")
  public void testBuilderForceCopy() {
    ImmutableList.Builder<Integer> builder = ImmutableList.builder();
    Object[] prevArray = null;
    for (int i = 0; i < 10; i++) {
      builder.add(i);
      assertNotSame(builder.contents, prevArray);
      prevArray = builder.contents;
      ImmutableList<Integer> unused = builder.build();
    }
  }

  @GwtIncompatible
  public void testBuilderExactlySizedReusesArray() {
    ImmutableList.Builder<Integer> builder = ImmutableList.builderWithExpectedSize(10);
    Object[] builderArray = builder.contents;
    for (int i = 0; i < 10; i++) {
      builder.add(i);
    }
    Object[] builderArrayAfterAdds = builder.contents;
    RegularImmutableList<Integer> list = (RegularImmutableList<Integer>) builder.build();
    Object[] listInternalArray = list.array;
    assertSame(builderArray, builderArrayAfterAdds);
    assertSame(builderArray, listInternalArray);
  }

  public void testBuilderAdd_varargs() {
    ImmutableList<String> list =
        new ImmutableList.Builder<String>().add("a", "b", "a", "c").build();
    assertEquals(asList("a", "b", "a", "c"), list);
  }

  public void testBuilderAddAll_iterable() {
    List<String> a = asList("a", "b");
    List<String> b = asList("c", "d");
    ImmutableList<String> list = new ImmutableList.Builder<String>().addAll(a).addAll(b).build();
    assertEquals(asList("a", "b", "c", "d"), list);
    b.set(0, "f");
    assertEquals(asList("a", "b", "c", "d"), list);
  }

  public void testBuilderAddAll_iterator() {
    List<String> a = asList("a", "b");
    List<String> b = asList("c", "d");
    ImmutableList<String> list =
        new ImmutableList.Builder<String>().addAll(a.iterator()).addAll(b.iterator()).build();
    assertEquals(asList("a", "b", "c", "d"), list);
    b.set(0, "f");
    assertEquals(asList("a", "b", "c", "d"), list);
  }

  public void testComplexBuilder() {
    List<Integer> colorElem = asList(0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF);
    ImmutableList.Builder<Integer> webSafeColorsBuilder = ImmutableList.builder();
    for (Integer red : colorElem) {
      for (Integer green : colorElem) {
        for (Integer blue : colorElem) {
          webSafeColorsBuilder.add((red << 16) + (green << 8) + blue);
        }
      }
    }
    ImmutableList<Integer> webSafeColors = webSafeColorsBuilder.build();
    assertEquals(216, webSafeColors.size());
    Integer[] webSafeColorArray = webSafeColors.toArray(new Integer[webSafeColors.size()]);
    assertEquals(0x000000, (int) webSafeColorArray[0]);
    assertEquals(0x000033, (int) webSafeColorArray[1]);
    assertEquals(0x000066, (int) webSafeColorArray[2]);
    assertEquals(0x003300, (int) webSafeColorArray[6]);
    assertEquals(0x330000, (int) webSafeColorArray[36]);
    assertEquals(0x000066, (int) webSafeColors.get(2));
    assertEquals(0x003300, (int) webSafeColors.get(6));
    ImmutableList<Integer> addedColor = webSafeColorsBuilder.add(0x00BFFF).build();
    assertEquals(
        "Modifying the builder should not have changed any already" + " built sets",
        216,
        webSafeColors.size());
    assertEquals("the new array should be one bigger than webSafeColors", 217, addedColor.size());
    Integer[] appendColorArray = addedColor.toArray(new Integer[addedColor.size()]);
    assertEquals(0x00BFFF, (int) appendColorArray[216]);
  }

  public void testBuilderAddHandlesNullsCorrectly() {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    try {
      builder.add((String) null);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    try {
      builder.add((String[]) null);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    try {
      builder.add("a", null, "b");
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }
  }

  public void testBuilderAddAllHandlesNullsCorrectly() {
    ImmutableList.Builder<String> builder = ImmutableList.builder();
    try {
      builder.addAll((Iterable<String>) null);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    try {
      builder.addAll((Iterator<String>) null);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    builder = ImmutableList.builder();
    List<String> listWithNulls = asList("a", null, "b");
    try {
      builder.addAll(listWithNulls);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    builder = ImmutableList.builder();
    Iterator<String> iteratorWithNulls = asList("a", null, "b").iterator();
    try {
      builder.addAll(iteratorWithNulls);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }

    Iterable<String> iterableWithNulls = MinimalIterable.of("a", null, "b");
    try {
      builder.addAll(iterableWithNulls);
      fail("expected NullPointerException");
    } catch (NullPointerException expected) {
    }
  }

  public void testAsList() {
    ImmutableList<String> list = ImmutableList.of("a", "b");
    assertSame(list, list.asList());
  }
}