Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions guava-tests/test/com/google/common/math/IntMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion guava-tests/test/com/google/common/math/LongMathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/math/IntMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions guava/src/com/google/common/math/LongMath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Loading