aboutsummaryrefslogtreecommitdiffstats
path: root/guava-tests/test/com/google/common/math/LongMathTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava-tests/test/com/google/common/math/LongMathTest.java')
-rw-r--r--guava-tests/test/com/google/common/math/LongMathTest.java178
1 files changed, 25 insertions, 153 deletions
diff --git a/guava-tests/test/com/google/common/math/LongMathTest.java b/guava-tests/test/com/google/common/math/LongMathTest.java
index 089bf03..3ab0a8e 100644
--- a/guava-tests/test/com/google/common/math/LongMathTest.java
+++ b/guava-tests/test/com/google/common/math/LongMathTest.java
@@ -30,8 +30,6 @@ import static java.math.BigInteger.valueOf;
import static java.math.RoundingMode.FLOOR;
import static java.math.RoundingMode.UNNECESSARY;
-import com.google.common.annotations.GwtCompatible;
-import com.google.common.annotations.GwtIncompatible;
import com.google.common.testing.NullPointerTester;
import junit.framework.TestCase;
@@ -45,92 +43,75 @@ import java.math.RoundingMode;
*
* @author Louis Wasserman
*/
-@GwtCompatible(emulated = true)
public class LongMathTest extends TestCase {
- @GwtIncompatible("TODO")
public void testConstantMaxPowerOfSqrt2Unsigned() {
assertEquals(BigIntegerMath.sqrt(BigInteger.ZERO.setBit(2 * Long.SIZE - 1), FLOOR).longValue(),
LongMath.MAX_POWER_OF_SQRT2_UNSIGNED);
}
- @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]);
- }
- }
-
- @GwtIncompatible("TODO")
public void testConstantsPowersOf10() {
- for (int i = 0; i < LongMath.powersOf10.length; i++) {
- assertEquals(LongMath.checkedPow(10, i), LongMath.powersOf10[i]);
+ for (int i = 0; i < LongMath.POWERS_OF_10.length; i++) {
+ assertEquals(LongMath.checkedPow(10, i), LongMath.POWERS_OF_10[i]);
}
try {
- LongMath.checkedPow(10, LongMath.powersOf10.length);
+ LongMath.checkedPow(10, LongMath.POWERS_OF_10.length);
fail("Expected ArithmeticException");
} catch (ArithmeticException expected) {}
}
- @GwtIncompatible("TODO")
public void testConstantsHalfPowersOf10() {
- for (int i = 0; i < LongMath.halfPowersOf10.length; i++) {
+ for (int i = 0; i < LongMath.HALF_POWERS_OF_10.length; i++) {
assertEquals(BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * i + 1), FLOOR),
- BigInteger.valueOf(LongMath.halfPowersOf10[i]));
+ BigInteger.valueOf(LongMath.HALF_POWERS_OF_10[i]));
}
BigInteger nextBigger =
- BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.halfPowersOf10.length + 1), FLOOR);
+ BigIntegerMath.sqrt(BigInteger.TEN.pow(2 * LongMath.HALF_POWERS_OF_10.length + 1), FLOOR);
assertTrue(nextBigger.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0);
}
- @GwtIncompatible("TODO")
public void testConstantsSqrtMaxLong() {
assertEquals(LongMath.sqrt(Long.MAX_VALUE, FLOOR), LongMath.FLOOR_SQRT_MAX_LONG);
}
- @GwtIncompatible("TODO")
public void testConstantsFactorials() {
long expected = 1;
- for (int i = 0; i < LongMath.factorials.length; i++, expected *= i) {
- assertEquals(expected, LongMath.factorials[i]);
+ for (int i = 0; i < LongMath.FACTORIALS.length; i++, expected *= i) {
+ assertEquals(expected, LongMath.FACTORIALS[i]);
}
try {
LongMath.checkedMultiply(
- LongMath.factorials[LongMath.factorials.length - 1], LongMath.factorials.length);
+ LongMath.FACTORIALS[LongMath.FACTORIALS.length - 1], LongMath.FACTORIALS.length);
fail("Expected ArithmeticException");
} catch (ArithmeticException expect) {}
}
- @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)));
+ for (int k = 0; k < LongMath.BIGGEST_BINOMIALS.length; k++) {
+ assertTrue(fitsInLong(BigIntegerMath.binomial(LongMath.BIGGEST_BINOMIALS[k], k)));
+ assertTrue(LongMath.BIGGEST_BINOMIALS[k] == Integer.MAX_VALUE
+ || !fitsInLong(BigIntegerMath.binomial(LongMath.BIGGEST_BINOMIALS[k] + 1, k)));
// 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;
+ int k = LongMath.BIGGEST_BINOMIALS.length;
assertFalse(fitsInLong(BigIntegerMath.binomial(2 * k, k)));
// 2 * k is the smallest value for which we don't replace k with (n-k).
}
- @GwtIncompatible("TODO")
public void testConstantsBiggestSimpleBinomials() {
- for (int k = 0; k < LongMath.biggestSimpleBinomials.length; k++) {
- assertTrue(LongMath.biggestSimpleBinomials[k] <= LongMath.biggestBinomials[k]);
- simpleBinomial(LongMath.biggestSimpleBinomials[k], k); // mustn't throw
- if (LongMath.biggestSimpleBinomials[k] < Integer.MAX_VALUE) {
+ for (int k = 0; k < LongMath.BIGGEST_SIMPLE_BINOMIALS.length; k++) {
+ assertTrue(LongMath.BIGGEST_SIMPLE_BINOMIALS[k] <= LongMath.BIGGEST_BINOMIALS[k]);
+ simpleBinomial(LongMath.BIGGEST_SIMPLE_BINOMIALS[k], k); // mustn't throw
+ if (LongMath.BIGGEST_SIMPLE_BINOMIALS[k] < Integer.MAX_VALUE) {
// unless all n are fair game with this k
try {
- simpleBinomial(LongMath.biggestSimpleBinomials[k] + 1, k);
+ simpleBinomial(LongMath.BIGGEST_SIMPLE_BINOMIALS[k] + 1, k);
fail("Expected ArithmeticException");
} catch (ArithmeticException expected) {}
}
}
try {
- int k = LongMath.biggestSimpleBinomials.length;
+ int k = LongMath.BIGGEST_SIMPLE_BINOMIALS.length;
simpleBinomial(2 * k, k);
// 2 * k is the smallest value for which we don't replace k with (n-k).
fail("Expected ArithmeticException");
@@ -138,7 +119,6 @@ public class LongMathTest extends TestCase {
}
// Throws an ArithmeticException if "the simple implementation" of binomial coefficients overflows
- @GwtIncompatible("TODO")
private long simpleBinomial(int n, int k) {
long accum = 1;
for (int i = 0; i < k; i++) {
@@ -148,12 +128,10 @@ public class LongMathTest extends TestCase {
return accum;
}
- @GwtIncompatible("java.math.BigInteger")
public void testIsPowerOfTwo() {
for (long x : ALL_LONG_CANDIDATES) {
// Checks for a single bit set.
- BigInteger bigX = BigInteger.valueOf(x);
- boolean expected = (bigX.signum() > 0) && (bigX.bitCount() == 1);
+ boolean expected = x > 0 & (x & (x - 1)) == 0L;
assertEquals(expected, LongMath.isPowerOfTwo(x));
}
}
@@ -202,7 +180,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testLog10ZeroAlwaysThrows() {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
try {
@@ -212,7 +189,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testLog10NegativeAlwaysThrows() {
for (long x : NEGATIVE_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
@@ -225,7 +201,6 @@ public class LongMathTest extends TestCase {
}
// Relies on the correctness of BigIntegerMath.log10 for all modes except UNNECESSARY.
- @GwtIncompatible("TODO")
public void testLog10MatchesBigInteger() {
for (long x : POSITIVE_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
@@ -235,7 +210,6 @@ public class LongMathTest extends TestCase {
}
// Relies on the correctness of log10(long, FLOOR) and of pow(long, int).
- @GwtIncompatible("TODO")
public void testLog10Exact() {
for (long x : POSITIVE_LONG_CANDIDATES) {
int floor = LongMath.log10(x, FLOOR);
@@ -249,7 +223,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testLog10TrivialOnPowerOf10() {
long x = 1000000000000L;
for (RoundingMode mode : ALL_ROUNDING_MODES) {
@@ -257,7 +230,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testSqrtNegativeAlwaysThrows() {
for (long x : NEGATIVE_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
@@ -270,7 +242,6 @@ public class LongMathTest extends TestCase {
}
// Relies on the correctness of BigIntegerMath.sqrt for all modes except UNNECESSARY.
- @GwtIncompatible("TODO")
public void testSqrtMatchesBigInteger() {
for (long x : POSITIVE_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_SAFE_ROUNDING_MODES) {
@@ -282,7 +253,6 @@ public class LongMathTest extends TestCase {
}
/* Relies on the correctness of sqrt(long, FLOOR). */
- @GwtIncompatible("TODO")
public void testSqrtExactMatchesFloorOrThrows() {
for (long x : POSITIVE_LONG_CANDIDATES) {
long logFloor = LongMath.sqrt(x, FLOOR);
@@ -297,7 +267,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testPow() {
for (long i : ALL_LONG_CANDIDATES) {
for (int exp : EXPONENTS) {
@@ -308,7 +277,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testDivNonZero() {
for (long p : NONZERO_LONG_CANDIDATES) {
for (long q : NONZERO_LONG_CANDIDATES) {
@@ -321,7 +289,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testDivNonZeroExact() {
for (long p : NONZERO_LONG_CANDIDATES) {
for (long q : NONZERO_LONG_CANDIDATES) {
@@ -337,7 +304,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testZeroDivIsAlwaysZero() {
for (long q : NONZERO_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
@@ -346,7 +312,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testDivByZeroAlwaysFails() {
for (long p : ALL_LONG_CANDIDATES) {
for (RoundingMode mode : ALL_ROUNDING_MODES) {
@@ -358,7 +323,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testIntMod() {
for (long x : ALL_LONG_CANDIDATES) {
for (int m : POSITIVE_INTEGER_CANDIDATES) {
@@ -369,7 +333,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testIntModNegativeModulusFails() {
for (long x : ALL_LONG_CANDIDATES) {
for (int m : NEGATIVE_INTEGER_CANDIDATES) {
@@ -381,7 +344,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testIntModZeroModulusFails() {
for (long x : ALL_LONG_CANDIDATES) {
try {
@@ -391,7 +353,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testMod() {
for (long x : ALL_LONG_CANDIDATES) {
for (long m : POSITIVE_LONG_CANDIDATES) {
@@ -402,7 +363,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testModNegativeModulusFails() {
for (long x : ALL_LONG_CANDIDATES) {
for (long m : NEGATIVE_LONG_CANDIDATES) {
@@ -414,7 +374,7 @@ public class LongMathTest extends TestCase {
}
}
- public void testGCDExhaustive() {
+ public void testGCD() {
for (long a : POSITIVE_LONG_CANDIDATES) {
for (long b : POSITIVE_LONG_CANDIDATES) {
assertEquals(valueOf(a).gcd(valueOf(b)), valueOf(LongMath.gcd(a, b)));
@@ -422,7 +382,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testGCDZero() {
for (long a : POSITIVE_LONG_CANDIDATES) {
assertEquals(a, LongMath.gcd(a, 0));
@@ -431,7 +390,6 @@ public class LongMathTest extends TestCase {
assertEquals(0, LongMath.gcd(0, 0));
}
- @GwtIncompatible("TODO")
public void testGCDNegativePositiveThrows() {
for (long a : NEGATIVE_LONG_CANDIDATES) {
try {
@@ -445,7 +403,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testGCDNegativeZeroThrows() {
for (long a : NEGATIVE_LONG_CANDIDATES) {
try {
@@ -459,7 +416,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testCheckedAdd() {
for (long a : ALL_INTEGER_CANDIDATES) {
for (long b : ALL_INTEGER_CANDIDATES) {
@@ -475,7 +431,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testCheckedSubtract() {
for (long a : ALL_INTEGER_CANDIDATES) {
for (long b : ALL_INTEGER_CANDIDATES) {
@@ -491,7 +446,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testCheckedMultiply() {
for (long a : ALL_INTEGER_CANDIDATES) {
for (long b : ALL_INTEGER_CANDIDATES) {
@@ -507,7 +461,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testCheckedPow() {
for (long b : ALL_INTEGER_CANDIDATES) {
for (int exp : EXPONENTS) {
@@ -524,7 +477,6 @@ public class LongMathTest extends TestCase {
}
// Depends on the correctness of BigIntegerMath.factorial.
- @GwtIncompatible("TODO")
public void testFactorial() {
for (int n = 0; n <= 50; n++) {
BigInteger expectedBig = BigIntegerMath.factorial(n);
@@ -533,7 +485,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("TODO")
public void testFactorialNegative() {
for (int n : NEGATIVE_INTEGER_CANDIDATES) {
try {
@@ -554,17 +505,6 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("Slow")
- public void testBinomial_exhaustiveNotOverflowing() {
- // Tests all of the inputs to LongMath.binomial that won't cause it to overflow, that weren't
- // 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));
- }
- }
- }
-
public void testBinomialOutside() {
for (int n = 0; n <= 50; n++) {
try {
@@ -587,81 +527,13 @@ public class LongMathTest extends TestCase {
}
}
- @GwtIncompatible("java.math.BigInteger")
- public void testMean() {
- // Odd-sized ranges have an obvious mean
- assertMean(2, 1, 3);
-
- assertMean(-2, -3, -1);
- assertMean(0, -1, 1);
- assertMean(1, -1, 3);
- assertMean((1L << 62) - 1, -1, Long.MAX_VALUE);
-
- // Even-sized ranges should prefer the lower mean
- assertMean(2, 1, 4);
- assertMean(-3, -4, -1);
- assertMean(0, -1, 2);
- assertMean(0, Long.MIN_VALUE + 2, Long.MAX_VALUE);
- assertMean(0, 0, 1);
- assertMean(-1, -1, 0);
- assertMean(-1, Long.MIN_VALUE, Long.MAX_VALUE);
-
- // x == y == mean
- assertMean(1, 1, 1);
- assertMean(0, 0, 0);
- assertMean(-1, -1, -1);
- assertMean(Long.MIN_VALUE, Long.MIN_VALUE, Long.MIN_VALUE);
- assertMean(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE);
-
- // Exhaustive checks
- for (long x : ALL_LONG_CANDIDATES) {
- for (long y : ALL_LONG_CANDIDATES) {
- assertMean(x, y);
- }
- }
- }
-
- /**
- * 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));
- assertMean(x, y);
- }
-
- /**
- * Helper method that asserts the arithmetic mean of x and y is equal
- *to the result of computeMeanSafely.
- */
- 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));
- }
-
- /**
- * Computes the mean in a way that is obvious and resilient to
- * overflow by using BigInteger arithmetic.
- */
- private static long computeMeanSafely(long x, long y) {
- BigInteger bigX = BigInteger.valueOf(x);
- BigInteger bigY = BigInteger.valueOf(y);
- BigDecimal bigMean = new BigDecimal(bigX.add(bigY))
- .divide(BigDecimal.valueOf(2), BigDecimal.ROUND_FLOOR);
- // parseInt blows up on overflow as opposed to intValue() which does not.
- return Long.parseLong(bigMean.toString());
- }
-
- private static boolean fitsInLong(BigInteger big) {
+ private boolean fitsInLong(BigInteger big) {
return big.bitLength() <= 63;
}
- @GwtIncompatible("NullPointerTester")
- public void testNullPointers() {
+ public void testNullPointers() throws Exception {
NullPointerTester tester = new NullPointerTester();
+ tester.setDefault(RoundingMode.class, FLOOR);
tester.setDefault(int.class, 1);
tester.setDefault(long.class, 1L);
tester.testAllPublicStaticMethods(LongMath.class);