diff --git a/guava-tests/test/com/google/common/math/IntMathTest.java b/guava-tests/test/com/google/common/math/IntMathTest.java index fa8b3d4c4ab5..126a32f4afff 100644 --- a/guava-tests/test/com/google/common/math/IntMathTest.java +++ b/guava-tests/test/com/google/common/math/IntMathTest.java @@ -25,6 +25,7 @@ import static com.google.common.math.MathTesting.POSITIVE_INTEGER_CANDIDATES; import static com.google.common.math.ReflectionFreeAssertThrows.assertThrows; import static com.google.common.math.TestPlatform.intsCanGoOutOfRange; +import static com.google.common.truth.Truth.assertWithMessage; import static java.lang.Math.min; import static java.math.BigInteger.valueOf; import static java.math.RoundingMode.FLOOR; @@ -373,12 +374,14 @@ public void testDivByZeroAlwaysFails() { public void testMod() { for (int x : ALL_INTEGER_CANDIDATES) { for (int m : POSITIVE_INTEGER_CANDIDATES) { - assertEquals(valueOf(x).mod(valueOf(m)).intValue(), IntMath.mod(x, m)); + assertWithMessage("%s mod %s", x, m) + .that(IntMath.mod(x, m)) + .isEqualTo(valueOf(x).mod(valueOf(m)).intValue()); } } } - public void testModNegativeModulusFails() { + public void testModNegativeModulus() { for (int x : POSITIVE_INTEGER_CANDIDATES) { for (int m : NEGATIVE_INTEGER_CANDIDATES) { assertThrows(ArithmeticException.class, () -> IntMath.mod(x, m)); diff --git a/guava-tests/test/com/google/common/math/LongMathTest.java b/guava-tests/test/com/google/common/math/LongMathTest.java index f88baa99f727..7aae34ed1b2a 100644 --- a/guava-tests/test/com/google/common/math/LongMathTest.java +++ b/guava-tests/test/com/google/common/math/LongMathTest.java @@ -448,7 +448,7 @@ public void testMod() { } @GwtIncompatible // TODO - public void testModNegativeModulusFails() { + public void testModNegativeModulus() { for (long x : ALL_LONG_CANDIDATES) { for (long m : NEGATIVE_LONG_CANDIDATES) { assertThrows(ArithmeticException.class, () -> LongMath.mod(x, m)); diff --git a/guava/src/com/google/common/math/IntMath.java b/guava/src/com/google/common/math/IntMath.java index fa0d782ea4bf..aacc89e0a5a3 100644 --- a/guava/src/com/google/common/math/IntMath.java +++ b/guava/src/com/google/common/math/IntMath.java @@ -385,8 +385,7 @@ public static int mod(int x, int m) { if (m <= 0) { throw new ArithmeticException("Modulus " + m + " must be > 0"); } - int result = x % m; - return (result >= 0) ? result : result + m; + return Math.floorMod(x, m); } /** diff --git a/guava/src/com/google/common/math/LongMath.java b/guava/src/com/google/common/math/LongMath.java index 65eed0a98cd0..51feebf95cc7 100644 --- a/guava/src/com/google/common/math/LongMath.java +++ b/guava/src/com/google/common/math/LongMath.java @@ -473,8 +473,7 @@ public static long mod(long x, long m) { if (m <= 0) { throw new ArithmeticException("Modulus must be positive"); } - long result = x % m; - return (result >= 0) ? result : result + m; + return Math.floorMod(x, m); } /**