Skip to content

Commit 8dac907

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
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
1 parent c8829bf commit 8dac907

12 files changed

+30
-42
lines changed

android/guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ public void testPopulationCovariance() {
179179
assertThrows(
180180
IllegalStateException.class,
181181
() -> emptyAccumulatorByAddAllEmptyPairedStats.populationCovariance());
182-
assertThat(oneValueAccumulator.populationCovariance()).isWithin(0.0).of(0.0);
183-
assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance())
184-
.isWithin(0.0)
185-
.of(0.0);
182+
assertThat(oneValueAccumulator.populationCovariance()).isEqualTo(0.0);
183+
assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()).isEqualTo(0.0);
186184
assertThat(twoValuesAccumulator.populationCovariance())
187185
.isWithin(ALLOWED_ERROR)
188186
.of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2);

android/guava-tests/test/com/google/common/math/PairedStatsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testYStats() {
8989

9090
public void testPopulationCovariance() {
9191
assertThrows(IllegalStateException.class, () -> EMPTY_PAIRED_STATS.populationCovariance());
92-
assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isWithin(0.0).of(0.0);
92+
assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isEqualTo(0.0);
9393
assertThat(createSingleStats(Double.POSITIVE_INFINITY, 1.23).populationCovariance()).isNaN();
9494
assertThat(createSingleStats(Double.NEGATIVE_INFINITY, 1.23).populationCovariance()).isNaN();
9595
assertThat(createSingleStats(Double.NaN, 1.23).populationCovariance()).isNaN();

android/guava-tests/test/com/google/common/math/StatsAccumulatorTest.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ public void testMean() {
269269
}
270270

271271
public void testSum() {
272-
assertThat(emptyAccumulator.sum()).isWithin(0.0).of(0.0);
273-
assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isWithin(0.0).of(0.0);
274-
assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isWithin(0.0).of(0.0);
272+
assertThat(emptyAccumulator.sum()).isEqualTo(0.0);
273+
assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isEqualTo(0.0);
274+
assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isEqualTo(0.0);
275275
assertThat(oneValueAccumulator.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
276276
assertThat(oneValueAccumulatorByAddAllEmptyStats.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
277277
assertThat(twoValuesAccumulator.sum()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN * 2);
@@ -317,8 +317,8 @@ public void testPopulationVariance() {
317317
() -> emptyAccumulatorByAddAllEmptyIterable.populationVariance());
318318
assertThrows(
319319
IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationVariance());
320-
assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0);
321-
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0);
320+
assertThat(oneValueAccumulator.populationVariance()).isEqualTo(0.0);
321+
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isEqualTo(0.0);
322322
assertThat(twoValuesAccumulator.populationVariance())
323323
.isWithin(ALLOWED_ERROR)
324324
.of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2);
@@ -392,10 +392,8 @@ public void testPopulationStandardDeviation() {
392392
assertThrows(
393393
IllegalStateException.class,
394394
() -> emptyAccumulatorByAddAllEmptyStats.populationStandardDeviation());
395-
assertThat(oneValueAccumulator.populationStandardDeviation()).isWithin(0.0).of(0.0);
396-
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation())
397-
.isWithin(0.0)
398-
.of(0.0);
395+
assertThat(oneValueAccumulator.populationStandardDeviation()).isEqualTo(0.0);
396+
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()).isEqualTo(0.0);
399397
assertThat(twoValuesAccumulator.populationStandardDeviation())
400398
.isWithin(ALLOWED_ERROR)
401399
.of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2));

android/guava-tests/test/com/google/common/math/StatsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void testSum() {
191191
public void testPopulationVariance() {
192192
assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationVariance());
193193
assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationVariance());
194-
assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0);
194+
assertThat(ONE_VALUE_STATS.populationVariance()).isEqualTo(0.0);
195195
assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN();
196196
assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN();
197197
assertThat(Stats.of(NaN).populationVariance()).isNaN();
@@ -245,7 +245,7 @@ public void testPopulationStandardDeviation() {
245245
IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationStandardDeviation());
246246
assertThrows(
247247
IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationStandardDeviation());
248-
assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isWithin(0.0).of(0.0);
248+
assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isEqualTo(0.0);
249249
assertThat(TWO_VALUES_STATS.populationStandardDeviation())
250250
.isWithin(ALLOWED_ERROR)
251251
.of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2));

android/guava-tests/test/com/google/common/math/StatsTesting.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ static void assertStatsApproxEqual(Stats expectedStats, Stats actualStats) {
354354
}
355355
} else if (expectedStats.count() == 1) {
356356
assertThat(actualStats.mean()).isWithin(ALLOWED_ERROR).of(expectedStats.mean());
357-
assertThat(actualStats.populationVariance()).isWithin(0.0).of(0.0);
357+
assertThat(actualStats.populationVariance()).isEqualTo(0.0);
358358
assertThat(actualStats.min()).isWithin(ALLOWED_ERROR).of(expectedStats.min());
359359
assertThat(actualStats.max()).isWithin(ALLOWED_ERROR).of(expectedStats.max());
360360
} else {

android/guava-tests/test/com/google/common/primitives/UnsignedLongTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ public void testFloatValue() {
151151
UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
152152
assertWithMessage("Float value of " + unsignedValue)
153153
.that(unsignedValue.floatValue())
154-
.isWithin(0.0f)
155-
.of(unsignedValue.bigIntegerValue().floatValue());
154+
.isEqualTo(unsignedValue.bigIntegerValue().floatValue());
156155
}
157156
}
158157

@@ -161,8 +160,7 @@ public void testDoubleValue() {
161160
UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
162161
assertWithMessage("Double value of " + unsignedValue)
163162
.that(unsignedValue.doubleValue())
164-
.isWithin(0.0)
165-
.of(unsignedValue.bigIntegerValue().doubleValue());
163+
.isEqualTo(unsignedValue.bigIntegerValue().doubleValue());
166164
}
167165
}
168166

guava-tests/test/com/google/common/math/PairedStatsAccumulatorTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ public void testPopulationCovariance() {
179179
assertThrows(
180180
IllegalStateException.class,
181181
() -> emptyAccumulatorByAddAllEmptyPairedStats.populationCovariance());
182-
assertThat(oneValueAccumulator.populationCovariance()).isWithin(0.0).of(0.0);
183-
assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance())
184-
.isWithin(0.0)
185-
.of(0.0);
182+
assertThat(oneValueAccumulator.populationCovariance()).isEqualTo(0.0);
183+
assertThat(oneValueAccumulatorByAddAllEmptyPairedStats.populationCovariance()).isEqualTo(0.0);
186184
assertThat(twoValuesAccumulator.populationCovariance())
187185
.isWithin(ALLOWED_ERROR)
188186
.of(TWO_VALUES_SUM_OF_PRODUCTS_OF_DELTAS / 2);

guava-tests/test/com/google/common/math/PairedStatsTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void testYStats() {
8989

9090
public void testPopulationCovariance() {
9191
assertThrows(IllegalStateException.class, () -> EMPTY_PAIRED_STATS.populationCovariance());
92-
assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isWithin(0.0).of(0.0);
92+
assertThat(ONE_VALUE_PAIRED_STATS.populationCovariance()).isEqualTo(0.0);
9393
assertThat(createSingleStats(Double.POSITIVE_INFINITY, 1.23).populationCovariance()).isNaN();
9494
assertThat(createSingleStats(Double.NEGATIVE_INFINITY, 1.23).populationCovariance()).isNaN();
9595
assertThat(createSingleStats(Double.NaN, 1.23).populationCovariance()).isNaN();

guava-tests/test/com/google/common/math/StatsAccumulatorTest.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ public void testMean() {
276276
}
277277

278278
public void testSum() {
279-
assertThat(emptyAccumulator.sum()).isWithin(0.0).of(0.0);
280-
assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isWithin(0.0).of(0.0);
281-
assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isWithin(0.0).of(0.0);
279+
assertThat(emptyAccumulator.sum()).isEqualTo(0.0);
280+
assertThat(emptyAccumulatorByAddAllEmptyIterable.sum()).isEqualTo(0.0);
281+
assertThat(emptyAccumulatorByAddAllEmptyStats.sum()).isEqualTo(0.0);
282282
assertThat(oneValueAccumulator.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
283283
assertThat(oneValueAccumulatorByAddAllEmptyStats.sum()).isWithin(ALLOWED_ERROR).of(ONE_VALUE);
284284
assertThat(twoValuesAccumulator.sum()).isWithin(ALLOWED_ERROR).of(TWO_VALUES_MEAN * 2);
@@ -324,8 +324,8 @@ public void testPopulationVariance() {
324324
() -> emptyAccumulatorByAddAllEmptyIterable.populationVariance());
325325
assertThrows(
326326
IllegalStateException.class, () -> emptyAccumulatorByAddAllEmptyStats.populationVariance());
327-
assertThat(oneValueAccumulator.populationVariance()).isWithin(0.0).of(0.0);
328-
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isWithin(0.0).of(0.0);
327+
assertThat(oneValueAccumulator.populationVariance()).isEqualTo(0.0);
328+
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationVariance()).isEqualTo(0.0);
329329
assertThat(twoValuesAccumulator.populationVariance())
330330
.isWithin(ALLOWED_ERROR)
331331
.of(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2);
@@ -399,10 +399,8 @@ public void testPopulationStandardDeviation() {
399399
assertThrows(
400400
IllegalStateException.class,
401401
() -> emptyAccumulatorByAddAllEmptyStats.populationStandardDeviation());
402-
assertThat(oneValueAccumulator.populationStandardDeviation()).isWithin(0.0).of(0.0);
403-
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation())
404-
.isWithin(0.0)
405-
.of(0.0);
402+
assertThat(oneValueAccumulator.populationStandardDeviation()).isEqualTo(0.0);
403+
assertThat(oneValueAccumulatorByAddAllEmptyStats.populationStandardDeviation()).isEqualTo(0.0);
406404
assertThat(twoValuesAccumulator.populationStandardDeviation())
407405
.isWithin(ALLOWED_ERROR)
408406
.of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2));

guava-tests/test/com/google/common/math/StatsTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public void testSum() {
199199
public void testPopulationVariance() {
200200
assertThrows(IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationVariance());
201201
assertThrows(IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationVariance());
202-
assertThat(ONE_VALUE_STATS.populationVariance()).isWithin(0.0).of(0.0);
202+
assertThat(ONE_VALUE_STATS.populationVariance()).isEqualTo(0.0);
203203
assertThat(Stats.of(POSITIVE_INFINITY).populationVariance()).isNaN();
204204
assertThat(Stats.of(NEGATIVE_INFINITY).populationVariance()).isNaN();
205205
assertThat(Stats.of(NaN).populationVariance()).isNaN();
@@ -253,7 +253,7 @@ public void testPopulationStandardDeviation() {
253253
IllegalStateException.class, () -> EMPTY_STATS_VARARGS.populationStandardDeviation());
254254
assertThrows(
255255
IllegalStateException.class, () -> EMPTY_STATS_ITERABLE.populationStandardDeviation());
256-
assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isWithin(0.0).of(0.0);
256+
assertThat(ONE_VALUE_STATS.populationStandardDeviation()).isEqualTo(0.0);
257257
assertThat(TWO_VALUES_STATS.populationStandardDeviation())
258258
.isWithin(ALLOWED_ERROR)
259259
.of(sqrt(TWO_VALUES_SUM_OF_SQUARES_OF_DELTAS / 2));

guava-tests/test/com/google/common/math/StatsTesting.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ static void assertStatsApproxEqual(Stats expectedStats, Stats actualStats) {
384384
}
385385
} else if (expectedStats.count() == 1) {
386386
assertThat(actualStats.mean()).isWithin(ALLOWED_ERROR).of(expectedStats.mean());
387-
assertThat(actualStats.populationVariance()).isWithin(0.0).of(0.0);
387+
assertThat(actualStats.populationVariance()).isEqualTo(0.0);
388388
assertThat(actualStats.min()).isWithin(ALLOWED_ERROR).of(expectedStats.min());
389389
assertThat(actualStats.max()).isWithin(ALLOWED_ERROR).of(expectedStats.max());
390390
} else {

guava-tests/test/com/google/common/primitives/UnsignedLongTest.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ public void testFloatValue() {
151151
UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
152152
assertWithMessage("Float value of " + unsignedValue)
153153
.that(unsignedValue.floatValue())
154-
.isWithin(0.0f)
155-
.of(unsignedValue.bigIntegerValue().floatValue());
154+
.isEqualTo(unsignedValue.bigIntegerValue().floatValue());
156155
}
157156
}
158157

@@ -161,8 +160,7 @@ public void testDoubleValue() {
161160
UnsignedLong unsignedValue = UnsignedLong.fromLongBits(value);
162161
assertWithMessage("Double value of " + unsignedValue)
163162
.that(unsignedValue.doubleValue())
164-
.isWithin(0.0)
165-
.of(unsignedValue.bigIntegerValue().doubleValue());
163+
.isEqualTo(unsignedValue.bigIntegerValue().doubleValue());
166164
}
167165
}
168166

0 commit comments

Comments
 (0)