Skip to content

Commit f4fb824

Browse files
lowasserGoogle Java Core Libraries
authored andcommitted
Clarify {Int,Long}Math tests
RELNOTES=n/a PiperOrigin-RevId: 755465381
1 parent ed0e518 commit f4fb824

File tree

4 files changed

+15
-19
lines changed

4 files changed

+15
-19
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,9 @@ private static void assertMean(int x, int y) {
657657
private static int computeMeanSafely(int x, int y) {
658658
BigInteger bigX = BigInteger.valueOf(x);
659659
BigInteger bigY = BigInteger.valueOf(y);
660-
BigDecimal bigMean =
661-
new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
662-
// parseInt blows up on overflow as opposed to intValue() which does not.
663-
return Integer.parseInt(bigMean.toString());
660+
BigDecimal two = BigDecimal.valueOf(2); // .TWO constant is absent in j2cl
661+
BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR);
662+
return bigMean.intValueExact();
664663
}
665664

666665
private static boolean fitsInInt(BigInteger big) {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public void testConstantsHalfPowersOf10() {
131131
}
132132
BigInteger nextBigger =
133133
BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR);
134-
assertTrue(nextBigger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0);
134+
assertThat(nextBigger).isGreaterThan(BigInteger.valueOf(Long.MAX_VALUE));
135135
}
136136

137137
@GwtIncompatible // TODO
@@ -696,7 +696,7 @@ public void testBinomialNegative() {
696696
public void testSqrtOfPerfectSquareAsDoubleIsPerfect() {
697697
// This takes just over a minute on my machine.
698698
for (long n = 0; n <= LongMath.FLOOR_SQRT_MAX_LONG; n++) {
699-
long actual = (long) Math.sqrt(n * n);
699+
long actual = (long) Math.sqrt((double) (n * n));
700700
assertTrue(actual == n);
701701
}
702702
}
@@ -768,10 +768,9 @@ private static void assertMean(long x, long y) {
768768
private static long computeMeanSafely(long x, long y) {
769769
BigInteger bigX = BigInteger.valueOf(x);
770770
BigInteger bigY = BigInteger.valueOf(y);
771-
BigDecimal bigMean =
772-
new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
773-
// parseInt blows up on overflow as opposed to intValue() which does not.
774-
return Long.parseLong(bigMean.toString());
771+
BigDecimal two = BigDecimal.valueOf(2); // .TWO constant is absent in j2cl
772+
BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR);
773+
return bigMean.longValueExact();
775774
}
776775

777776
private static boolean fitsInLong(BigInteger big) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,10 +657,9 @@ private static void assertMean(int x, int y) {
657657
private static int computeMeanSafely(int x, int y) {
658658
BigInteger bigX = BigInteger.valueOf(x);
659659
BigInteger bigY = BigInteger.valueOf(y);
660-
BigDecimal bigMean =
661-
new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
662-
// parseInt blows up on overflow as opposed to intValue() which does not.
663-
return Integer.parseInt(bigMean.toString());
660+
BigDecimal two = BigDecimal.valueOf(2); // .TWO constant is absent in j2cl
661+
BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR);
662+
return bigMean.intValueExact();
664663
}
665664

666665
private static boolean fitsInInt(BigInteger big) {

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ public void testBinomialNegative() {
696696
public void testSqrtOfPerfectSquareAsDoubleIsPerfect() {
697697
// This takes just over a minute on my machine.
698698
for (long n = 0; n <= LongMath.FLOOR_SQRT_MAX_LONG; n++) {
699-
long actual = (long) Math.sqrt(n * n);
699+
long actual = (long) Math.sqrt((double) (n * n));
700700
assertTrue(actual == n);
701701
}
702702
}
@@ -768,10 +768,9 @@ private static void assertMean(long x, long y) {
768768
private static long computeMeanSafely(long x, long y) {
769769
BigInteger bigX = BigInteger.valueOf(x);
770770
BigInteger bigY = BigInteger.valueOf(y);
771-
BigDecimal bigMean =
772-
new BigDecimal(bigX.add(bigY)).divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
773-
// parseInt blows up on overflow as opposed to intValue() which does not.
774-
return Long.parseLong(bigMean.toString());
771+
BigDecimal two = BigDecimal.valueOf(2); // .TWO constant is absent in j2cl
772+
BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR);
773+
return bigMean.longValueExact();
775774
}
776775

777776
private static boolean fitsInLong(BigInteger big) {

0 commit comments

Comments
 (0)