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