ArithmeticUtils { public static int subAndCheck(int x, int y) throws MathArithmeticException { long s = (long)x - (long)y; if (s < Integer.MIN_VALUE || s > Integer.MAX_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, x, y); } return (int)s; } private  ArithmeticUtils(); static int addAndCheck(int x, int y); static long addAndCheck(long a, long b); @Deprecated static long binomialCoefficient(final int n, final int k); @Deprecated static double binomialCoefficientDouble(final int n, final int k); @Deprecated static double binomialCoefficientLog(final int n, final int k); @Deprecated static long factorial(final int n); @Deprecated static double factorialDouble(final int n); @Deprecated static double factorialLog(final int n); static int gcd(int p, int q); static long gcd(final long p, final long q); static int lcm(int a, int b); static long lcm(long a, long b); static int mulAndCheck(int x, int y); static long mulAndCheck(long a, long b); static int subAndCheck(int x, int y); static long subAndCheck(long a, long b); static int pow(final int k,
                          final int e); @Deprecated static int pow(final int k, long e); static long pow(final long k,
                           final int e); @Deprecated static long pow(final long k, long e); static BigInteger pow(final BigInteger k, int e); static BigInteger pow(final BigInteger k, long e); static BigInteger pow(final BigInteger k, BigInteger e); @Deprecated static long stirlingS2(final int n, final int k); static boolean isPowerOfTwo(long n);  }
@Test public void testSubAndCheck() { int big = Integer.MAX_VALUE; int bigNeg = Integer.MIN_VALUE; Assert.assertEquals(big, ArithmeticUtils.subAndCheck(big, 0)); Assert.assertEquals(bigNeg + 1, ArithmeticUtils.subAndCheck(bigNeg, -1)); Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(bigNeg, -big)); try { ArithmeticUtils.subAndCheck(big, -1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } try { ArithmeticUtils.subAndCheck(bigNeg, 1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { } }
@Test public void testSubAndCheckErrorMessage() { int big = Integer.MAX_VALUE; try { ArithmeticUtils.subAndCheck(big, -1); Assert.fail("Expecting MathArithmeticException"); } catch (MathArithmeticException ex) { Assert.assertTrue(ex.getMessage().length() > 1); } }
@Test public void testSubAndCheckLong() { long max = Long.MAX_VALUE; long min = Long.MIN_VALUE; Assert.assertEquals(max, ArithmeticUtils.subAndCheck(max, 0)); Assert.assertEquals(min, ArithmeticUtils.subAndCheck(min, 0)); Assert.assertEquals(-max, ArithmeticUtils.subAndCheck(0, max)); Assert.assertEquals(min + 1, ArithmeticUtils.subAndCheck(min, -1)); Assert.assertEquals(-1, ArithmeticUtils.subAndCheck(-max - 1, -max)); Assert.assertEquals(max, ArithmeticUtils.subAndCheck(-1, -1 - max)); testSubAndCheckLongFailure(0L, min); testSubAndCheckLongFailure(max, -1L); testSubAndCheckLongFailure(min, 1L); }