|
16 | 16 |
|
17 | 17 | package com.google.common.math;
|
18 | 18 |
|
| 19 | +import static com.google.common.math.Stats.toStats; |
19 | 20 | import static com.google.common.math.StatsTesting.ALLOWED_ERROR;
|
20 | 21 | import static com.google.common.math.StatsTesting.ALL_MANY_VALUES;
|
21 | 22 | import static com.google.common.math.StatsTesting.ALL_STATS;
|
|
55 | 56 | import static com.google.common.math.StatsTesting.MANY_VALUES_STATS_SNAPSHOT;
|
56 | 57 | import static com.google.common.math.StatsTesting.MANY_VALUES_STATS_VARARGS;
|
57 | 58 | import static com.google.common.math.StatsTesting.MANY_VALUES_SUM_OF_SQUARES_OF_DELTAS;
|
| 59 | +import static com.google.common.math.StatsTesting.MEGA_STREAM_COUNT; |
| 60 | +import static com.google.common.math.StatsTesting.MEGA_STREAM_MAX; |
| 61 | +import static com.google.common.math.StatsTesting.MEGA_STREAM_MEAN; |
| 62 | +import static com.google.common.math.StatsTesting.MEGA_STREAM_MIN; |
| 63 | +import static com.google.common.math.StatsTesting.MEGA_STREAM_POPULATION_VARIANCE; |
58 | 64 | import static com.google.common.math.StatsTesting.ONE_VALUE;
|
59 | 65 | import static com.google.common.math.StatsTesting.ONE_VALUE_STATS;
|
60 | 66 | import static com.google.common.math.StatsTesting.TWO_VALUES;
|
|
63 | 69 | import static com.google.common.math.StatsTesting.TWO_VALUES_MIN;
|
64 | 70 | import static com.google.common.math.StatsTesting.TWO_VALUES_STATS;
|
65 | 71 | import static com.google.common.math.StatsTesting.TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS;
|
| 72 | +import static com.google.common.math.StatsTesting.megaPrimitiveDoubleStream; |
66 | 73 | import static com.google.common.truth.Truth.assertThat;
|
67 | 74 | import static com.google.common.truth.Truth.assertWithMessage;
|
68 | 75 | import static java.lang.Double.NEGATIVE_INFINITY;
|
69 | 76 | import static java.lang.Double.NaN;
|
70 | 77 | import static java.lang.Double.POSITIVE_INFINITY;
|
71 | 78 | import static java.lang.Math.sqrt;
|
| 79 | +import static java.util.Arrays.stream; |
72 | 80 | import static org.junit.Assert.assertThrows;
|
73 | 81 |
|
74 | 82 | import com.google.common.collect.ImmutableList;
|
|
77 | 85 | import com.google.common.primitives.Longs;
|
78 | 86 | import com.google.common.testing.EqualsTester;
|
79 | 87 | import com.google.common.testing.SerializableTester;
|
| 88 | +import java.math.BigDecimal; |
80 | 89 | import java.nio.ByteBuffer;
|
81 | 90 | import java.nio.ByteOrder;
|
| 91 | +import java.util.DoubleSummaryStatistics; |
82 | 92 | import junit.framework.TestCase;
|
83 | 93 |
|
84 | 94 | /**
|
@@ -401,6 +411,39 @@ public void testMin() {
|
401 | 411 | assertThat(LONG_MANY_VALUES_STATS_SNAPSHOT.min()).isEqualTo(LONG_MANY_VALUES_MIN);
|
402 | 412 | }
|
403 | 413 |
|
| 414 | + public void testOfPrimitiveDoubleStream() { |
| 415 | + Stats stats = Stats.of(megaPrimitiveDoubleStream()); |
| 416 | + assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT); |
| 417 | + assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN); |
| 418 | + assertThat(stats.populationVariance()) |
| 419 | + .isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT) |
| 420 | + .of(MEGA_STREAM_POPULATION_VARIANCE); |
| 421 | + assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN); |
| 422 | + assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX); |
| 423 | + } |
| 424 | + |
| 425 | + public void testOfPrimitiveIntStream() { |
| 426 | + Stats stats = Stats.of(megaPrimitiveDoubleStream().mapToInt(x -> (int) x)); |
| 427 | + assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT); |
| 428 | + assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN); |
| 429 | + assertThat(stats.populationVariance()) |
| 430 | + .isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT) |
| 431 | + .of(MEGA_STREAM_POPULATION_VARIANCE); |
| 432 | + assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN); |
| 433 | + assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX); |
| 434 | + } |
| 435 | + |
| 436 | + public void testOfPrimitiveLongStream() { |
| 437 | + Stats stats = Stats.of(megaPrimitiveDoubleStream().mapToLong(x -> (long) x)); |
| 438 | + assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT); |
| 439 | + assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN); |
| 440 | + assertThat(stats.populationVariance()) |
| 441 | + .isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT) |
| 442 | + .of(MEGA_STREAM_POPULATION_VARIANCE); |
| 443 | + assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN); |
| 444 | + assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX); |
| 445 | + } |
| 446 | + |
404 | 447 | public void testEqualsAndHashCode() {
|
405 | 448 | new EqualsTester()
|
406 | 449 | .addEqualityGroup(
|
@@ -523,4 +566,62 @@ public void testFromByteArrayWithTooShortArrayInputThrowsIllegalArgumentExceptio
|
523 | 566 | .array();
|
524 | 567 | assertThrows(IllegalArgumentException.class, () -> Stats.fromByteArray(tooShortByteArray));
|
525 | 568 | }
|
| 569 | + |
| 570 | + public void testEquivalentStreams() { |
| 571 | + // For datasets of many double values created from an array, we test many combinations of finite |
| 572 | + // and non-finite values: |
| 573 | + for (ManyValues values : ALL_MANY_VALUES) { |
| 574 | + double[] array = values.asArray(); |
| 575 | + Stats stats = Stats.of(array); |
| 576 | + // instance methods on Stats vs on instance methods on DoubleStream |
| 577 | + assertThat(stats.count()).isEqualTo(stream(array).count()); |
| 578 | + assertEquivalent(stats.mean(), stream(array).average().getAsDouble()); |
| 579 | + assertEquivalent(stats.sum(), stream(array).sum()); |
| 580 | + assertEquivalent(stats.max(), stream(array).max().getAsDouble()); |
| 581 | + assertEquivalent(stats.min(), stream(array).min().getAsDouble()); |
| 582 | + // static method on Stats vs on instance method on DoubleStream |
| 583 | + assertEquivalent(Stats.meanOf(array), stream(array).average().getAsDouble()); |
| 584 | + // instance methods on Stats vs instance methods on DoubleSummaryStatistics |
| 585 | + DoubleSummaryStatistics streamStats = stream(array).summaryStatistics(); |
| 586 | + assertThat(stats.count()).isEqualTo(streamStats.getCount()); |
| 587 | + assertEquivalent(stats.mean(), streamStats.getAverage()); |
| 588 | + assertEquivalent(stats.sum(), streamStats.getSum()); |
| 589 | + assertEquivalent(stats.max(), streamStats.getMax()); |
| 590 | + assertEquivalent(stats.min(), streamStats.getMin()); |
| 591 | + } |
| 592 | + } |
| 593 | + |
| 594 | + private static void assertEquivalent(double actual, double expected) { |
| 595 | + if (expected == POSITIVE_INFINITY) { |
| 596 | + assertThat(actual).isPositiveInfinity(); |
| 597 | + } else if (expected == NEGATIVE_INFINITY) { |
| 598 | + assertThat(actual).isNegativeInfinity(); |
| 599 | + } else if (Double.isNaN(expected)) { |
| 600 | + assertThat(actual).isNaN(); |
| 601 | + } else { |
| 602 | + assertThat(actual).isWithin(ALLOWED_ERROR).of(expected); |
| 603 | + } |
| 604 | + } |
| 605 | + |
| 606 | + public void testBoxedDoubleStreamToStats() { |
| 607 | + Stats stats = megaPrimitiveDoubleStream().boxed().collect(toStats()); |
| 608 | + assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT); |
| 609 | + assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN); |
| 610 | + assertThat(stats.populationVariance()) |
| 611 | + .isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT) |
| 612 | + .of(MEGA_STREAM_POPULATION_VARIANCE); |
| 613 | + assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN); |
| 614 | + assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX); |
| 615 | + } |
| 616 | + |
| 617 | + public void testBoxedBigDecimalStreamToStats() { |
| 618 | + Stats stats = megaPrimitiveDoubleStream().mapToObj(BigDecimal::valueOf).collect(toStats()); |
| 619 | + assertThat(stats.count()).isEqualTo(MEGA_STREAM_COUNT); |
| 620 | + assertThat(stats.mean()).isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT).of(MEGA_STREAM_MEAN); |
| 621 | + assertThat(stats.populationVariance()) |
| 622 | + .isWithin(ALLOWED_ERROR * MEGA_STREAM_COUNT) |
| 623 | + .of(MEGA_STREAM_POPULATION_VARIANCE); |
| 624 | + assertThat(stats.min()).isEqualTo(MEGA_STREAM_MIN); |
| 625 | + assertThat(stats.max()).isEqualTo(MEGA_STREAM_MAX); |
| 626 | + } |
526 | 627 | }
|
0 commit comments