• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.primitives;
16 
17 import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
18 import static com.google.common.testing.SerializableTester.reserialize;
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.annotations.J2ktIncompatible;
24 import com.google.common.collect.ImmutableList;
25 import com.google.common.collect.ObjectArrays;
26 import com.google.common.collect.testing.ListTestSuiteBuilder;
27 import com.google.common.collect.testing.SampleElements;
28 import com.google.common.collect.testing.TestListGenerator;
29 import com.google.common.collect.testing.features.CollectionFeature;
30 import com.google.common.collect.testing.features.CollectionSize;
31 import com.google.common.testing.EqualsTester;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.Iterator;
37 import java.util.List;
38 import java.util.Random;
39 import java.util.concurrent.atomic.AtomicInteger;
40 import java.util.stream.IntStream;
41 import junit.framework.Test;
42 import junit.framework.TestCase;
43 import junit.framework.TestSuite;
44 
45 /** @author Kevin Bourrillion */
46 @GwtCompatible(emulated = true)
47 public class ImmutableIntArrayTest extends TestCase {
48   // Test all creation paths very lazily: by assuming asList() works
49 
testOf0()50   public void testOf0() {
51     assertThat(ImmutableIntArray.of().asList()).isEmpty();
52   }
53 
testOf1()54   public void testOf1() {
55     assertThat(ImmutableIntArray.of(0).asList()).containsExactly(0);
56   }
57 
testOf2()58   public void testOf2() {
59     assertThat(ImmutableIntArray.of(0, 1).asList()).containsExactly(0, 1).inOrder();
60   }
61 
testOf3()62   public void testOf3() {
63     assertThat(ImmutableIntArray.of(0, 1, 3).asList()).containsExactly(0, 1, 3).inOrder();
64   }
65 
testOf4()66   public void testOf4() {
67     assertThat(ImmutableIntArray.of(0, 1, 3, 6).asList()).containsExactly(0, 1, 3, 6).inOrder();
68   }
69 
testOf5()70   public void testOf5() {
71     assertThat(ImmutableIntArray.of(0, 1, 3, 6, 10).asList())
72         .containsExactly(0, 1, 3, 6, 10)
73         .inOrder();
74   }
75 
testOf6()76   public void testOf6() {
77     assertThat(ImmutableIntArray.of(0, 1, 3, 6, 10, 15).asList())
78         .containsExactly(0, 1, 3, 6, 10, 15)
79         .inOrder();
80   }
81 
testOf7()82   public void testOf7() {
83     assertThat(ImmutableIntArray.of(0, 1, 3, 6, 10, 15, 21).asList())
84         .containsExactly(0, 1, 3, 6, 10, 15, 21)
85         .inOrder();
86   }
87 
testCopyOf_array_empty()88   public void testCopyOf_array_empty() {
89     /*
90      * We don't guarantee the same-as property, so we aren't obligated to test it. However, it's
91      * useful in testing - when two things are the same then one can't have bugs the other doesn't.
92      */
93     assertThat(ImmutableIntArray.copyOf(new int[0])).isSameInstanceAs(ImmutableIntArray.of());
94   }
95 
testCopyOf_array_nonempty()96   public void testCopyOf_array_nonempty() {
97     int[] array = new int[] {0, 1, 3};
98     ImmutableIntArray iia = ImmutableIntArray.copyOf(array);
99     array[2] = 2;
100     assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
101   }
102 
testCopyOf_iterable_notCollection_empty()103   public void testCopyOf_iterable_notCollection_empty() {
104     Iterable<Integer> iterable = iterable(Collections.<Integer>emptySet());
105     assertThat(ImmutableIntArray.copyOf(iterable)).isSameInstanceAs(ImmutableIntArray.of());
106   }
107 
testCopyOf_iterable_notCollection_nonempty()108   public void testCopyOf_iterable_notCollection_nonempty() {
109     List<Integer> list = Arrays.asList(0, 1, 3);
110     ImmutableIntArray iia = ImmutableIntArray.copyOf(iterable(list));
111     list.set(2, 2);
112     assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
113   }
114 
testCopyOf_iterable_collection_empty()115   public void testCopyOf_iterable_collection_empty() {
116     Iterable<Integer> iterable = Collections.emptySet();
117     assertThat(ImmutableIntArray.copyOf(iterable)).isSameInstanceAs(ImmutableIntArray.of());
118   }
119 
testCopyOf_iterable_collection_nonempty()120   public void testCopyOf_iterable_collection_nonempty() {
121     List<Integer> list = Arrays.asList(0, 1, 3);
122     ImmutableIntArray iia = ImmutableIntArray.copyOf((Iterable<Integer>) list);
123     list.set(2, 2);
124     assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
125   }
126 
testCopyOf_collection_empty()127   public void testCopyOf_collection_empty() {
128     Collection<Integer> iterable = Collections.emptySet();
129     assertThat(ImmutableIntArray.copyOf(iterable)).isSameInstanceAs(ImmutableIntArray.of());
130   }
131 
testCopyOf_collection_nonempty()132   public void testCopyOf_collection_nonempty() {
133     List<Integer> list = Arrays.asList(0, 1, 3);
134     ImmutableIntArray iia = ImmutableIntArray.copyOf(list);
135     list.set(2, 2);
136     assertThat(iia.asList()).containsExactly(0, 1, 3).inOrder();
137   }
138 
testCopyOf_stream()139   public void testCopyOf_stream() {
140     assertThat(ImmutableIntArray.copyOf(IntStream.empty()))
141         .isSameInstanceAs(ImmutableIntArray.of());
142     assertThat(ImmutableIntArray.copyOf(IntStream.of(0, 1, 3)).asList())
143         .containsExactly(0, 1, 3)
144         .inOrder();
145   }
146 
testBuilder_presize_zero()147   public void testBuilder_presize_zero() {
148     ImmutableIntArray.Builder builder = ImmutableIntArray.builder(0);
149     builder.add(5);
150     ImmutableIntArray array = builder.build();
151     assertThat(array.asList()).containsExactly(5);
152   }
153 
testBuilder_presize_negative()154   public void testBuilder_presize_negative() {
155     try {
156       ImmutableIntArray.builder(-1);
157       fail();
158     } catch (IllegalArgumentException expected) {
159     }
160   }
161 
162   /**
163    * If there's a bug in builder growth, we wouldn't know how to expose it. So, brute force the hell
164    * out of it for a while and see what happens.
165    */
testBuilder_bruteForce()166   public void testBuilder_bruteForce() {
167     for (int i = 0; i < reduceIterationsIfGwt(100); i++) {
168       ImmutableIntArray.Builder builder = ImmutableIntArray.builder(RANDOM.nextInt(20));
169       AtomicInteger counter = new AtomicInteger(0);
170       while (counter.get() < 1000) {
171         BuilderOp op = BuilderOp.randomOp();
172         op.doIt(builder, counter);
173       }
174       ImmutableIntArray iia = builder.build();
175       for (int j = 0; j < iia.length(); j++) {
176         assertThat(iia.get(j)).isEqualTo(j);
177       }
178     }
179   }
180 
181   private enum BuilderOp {
182     ADD_ONE {
183       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)184       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
185         builder.add(counter.getAndIncrement());
186       }
187     },
188     ADD_ARRAY {
189       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)190       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
191         int[] array = new int[RANDOM.nextInt(10)];
192         for (int i = 0; i < array.length; i++) {
193           array[i] = counter.getAndIncrement();
194         }
195         builder.addAll(array);
196       }
197     },
198     ADD_COLLECTION {
199       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)200       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
201         List<Integer> list = new ArrayList<>();
202         int num = RANDOM.nextInt(10);
203         for (int i = 0; i < num; i++) {
204           list.add(counter.getAndIncrement());
205         }
206         builder.addAll(list);
207       }
208     },
209     ADD_ITERABLE {
210       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)211       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
212         List<Integer> list = new ArrayList<>();
213         int num = RANDOM.nextInt(10);
214         for (int i = 0; i < num; i++) {
215           list.add(counter.getAndIncrement());
216         }
217         builder.addAll(iterable(list));
218       }
219     },
220     ADD_STREAM {
221       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)222       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
223         int[] array = new int[RANDOM.nextInt(10)];
224         for (int i = 0; i < array.length; i++) {
225           array[i] = counter.getAndIncrement();
226         }
227         builder.addAll(Arrays.stream(array));
228       }
229     },
230     ADD_IIA {
231       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)232       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
233         int[] array = new int[RANDOM.nextInt(10)];
234         for (int i = 0; i < array.length; i++) {
235           array[i] = counter.getAndIncrement();
236         }
237         builder.addAll(ImmutableIntArray.copyOf(array));
238       }
239     },
240     ADD_LARGER_ARRAY {
241       @Override
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)242       void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter) {
243         int[] array = new int[RANDOM.nextInt(200) + 200];
244         for (int i = 0; i < array.length; i++) {
245           array[i] = counter.getAndIncrement();
246         }
247         builder.addAll(array);
248       }
249     },
250     ;
251 
252     static final BuilderOp[] values = values();
253 
randomOp()254     static BuilderOp randomOp() {
255       return values[RANDOM.nextInt(values.length)];
256     }
257 
doIt(ImmutableIntArray.Builder builder, AtomicInteger counter)258     abstract void doIt(ImmutableIntArray.Builder builder, AtomicInteger counter);
259   }
260 
261   private static final Random RANDOM = new Random(42);
262 
testLength()263   public void testLength() {
264     assertThat(ImmutableIntArray.of().length()).isEqualTo(0);
265     assertThat(ImmutableIntArray.of(0).length()).isEqualTo(1);
266     assertThat(ImmutableIntArray.of(0, 1, 3).length()).isEqualTo(3);
267     assertThat(ImmutableIntArray.of(0, 1, 3).subArray(1, 1).length()).isEqualTo(0);
268     assertThat(ImmutableIntArray.of(0, 1, 3).subArray(1, 2).length()).isEqualTo(1);
269   }
270 
testIsEmpty()271   public void testIsEmpty() {
272     assertThat(ImmutableIntArray.of().isEmpty()).isTrue();
273     assertThat(ImmutableIntArray.of(0).isEmpty()).isFalse();
274     assertThat(ImmutableIntArray.of(0, 1, 3).isEmpty()).isFalse();
275     assertThat(ImmutableIntArray.of(0, 1, 3).subArray(1, 1).isEmpty()).isTrue();
276     assertThat(ImmutableIntArray.of(0, 1, 3).subArray(1, 2).isEmpty()).isFalse();
277   }
278 
testGet_good()279   public void testGet_good() {
280     ImmutableIntArray iia = ImmutableIntArray.of(0, 1, 3);
281     assertThat(iia.get(0)).isEqualTo(0);
282     assertThat(iia.get(2)).isEqualTo(3);
283     assertThat(iia.subArray(1, 3).get(1)).isEqualTo(3);
284   }
285 
testGet_bad()286   public void testGet_bad() {
287     ImmutableIntArray iia = ImmutableIntArray.of(0, 1, 3);
288     try {
289       iia.get(-1);
290       fail();
291     } catch (IndexOutOfBoundsException expected) {
292     }
293     try {
294       iia.get(3);
295       fail();
296     } catch (IndexOutOfBoundsException expected) {
297     }
298 
299     iia = iia.subArray(1, 2);
300     try {
301       iia.get(-1);
302       fail();
303     } catch (IndexOutOfBoundsException expected) {
304     }
305   }
306 
testIndexOf()307   public void testIndexOf() {
308     ImmutableIntArray iia = ImmutableIntArray.of(1, 1, 2, 3, 5, 8);
309     assertThat(iia.indexOf(1)).isEqualTo(0);
310     assertThat(iia.indexOf(8)).isEqualTo(5);
311     assertThat(iia.indexOf(4)).isEqualTo(-1);
312     assertThat(ImmutableIntArray.of(13).indexOf(13)).isEqualTo(0);
313     assertThat(ImmutableIntArray.of().indexOf(21)).isEqualTo(-1);
314     assertThat(iia.subArray(1, 5).indexOf(1)).isEqualTo(0);
315   }
316 
testLastIndexOf()317   public void testLastIndexOf() {
318     ImmutableIntArray iia = ImmutableIntArray.of(1, 1, 2, 3, 5, 8);
319     assertThat(iia.lastIndexOf(1)).isEqualTo(1);
320     assertThat(iia.lastIndexOf(8)).isEqualTo(5);
321     assertThat(iia.lastIndexOf(4)).isEqualTo(-1);
322     assertThat(ImmutableIntArray.of(13).lastIndexOf(13)).isEqualTo(0);
323     assertThat(ImmutableIntArray.of().lastIndexOf(21)).isEqualTo(-1);
324     assertThat(iia.subArray(1, 5).lastIndexOf(1)).isEqualTo(0);
325   }
326 
testContains()327   public void testContains() {
328     ImmutableIntArray iia = ImmutableIntArray.of(1, 1, 2, 3, 5, 8);
329     assertThat(iia.contains(1)).isTrue();
330     assertThat(iia.contains(8)).isTrue();
331     assertThat(iia.contains(4)).isFalse();
332     assertThat(ImmutableIntArray.of(13).contains(13)).isTrue();
333     assertThat(ImmutableIntArray.of().contains(21)).isFalse();
334     assertThat(iia.subArray(1, 5).contains(1)).isTrue();
335   }
336 
testForEach()337   public void testForEach() {
338     ImmutableIntArray.of().forEach(i -> fail());
339     ImmutableIntArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail());
340 
341     AtomicInteger count = new AtomicInteger(0);
342     ImmutableIntArray.of(0, 1, 2, 3).forEach(i -> assertThat(i).isEqualTo(count.getAndIncrement()));
343     assertThat(count.get()).isEqualTo(4);
344   }
345 
testStream()346   public void testStream() {
347     ImmutableIntArray.of().stream().forEach(i -> fail());
348     ImmutableIntArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail());
349     assertThat(ImmutableIntArray.of(0, 1, 3).stream().toArray()).isEqualTo(new int[] {0, 1, 3});
350   }
351 
testSubArray()352   public void testSubArray() {
353     ImmutableIntArray iia0 = ImmutableIntArray.of();
354     ImmutableIntArray iia1 = ImmutableIntArray.of(5);
355     ImmutableIntArray iia3 = ImmutableIntArray.of(5, 25, 125);
356 
357     assertThat(iia0.subArray(0, 0)).isSameInstanceAs(ImmutableIntArray.of());
358     assertThat(iia1.subArray(0, 0)).isSameInstanceAs(ImmutableIntArray.of());
359     assertThat(iia1.subArray(1, 1)).isSameInstanceAs(ImmutableIntArray.of());
360     assertThat(iia1.subArray(0, 1).asList()).containsExactly(5);
361     assertThat(iia3.subArray(0, 2).asList()).containsExactly(5, 25).inOrder();
362     assertThat(iia3.subArray(1, 3).asList()).containsExactly(25, 125).inOrder();
363 
364     try {
365       iia3.subArray(-1, 1);
366       fail();
367     } catch (IndexOutOfBoundsException expected) {
368     }
369     try {
370       iia3.subArray(1, 4);
371       fail();
372     } catch (IndexOutOfBoundsException expected) {
373     }
374   }
375 
376   /*
377    * Whenever an implementation uses `instanceof` on a parameter instance, the test has to know that
378    * (so much for "black box") and try instances that both do and don't pass the check. The "don't"
379    * half of that is more awkward to arrange...
380    */
iterable(final Collection<T> collection)381   private static <T> Iterable<T> iterable(final Collection<T> collection) {
382     // return collection::iterator;
383     return new Iterable<T>() {
384       @Override
385       public Iterator<T> iterator() {
386         return collection.iterator();
387       }
388     };
389   }
390 
391   public void testEquals() {
392     new EqualsTester()
393         .addEqualityGroup(ImmutableIntArray.of())
394         .addEqualityGroup(
395             ImmutableIntArray.of(1, 2),
396             reserialize(ImmutableIntArray.of(1, 2)),
397             ImmutableIntArray.of(0, 1, 2, 3).subArray(1, 3))
398         .addEqualityGroup(ImmutableIntArray.of(1, 3))
399         .addEqualityGroup(ImmutableIntArray.of(1, 2, 3))
400         .testEquals();
401   }
402 
403   /**
404    * This is probably a weird and hacky way to test what we're really trying to test, but hey, it
405    * caught a bug.
406    */
407   public void testTrimmed() {
408     ImmutableIntArray iia = ImmutableIntArray.of(0, 1, 3);
409     assertDoesntActuallyTrim(iia);
410     assertDoesntActuallyTrim(iia.subArray(0, 3));
411     assertActuallyTrims(iia.subArray(0, 2));
412     assertActuallyTrims(iia.subArray(1, 3));
413 
414     ImmutableIntArray rightSized = ImmutableIntArray.builder(3).add(0).add(1).add(3).build();
415     assertDoesntActuallyTrim(rightSized);
416 
417     ImmutableIntArray overSized = ImmutableIntArray.builder(3).add(0).add(1).build();
418     assertActuallyTrims(overSized);
419 
420     ImmutableIntArray underSized = ImmutableIntArray.builder(2).add(0).add(1).add(3).build();
421     assertActuallyTrims(underSized);
422   }
423 
424   @J2ktIncompatible
425   @GwtIncompatible // SerializableTester
426   public void testSerialization() {
427     assertThat(reserialize(ImmutableIntArray.of())).isSameInstanceAs(ImmutableIntArray.of());
428     assertThat(reserialize(ImmutableIntArray.of(0, 1).subArray(1, 1)))
429         .isSameInstanceAs(ImmutableIntArray.of());
430 
431     ImmutableIntArray iia = ImmutableIntArray.of(0, 1, 3, 6).subArray(1, 3);
432     ImmutableIntArray iia2 = reserialize(iia);
433     assertThat(iia2).isEqualTo(iia);
434     assertDoesntActuallyTrim(iia2);
435   }
436 
437   private static void assertActuallyTrims(ImmutableIntArray iia) {
438     ImmutableIntArray trimmed = iia.trimmed();
439     assertThat(trimmed).isNotSameInstanceAs(iia);
440 
441     // Yes, this is apparently how you check array equality in Truth
442     assertThat(trimmed.toArray()).isEqualTo(iia.toArray());
443   }
444 
445   private static void assertDoesntActuallyTrim(ImmutableIntArray iia) {
446     assertThat(iia.trimmed()).isSameInstanceAs(iia);
447   }
448 
449   @J2ktIncompatible
450   @GwtIncompatible // suite
451   public static Test suite() {
452     List<ListTestSuiteBuilder<Integer>> builders =
453         ImmutableList.of(
454             ListTestSuiteBuilder.using(new ImmutableIntArrayAsListGenerator())
455                 .named("ImmutableIntArray.asList"),
456             ListTestSuiteBuilder.using(new ImmutableIntArrayHeadSubListAsListGenerator())
457                 .named("ImmutableIntArray.asList, head subList"),
458             ListTestSuiteBuilder.using(new ImmutableIntArrayTailSubListAsListGenerator())
459                 .named("ImmutableIntArray.asList, tail subList"),
460             ListTestSuiteBuilder.using(new ImmutableIntArrayMiddleSubListAsListGenerator())
461                 .named("ImmutableIntArray.asList, middle subList"));
462 
463     TestSuite suite = new TestSuite();
464     for (ListTestSuiteBuilder<Integer> builder : builders) {
465       suite.addTest(
466           builder
467               .withFeatures(
468                   CollectionSize.ZERO,
469                   CollectionSize.ONE,
470                   CollectionSize.SEVERAL,
471                   CollectionFeature.ALLOWS_NULL_QUERIES,
472                   CollectionFeature.RESTRICTS_ELEMENTS,
473                   CollectionFeature.KNOWN_ORDER,
474                   CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)
475               .createTestSuite());
476     }
477     suite.addTestSuite(ImmutableIntArrayTest.class);
478     return suite;
479   }
480 
481   @J2ktIncompatible
482   @GwtIncompatible // used only from suite
483   private static ImmutableIntArray makeArray(Integer[] values) {
484     return ImmutableIntArray.copyOf(Arrays.asList(values));
485   }
486 
487   // Test generators.  To let the GWT test suite generator access them, they need to be public named
488   // classes with a public default constructor (not that we run these suites under GWT yet).
489 
490   @J2ktIncompatible
491   @GwtIncompatible // used only from suite
492   public static final class ImmutableIntArrayAsListGenerator extends TestIntegerListGenerator {
493     @Override
494     protected List<Integer> create(Integer[] elements) {
495       return makeArray(elements).asList();
496     }
497   }
498 
499   @J2ktIncompatible
500   @GwtIncompatible // used only from suite
501   public static final class ImmutableIntArrayHeadSubListAsListGenerator
502       extends TestIntegerListGenerator {
503     @Override
504     protected List<Integer> create(Integer[] elements) {
505       Integer[] suffix = {Integer.MIN_VALUE, Integer.MAX_VALUE};
506       Integer[] all = concat(elements, suffix);
507       return makeArray(all).subArray(0, elements.length).asList();
508     }
509   }
510 
511   @J2ktIncompatible
512   @GwtIncompatible // used only from suite
513   public static final class ImmutableIntArrayTailSubListAsListGenerator
514       extends TestIntegerListGenerator {
515     @Override
516     protected List<Integer> create(Integer[] elements) {
517       Integer[] prefix = {86, 99};
518       Integer[] all = concat(prefix, elements);
519       return makeArray(all).subArray(2, elements.length + 2).asList();
520     }
521   }
522 
523   @J2ktIncompatible
524   @GwtIncompatible // used only from suite
525   public static final class ImmutableIntArrayMiddleSubListAsListGenerator
526       extends TestIntegerListGenerator {
527     @Override
528     protected List<Integer> create(Integer[] elements) {
529       Integer[] prefix = {Integer.MIN_VALUE, Integer.MAX_VALUE};
530       Integer[] suffix = {86, 99};
531       Integer[] all = concat(concat(prefix, elements), suffix);
532       return makeArray(all).subArray(2, elements.length + 2).asList();
533     }
534   }
535 
536   @J2ktIncompatible
537   @GwtIncompatible // used only from suite
538   private static Integer[] concat(Integer[] a, Integer[] b) {
539     return ObjectArrays.concat(a, b, Integer.class);
540   }
541 
542   @J2ktIncompatible
543   @GwtIncompatible // used only from suite
544   public abstract static class TestIntegerListGenerator implements TestListGenerator<Integer> {
545     @Override
546     public SampleElements<Integer> samples() {
547       return new SampleIntegers();
548     }
549 
550     @Override
551     public List<Integer> create(Object... elements) {
552       Integer[] array = new Integer[elements.length];
553       int i = 0;
554       for (Object e : elements) {
555         array[i++] = (Integer) e;
556       }
557       return create(array);
558     }
559 
560     /**
561      * Creates a new collection containing the given elements; implement this method instead of
562      * {@link #create(Object...)}.
563      */
564     protected abstract List<Integer> create(Integer[] elements);
565 
566     @Override
567     public Integer[] createArray(int length) {
568       return new Integer[length];
569     }
570 
571     /** Returns the original element list, unchanged. */
572     @Override
573     public List<Integer> order(List<Integer> insertionOrder) {
574       return insertionOrder;
575     }
576   }
577 
578   @J2ktIncompatible
579   @GwtIncompatible // used only from suite
580   public static class SampleIntegers extends SampleElements<Integer> {
581     public SampleIntegers() {
582       super(1, 3, 6, 10, 15);
583     }
584   }
585 }
586