From 8dac907542aff4cae90209e93a810a8ca0bfff92 Mon Sep 17 00:00:00 2001 From: cpovirk Date: Wed, 3 Jul 2024 14:14:28 -0700 Subject: [PATCH] Compare for equality, not approximate equality within a tolerance of `0.0`. IIRC, the behavior of the two differs only for: - comparing NaN to itself - comparing either infinity to itself - comparing positive zero to negative zero And as best I can tell, the only one of those that has any chance of coming up in these tests is the zero case. And, as best I can tell, in those cases, we actively want to test that we're returning positive zero, not negative zero. So let's test for that, and let's simplify the code in doing so. (followup to cl/649135877; "followup" to cl/108355695, which ported these assertions from JUnit, known for steering users away from exact-equality comparisons) RELNOTES=n/a PiperOrigin-RevId: 649195688 --- .../common/math/PairedStatsAccumulatorTest.java | 6 ++---- .../com/google/common/math/PairedStatsTest.java | 2 +- .../google/common/math/StatsAccumulatorTest.java | 16 +++++++--------- .../test/com/google/common/math/StatsTest.java | 4 ++-- .../com/google/common/math/StatsTesting.java | 2 +- .../common/primitives/UnsignedLongTest.java | 6 ++---- .../common/math/PairedStatsAccumulatorTest.java | 6 ++---- .../com/google/common/math/PairedStatsTest.java | 2 +- .../google/common/math/StatsAccumulatorTest.java | 16 +++++++--------- .../test/com/google/common/math/StatsTest.java | 4 ++-- .../com/google/common/math/StatsTesting.java | 2 +- .../common/primitives/UnsignedLongTest.java | 6 ++---- 12 files changed, 30 insertions(+), 42 deletions(-) diff --git a/android/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java b/android/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java index 1f3d68ff6ee9..3f0d28e9a4da 100644 --- a/android/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java +++ b/android/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java @@ -179,10 +179,8 @@ public void testPopulationCovariance() { assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyPairedStats.populationCovariance()); - assertThat(oneValueAccumulator.populationCovariance()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()) - .isWithin(0.0) - .of(0.0); + assertThat(oneValueAccumulator.populationCovariance()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationCovariance()) .isWithin(ALLOWED_ERROR) .of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2); diff --git a/android/guava-tests/test/com/google/common/math/PairedStatsTest.java b/android/guava-tests/test/com/google/common/math/PairedStatsTest.java index 3219bb5f981c..95ca1241b772 100644 --- a/android/guava-tests/test/com/google/common/math/PairedStatsTest.java +++ b/android/guava-tests/test/com/google/common/math/PairedStatsTest.java @@ -89,7 +89,7 @@ public void testYStats() { public void testPopulationCovariance() { assertThrows(IllegalStateException.class, () -> EMPTY_PAIRED_STATS.populationCovariance()); - assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isEqualTo(0.0); assertThat(createSingleStats(Double.POSITIVE_INFINITY, 1.23).populationCovariance()).isNaN(); assertThat(createSingleStats(Double.NEGATIVE_INFINITY, 1.23).populationCovariance()).isNaN(); assertThat(createSingleStats(Double.NaN, 1.23).populationCovariance()).isNaN(); diff --git a/android/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java b/android/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java index bf4396f9a675..88319e75bcfb 100644 --- a/android/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java +++ b/android/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java @@ -269,9 +269,9 @@ public void testMean() { } public void testSum() { - assertThat(emptyAccumulator.sum()).isWithin(0.0).of(0.0); - assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isWithin(0.0).of(0.0); - assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isWithin(0.0).of(0.0); + assertThat(emptyAccumulator.sum()).isEqualTo(0.0); + assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isEqualTo(0.0); + assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isEqualTo(0.0); assertThat(oneValueAccumulator.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(oneValueAccumulatorByAddAllEmptyStats.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(twoValuesAccumulator.sum()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN * 2); @@ -317,8 +317,8 @@ public void testPopulationVariance() { () -> emptyAccumulatorByAddAllEmptyIterable.populationVariance()); assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationVariance()); - assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0); + assertThat(oneValueAccumulator.populationVariance()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationVariance()) .isWithin(ALLOWED_ERROR) .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2); @@ -392,10 +392,8 @@ public void testPopulationStandardDeviation() { assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationStandardDeviation()); - assertThat(oneValueAccumulator.populationStandardDeviation()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()) - .isWithin(0.0) - .of(0.0); + assertThat(oneValueAccumulator.populationStandardDeviation()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationStandardDeviation()) .isWithin(ALLOWED_ERROR) .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); diff --git a/android/guava-tests/test/com/google/common/math/StatsTest.java b/android/guava-tests/test/com/google/common/math/StatsTest.java index 9fe9a96b1120..e02da5abe3f2 100644 --- a/android/guava-tests/test/com/google/common/math/StatsTest.java +++ b/android/guava-tests/test/com/google/common/math/StatsTest.java @@ -191,7 +191,7 @@ public void testSum() { public void testPopulationVariance() { assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationVariance()); assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationVariance()); - assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_STATS.populationVariance()).isEqualTo(0.0); assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN(); assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN(); assertThat(Stats.of(NaN).populationVariance()).isNaN(); @@ -245,7 +245,7 @@ public void testPopulationStandardDeviation() { IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationStandardDeviation()); assertThrows( IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationStandardDeviation()); - assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isEqualTo(0.0); assertThat(TWO_VALUES_STATS.populationStandardDeviation()) .isWithin(ALLOWED_ERROR) .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); diff --git a/android/guava-tests/test/com/google/common/math/StatsTesting.java b/android/guava-tests/test/com/google/common/math/StatsTesting.java index 1dc0235f9fbd..6b2d9f110f19 100644 --- a/android/guava-tests/test/com/google/common/math/StatsTesting.java +++ b/android/guava-tests/test/com/google/common/math/StatsTesting.java @@ -354,7 +354,7 @@ static void assertStatsApproxEqual(Stats expectedStats, Stats actualStats) { } } else if (expectedStats.count() == 1) { assertThat(actualStats.mean()).isWithin(ALLOWED_ERROR).of(expectedStats.mean()); - assertThat(actualStats.populationVariance()).isWithin(0.0).of(0.0); + assertThat(actualStats.populationVariance()).isEqualTo(0.0); assertThat(actualStats.min()).isWithin(ALLOWED_ERROR).of(expectedStats.min()); assertThat(actualStats.max()).isWithin(ALLOWED_ERROR).of(expectedStats.max()); } else { diff --git a/android/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java b/android/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java index 1918cc032529..5b5af807c11c 100644 --- a/android/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java +++ b/android/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java @@ -151,8 +151,7 @@ public void testFloatValue() { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertWithMessage("Float value of " + unsignedValue) .that(unsignedValue.floatValue()) - .isWithin(0.0f) - .of(unsignedValue.bigIntegerValue().floatValue()); + .isEqualTo(unsignedValue.bigIntegerValue().floatValue()); } } @@ -161,8 +160,7 @@ public void testDoubleValue() { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertWithMessage("Double value of " + unsignedValue) .that(unsignedValue.doubleValue()) - .isWithin(0.0) - .of(unsignedValue.bigIntegerValue().doubleValue()); + .isEqualTo(unsignedValue.bigIntegerValue().doubleValue()); } } diff --git a/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java b/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java index 1f3d68ff6ee9..3f0d28e9a4da 100644 --- a/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java +++ b/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java @@ -179,10 +179,8 @@ public void testPopulationCovariance() { assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyPairedStats.populationCovariance()); - assertThat(oneValueAccumulator.populationCovariance()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()) - .isWithin(0.0) - .of(0.0); + assertThat(oneValueAccumulator.populationCovariance()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationCovariance()) .isWithin(ALLOWED_ERROR) .of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2); diff --git a/guava-tests/test/com/google/common/math/PairedStatsTest.java b/guava-tests/test/com/google/common/math/PairedStatsTest.java index 3219bb5f981c..95ca1241b772 100644 --- a/guava-tests/test/com/google/common/math/PairedStatsTest.java +++ b/guava-tests/test/com/google/common/math/PairedStatsTest.java @@ -89,7 +89,7 @@ public void testYStats() { public void testPopulationCovariance() { assertThrows(IllegalStateException.class, () -> EMPTY_PAIRED_STATS.populationCovariance()); - assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isEqualTo(0.0); assertThat(createSingleStats(Double.POSITIVE_INFINITY, 1.23).populationCovariance()).isNaN(); assertThat(createSingleStats(Double.NEGATIVE_INFINITY, 1.23).populationCovariance()).isNaN(); assertThat(createSingleStats(Double.NaN, 1.23).populationCovariance()).isNaN(); diff --git a/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java b/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java index c34c5b2a9c9d..372b5b7a1272 100644 --- a/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java +++ b/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java @@ -276,9 +276,9 @@ public void testMean() { } public void testSum() { - assertThat(emptyAccumulator.sum()).isWithin(0.0).of(0.0); - assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isWithin(0.0).of(0.0); - assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isWithin(0.0).of(0.0); + assertThat(emptyAccumulator.sum()).isEqualTo(0.0); + assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isEqualTo(0.0); + assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isEqualTo(0.0); assertThat(oneValueAccumulator.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(oneValueAccumulatorByAddAllEmptyStats.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE); assertThat(twoValuesAccumulator.sum()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN * 2); @@ -324,8 +324,8 @@ public void testPopulationVariance() { () -> emptyAccumulatorByAddAllEmptyIterable.populationVariance()); assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationVariance()); - assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0); + assertThat(oneValueAccumulator.populationVariance()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationVariance()) .isWithin(ALLOWED_ERROR) .of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2); @@ -399,10 +399,8 @@ public void testPopulationStandardDeviation() { assertThrows( IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationStandardDeviation()); - assertThat(oneValueAccumulator.populationStandardDeviation()).isWithin(0.0).of(0.0); - assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()) - .isWithin(0.0) - .of(0.0); + assertThat(oneValueAccumulator.populationStandardDeviation()).isEqualTo(0.0); + assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()).isEqualTo(0.0); assertThat(twoValuesAccumulator.populationStandardDeviation()) .isWithin(ALLOWED_ERROR) .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); diff --git a/guava-tests/test/com/google/common/math/StatsTest.java b/guava-tests/test/com/google/common/math/StatsTest.java index 46640e8a715f..da66304d1ab9 100644 --- a/guava-tests/test/com/google/common/math/StatsTest.java +++ b/guava-tests/test/com/google/common/math/StatsTest.java @@ -199,7 +199,7 @@ public void testSum() { public void testPopulationVariance() { assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationVariance()); assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationVariance()); - assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_STATS.populationVariance()).isEqualTo(0.0); assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN(); assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN(); assertThat(Stats.of(NaN).populationVariance()).isNaN(); @@ -253,7 +253,7 @@ public void testPopulationStandardDeviation() { IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationStandardDeviation()); assertThrows( IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationStandardDeviation()); - assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isWithin(0.0).of(0.0); + assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isEqualTo(0.0); assertThat(TWO_VALUES_STATS.populationStandardDeviation()) .isWithin(ALLOWED_ERROR) .of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2)); diff --git a/guava-tests/test/com/google/common/math/StatsTesting.java b/guava-tests/test/com/google/common/math/StatsTesting.java index 70373d469860..e4a6cf4c5561 100644 --- a/guava-tests/test/com/google/common/math/StatsTesting.java +++ b/guava-tests/test/com/google/common/math/StatsTesting.java @@ -384,7 +384,7 @@ static void assertStatsApproxEqual(Stats expectedStats, Stats actualStats) { } } else if (expectedStats.count() == 1) { assertThat(actualStats.mean()).isWithin(ALLOWED_ERROR).of(expectedStats.mean()); - assertThat(actualStats.populationVariance()).isWithin(0.0).of(0.0); + assertThat(actualStats.populationVariance()).isEqualTo(0.0); assertThat(actualStats.min()).isWithin(ALLOWED_ERROR).of(expectedStats.min()); assertThat(actualStats.max()).isWithin(ALLOWED_ERROR).of(expectedStats.max()); } else { diff --git a/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java b/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java index 1918cc032529..5b5af807c11c 100644 --- a/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java +++ b/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java @@ -151,8 +151,7 @@ public void testFloatValue() { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertWithMessage("Float value of " + unsignedValue) .that(unsignedValue.floatValue()) - .isWithin(0.0f) - .of(unsignedValue.bigIntegerValue().floatValue()); + .isEqualTo(unsignedValue.bigIntegerValue().floatValue()); } } @@ -161,8 +160,7 @@ public void testDoubleValue() { UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value); assertWithMessage("Double value of " + unsignedValue) .that(unsignedValue.doubleValue()) - .isWithin(0.0) - .of(unsignedValue.bigIntegerValue().doubleValue()); + .isEqualTo(unsignedValue.bigIntegerValue().doubleValue()); } }