FastMath { public static int decrementExact(final int n) throws MathArithmeticException { if (n == Integer.MIN_VALUE) { throw new MathArithmeticException(LocalizedFormats.OVERFLOW_IN_SUBTRACTION, n, 1); } return n - 1; } private  FastMath(); static double sqrt(final double a); static double cosh(double x); static double sinh(double x); static double tanh(double x); static double acosh(final double a); static double asinh(double a); static double atanh(double a); static double signum(final double a); static float signum(final float a); static double nextUp(final double a); static float nextUp(final float a); static double nextDown(final double a); static float nextDown(final float a); static double random(); static double exp(double x); static double expm1(double x); static double log(final double x); static double log1p(final double x); static double log10(final double x); static double log(double base, double x); static double pow(double x, double y); static double pow(double d, int e); static double sin(double x); static double cos(double x); static double tan(double x); static double atan(double x); static double atan2(double y, double x); static double asin(double x); static double acos(double x); static double cbrt(double x); static double toRadians(double x); static double toDegrees(double x); static int abs(final int x); static long abs(final long x); static float abs(final float x); static double abs(double x); static double ulp(double x); static float ulp(float x); static double scalb(final double d, final int n); static float scalb(final float f, final int n); static double nextAfter(double d, double direction); static float nextAfter(final float f, final double direction); static double floor(double x); static double ceil(double x); static double rint(double x); static long round(double x); static int round(final float x); static int min(final int a, final int b); static long min(final long a, final long b); static float min(final float a, final float b); static double min(final double a, final double b); static int max(final int a, final int b); static long max(final long a, final long b); static float max(final float a, final float b); static double max(final double a, final double b); static double hypot(final double x, final double y); static double IEEEremainder(double dividend, double divisor); static int toIntExact(final long n); static int incrementExact(final int n); static long incrementExact(final long n); static int decrementExact(final int n); static long decrementExact(final long n); static int addExact(final int a, final int b); static long addExact(final long a, final long b); static int subtractExact(final int a, final int b); static long subtractExact(final long a, final long b); static int multiplyExact(final int a, final int b); static long multiplyExact(final long a, final long b); static int floorDiv(final int a, final int b); static long floorDiv(final long a, final long b); static int floorMod(final int a, final int b); static long floorMod(final long a, final long b); static double copySign(double magnitude, double sign); static float copySign(float magnitude, float sign); static int getExponent(final double d); static int getExponent(final float f); static void main(String[] a); static final double PI; static final double E; }
@Test public void testDecrementExactInt() { int[] specialValues = new int[] { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MIN_VALUE + 2, Integer.MAX_VALUE, Integer.MAX_VALUE - 1, Integer.MAX_VALUE - 2, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -1 - (Integer.MIN_VALUE / 2), 0 - (Integer.MIN_VALUE / 2), 1 - (Integer.MIN_VALUE / 2), -1 + (Integer.MAX_VALUE / 2), 0 + (Integer.MAX_VALUE / 2), 1 + (Integer.MAX_VALUE / 2), }; for (int a : specialValues) { BigInteger bdA = BigInteger.valueOf(a); BigInteger bdSub = bdA.subtract(BigInteger.ONE); if (bdSub.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 || bdSub.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) { try { FastMath.decrementExact(a); Assert.fail("an exception should have been thrown"); } catch (MathArithmeticException mae) { } } else { Assert.assertEquals(bdSub, BigInteger.valueOf(FastMath.decrementExact(a))); } } }