|
18 | 18 | import static com.google.common.primitives.TestPlatform.reduceIterationsIfGwt;
|
19 | 19 | import static com.google.common.testing.SerializableTester.reserialize;
|
20 | 20 | import static com.google.common.truth.Truth.assertThat;
|
| 21 | +import static java.util.Arrays.stream; |
21 | 22 |
|
22 | 23 | import com.google.common.annotations.GwtCompatible;
|
23 | 24 | import com.google.common.annotations.GwtIncompatible;
|
|
38 | 39 | import java.util.List;
|
39 | 40 | import java.util.Random;
|
40 | 41 | import java.util.concurrent.atomic.AtomicInteger;
|
| 42 | +import java.util.stream.DoubleStream; |
41 | 43 | import junit.framework.Test;
|
42 | 44 | import junit.framework.TestCase;
|
43 | 45 | import junit.framework.TestSuite;
|
|
46 | 48 | * @author Kevin Bourrillion
|
47 | 49 | */
|
48 | 50 | @GwtCompatible(emulated = true)
|
49 |
| -@ElementTypesAreNonnullByDefault |
50 | 51 | public class ImmutableDoubleArrayTest extends TestCase {
|
51 | 52 | // Test all creation paths very lazily: by assuming asList() works
|
52 | 53 |
|
@@ -142,6 +143,14 @@ public void testCopyOf_collection_nonempty() {
|
142 | 143 | assertThat(iia.asList()).containsExactly(0.0, 1.0, 3.0).inOrder();
|
143 | 144 | }
|
144 | 145 |
|
| 146 | + public void testCopyOf_stream() { |
| 147 | + assertThat(ImmutableDoubleArray.copyOf(DoubleStream.empty())) |
| 148 | + .isSameInstanceAs(ImmutableDoubleArray.of()); |
| 149 | + assertThat(ImmutableDoubleArray.copyOf(DoubleStream.of(0, 1, 3)).asList()) |
| 150 | + .containsExactly(0.0, 1.0, 3.0) |
| 151 | + .inOrder(); |
| 152 | + } |
| 153 | + |
145 | 154 | public void testBuilder_presize_zero() {
|
146 | 155 | ImmutableDoubleArray.Builder builder = ImmutableDoubleArray.builder(0);
|
147 | 156 | builder.add(5.0);
|
@@ -211,6 +220,16 @@ void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
|
211 | 220 | builder.addAll(iterable(list));
|
212 | 221 | }
|
213 | 222 | },
|
| 223 | + ADD_STREAM { |
| 224 | + @Override |
| 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(stream(array)); |
| 231 | + } |
| 232 | + }, |
214 | 233 | ADD_IIA {
|
215 | 234 | @Override
|
216 | 235 | void doIt(ImmutableDoubleArray.Builder builder, AtomicInteger counter) {
|
@@ -316,6 +335,23 @@ public void testContains() {
|
316 | 335 | assertThat(iia.subArray(1, 5).contains(1)).isTrue();
|
317 | 336 | }
|
318 | 337 |
|
| 338 | + public void testForEach() { |
| 339 | + ImmutableDoubleArray.of().forEach(i -> fail()); |
| 340 | + ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).forEach(i -> fail()); |
| 341 | + |
| 342 | + AtomicInteger count = new AtomicInteger(0); |
| 343 | + ImmutableDoubleArray.of(0, 1, 2, 3) |
| 344 | + .forEach(i -> assertThat(i).isEqualTo((double) count.getAndIncrement())); |
| 345 | + assertThat(count.get()).isEqualTo(4); |
| 346 | + } |
| 347 | + |
| 348 | + public void testStream() { |
| 349 | + ImmutableDoubleArray.of().stream().forEach(i -> fail()); |
| 350 | + ImmutableDoubleArray.of(0, 1, 3).subArray(1, 1).stream().forEach(i -> fail()); |
| 351 | + assertThat(ImmutableDoubleArray.of(0, 1, 3).stream().toArray()) |
| 352 | + .isEqualTo(new double[] {0, 1, 3}); |
| 353 | + } |
| 354 | + |
319 | 355 | public void testSubArray() {
|
320 | 356 | ImmutableDoubleArray iia0 = ImmutableDoubleArray.of();
|
321 | 357 | ImmutableDoubleArray iia1 = ImmutableDoubleArray.of(5);
|
|
0 commit comments