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