diff --git a/android/guava-tests/test/com/google/common/math/IntMathTest.java b/android/guava-tests/test/com/google/common/math/IntMathTest.java index e31f7454dd59..734ebeb73a50 100644 --- a/android/guava-tests/test/com/google/common/math/IntMathTest.java +++ b/android/guava-tests/test/com/google/common/math/IntMathTest.java @@ -28,6 +28,8 @@ import static com.google.common.math.MathTesting.NONZERO_INTEGER_CANDIDATES; import static com.google.common.math.MathTesting.POSITIVE_INTEGER_CANDIDATES; import static com.google.common.math.TestPlatform.intsCanGoOutOfRange; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static java.lang.Math.min; import static java.math.RoundingMode.DOWN; import static java.math.RoundingMode.FLOOR; @@ -55,19 +57,26 @@ @SuppressWarnings("IntMathMod") // We are testing IntMathMod against alternatives. public class IntMathTest extends TestCase { public void testMaxSignedPowerOfTwo() { - assertTrue(IntMath.isPowerOfTwo(IntMath.MAX_SIGNED_POWER_OF_TWO)); + assertThat(IntMath.isPowerOfTwo(IntMath.MAX_SIGNED_POWER_OF_TWO)).isTrue(); // Extra work required to make GWT happy. long value = IntMath.MAX_SIGNED_POWER_OF_TWO * 2L; - assertFalse(IntMath.isPowerOfTwo((int) value)); + assertThat(IntMath.isPowerOfTwo((int) value)).isFalse(); } public void testCeilingPowerOfTwo() { for (int x : POSITIVE_INTEGER_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); if (fitsInInt(expectedResult)) { - assertEquals(expectedResult.intValue(), IntMath.ceilingPowerOfTwo(x)); - } else { + assertThat(IntMath.ceilingPowerOfTwo(x)).isEqualTo(expectedResult.intValue()); + } + } + } + + public void testCeilingPowerOfTwo_overflows() { + for (int x : POSITIVE_INTEGER_CANDIDATES) { + BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); + if (!fitsInInt(expectedResult)) { assertThrows(ArithmeticException.class, () -> IntMath.ceilingPowerOfTwo(x)); } } @@ -76,7 +85,7 @@ public void testCeilingPowerOfTwo() { public void testFloorPowerOfTwo() { for (int x : POSITIVE_INTEGER_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.floorPowerOfTwo(bigInt(x)); - assertEquals(expectedResult.intValue(), IntMath.floorPowerOfTwo(x)); + assertThat(IntMath.floorPowerOfTwo(x)).isEqualTo(expectedResult.intValue()); } } @@ -100,58 +109,63 @@ public void testFloorPowerOfTwoZero() { assertThrows(IllegalArgumentException.class, () -> IntMath.floorPowerOfTwo(0)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testConstantMaxPowerOfSqrt2Unsigned() { - assertEquals( - BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR).intValue(), - IntMath.MAX_POWER_OF_SQRT2_UNSIGNED); + assertThat(IntMath.MAX_POWER_OF_SQRT2_UNSIGNED) + .isEqualTo( + BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR).intValue()); } @GwtIncompatible // pow() public void testConstantsPowersOf10() { for (int i = 0; i < IntMath.powersOf10.length - 1; i++) { - assertEquals(IntMath.pow(10, i), IntMath.powersOf10[i]); + assertThat(IntMath.powersOf10[i]).isEqualTo(IntMath.pow(10, i)); } } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testMaxLog10ForLeadingZeros() { for (int i = 0; i < Integer.SIZE; i++) { - assertEquals( - BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Integer.SIZE - i), FLOOR), - IntMath.maxLog10ForLeadingZeros[i]); + assertThat(IntMath.maxLog10ForLeadingZeros[i]) + .isEqualTo(BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Integer.SIZE - i), FLOOR)); } } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testConstantsHalfPowersOf10() { for (int i = 0; i < IntMath.halfPowersOf10.length; i++) { - assertEquals( - IntMath.halfPowersOf10[i], - min( - Integer.MAX_VALUE, - BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR).longValue())); + assertThat( + min( + Integer.MAX_VALUE, + BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR).longValue())) + .isEqualTo(IntMath.halfPowersOf10[i]); } } public void testConstantsBiggestBinomials() { for (int k = 0; k < IntMath.biggestBinomials.length; k++) { - assertTrue(fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k], k))); - assertTrue( - IntMath.biggestBinomials[k] == Integer.MAX_VALUE - || !fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k] + 1, k))); + assertThat(fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k], k))).isTrue(); + assertThat( + IntMath.biggestBinomials[k] == Integer.MAX_VALUE + || !fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k] + 1, k))) + .isTrue(); // In the first case, any int is valid; in the second, we want to test that the next-bigger // int overflows. } - assertFalse( - fitsInInt( - BigIntegerMath.binomial( - 2 * IntMath.biggestBinomials.length, IntMath.biggestBinomials.length))); + assertThat( + fitsInInt( + BigIntegerMath.binomial( + 2 * IntMath.biggestBinomials.length, IntMath.biggestBinomials.length))) + .isFalse(); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // sqrt public void testPowersSqrtMaxInt() { - assertEquals(sqrt(Integer.MAX_VALUE, FLOOR), IntMath.FLOOR_SQRT_MAX_INT); + assertThat(IntMath.FLOOR_SQRT_MAX_INT).isEqualTo(sqrt(Integer.MAX_VALUE, FLOOR)); } @AndroidIncompatible // presumably slow @@ -159,9 +173,7 @@ public void testLessThanBranchFree() { for (int x : ALL_INTEGER_CANDIDATES) { for (int y : ALL_INTEGER_CANDIDATES) { if (LongMath.fitsInInt((long) x - y)) { - int expected = (x < y) ? 1 : 0; - int actual = IntMath.lessThanBranchFree(x, y); - assertEquals(expected, actual); + assertThat(IntMath.lessThanBranchFree(x, y)).isEqualTo(x < y ? 1 : 0); } } } @@ -171,9 +183,7 @@ public void testLessThanBranchFree() { public void testIsPowerOfTwo() { for (int x : ALL_INTEGER_CANDIDATES) { // Checks for a single bit set. - BigInteger bigX = bigInt(x); - boolean expected = (bigX.signum() > 0) && (bigX.bitCount() == 1); - assertEquals(expected, IntMath.isPowerOfTwo(x)); + assertThat(IntMath.isPowerOfTwo(x)).isEqualTo(x > 0 && bigInt(x).bitCount() == 1); } } @@ -195,7 +205,7 @@ public void testLog2NegativeAlwaysThrows() { public void testLog2MatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { - assertEquals(BigIntegerMath.log2(bigInt(x), mode), IntMath.log2(x, mode)); + assertThat(IntMath.log2(x, mode)).isEqualTo(BigIntegerMath.log2(bigInt(x), mode)); } } } @@ -203,13 +213,17 @@ public void testLog2MatchesBigInteger() { // Relies on the correctness of isPowerOfTwo(int). public void testLog2Exact() { for (int x : POSITIVE_INTEGER_CANDIDATES) { - // We only expect an exception if x was not a power of 2. - boolean isPowerOf2 = IntMath.isPowerOfTwo(x); - try { - assertEquals(x, 1 << IntMath.log2(x, UNNECESSARY)); - assertTrue(isPowerOf2); - } catch (ArithmeticException e) { - assertFalse(isPowerOf2); + if (IntMath.isPowerOfTwo(x)) { + assertThat(1 << IntMath.log2(x, UNNECESSARY)).isEqualTo(x); + } + } + } + + // Relies on the correctness of isPowerOfTwo(int). + public void testLog2Exact_notPowerOfTwo() { + for (int x : POSITIVE_INTEGER_CANDIDATES) { + if (!IntMath.isPowerOfTwo(x)) { + assertThrows(ArithmeticException.class, () -> IntMath.log2(x, UNNECESSARY)); } } } @@ -236,7 +250,7 @@ public void testLog10MatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. - assertEquals(BigIntegerMath.log10(bigInt(x), mode), IntMath.log10(x, mode)); + assertThat(IntMath.log10(x, mode)).isEqualTo(BigIntegerMath.log10(bigInt(x), mode)); } } } @@ -248,10 +262,10 @@ public void testLog10Exact() { int floor = IntMath.log10(x, FLOOR); boolean expectSuccess = IntMath.pow(10, floor) == x; try { - assertEquals(floor, IntMath.log10(x, UNNECESSARY)); - assertTrue(expectSuccess); + assertThat(IntMath.log10(x, UNNECESSARY)).isEqualTo(floor); + assertThat(expectSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectSuccess); + assertThat(expectSuccess).isFalse(); } } } @@ -260,7 +274,7 @@ public void testLog10Exact() { public void testLog10TrivialOnPowerOfTen() { int x = 1000000; for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(6, IntMath.log10(x, mode)); + assertThat(IntMath.log10(x, mode)).isEqualTo(6); } } @@ -268,7 +282,7 @@ public void testLog10TrivialOnPowerOfTen() { @GwtIncompatible // sqrt public void testSqrtZeroAlwaysZero() { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0, sqrt(0, mode)); + assertThat(sqrt(0, mode)).isEqualTo(0); } } @@ -290,7 +304,7 @@ public void testSqrtMatchesBigInteger() { // The BigInteger implementation is tested separately, use it as the reference. // Promote the int value (rather than using intValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. - assertEquals(BigIntegerMath.sqrt(bigInt(x), mode), bigInt(sqrt(x, mode))); + assertThat(bigInt(sqrt(x, mode))).isEqualTo(BigIntegerMath.sqrt(bigInt(x), mode)); } } } @@ -303,10 +317,10 @@ public void testSqrtExactMatchesFloorOrThrows() { // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = floor * floor == x; try { - assertEquals(floor, sqrt(x, UNNECESSARY)); - assertTrue(isPerfectSquare); + assertThat(sqrt(x, UNNECESSARY)).isEqualTo(floor); + assertThat(isPerfectSquare).isTrue(); } catch (ArithmeticException e) { - assertFalse(isPerfectSquare); + assertThat(isPerfectSquare).isFalse(); } } } @@ -315,7 +329,9 @@ public void testSqrtExactMatchesFloorOrThrows() { public void testPow() { for (int i : ALL_INTEGER_CANDIDATES) { for (int pow : EXPONENTS) { - assertEquals(i + "^" + pow, bigInt(i).pow(pow).intValue(), IntMath.pow(i, pow)); + assertWithMessage("%s^%s", i, pow) + .that(IntMath.pow(i, pow)) + .isEqualTo(bigInt(i).pow(pow).intValue()); } } } @@ -333,12 +349,16 @@ public void testDivNonZero() { } int expected = new BigDecimal(bigInt(p)).divide(new BigDecimal(bigInt(q)), 0, mode).intValue(); - assertEquals(p + "/" + q, force32(expected), IntMath.divide(p, q, mode)); + assertWithMessage("%s/%s", p, q) + .that(IntMath.divide(p, q, mode)) + .isEqualTo(force32(expected)); // Check the assertions we make in the javadoc. if (mode == DOWN) { - assertEquals(p + "/" + q, p / q, IntMath.divide(p, q, mode)); + assertWithMessage("%s/%s", p, q).that(IntMath.divide(p, q, mode)).isEqualTo(p / q); } else if (mode == FLOOR) { - assertEquals("⌊" + p + "/" + q + "⌋", Math.floorDiv(p, q), IntMath.divide(p, q, mode)); + assertWithMessage("⌊%s/%s⌋", p, q) + .that(IntMath.divide(p, q, mode)) + .isEqualTo(Math.floorDiv(p, q)); } } } @@ -355,10 +375,12 @@ public void testDivNonZeroExact() { } boolean dividesEvenly = (p % q) == 0; try { - assertEquals(p + "/" + q, p, IntMath.divide(p, q, UNNECESSARY) * q); - assertTrue(p + "/" + q + " not expected to divide evenly", dividesEvenly); + assertWithMessage("%s/%s", p, q).that(IntMath.divide(p, q, UNNECESSARY) * q).isEqualTo(p); + assertWithMessage("%s/%s not expected to divide evenly", p, q) + .that(dividesEvenly) + .isTrue(); } catch (ArithmeticException e) { - assertFalse(p + "/" + q + " expected to divide evenly", dividesEvenly); + assertWithMessage("%s/%s expected to divide evenly", p, q).that(dividesEvenly).isFalse(); } } } @@ -367,7 +389,7 @@ public void testDivNonZeroExact() { public void testZeroDivIsAlwaysZero() { for (int q : NONZERO_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0, IntMath.divide(0, q, mode)); + assertThat(IntMath.divide(0, q, mode)).isEqualTo(0); } } } @@ -383,7 +405,7 @@ public void testDivByZeroAlwaysFails() { public void testMod() { for (int x : ALL_INTEGER_CANDIDATES) { for (int m : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).intValue(), IntMath.mod(x, m)); + assertThat(IntMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).intValue()); } } } @@ -405,17 +427,17 @@ public void testModZeroModulusFails() { public void testGCD() { for (int a : POSITIVE_INTEGER_CANDIDATES) { for (int b : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(a).gcd(bigInt(b)), bigInt(IntMath.gcd(a, b))); + assertThat(bigInt(IntMath.gcd(a, b))).isEqualTo(bigInt(a).gcd(bigInt(b))); } } } public void testGCDZero() { for (int a : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(a, IntMath.gcd(a, 0)); - assertEquals(a, IntMath.gcd(0, a)); + assertThat(IntMath.gcd(a, 0)).isEqualTo(a); + assertThat(IntMath.gcd(0, a)).isEqualTo(a); } - assertEquals(0, IntMath.gcd(0, 0)); + assertThat(IntMath.gcd(0, 0)).isEqualTo(0); } public void testGCDNegativePositiveThrows() { @@ -441,10 +463,10 @@ public void testCheckedAdd() { BigInteger expectedResult = bigInt(a).add(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a + b, checkedAdd(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedAdd(a, b)).isEqualTo(a + b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -459,10 +481,10 @@ public void testCheckedSubtract() { BigInteger expectedResult = bigInt(a).subtract(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a - b, checkedSubtract(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedSubtract(a, b)).isEqualTo(a - b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -477,10 +499,10 @@ public void testCheckedMultiply() { BigInteger expectedResult = bigInt(a).multiply(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a * b, checkedMultiply(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedMultiply(a, b)).isEqualTo(a * b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -492,10 +514,12 @@ public void testCheckedPow() { BigInteger expectedResult = bigInt(b).pow(k); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(b + "^" + k, force32(expectedResult.intValue()), IntMath.checkedPow(b, k)); - assertTrue(b + "^" + k + " should have succeeded", expectedSuccess); + assertWithMessage("%s^%s", b, k) + .that(IntMath.checkedPow(b, k)) + .isEqualTo(force32(expectedResult.intValue())); + assertWithMessage("%s^%s should have succeeded", b, k).that(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(b + "^" + k + " should have failed", expectedSuccess); + assertWithMessage("%s^%s should have failed", b, k).that(expectedSuccess).isFalse(); } } } @@ -576,7 +600,7 @@ public void testFactorial() { for (int n = 0; n <= 50; n++) { BigInteger expectedBig = BigIntegerMath.factorial(n); int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE; - assertEquals(expectedInt, IntMath.factorial(n)); + assertThat(IntMath.factorial(n)).isEqualTo(expectedInt); } } @@ -592,7 +616,7 @@ public void testBinomial() { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE; - assertEquals(expectedInt, IntMath.binomial(n, k)); + assertThat(IntMath.binomial(n, k)).isEqualTo(expectedInt); } } } @@ -648,10 +672,9 @@ public void testMean() { /** Helper method that asserts the arithmetic mean of x and y is equal to the expectedMean. */ private static void assertMean(int expectedMean, int x, int y) { - assertEquals( - "The expectedMean should be the same as computeMeanSafely", - expectedMean, - computeMeanSafely(x, y)); + assertWithMessage("The expectedMean should be the same as computeMeanSafely") + .that(computeMeanSafely(x, y)) + .isEqualTo(expectedMean); assertMean(x, y); } @@ -661,9 +684,10 @@ private static void assertMean(int expectedMean, int x, int y) { */ private static void assertMean(int x, int y) { int expectedMean = computeMeanSafely(x, y); - assertEquals(expectedMean, IntMath.mean(x, y)); - assertEquals( - "The mean of x and y should equal the mean of y and x", expectedMean, IntMath.mean(y, x)); + assertThat(IntMath.mean(x, y)).isEqualTo(expectedMean); + assertWithMessage("The mean of x and y should equal the mean of y and x") + .that(IntMath.mean(y, x)) + .isEqualTo(expectedMean); } /** @@ -673,7 +697,8 @@ private static void assertMean(int x, int y) { private static int computeMeanSafely(int x, int y) { BigInteger bigX = bigInt(x); BigInteger bigY = bigInt(y); - BigDecimal two = BigDecimal.valueOf(2); // Android doesn't have BigDecimal.TWO yet + @SuppressWarnings("ConstantTwo") // Android doesn't have BigDecimal.TWO yet + BigDecimal two = BigDecimal.valueOf(2); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR); return bigMean.intValueExact(); } @@ -696,26 +721,26 @@ public void testIsPrime() { // Check the first 100,000 integers for (int i = 0; i < 100000; i++) { - assertEquals(LongMath.isPrime(i), IntMath.isPrime(i)); + assertThat(IntMath.isPrime(i)).isEqualTo(LongMath.isPrime(i)); } // Then check 1000 deterministic pseudo-random int values. Random rand = new Random(1); for (int i = 0; i < 1000; i++) { int n = rand.nextInt(Integer.MAX_VALUE); - assertEquals(LongMath.isPrime(n), IntMath.isPrime(n)); + assertThat(IntMath.isPrime(n)).isEqualTo(LongMath.isPrime(n)); } } public void testSaturatedAbs() { - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(Integer.MIN_VALUE)); - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(Integer.MAX_VALUE)); - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(-Integer.MAX_VALUE)); - assertEquals(0, IntMath.saturatedAbs(0)); - assertEquals(1, IntMath.saturatedAbs(1)); - assertEquals(1, IntMath.saturatedAbs(-1)); - assertEquals(10, IntMath.saturatedAbs(10)); - assertEquals(10, IntMath.saturatedAbs(-10)); + assertThat(IntMath.saturatedAbs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(Integer.MAX_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(-Integer.MAX_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(0)).isEqualTo(0); + assertThat(IntMath.saturatedAbs(1)).isEqualTo(1); + assertThat(IntMath.saturatedAbs(-1)).isEqualTo(1); + assertThat(IntMath.saturatedAbs(10)).isEqualTo(10); + assertThat(IntMath.saturatedAbs(-10)).isEqualTo(10); } private static int force32(int value) { diff --git a/android/guava-tests/test/com/google/common/math/LongMathTest.java b/android/guava-tests/test/com/google/common/math/LongMathTest.java index f6009bcc3f59..25b0ecfd9c7d 100644 --- a/android/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/android/guava-tests/test/com/google/common/math/LongMathTest.java @@ -60,16 +60,24 @@ public class LongMathTest extends TestCase { @SuppressWarnings("ConstantOverflow") public void testMaxSignedPowerOfTwo() { - assertTrue(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO)); - assertFalse(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO * 2)); + assertThat(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO)).isTrue(); + assertThat(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO * 2)).isFalse(); } public void testCeilingPowerOfTwo() { for (long x : POSITIVE_LONG_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); if (fitsInLong(expectedResult)) { - assertEquals(expectedResult.longValue(), LongMath.ceilingPowerOfTwo(x)); - } else { + assertThat(LongMath.ceilingPowerOfTwo(x)) + .isEqualTo(BigIntegerMath.ceilingPowerOfTwo(bigInt(x)).longValue()); + } + } + } + + public void testCeilingPowerOfTwo_overflows() { + for (long x : POSITIVE_LONG_CANDIDATES) { + BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); + if (!fitsInLong(expectedResult)) { assertThrows(ArithmeticException.class, () -> LongMath.ceilingPowerOfTwo(x)); } } @@ -78,7 +86,7 @@ public void testCeilingPowerOfTwo() { public void testFloorPowerOfTwo() { for (long x : POSITIVE_LONG_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.floorPowerOfTwo(bigInt(x)); - assertEquals(expectedResult.longValue(), LongMath.floorPowerOfTwo(x)); + assertThat(LongMath.floorPowerOfTwo(x)).isEqualTo(expectedResult.longValue()); } } @@ -102,26 +110,27 @@ public void testFloorPowerOfTwoZero() { assertThrows(IllegalArgumentException.class, () -> LongMath.floorPowerOfTwo(0L)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // TODO public void testConstantMaxPowerOfSqrt2Unsigned() { - assertEquals( - BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR).longValue(), - LongMath.MAX_POWER_OF_SQRT2_UNSIGNED); + assertThat(LongMath.MAX_POWER_OF_SQRT2_UNSIGNED) + .isEqualTo( + BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR).longValue()); } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testMaxLog10ForLeadingZeros() { for (int i = 0; i < Long.SIZE; i++) { - assertEquals( - BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Long.SIZE - i), FLOOR), - LongMath.maxLog10ForLeadingZeros[i]); + assertThat(LongMath.maxLog10ForLeadingZeros[i]) + .isEqualTo(BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Long.SIZE - i), FLOOR)); } } @GwtIncompatible // TODO public void testConstantsPowersOf10() { for (int i = 0; i < LongMath.powersOf10.length; i++) { - assertEquals(LongMath.checkedPow(10, i), LongMath.powersOf10[i]); + assertThat(LongMath.powersOf10[i]).isEqualTo(LongMath.checkedPow(10, i)); } assertThrows( ArithmeticException.class, () -> LongMath.checkedPow(10, LongMath.powersOf10.length)); @@ -130,25 +139,26 @@ public void testConstantsPowersOf10() { @GwtIncompatible // TODO public void testConstantsHalfPowersOf10() { for (int i = 0; i < LongMath.halfPowersOf10.length; i++) { - assertEquals( - BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR), - bigInt(LongMath.halfPowersOf10[i])); + assertThat(bigInt(LongMath.halfPowersOf10[i])) + .isEqualTo(BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR)); } BigInteger nextBigger = BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR); assertThat(nextBigger).isGreaterThan(bigInt(Long.MAX_VALUE)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // TODO public void testConstantsSqrtMaxLong() { - assertEquals(sqrt(Long.MAX_VALUE, FLOOR), LongMath.FLOOR_SQRT_MAX_LONG); + assertThat(LongMath.FLOOR_SQRT_MAX_LONG).isEqualTo(sqrt(Long.MAX_VALUE, FLOOR)); } @GwtIncompatible // TODO public void testConstantsFactorials() { long expected = 1; for (int i = 0; i < LongMath.factorials.length; i++, expected *= i) { - assertEquals(expected, LongMath.factorials[i]); + assertThat(LongMath.factorials[i]).isEqualTo(expected); } assertThrows( ArithmeticException.class, @@ -161,15 +171,16 @@ public void testConstantsFactorials() { @GwtIncompatible // TODO public void testConstantsBiggestBinomials() { for (int k = 0; k < LongMath.biggestBinomials.length; k++) { - assertTrue(fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k], k))); - assertTrue( - LongMath.biggestBinomials[k] == Integer.MAX_VALUE - || !fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k] + 1, k))); + assertThat(fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k], k))).isTrue(); + assertThat( + LongMath.biggestBinomials[k] == Integer.MAX_VALUE + || !fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k] + 1, k))) + .isTrue(); // In the first case, any long is valid; in the second, we want to test that the next-bigger // long overflows. } int k = LongMath.biggestBinomials.length; - assertFalse(fitsInLong(BigIntegerMath.binomial(2 * k, k))); + assertThat(fitsInLong(BigIntegerMath.binomial(2 * k, k))).isFalse(); // 2 * k is the smallest value for which we don't replace k with (n-k). } @@ -177,7 +188,7 @@ public void testConstantsBiggestBinomials() { public void testConstantsBiggestSimpleBinomials() { for (int i = 0; i < LongMath.biggestSimpleBinomials.length; i++) { int k = i; - assertTrue(LongMath.biggestSimpleBinomials[k] <= LongMath.biggestBinomials[k]); + assertThat(LongMath.biggestSimpleBinomials[k]).isAtMost(LongMath.biggestBinomials[k]); long unused = simpleBinomial(LongMath.biggestSimpleBinomials[k], k); // mustn't throw if (LongMath.biggestSimpleBinomials[k] < Integer.MAX_VALUE) { // unless all n are fair game with this k @@ -194,11 +205,8 @@ public void testConstantsBiggestSimpleBinomials() { public void testLessThanBranchFree() { for (long x : ALL_LONG_CANDIDATES) { for (long y : ALL_LONG_CANDIDATES) { - BigInteger difference = bigInt(x).subtract(bigInt(y)); - if (fitsInLong(difference)) { - int expected = (x < y) ? 1 : 0; - int actual = LongMath.lessThanBranchFree(x, y); - assertEquals(expected, actual); + if (fitsInLong(bigInt(x).subtract(bigInt(y)))) { + assertThat(LongMath.lessThanBranchFree(x, y)).isEqualTo(x < y ? 1 : 0); } } } @@ -219,9 +227,7 @@ private long simpleBinomial(int n, int k) { public void testIsPowerOfTwo() { for (long x : ALL_LONG_CANDIDATES) { // Checks for a single bit set. - BigInteger bigX = bigInt(x); - boolean expected = (bigX.signum() > 0) && (bigX.bitCount() == 1); - assertEquals(expected, LongMath.isPowerOfTwo(x)); + assertThat(LongMath.isPowerOfTwo(x)).isEqualTo(x > 0 && bigInt(x).bitCount() == 1); } } @@ -244,21 +250,25 @@ public void testLog2MatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. - assertEquals(BigIntegerMath.log2(bigInt(x), mode), LongMath.log2(x, mode)); + assertThat(LongMath.log2(x, mode)).isEqualTo(BigIntegerMath.log2(bigInt(x), mode)); } } } - /* Relies on the correctness of isPowerOfTwo(long). */ + // Relies on the correctness of isPowerOfTwo(long). public void testLog2Exact() { for (long x : POSITIVE_LONG_CANDIDATES) { - // We only expect an exception if x was not a power of 2. - boolean isPowerOf2 = LongMath.isPowerOfTwo(x); - try { - assertEquals(x, 1L << LongMath.log2(x, UNNECESSARY)); - assertTrue(isPowerOf2); - } catch (ArithmeticException e) { - assertFalse(isPowerOf2); + if (LongMath.isPowerOfTwo(x)) { + assertThat(1L << LongMath.log2(x, UNNECESSARY)).isEqualTo(x); + } + } + } + + // Relies on the correctness of isPowerOfTwo(long). + public void testLog2Exact_notPowerOfTwo() { + for (long x : POSITIVE_LONG_CANDIDATES) { + if (!LongMath.isPowerOfTwo(x)) { + assertThrows(ArithmeticException.class, () -> LongMath.log2(x, UNNECESSARY)); } } } @@ -284,7 +294,7 @@ public void testLog10NegativeAlwaysThrows() { public void testLog10MatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { - assertEquals(BigIntegerMath.log10(bigInt(x), mode), LongMath.log10(x, mode)); + assertThat(LongMath.log10(x, mode)).isEqualTo(BigIntegerMath.log10(bigInt(x), mode)); } } } @@ -296,8 +306,8 @@ public void testLog10Exact() { int floor = LongMath.log10(x, FLOOR); boolean expectedSuccess = LongMath.pow(10, floor) == x; try { - assertEquals(floor, LongMath.log10(x, UNNECESSARY)); - assertTrue(expectedSuccess); + assertThat(LongMath.log10(x, UNNECESSARY)).isEqualTo(floor); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat("expected log10(%s, UNNECESSARY) = %s; got ArithmeticException", x, floor); @@ -310,7 +320,7 @@ public void testLog10Exact() { public void testLog10TrivialOnPowerOf10() { long x = 1000000000000L; for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(12, LongMath.log10(x, mode)); + assertThat(LongMath.log10(x, mode)).isEqualTo(12); } } @@ -330,7 +340,7 @@ public void testSqrtMatchesBigInteger() { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // Promote the long value (rather than using longValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. - assertEquals(BigIntegerMath.sqrt(bigInt(x), mode), bigInt(sqrt(x, mode))); + assertThat(bigInt(sqrt(x, mode))).isEqualTo(BigIntegerMath.sqrt(bigInt(x), mode)); } } } @@ -343,10 +353,10 @@ public void testSqrtExactMatchesFloorOrThrows() { // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = sqrtFloor * sqrtFloor == x; try { - assertEquals(sqrtFloor, sqrt(x, UNNECESSARY)); - assertTrue(isPerfectSquare); + assertThat(sqrt(x, UNNECESSARY)).isEqualTo(sqrtFloor); + assertThat(isPerfectSquare).isTrue(); } catch (ArithmeticException e) { - assertFalse(isPerfectSquare); + assertThat(isPerfectSquare).isFalse(); } } } @@ -355,7 +365,7 @@ public void testSqrtExactMatchesFloorOrThrows() { public void testPow() { for (long i : ALL_LONG_CANDIDATES) { for (int exp : EXPONENTS) { - assertEquals(LongMath.pow(i, exp), bigInt(i).pow(exp).longValue()); + assertThat(LongMath.pow(i, exp)).isEqualTo(bigInt(i).pow(exp).longValue()); } } } @@ -375,9 +385,9 @@ public void testDivNonZero() { } // Check the assertions we make in the javadoc. if (mode == DOWN) { - assertEquals(p / q, LongMath.divide(p, q, mode)); + assertThat(LongMath.divide(p, q, mode)).isEqualTo(p / q); } else if (mode == FLOOR) { - assertEquals(Math.floorDiv(p, q), LongMath.divide(p, q, mode)); + assertThat(LongMath.divide(p, q, mode)).isEqualTo(Math.floorDiv(p, q)); } } } @@ -392,8 +402,8 @@ public void testDivNonZeroExact() { boolean expectedSuccess = (p % q) == 0L; try { - assertEquals(p, LongMath.divide(p, q, UNNECESSARY) * q); - assertTrue(expectedSuccess); + assertThat(LongMath.divide(p, q, UNNECESSARY) * q).isEqualTo(p); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -408,7 +418,7 @@ public void testDivNonZeroExact() { public void testZeroDivIsAlwaysZero() { for (long q : NONZERO_LONG_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0L, LongMath.divide(0L, q, mode)); + assertThat(LongMath.divide(0L, q, mode)).isEqualTo(0L); } } } @@ -426,7 +436,7 @@ public void testDivByZeroAlwaysFails() { public void testIntMod() { for (long x : ALL_LONG_CANDIDATES) { for (int m : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).intValue(), LongMath.mod(x, m)); + assertThat(LongMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).intValue()); } } } @@ -452,7 +462,7 @@ public void testIntModZeroModulusFails() { public void testMod() { for (long x : ALL_LONG_CANDIDATES) { for (long m : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).longValue(), LongMath.mod(x, m)); + assertThat(LongMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).longValue()); } } } @@ -469,7 +479,7 @@ public void testModNegativeModulusFails() { public void testGCDExhaustive() { for (long a : POSITIVE_LONG_CANDIDATES) { for (long b : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(a).gcd(bigInt(b)), bigInt(LongMath.gcd(a, b))); + assertThat(bigInt(LongMath.gcd(a, b))).isEqualTo(bigInt(a).gcd(bigInt(b))); } } } @@ -477,10 +487,10 @@ public void testGCDExhaustive() { @GwtIncompatible // TODO public void testGCDZero() { for (long a : POSITIVE_LONG_CANDIDATES) { - assertEquals(a, LongMath.gcd(a, 0)); - assertEquals(a, LongMath.gcd(0, a)); + assertThat(LongMath.gcd(a, 0)).isEqualTo(a); + assertThat(LongMath.gcd(0, a)).isEqualTo(a); } - assertEquals(0, LongMath.gcd(0, 0)); + assertThat(LongMath.gcd(0, 0)).isEqualTo(0); } @GwtIncompatible // TODO @@ -508,8 +518,8 @@ public void testCheckedAdd() { BigInteger expectedResult = bigInt(a).add(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a + b, checkedAdd(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedAdd(a, b)).isEqualTo(a + b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -529,8 +539,8 @@ public void testCheckedSubtract() { BigInteger expectedResult = bigInt(a).subtract(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a - b, checkedSubtract(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedSubtract(a, b)).isEqualTo(a - b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -562,8 +572,8 @@ public void testCheckedMultiply() { BigInteger expectedResult = bigInt(a).multiply(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a * b, checkedMultiply(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedMultiply(a, b)).isEqualTo(a * b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -582,8 +592,8 @@ public void testCheckedPow() { BigInteger expectedResult = bigInt(b).pow(exp); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(expectedResult.longValue(), LongMath.checkedPow(b, exp)); - assertTrue(expectedSuccess); + assertThat(LongMath.checkedPow(b, exp)).isEqualTo(expectedResult.longValue()); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -658,7 +668,7 @@ public void testFactorial() { for (int n = 0; n <= 50; n++) { BigInteger expectedBig = BigIntegerMath.factorial(n); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; - assertEquals(expectedLong, LongMath.factorial(n)); + assertThat(LongMath.factorial(n)).isEqualTo(expectedLong); } } @@ -675,7 +685,7 @@ public void testBinomial() { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; - assertEquals(expectedLong, LongMath.binomial(n, k)); + assertThat(LongMath.binomial(n, k)).isEqualTo(expectedLong); } } } @@ -687,7 +697,7 @@ public void testBinomial_exhaustiveNotOverflowing() { // tested in the previous method, for k >= 3. for (int k = 3; k < LongMath.biggestBinomials.length; k++) { for (int n = 70; n <= LongMath.biggestBinomials[k]; n++) { - assertEquals(BigIntegerMath.binomial(n, k).longValue(), LongMath.binomial(n, k)); + assertThat(LongMath.binomial(n, k)).isEqualTo(BigIntegerMath.binomial(n, k).longValue()); } } } @@ -713,13 +723,13 @@ public void testSqrtOfPerfectSquareAsDoubleIsPerfect() { // This takes just over a minute on my machine. for (long n = 0; n <= LongMath.FLOOR_SQRT_MAX_LONG; n++) { long actual = (long) Math.sqrt((double) (n * n)); - assertTrue(actual == n); + assertThat(actual).isEqualTo(n); } } public void testSqrtOfLongIsAtMostFloorSqrtMaxLong() { long sqrtMaxLong = (long) Math.sqrt(Long.MAX_VALUE); - assertTrue(sqrtMaxLong <= LongMath.FLOOR_SQRT_MAX_LONG); + assertThat(sqrtMaxLong).isAtMost(LongMath.FLOOR_SQRT_MAX_LONG); } @AndroidIncompatible // slow @@ -759,10 +769,9 @@ public void testMean() { /** Helper method that asserts the arithmetic mean of x and y is equal to the expectedMean. */ private static void assertMean(long expectedMean, long x, long y) { - assertEquals( - "The expectedMean should be the same as computeMeanSafely", - expectedMean, - computeMeanSafely(x, y)); + assertWithMessage("The expectedMean should be the same as computeMeanSafely") + .that(computeMeanSafely(x, y)) + .isEqualTo(expectedMean); assertMean(x, y); } @@ -772,9 +781,10 @@ private static void assertMean(long expectedMean, long x, long y) { */ private static void assertMean(long x, long y) { long expectedMean = computeMeanSafely(x, y); - assertEquals(expectedMean, LongMath.mean(x, y)); - assertEquals( - "The mean of x and y should equal the mean of y and x", expectedMean, LongMath.mean(y, x)); + assertThat(LongMath.mean(x, y)).isEqualTo(expectedMean); + assertWithMessage("The mean of x and y should equal the mean of y and x") + .that(LongMath.mean(y, x)) + .isEqualTo(expectedMean); } /** @@ -784,7 +794,8 @@ private static void assertMean(long x, long y) { private static long computeMeanSafely(long x, long y) { BigInteger bigX = bigInt(x); BigInteger bigY = bigInt(y); - BigDecimal two = BigDecimal.valueOf(2); // Android doesn't have BigDecimal.TWO yet + @SuppressWarnings("ConstantTwo") // Android doesn't have BigDecimal.TWO yet + BigDecimal two = BigDecimal.valueOf(2); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR); return bigMean.longValueExact(); } @@ -819,7 +830,7 @@ public void testNullPointers() { public void testIsPrimeSmall() { // Check the first 1000 integers for (int i = 2; i < 1000; i++) { - assertEquals(bigInt(i).isProbablePrime(100), LongMath.isPrime(i)); + assertThat(LongMath.isPrime(i)).isEqualTo(bigInt(i).isProbablePrime(100)); } } @@ -828,7 +839,7 @@ public void testIsPrimeManyConstants() { // Test the thorough test inputs, which also includes special constants in the Miller-Rabin // tests. for (long l : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(l).isProbablePrime(100), LongMath.isPrime(l)); + assertThat(LongMath.isPrime(l)).isEqualTo(bigInt(l).isProbablePrime(100)); } } @@ -839,7 +850,7 @@ public void testIsPrimeOnUniformRandom() { for (int i = 0; i < 2000; i++) { // A random long between 0 and Long.MAX_VALUE, inclusive. long l = rand.nextLong() & ((1L << bits) - 1); - assertEquals(bigInt(l).isProbablePrime(100), LongMath.isPrime(l)); + assertThat(LongMath.isPrime(l)).isEqualTo(bigInt(l).isProbablePrime(100)); } } } @@ -850,7 +861,7 @@ public void testIsPrimeOnRandomPrimes() { for (int bits = 10; bits < 63; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); - assertTrue(LongMath.isPrime(p)); + assertThat(LongMath.isPrime(p)).isTrue(); } } } @@ -862,7 +873,7 @@ public void testIsPrimeOnRandomComposites() { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); long q = BigInteger.probablePrime(bits, rand).longValue(); - assertFalse(LongMath.isPrime(p * q)); + assertThat(LongMath.isPrime(p * q)).isFalse(); } } } @@ -939,14 +950,14 @@ public void testRoundToDoubleAgainstBigIntegerUnnecessary() { } public void testSaturatedAbs() { - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(Long.MIN_VALUE)); - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(Long.MAX_VALUE)); - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(-Long.MAX_VALUE)); - assertEquals(0, LongMath.saturatedAbs(0)); - assertEquals(1, LongMath.saturatedAbs(1)); - assertEquals(1, LongMath.saturatedAbs(-1)); - assertEquals(10, LongMath.saturatedAbs(10)); - assertEquals(10, LongMath.saturatedAbs(-10)); + assertThat(LongMath.saturatedAbs(Long.MIN_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(Long.MAX_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(-Long.MAX_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(0)).isEqualTo(0); + assertThat(LongMath.saturatedAbs(1)).isEqualTo(1); + assertThat(LongMath.saturatedAbs(-1)).isEqualTo(1); + assertThat(LongMath.saturatedAbs(10)).isEqualTo(10); + assertThat(LongMath.saturatedAbs(-10)).isEqualTo(10); } private static void failFormat(String template, Object... args) { diff --git a/guava-tests/test/com/google/common/math/IntMathTest.java b/guava-tests/test/com/google/common/math/IntMathTest.java index e31f7454dd59..734ebeb73a50 100644 --- a/guava-tests/test/com/google/common/math/IntMathTest.java +++ b/guava-tests/test/com/google/common/math/IntMathTest.java @@ -28,6 +28,8 @@ import static com.google.common.math.MathTesting.NONZERO_INTEGER_CANDIDATES; import static com.google.common.math.MathTesting.POSITIVE_INTEGER_CANDIDATES; import static com.google.common.math.TestPlatform.intsCanGoOutOfRange; +import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static java.lang.Math.min; import static java.math.RoundingMode.DOWN; import static java.math.RoundingMode.FLOOR; @@ -55,19 +57,26 @@ @SuppressWarnings("IntMathMod") // We are testing IntMathMod against alternatives. public class IntMathTest extends TestCase { public void testMaxSignedPowerOfTwo() { - assertTrue(IntMath.isPowerOfTwo(IntMath.MAX_SIGNED_POWER_OF_TWO)); + assertThat(IntMath.isPowerOfTwo(IntMath.MAX_SIGNED_POWER_OF_TWO)).isTrue(); // Extra work required to make GWT happy. long value = IntMath.MAX_SIGNED_POWER_OF_TWO * 2L; - assertFalse(IntMath.isPowerOfTwo((int) value)); + assertThat(IntMath.isPowerOfTwo((int) value)).isFalse(); } public void testCeilingPowerOfTwo() { for (int x : POSITIVE_INTEGER_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); if (fitsInInt(expectedResult)) { - assertEquals(expectedResult.intValue(), IntMath.ceilingPowerOfTwo(x)); - } else { + assertThat(IntMath.ceilingPowerOfTwo(x)).isEqualTo(expectedResult.intValue()); + } + } + } + + public void testCeilingPowerOfTwo_overflows() { + for (int x : POSITIVE_INTEGER_CANDIDATES) { + BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); + if (!fitsInInt(expectedResult)) { assertThrows(ArithmeticException.class, () -> IntMath.ceilingPowerOfTwo(x)); } } @@ -76,7 +85,7 @@ public void testCeilingPowerOfTwo() { public void testFloorPowerOfTwo() { for (int x : POSITIVE_INTEGER_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.floorPowerOfTwo(bigInt(x)); - assertEquals(expectedResult.intValue(), IntMath.floorPowerOfTwo(x)); + assertThat(IntMath.floorPowerOfTwo(x)).isEqualTo(expectedResult.intValue()); } } @@ -100,58 +109,63 @@ public void testFloorPowerOfTwoZero() { assertThrows(IllegalArgumentException.class, () -> IntMath.floorPowerOfTwo(0)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testConstantMaxPowerOfSqrt2Unsigned() { - assertEquals( - BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR).intValue(), - IntMath.MAX_POWER_OF_SQRT2_UNSIGNED); + assertThat(IntMath.MAX_POWER_OF_SQRT2_UNSIGNED) + .isEqualTo( + BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Integer.SIZE - 1), FLOOR).intValue()); } @GwtIncompatible // pow() public void testConstantsPowersOf10() { for (int i = 0; i < IntMath.powersOf10.length - 1; i++) { - assertEquals(IntMath.pow(10, i), IntMath.powersOf10[i]); + assertThat(IntMath.powersOf10[i]).isEqualTo(IntMath.pow(10, i)); } } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testMaxLog10ForLeadingZeros() { for (int i = 0; i < Integer.SIZE; i++) { - assertEquals( - BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Integer.SIZE - i), FLOOR), - IntMath.maxLog10ForLeadingZeros[i]); + assertThat(IntMath.maxLog10ForLeadingZeros[i]) + .isEqualTo(BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Integer.SIZE - i), FLOOR)); } } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testConstantsHalfPowersOf10() { for (int i = 0; i < IntMath.halfPowersOf10.length; i++) { - assertEquals( - IntMath.halfPowersOf10[i], - min( - Integer.MAX_VALUE, - BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR).longValue())); + assertThat( + min( + Integer.MAX_VALUE, + BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR).longValue())) + .isEqualTo(IntMath.halfPowersOf10[i]); } } public void testConstantsBiggestBinomials() { for (int k = 0; k < IntMath.biggestBinomials.length; k++) { - assertTrue(fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k], k))); - assertTrue( - IntMath.biggestBinomials[k] == Integer.MAX_VALUE - || !fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k] + 1, k))); + assertThat(fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k], k))).isTrue(); + assertThat( + IntMath.biggestBinomials[k] == Integer.MAX_VALUE + || !fitsInInt(BigIntegerMath.binomial(IntMath.biggestBinomials[k] + 1, k))) + .isTrue(); // In the first case, any int is valid; in the second, we want to test that the next-bigger // int overflows. } - assertFalse( - fitsInInt( - BigIntegerMath.binomial( - 2 * IntMath.biggestBinomials.length, IntMath.biggestBinomials.length))); + assertThat( + fitsInInt( + BigIntegerMath.binomial( + 2 * IntMath.biggestBinomials.length, IntMath.biggestBinomials.length))) + .isFalse(); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // sqrt public void testPowersSqrtMaxInt() { - assertEquals(sqrt(Integer.MAX_VALUE, FLOOR), IntMath.FLOOR_SQRT_MAX_INT); + assertThat(IntMath.FLOOR_SQRT_MAX_INT).isEqualTo(sqrt(Integer.MAX_VALUE, FLOOR)); } @AndroidIncompatible // presumably slow @@ -159,9 +173,7 @@ public void testLessThanBranchFree() { for (int x : ALL_INTEGER_CANDIDATES) { for (int y : ALL_INTEGER_CANDIDATES) { if (LongMath.fitsInInt((long) x - y)) { - int expected = (x < y) ? 1 : 0; - int actual = IntMath.lessThanBranchFree(x, y); - assertEquals(expected, actual); + assertThat(IntMath.lessThanBranchFree(x, y)).isEqualTo(x < y ? 1 : 0); } } } @@ -171,9 +183,7 @@ public void testLessThanBranchFree() { public void testIsPowerOfTwo() { for (int x : ALL_INTEGER_CANDIDATES) { // Checks for a single bit set. - BigInteger bigX = bigInt(x); - boolean expected = (bigX.signum() > 0) && (bigX.bitCount() == 1); - assertEquals(expected, IntMath.isPowerOfTwo(x)); + assertThat(IntMath.isPowerOfTwo(x)).isEqualTo(x > 0 && bigInt(x).bitCount() == 1); } } @@ -195,7 +205,7 @@ public void testLog2NegativeAlwaysThrows() { public void testLog2MatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { - assertEquals(BigIntegerMath.log2(bigInt(x), mode), IntMath.log2(x, mode)); + assertThat(IntMath.log2(x, mode)).isEqualTo(BigIntegerMath.log2(bigInt(x), mode)); } } } @@ -203,13 +213,17 @@ public void testLog2MatchesBigInteger() { // Relies on the correctness of isPowerOfTwo(int). public void testLog2Exact() { for (int x : POSITIVE_INTEGER_CANDIDATES) { - // We only expect an exception if x was not a power of 2. - boolean isPowerOf2 = IntMath.isPowerOfTwo(x); - try { - assertEquals(x, 1 << IntMath.log2(x, UNNECESSARY)); - assertTrue(isPowerOf2); - } catch (ArithmeticException e) { - assertFalse(isPowerOf2); + if (IntMath.isPowerOfTwo(x)) { + assertThat(1 << IntMath.log2(x, UNNECESSARY)).isEqualTo(x); + } + } + } + + // Relies on the correctness of isPowerOfTwo(int). + public void testLog2Exact_notPowerOfTwo() { + for (int x : POSITIVE_INTEGER_CANDIDATES) { + if (!IntMath.isPowerOfTwo(x)) { + assertThrows(ArithmeticException.class, () -> IntMath.log2(x, UNNECESSARY)); } } } @@ -236,7 +250,7 @@ public void testLog10MatchesBigInteger() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. - assertEquals(BigIntegerMath.log10(bigInt(x), mode), IntMath.log10(x, mode)); + assertThat(IntMath.log10(x, mode)).isEqualTo(BigIntegerMath.log10(bigInt(x), mode)); } } } @@ -248,10 +262,10 @@ public void testLog10Exact() { int floor = IntMath.log10(x, FLOOR); boolean expectSuccess = IntMath.pow(10, floor) == x; try { - assertEquals(floor, IntMath.log10(x, UNNECESSARY)); - assertTrue(expectSuccess); + assertThat(IntMath.log10(x, UNNECESSARY)).isEqualTo(floor); + assertThat(expectSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectSuccess); + assertThat(expectSuccess).isFalse(); } } } @@ -260,7 +274,7 @@ public void testLog10Exact() { public void testLog10TrivialOnPowerOfTen() { int x = 1000000; for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(6, IntMath.log10(x, mode)); + assertThat(IntMath.log10(x, mode)).isEqualTo(6); } } @@ -268,7 +282,7 @@ public void testLog10TrivialOnPowerOfTen() { @GwtIncompatible // sqrt public void testSqrtZeroAlwaysZero() { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0, sqrt(0, mode)); + assertThat(sqrt(0, mode)).isEqualTo(0); } } @@ -290,7 +304,7 @@ public void testSqrtMatchesBigInteger() { // The BigInteger implementation is tested separately, use it as the reference. // Promote the int value (rather than using intValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. - assertEquals(BigIntegerMath.sqrt(bigInt(x), mode), bigInt(sqrt(x, mode))); + assertThat(bigInt(sqrt(x, mode))).isEqualTo(BigIntegerMath.sqrt(bigInt(x), mode)); } } } @@ -303,10 +317,10 @@ public void testSqrtExactMatchesFloorOrThrows() { // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = floor * floor == x; try { - assertEquals(floor, sqrt(x, UNNECESSARY)); - assertTrue(isPerfectSquare); + assertThat(sqrt(x, UNNECESSARY)).isEqualTo(floor); + assertThat(isPerfectSquare).isTrue(); } catch (ArithmeticException e) { - assertFalse(isPerfectSquare); + assertThat(isPerfectSquare).isFalse(); } } } @@ -315,7 +329,9 @@ public void testSqrtExactMatchesFloorOrThrows() { public void testPow() { for (int i : ALL_INTEGER_CANDIDATES) { for (int pow : EXPONENTS) { - assertEquals(i + "^" + pow, bigInt(i).pow(pow).intValue(), IntMath.pow(i, pow)); + assertWithMessage("%s^%s", i, pow) + .that(IntMath.pow(i, pow)) + .isEqualTo(bigInt(i).pow(pow).intValue()); } } } @@ -333,12 +349,16 @@ public void testDivNonZero() { } int expected = new BigDecimal(bigInt(p)).divide(new BigDecimal(bigInt(q)), 0, mode).intValue(); - assertEquals(p + "/" + q, force32(expected), IntMath.divide(p, q, mode)); + assertWithMessage("%s/%s", p, q) + .that(IntMath.divide(p, q, mode)) + .isEqualTo(force32(expected)); // Check the assertions we make in the javadoc. if (mode == DOWN) { - assertEquals(p + "/" + q, p / q, IntMath.divide(p, q, mode)); + assertWithMessage("%s/%s", p, q).that(IntMath.divide(p, q, mode)).isEqualTo(p / q); } else if (mode == FLOOR) { - assertEquals("⌊" + p + "/" + q + "⌋", Math.floorDiv(p, q), IntMath.divide(p, q, mode)); + assertWithMessage("⌊%s/%s⌋", p, q) + .that(IntMath.divide(p, q, mode)) + .isEqualTo(Math.floorDiv(p, q)); } } } @@ -355,10 +375,12 @@ public void testDivNonZeroExact() { } boolean dividesEvenly = (p % q) == 0; try { - assertEquals(p + "/" + q, p, IntMath.divide(p, q, UNNECESSARY) * q); - assertTrue(p + "/" + q + " not expected to divide evenly", dividesEvenly); + assertWithMessage("%s/%s", p, q).that(IntMath.divide(p, q, UNNECESSARY) * q).isEqualTo(p); + assertWithMessage("%s/%s not expected to divide evenly", p, q) + .that(dividesEvenly) + .isTrue(); } catch (ArithmeticException e) { - assertFalse(p + "/" + q + " expected to divide evenly", dividesEvenly); + assertWithMessage("%s/%s expected to divide evenly", p, q).that(dividesEvenly).isFalse(); } } } @@ -367,7 +389,7 @@ public void testDivNonZeroExact() { public void testZeroDivIsAlwaysZero() { for (int q : NONZERO_INTEGER_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0, IntMath.divide(0, q, mode)); + assertThat(IntMath.divide(0, q, mode)).isEqualTo(0); } } } @@ -383,7 +405,7 @@ public void testDivByZeroAlwaysFails() { public void testMod() { for (int x : ALL_INTEGER_CANDIDATES) { for (int m : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).intValue(), IntMath.mod(x, m)); + assertThat(IntMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).intValue()); } } } @@ -405,17 +427,17 @@ public void testModZeroModulusFails() { public void testGCD() { for (int a : POSITIVE_INTEGER_CANDIDATES) { for (int b : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(a).gcd(bigInt(b)), bigInt(IntMath.gcd(a, b))); + assertThat(bigInt(IntMath.gcd(a, b))).isEqualTo(bigInt(a).gcd(bigInt(b))); } } } public void testGCDZero() { for (int a : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(a, IntMath.gcd(a, 0)); - assertEquals(a, IntMath.gcd(0, a)); + assertThat(IntMath.gcd(a, 0)).isEqualTo(a); + assertThat(IntMath.gcd(0, a)).isEqualTo(a); } - assertEquals(0, IntMath.gcd(0, 0)); + assertThat(IntMath.gcd(0, 0)).isEqualTo(0); } public void testGCDNegativePositiveThrows() { @@ -441,10 +463,10 @@ public void testCheckedAdd() { BigInteger expectedResult = bigInt(a).add(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a + b, checkedAdd(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedAdd(a, b)).isEqualTo(a + b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -459,10 +481,10 @@ public void testCheckedSubtract() { BigInteger expectedResult = bigInt(a).subtract(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a - b, checkedSubtract(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedSubtract(a, b)).isEqualTo(a - b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -477,10 +499,10 @@ public void testCheckedMultiply() { BigInteger expectedResult = bigInt(a).multiply(bigInt(b)); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(a * b, checkedMultiply(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedMultiply(a, b)).isEqualTo(a * b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(expectedSuccess); + assertThat(expectedSuccess).isFalse(); } } } @@ -492,10 +514,12 @@ public void testCheckedPow() { BigInteger expectedResult = bigInt(b).pow(k); boolean expectedSuccess = fitsInInt(expectedResult); try { - assertEquals(b + "^" + k, force32(expectedResult.intValue()), IntMath.checkedPow(b, k)); - assertTrue(b + "^" + k + " should have succeeded", expectedSuccess); + assertWithMessage("%s^%s", b, k) + .that(IntMath.checkedPow(b, k)) + .isEqualTo(force32(expectedResult.intValue())); + assertWithMessage("%s^%s should have succeeded", b, k).that(expectedSuccess).isTrue(); } catch (ArithmeticException e) { - assertFalse(b + "^" + k + " should have failed", expectedSuccess); + assertWithMessage("%s^%s should have failed", b, k).that(expectedSuccess).isFalse(); } } } @@ -576,7 +600,7 @@ public void testFactorial() { for (int n = 0; n <= 50; n++) { BigInteger expectedBig = BigIntegerMath.factorial(n); int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE; - assertEquals(expectedInt, IntMath.factorial(n)); + assertThat(IntMath.factorial(n)).isEqualTo(expectedInt); } } @@ -592,7 +616,7 @@ public void testBinomial() { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); int expectedInt = fitsInInt(expectedBig) ? expectedBig.intValue() : Integer.MAX_VALUE; - assertEquals(expectedInt, IntMath.binomial(n, k)); + assertThat(IntMath.binomial(n, k)).isEqualTo(expectedInt); } } } @@ -648,10 +672,9 @@ public void testMean() { /** Helper method that asserts the arithmetic mean of x and y is equal to the expectedMean. */ private static void assertMean(int expectedMean, int x, int y) { - assertEquals( - "The expectedMean should be the same as computeMeanSafely", - expectedMean, - computeMeanSafely(x, y)); + assertWithMessage("The expectedMean should be the same as computeMeanSafely") + .that(computeMeanSafely(x, y)) + .isEqualTo(expectedMean); assertMean(x, y); } @@ -661,9 +684,10 @@ private static void assertMean(int expectedMean, int x, int y) { */ private static void assertMean(int x, int y) { int expectedMean = computeMeanSafely(x, y); - assertEquals(expectedMean, IntMath.mean(x, y)); - assertEquals( - "The mean of x and y should equal the mean of y and x", expectedMean, IntMath.mean(y, x)); + assertThat(IntMath.mean(x, y)).isEqualTo(expectedMean); + assertWithMessage("The mean of x and y should equal the mean of y and x") + .that(IntMath.mean(y, x)) + .isEqualTo(expectedMean); } /** @@ -673,7 +697,8 @@ private static void assertMean(int x, int y) { private static int computeMeanSafely(int x, int y) { BigInteger bigX = bigInt(x); BigInteger bigY = bigInt(y); - BigDecimal two = BigDecimal.valueOf(2); // Android doesn't have BigDecimal.TWO yet + @SuppressWarnings("ConstantTwo") // Android doesn't have BigDecimal.TWO yet + BigDecimal two = BigDecimal.valueOf(2); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR); return bigMean.intValueExact(); } @@ -696,26 +721,26 @@ public void testIsPrime() { // Check the first 100,000 integers for (int i = 0; i < 100000; i++) { - assertEquals(LongMath.isPrime(i), IntMath.isPrime(i)); + assertThat(IntMath.isPrime(i)).isEqualTo(LongMath.isPrime(i)); } // Then check 1000 deterministic pseudo-random int values. Random rand = new Random(1); for (int i = 0; i < 1000; i++) { int n = rand.nextInt(Integer.MAX_VALUE); - assertEquals(LongMath.isPrime(n), IntMath.isPrime(n)); + assertThat(IntMath.isPrime(n)).isEqualTo(LongMath.isPrime(n)); } } public void testSaturatedAbs() { - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(Integer.MIN_VALUE)); - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(Integer.MAX_VALUE)); - assertEquals(Integer.MAX_VALUE, IntMath.saturatedAbs(-Integer.MAX_VALUE)); - assertEquals(0, IntMath.saturatedAbs(0)); - assertEquals(1, IntMath.saturatedAbs(1)); - assertEquals(1, IntMath.saturatedAbs(-1)); - assertEquals(10, IntMath.saturatedAbs(10)); - assertEquals(10, IntMath.saturatedAbs(-10)); + assertThat(IntMath.saturatedAbs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(Integer.MAX_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(-Integer.MAX_VALUE)).isEqualTo(Integer.MAX_VALUE); + assertThat(IntMath.saturatedAbs(0)).isEqualTo(0); + assertThat(IntMath.saturatedAbs(1)).isEqualTo(1); + assertThat(IntMath.saturatedAbs(-1)).isEqualTo(1); + assertThat(IntMath.saturatedAbs(10)).isEqualTo(10); + assertThat(IntMath.saturatedAbs(-10)).isEqualTo(10); } private static int force32(int value) { diff --git a/guava-tests/test/com/google/common/math/LongMathTest.java b/guava-tests/test/com/google/common/math/LongMathTest.java index f6009bcc3f59..25b0ecfd9c7d 100644 --- a/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/guava-tests/test/com/google/common/math/LongMathTest.java @@ -60,16 +60,24 @@ public class LongMathTest extends TestCase { @SuppressWarnings("ConstantOverflow") public void testMaxSignedPowerOfTwo() { - assertTrue(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO)); - assertFalse(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO * 2)); + assertThat(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO)).isTrue(); + assertThat(LongMath.isPowerOfTwo(LongMath.MAX_SIGNED_POWER_OF_TWO * 2)).isFalse(); } public void testCeilingPowerOfTwo() { for (long x : POSITIVE_LONG_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); if (fitsInLong(expectedResult)) { - assertEquals(expectedResult.longValue(), LongMath.ceilingPowerOfTwo(x)); - } else { + assertThat(LongMath.ceilingPowerOfTwo(x)) + .isEqualTo(BigIntegerMath.ceilingPowerOfTwo(bigInt(x)).longValue()); + } + } + } + + public void testCeilingPowerOfTwo_overflows() { + for (long x : POSITIVE_LONG_CANDIDATES) { + BigInteger expectedResult = BigIntegerMath.ceilingPowerOfTwo(bigInt(x)); + if (!fitsInLong(expectedResult)) { assertThrows(ArithmeticException.class, () -> LongMath.ceilingPowerOfTwo(x)); } } @@ -78,7 +86,7 @@ public void testCeilingPowerOfTwo() { public void testFloorPowerOfTwo() { for (long x : POSITIVE_LONG_CANDIDATES) { BigInteger expectedResult = BigIntegerMath.floorPowerOfTwo(bigInt(x)); - assertEquals(expectedResult.longValue(), LongMath.floorPowerOfTwo(x)); + assertThat(LongMath.floorPowerOfTwo(x)).isEqualTo(expectedResult.longValue()); } } @@ -102,26 +110,27 @@ public void testFloorPowerOfTwoZero() { assertThrows(IllegalArgumentException.class, () -> LongMath.floorPowerOfTwo(0L)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // TODO public void testConstantMaxPowerOfSqrt2Unsigned() { - assertEquals( - BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR).longValue(), - LongMath.MAX_POWER_OF_SQRT2_UNSIGNED); + assertThat(LongMath.MAX_POWER_OF_SQRT2_UNSIGNED) + .isEqualTo( + BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR).longValue()); } @GwtIncompatible // BigIntegerMath // TODO(cpovirk): GWT-enable BigIntegerMath public void testMaxLog10ForLeadingZeros() { for (int i = 0; i < Long.SIZE; i++) { - assertEquals( - BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Long.SIZE - i), FLOOR), - LongMath.maxLog10ForLeadingZeros[i]); + assertThat(LongMath.maxLog10ForLeadingZeros[i]) + .isEqualTo(BigIntegerMath.log10(BigInteger.ONE.shiftLeft(Long.SIZE - i), FLOOR)); } } @GwtIncompatible // TODO public void testConstantsPowersOf10() { for (int i = 0; i < LongMath.powersOf10.length; i++) { - assertEquals(LongMath.checkedPow(10, i), LongMath.powersOf10[i]); + assertThat(LongMath.powersOf10[i]).isEqualTo(LongMath.checkedPow(10, i)); } assertThrows( ArithmeticException.class, () -> LongMath.checkedPow(10, LongMath.powersOf10.length)); @@ -130,25 +139,26 @@ public void testConstantsPowersOf10() { @GwtIncompatible // TODO public void testConstantsHalfPowersOf10() { for (int i = 0; i < LongMath.halfPowersOf10.length; i++) { - assertEquals( - BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR), - bigInt(LongMath.halfPowersOf10[i])); + assertThat(bigInt(LongMath.halfPowersOf10[i])) + .isEqualTo(BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR)); } BigInteger nextBigger = BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR); assertThat(nextBigger).isGreaterThan(bigInt(Long.MAX_VALUE)); } + // We want to test that we've defined the constant with the correct value. + @SuppressWarnings("TruthConstantAsserts") @GwtIncompatible // TODO public void testConstantsSqrtMaxLong() { - assertEquals(sqrt(Long.MAX_VALUE, FLOOR), LongMath.FLOOR_SQRT_MAX_LONG); + assertThat(LongMath.FLOOR_SQRT_MAX_LONG).isEqualTo(sqrt(Long.MAX_VALUE, FLOOR)); } @GwtIncompatible // TODO public void testConstantsFactorials() { long expected = 1; for (int i = 0; i < LongMath.factorials.length; i++, expected *= i) { - assertEquals(expected, LongMath.factorials[i]); + assertThat(LongMath.factorials[i]).isEqualTo(expected); } assertThrows( ArithmeticException.class, @@ -161,15 +171,16 @@ public void testConstantsFactorials() { @GwtIncompatible // TODO public void testConstantsBiggestBinomials() { for (int k = 0; k < LongMath.biggestBinomials.length; k++) { - assertTrue(fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k], k))); - assertTrue( - LongMath.biggestBinomials[k] == Integer.MAX_VALUE - || !fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k] + 1, k))); + assertThat(fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k], k))).isTrue(); + assertThat( + LongMath.biggestBinomials[k] == Integer.MAX_VALUE + || !fitsInLong(BigIntegerMath.binomial(LongMath.biggestBinomials[k] + 1, k))) + .isTrue(); // In the first case, any long is valid; in the second, we want to test that the next-bigger // long overflows. } int k = LongMath.biggestBinomials.length; - assertFalse(fitsInLong(BigIntegerMath.binomial(2 * k, k))); + assertThat(fitsInLong(BigIntegerMath.binomial(2 * k, k))).isFalse(); // 2 * k is the smallest value for which we don't replace k with (n-k). } @@ -177,7 +188,7 @@ public void testConstantsBiggestBinomials() { public void testConstantsBiggestSimpleBinomials() { for (int i = 0; i < LongMath.biggestSimpleBinomials.length; i++) { int k = i; - assertTrue(LongMath.biggestSimpleBinomials[k] <= LongMath.biggestBinomials[k]); + assertThat(LongMath.biggestSimpleBinomials[k]).isAtMost(LongMath.biggestBinomials[k]); long unused = simpleBinomial(LongMath.biggestSimpleBinomials[k], k); // mustn't throw if (LongMath.biggestSimpleBinomials[k] < Integer.MAX_VALUE) { // unless all n are fair game with this k @@ -194,11 +205,8 @@ public void testConstantsBiggestSimpleBinomials() { public void testLessThanBranchFree() { for (long x : ALL_LONG_CANDIDATES) { for (long y : ALL_LONG_CANDIDATES) { - BigInteger difference = bigInt(x).subtract(bigInt(y)); - if (fitsInLong(difference)) { - int expected = (x < y) ? 1 : 0; - int actual = LongMath.lessThanBranchFree(x, y); - assertEquals(expected, actual); + if (fitsInLong(bigInt(x).subtract(bigInt(y)))) { + assertThat(LongMath.lessThanBranchFree(x, y)).isEqualTo(x < y ? 1 : 0); } } } @@ -219,9 +227,7 @@ private long simpleBinomial(int n, int k) { public void testIsPowerOfTwo() { for (long x : ALL_LONG_CANDIDATES) { // Checks for a single bit set. - BigInteger bigX = bigInt(x); - boolean expected = (bigX.signum() > 0) && (bigX.bitCount() == 1); - assertEquals(expected, LongMath.isPowerOfTwo(x)); + assertThat(LongMath.isPowerOfTwo(x)).isEqualTo(x > 0 && bigInt(x).bitCount() == 1); } } @@ -244,21 +250,25 @@ public void testLog2MatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // The BigInteger implementation is tested separately, use it as the reference. - assertEquals(BigIntegerMath.log2(bigInt(x), mode), LongMath.log2(x, mode)); + assertThat(LongMath.log2(x, mode)).isEqualTo(BigIntegerMath.log2(bigInt(x), mode)); } } } - /* Relies on the correctness of isPowerOfTwo(long). */ + // Relies on the correctness of isPowerOfTwo(long). public void testLog2Exact() { for (long x : POSITIVE_LONG_CANDIDATES) { - // We only expect an exception if x was not a power of 2. - boolean isPowerOf2 = LongMath.isPowerOfTwo(x); - try { - assertEquals(x, 1L << LongMath.log2(x, UNNECESSARY)); - assertTrue(isPowerOf2); - } catch (ArithmeticException e) { - assertFalse(isPowerOf2); + if (LongMath.isPowerOfTwo(x)) { + assertThat(1L << LongMath.log2(x, UNNECESSARY)).isEqualTo(x); + } + } + } + + // Relies on the correctness of isPowerOfTwo(long). + public void testLog2Exact_notPowerOfTwo() { + for (long x : POSITIVE_LONG_CANDIDATES) { + if (!LongMath.isPowerOfTwo(x)) { + assertThrows(ArithmeticException.class, () -> LongMath.log2(x, UNNECESSARY)); } } } @@ -284,7 +294,7 @@ public void testLog10NegativeAlwaysThrows() { public void testLog10MatchesBigInteger() { for (long x : POSITIVE_LONG_CANDIDATES) { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { - assertEquals(BigIntegerMath.log10(bigInt(x), mode), LongMath.log10(x, mode)); + assertThat(LongMath.log10(x, mode)).isEqualTo(BigIntegerMath.log10(bigInt(x), mode)); } } } @@ -296,8 +306,8 @@ public void testLog10Exact() { int floor = LongMath.log10(x, FLOOR); boolean expectedSuccess = LongMath.pow(10, floor) == x; try { - assertEquals(floor, LongMath.log10(x, UNNECESSARY)); - assertTrue(expectedSuccess); + assertThat(LongMath.log10(x, UNNECESSARY)).isEqualTo(floor); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat("expected log10(%s, UNNECESSARY) = %s; got ArithmeticException", x, floor); @@ -310,7 +320,7 @@ public void testLog10Exact() { public void testLog10TrivialOnPowerOf10() { long x = 1000000000000L; for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(12, LongMath.log10(x, mode)); + assertThat(LongMath.log10(x, mode)).isEqualTo(12); } } @@ -330,7 +340,7 @@ public void testSqrtMatchesBigInteger() { for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) { // Promote the long value (rather than using longValue() on the expected value) to avoid // any risk of truncation which could lead to a false positive. - assertEquals(BigIntegerMath.sqrt(bigInt(x), mode), bigInt(sqrt(x, mode))); + assertThat(bigInt(sqrt(x, mode))).isEqualTo(BigIntegerMath.sqrt(bigInt(x), mode)); } } } @@ -343,10 +353,10 @@ public void testSqrtExactMatchesFloorOrThrows() { // We only expect an exception if x was not a perfect square. boolean isPerfectSquare = sqrtFloor * sqrtFloor == x; try { - assertEquals(sqrtFloor, sqrt(x, UNNECESSARY)); - assertTrue(isPerfectSquare); + assertThat(sqrt(x, UNNECESSARY)).isEqualTo(sqrtFloor); + assertThat(isPerfectSquare).isTrue(); } catch (ArithmeticException e) { - assertFalse(isPerfectSquare); + assertThat(isPerfectSquare).isFalse(); } } } @@ -355,7 +365,7 @@ public void testSqrtExactMatchesFloorOrThrows() { public void testPow() { for (long i : ALL_LONG_CANDIDATES) { for (int exp : EXPONENTS) { - assertEquals(LongMath.pow(i, exp), bigInt(i).pow(exp).longValue()); + assertThat(LongMath.pow(i, exp)).isEqualTo(bigInt(i).pow(exp).longValue()); } } } @@ -375,9 +385,9 @@ public void testDivNonZero() { } // Check the assertions we make in the javadoc. if (mode == DOWN) { - assertEquals(p / q, LongMath.divide(p, q, mode)); + assertThat(LongMath.divide(p, q, mode)).isEqualTo(p / q); } else if (mode == FLOOR) { - assertEquals(Math.floorDiv(p, q), LongMath.divide(p, q, mode)); + assertThat(LongMath.divide(p, q, mode)).isEqualTo(Math.floorDiv(p, q)); } } } @@ -392,8 +402,8 @@ public void testDivNonZeroExact() { boolean expectedSuccess = (p % q) == 0L; try { - assertEquals(p, LongMath.divide(p, q, UNNECESSARY) * q); - assertTrue(expectedSuccess); + assertThat(LongMath.divide(p, q, UNNECESSARY) * q).isEqualTo(p); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -408,7 +418,7 @@ public void testDivNonZeroExact() { public void testZeroDivIsAlwaysZero() { for (long q : NONZERO_LONG_CANDIDATES) { for (RoundingMode mode : ALL_ROUNDING_MODES) { - assertEquals(0L, LongMath.divide(0L, q, mode)); + assertThat(LongMath.divide(0L, q, mode)).isEqualTo(0L); } } } @@ -426,7 +436,7 @@ public void testDivByZeroAlwaysFails() { public void testIntMod() { for (long x : ALL_LONG_CANDIDATES) { for (int m : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).intValue(), LongMath.mod(x, m)); + assertThat(LongMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).intValue()); } } } @@ -452,7 +462,7 @@ public void testIntModZeroModulusFails() { public void testMod() { for (long x : ALL_LONG_CANDIDATES) { for (long m : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(x).mod(bigInt(m)).longValue(), LongMath.mod(x, m)); + assertThat(LongMath.mod(x, m)).isEqualTo(bigInt(x).mod(bigInt(m)).longValue()); } } } @@ -469,7 +479,7 @@ public void testModNegativeModulusFails() { public void testGCDExhaustive() { for (long a : POSITIVE_LONG_CANDIDATES) { for (long b : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(a).gcd(bigInt(b)), bigInt(LongMath.gcd(a, b))); + assertThat(bigInt(LongMath.gcd(a, b))).isEqualTo(bigInt(a).gcd(bigInt(b))); } } } @@ -477,10 +487,10 @@ public void testGCDExhaustive() { @GwtIncompatible // TODO public void testGCDZero() { for (long a : POSITIVE_LONG_CANDIDATES) { - assertEquals(a, LongMath.gcd(a, 0)); - assertEquals(a, LongMath.gcd(0, a)); + assertThat(LongMath.gcd(a, 0)).isEqualTo(a); + assertThat(LongMath.gcd(0, a)).isEqualTo(a); } - assertEquals(0, LongMath.gcd(0, 0)); + assertThat(LongMath.gcd(0, 0)).isEqualTo(0); } @GwtIncompatible // TODO @@ -508,8 +518,8 @@ public void testCheckedAdd() { BigInteger expectedResult = bigInt(a).add(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a + b, checkedAdd(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedAdd(a, b)).isEqualTo(a + b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -529,8 +539,8 @@ public void testCheckedSubtract() { BigInteger expectedResult = bigInt(a).subtract(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a - b, checkedSubtract(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedSubtract(a, b)).isEqualTo(a - b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -562,8 +572,8 @@ public void testCheckedMultiply() { BigInteger expectedResult = bigInt(a).multiply(bigInt(b)); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(a * b, checkedMultiply(a, b)); - assertTrue(expectedSuccess); + assertThat(checkedMultiply(a, b)).isEqualTo(a * b); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -582,8 +592,8 @@ public void testCheckedPow() { BigInteger expectedResult = bigInt(b).pow(exp); boolean expectedSuccess = fitsInLong(expectedResult); try { - assertEquals(expectedResult.longValue(), LongMath.checkedPow(b, exp)); - assertTrue(expectedSuccess); + assertThat(LongMath.checkedPow(b, exp)).isEqualTo(expectedResult.longValue()); + assertThat(expectedSuccess).isTrue(); } catch (ArithmeticException e) { if (expectedSuccess) { failFormat( @@ -658,7 +668,7 @@ public void testFactorial() { for (int n = 0; n <= 50; n++) { BigInteger expectedBig = BigIntegerMath.factorial(n); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; - assertEquals(expectedLong, LongMath.factorial(n)); + assertThat(LongMath.factorial(n)).isEqualTo(expectedLong); } } @@ -675,7 +685,7 @@ public void testBinomial() { for (int k = 0; k <= n; k++) { BigInteger expectedBig = BigIntegerMath.binomial(n, k); long expectedLong = fitsInLong(expectedBig) ? expectedBig.longValue() : Long.MAX_VALUE; - assertEquals(expectedLong, LongMath.binomial(n, k)); + assertThat(LongMath.binomial(n, k)).isEqualTo(expectedLong); } } } @@ -687,7 +697,7 @@ public void testBinomial_exhaustiveNotOverflowing() { // tested in the previous method, for k >= 3. for (int k = 3; k < LongMath.biggestBinomials.length; k++) { for (int n = 70; n <= LongMath.biggestBinomials[k]; n++) { - assertEquals(BigIntegerMath.binomial(n, k).longValue(), LongMath.binomial(n, k)); + assertThat(LongMath.binomial(n, k)).isEqualTo(BigIntegerMath.binomial(n, k).longValue()); } } } @@ -713,13 +723,13 @@ public void testSqrtOfPerfectSquareAsDoubleIsPerfect() { // This takes just over a minute on my machine. for (long n = 0; n <= LongMath.FLOOR_SQRT_MAX_LONG; n++) { long actual = (long) Math.sqrt((double) (n * n)); - assertTrue(actual == n); + assertThat(actual).isEqualTo(n); } } public void testSqrtOfLongIsAtMostFloorSqrtMaxLong() { long sqrtMaxLong = (long) Math.sqrt(Long.MAX_VALUE); - assertTrue(sqrtMaxLong <= LongMath.FLOOR_SQRT_MAX_LONG); + assertThat(sqrtMaxLong).isAtMost(LongMath.FLOOR_SQRT_MAX_LONG); } @AndroidIncompatible // slow @@ -759,10 +769,9 @@ public void testMean() { /** Helper method that asserts the arithmetic mean of x and y is equal to the expectedMean. */ private static void assertMean(long expectedMean, long x, long y) { - assertEquals( - "The expectedMean should be the same as computeMeanSafely", - expectedMean, - computeMeanSafely(x, y)); + assertWithMessage("The expectedMean should be the same as computeMeanSafely") + .that(computeMeanSafely(x, y)) + .isEqualTo(expectedMean); assertMean(x, y); } @@ -772,9 +781,10 @@ private static void assertMean(long expectedMean, long x, long y) { */ private static void assertMean(long x, long y) { long expectedMean = computeMeanSafely(x, y); - assertEquals(expectedMean, LongMath.mean(x, y)); - assertEquals( - "The mean of x and y should equal the mean of y and x", expectedMean, LongMath.mean(y, x)); + assertThat(LongMath.mean(x, y)).isEqualTo(expectedMean); + assertWithMessage("The mean of x and y should equal the mean of y and x") + .that(LongMath.mean(y, x)) + .isEqualTo(expectedMean); } /** @@ -784,7 +794,8 @@ private static void assertMean(long x, long y) { private static long computeMeanSafely(long x, long y) { BigInteger bigX = bigInt(x); BigInteger bigY = bigInt(y); - BigDecimal two = BigDecimal.valueOf(2); // Android doesn't have BigDecimal.TWO yet + @SuppressWarnings("ConstantTwo") // Android doesn't have BigDecimal.TWO yet + BigDecimal two = BigDecimal.valueOf(2); BigDecimal bigMean = new BigDecimal(bigX.add(bigY)).divide(two, RoundingMode.FLOOR); return bigMean.longValueExact(); } @@ -819,7 +830,7 @@ public void testNullPointers() { public void testIsPrimeSmall() { // Check the first 1000 integers for (int i = 2; i < 1000; i++) { - assertEquals(bigInt(i).isProbablePrime(100), LongMath.isPrime(i)); + assertThat(LongMath.isPrime(i)).isEqualTo(bigInt(i).isProbablePrime(100)); } } @@ -828,7 +839,7 @@ public void testIsPrimeManyConstants() { // Test the thorough test inputs, which also includes special constants in the Miller-Rabin // tests. for (long l : POSITIVE_LONG_CANDIDATES) { - assertEquals(bigInt(l).isProbablePrime(100), LongMath.isPrime(l)); + assertThat(LongMath.isPrime(l)).isEqualTo(bigInt(l).isProbablePrime(100)); } } @@ -839,7 +850,7 @@ public void testIsPrimeOnUniformRandom() { for (int i = 0; i < 2000; i++) { // A random long between 0 and Long.MAX_VALUE, inclusive. long l = rand.nextLong() & ((1L << bits) - 1); - assertEquals(bigInt(l).isProbablePrime(100), LongMath.isPrime(l)); + assertThat(LongMath.isPrime(l)).isEqualTo(bigInt(l).isProbablePrime(100)); } } } @@ -850,7 +861,7 @@ public void testIsPrimeOnRandomPrimes() { for (int bits = 10; bits < 63; bits++) { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); - assertTrue(LongMath.isPrime(p)); + assertThat(LongMath.isPrime(p)).isTrue(); } } } @@ -862,7 +873,7 @@ public void testIsPrimeOnRandomComposites() { for (int i = 0; i < 100; i++) { long p = BigInteger.probablePrime(bits, rand).longValue(); long q = BigInteger.probablePrime(bits, rand).longValue(); - assertFalse(LongMath.isPrime(p * q)); + assertThat(LongMath.isPrime(p * q)).isFalse(); } } } @@ -939,14 +950,14 @@ public void testRoundToDoubleAgainstBigIntegerUnnecessary() { } public void testSaturatedAbs() { - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(Long.MIN_VALUE)); - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(Long.MAX_VALUE)); - assertEquals(Long.MAX_VALUE, LongMath.saturatedAbs(-Long.MAX_VALUE)); - assertEquals(0, LongMath.saturatedAbs(0)); - assertEquals(1, LongMath.saturatedAbs(1)); - assertEquals(1, LongMath.saturatedAbs(-1)); - assertEquals(10, LongMath.saturatedAbs(10)); - assertEquals(10, LongMath.saturatedAbs(-10)); + assertThat(LongMath.saturatedAbs(Long.MIN_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(Long.MAX_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(-Long.MAX_VALUE)).isEqualTo(Long.MAX_VALUE); + assertThat(LongMath.saturatedAbs(0)).isEqualTo(0); + assertThat(LongMath.saturatedAbs(1)).isEqualTo(1); + assertThat(LongMath.saturatedAbs(-1)).isEqualTo(1); + assertThat(LongMath.saturatedAbs(10)).isEqualTo(10); + assertThat(LongMath.saturatedAbs(-10)).isEqualTo(10); } private static void failFormat(String template, Object... args) {