|
25 | 25 | import static com.google.common.math.MathTesting.NONZERO_LONG_CANDIDATES; |
26 | 26 | import static com.google.common.math.MathTesting.POSITIVE_INTEGER_CANDIDATES; |
27 | 27 | import static com.google.common.math.MathTesting.POSITIVE_LONG_CANDIDATES; |
| 28 | +import static com.google.common.truth.Truth.assertThat; |
28 | 29 | import static com.google.common.truth.Truth.assertWithMessage; |
29 | 30 | import static java.math.BigInteger.valueOf; |
| 31 | +import static java.math.RoundingMode.CEILING; |
| 32 | +import static java.math.RoundingMode.DOWN; |
30 | 33 | import static java.math.RoundingMode.FLOOR; |
| 34 | +import static java.math.RoundingMode.HALF_DOWN; |
| 35 | +import static java.math.RoundingMode.HALF_EVEN; |
| 36 | +import static java.math.RoundingMode.HALF_UP; |
31 | 37 | import static java.math.RoundingMode.UNNECESSARY; |
| 38 | +import static java.math.RoundingMode.UP; |
| 39 | +import static java.math.RoundingMode.values; |
32 | 40 |
|
33 | 41 | import com.google.common.annotations.GwtCompatible; |
34 | 42 | import com.google.common.annotations.GwtIncompatible; |
35 | 43 | import com.google.common.testing.NullPointerTester; |
36 | 44 | import java.math.BigDecimal; |
37 | 45 | import java.math.BigInteger; |
38 | 46 | import java.math.RoundingMode; |
| 47 | +import java.util.EnumMap; |
| 48 | +import java.util.EnumSet; |
| 49 | +import java.util.Map; |
39 | 50 | import java.util.Random; |
40 | 51 | import junit.framework.TestCase; |
41 | 52 |
|
@@ -949,6 +960,159 @@ public void testIsPrimeThrowsOnNegative() { |
949 | 960 | } |
950 | 961 | } |
951 | 962 |
|
| 963 | + @GwtIncompatible |
| 964 | + private static final class RoundToDoubleTester { |
| 965 | + private final long input; |
| 966 | + private final Map<RoundingMode, Double> expectedValues = new EnumMap<>(RoundingMode.class); |
| 967 | + private boolean unnecessaryShouldThrow = false; |
| 968 | + |
| 969 | + RoundToDoubleTester(long input) { |
| 970 | + this.input = input; |
| 971 | + } |
| 972 | + |
| 973 | + RoundToDoubleTester setExpectation(double expectedValue, RoundingMode... modes) { |
| 974 | + for (RoundingMode mode : modes) { |
| 975 | + Double previous = expectedValues.put(mode, expectedValue); |
| 976 | + if (previous != null) { |
| 977 | + throw new AssertionError(); |
| 978 | + } |
| 979 | + } |
| 980 | + return this; |
| 981 | + } |
| 982 | + |
| 983 | + public RoundToDoubleTester roundUnnecessaryShouldThrow() { |
| 984 | + unnecessaryShouldThrow = true; |
| 985 | + return this; |
| 986 | + } |
| 987 | + |
| 988 | + public void test() { |
| 989 | + assertThat(expectedValues.keySet()) |
| 990 | + .containsAtLeastElementsIn(EnumSet.complementOf(EnumSet.of(UNNECESSARY))); |
| 991 | + for (Map.Entry<RoundingMode, Double> entry : expectedValues.entrySet()) { |
| 992 | + RoundingMode mode = entry.getKey(); |
| 993 | + Double expectation = entry.getValue(); |
| 994 | + assertWithMessage("roundToDouble(" + input + ", " + mode + ")") |
| 995 | + .that(LongMath.roundToDouble(input, mode)) |
| 996 | + .isEqualTo(expectation); |
| 997 | + } |
| 998 | + |
| 999 | + if (!expectedValues.containsKey(UNNECESSARY)) { |
| 1000 | + assertWithMessage("Expected roundUnnecessaryShouldThrow call") |
| 1001 | + .that(unnecessaryShouldThrow) |
| 1002 | + .isTrue(); |
| 1003 | + try { |
| 1004 | + LongMath.roundToDouble(input, UNNECESSARY); |
| 1005 | + fail("Expected ArithmeticException for roundToDouble(" + input + ", UNNECESSARY)"); |
| 1006 | + } catch (ArithmeticException expected) { |
| 1007 | + // expected |
| 1008 | + } |
| 1009 | + } |
| 1010 | + } |
| 1011 | + } |
| 1012 | + |
| 1013 | + @GwtIncompatible |
| 1014 | + public void testRoundToDouble_zero() { |
| 1015 | + new RoundToDoubleTester(0).setExpectation(0.0, values()).test(); |
| 1016 | + } |
| 1017 | + |
| 1018 | + @GwtIncompatible |
| 1019 | + public void testRoundToDouble_smallPositive() { |
| 1020 | + new RoundToDoubleTester(16).setExpectation(16.0, values()).test(); |
| 1021 | + } |
| 1022 | + |
| 1023 | + @GwtIncompatible |
| 1024 | + public void testRoundToDouble_maxPreciselyRepresentable() { |
| 1025 | + new RoundToDoubleTester(1L << 53).setExpectation(Math.pow(2, 53), values()).test(); |
| 1026 | + } |
| 1027 | + |
| 1028 | + @GwtIncompatible |
| 1029 | + public void testRoundToDouble_maxPreciselyRepresentablePlusOne() { |
| 1030 | + double twoToThe53 = Math.pow(2, 53); |
| 1031 | + // the representable doubles are 2^53 and 2^53 + 2. |
| 1032 | + // 2^53+1 is halfway between, so HALF_UP will go up and HALF_DOWN will go down. |
| 1033 | + // 2^53 is "more even" -- it's a multiple of a larger power of two -- so HALF_EVEN goes to it. |
| 1034 | + new RoundToDoubleTester((1L << 53) + 1) |
| 1035 | + .setExpectation(twoToThe53, DOWN, FLOOR, HALF_DOWN, HALF_EVEN) |
| 1036 | + .setExpectation(Math.nextUp(twoToThe53), CEILING, UP, HALF_UP) |
| 1037 | + .roundUnnecessaryShouldThrow() |
| 1038 | + .test(); |
| 1039 | + } |
| 1040 | + |
| 1041 | + @GwtIncompatible |
| 1042 | + public void testRoundToDouble_twoToThe54PlusOne() { |
| 1043 | + double twoToThe54 = Math.pow(2, 54); |
| 1044 | + // the representable doubles are 2^54 and 2^54 + 4 |
| 1045 | + // 2^54+1 is less than halfway between, so HALF_* will all go down. |
| 1046 | + new RoundToDoubleTester((1L << 54) + 1) |
| 1047 | + .setExpectation(twoToThe54, DOWN, FLOOR, HALF_DOWN, HALF_UP, HALF_EVEN) |
| 1048 | + .setExpectation(Math.nextUp(twoToThe54), CEILING, UP) |
| 1049 | + .roundUnnecessaryShouldThrow() |
| 1050 | + .test(); |
| 1051 | + } |
| 1052 | + |
| 1053 | + @GwtIncompatible |
| 1054 | + public void testRoundToDouble_twoToThe54PlusThree() { |
| 1055 | + double twoToThe54 = Math.pow(2, 54); |
| 1056 | + // the representable doubles are 2^54 and 2^54 + 4 |
| 1057 | + // 2^54+3 is more than halfway between, so HALF_* will all go up. |
| 1058 | + new RoundToDoubleTester((1L << 54) + 3) |
| 1059 | + .setExpectation(twoToThe54, DOWN, FLOOR) |
| 1060 | + .setExpectation(Math.nextUp(twoToThe54), CEILING, UP, HALF_DOWN, HALF_UP, HALF_EVEN) |
| 1061 | + .roundUnnecessaryShouldThrow() |
| 1062 | + .test(); |
| 1063 | + } |
| 1064 | + |
| 1065 | + @GwtIncompatible |
| 1066 | + public void testRoundToDouble_twoToThe54PlusFour() { |
| 1067 | + new RoundToDoubleTester((1L << 54) + 4).setExpectation(Math.pow(2, 54) + 4, values()).test(); |
| 1068 | + } |
| 1069 | + |
| 1070 | + @GwtIncompatible |
| 1071 | + public void testRoundToDouble_smallNegative() { |
| 1072 | + new RoundToDoubleTester(-16).setExpectation(-16.0, values()).test(); |
| 1073 | + } |
| 1074 | + |
| 1075 | + @GwtIncompatible |
| 1076 | + public void testRoundToDouble_minPreciselyRepresentable() { |
| 1077 | + new RoundToDoubleTester(-1L << 53).setExpectation(-Math.pow(2, 53), values()).test(); |
| 1078 | + } |
| 1079 | + |
| 1080 | + @GwtIncompatible |
| 1081 | + public void testRoundToDouble_minPreciselyRepresentableMinusOne() { |
| 1082 | + // the representable doubles are -2^53 and -2^53 - 2. |
| 1083 | + // -2^53-1 is halfway between, so HALF_UP will go up and HALF_DOWN will go down. |
| 1084 | + // -2^53 is "more even" -- a multiple of a greater power of two -- so HALF_EVEN will go to it. |
| 1085 | + new RoundToDoubleTester((-1L << 53) - 1) |
| 1086 | + .setExpectation(-Math.pow(2, 53), DOWN, CEILING, HALF_DOWN, HALF_EVEN) |
| 1087 | + .setExpectation(DoubleUtils.nextDown(-Math.pow(2, 53)), FLOOR, UP, HALF_UP) |
| 1088 | + .roundUnnecessaryShouldThrow() |
| 1089 | + .test(); |
| 1090 | + } |
| 1091 | + |
| 1092 | + @GwtIncompatible |
| 1093 | + public void testRoundToDouble_negativeTwoToThe54MinusOne() { |
| 1094 | + new RoundToDoubleTester((-1L << 54) - 1) |
| 1095 | + .setExpectation(-Math.pow(2, 54), DOWN, CEILING, HALF_DOWN, HALF_UP, HALF_EVEN) |
| 1096 | + .setExpectation(DoubleUtils.nextDown(-Math.pow(2, 54)), FLOOR, UP) |
| 1097 | + .roundUnnecessaryShouldThrow() |
| 1098 | + .test(); |
| 1099 | + } |
| 1100 | + |
| 1101 | + @GwtIncompatible |
| 1102 | + public void testRoundToDouble_negativeTwoToThe54MinusThree() { |
| 1103 | + new RoundToDoubleTester((-1L << 54) - 3) |
| 1104 | + .setExpectation(-Math.pow(2, 54), DOWN, CEILING) |
| 1105 | + .setExpectation( |
| 1106 | + DoubleUtils.nextDown(-Math.pow(2, 54)), FLOOR, UP, HALF_DOWN, HALF_UP, HALF_EVEN) |
| 1107 | + .roundUnnecessaryShouldThrow() |
| 1108 | + .test(); |
| 1109 | + } |
| 1110 | + |
| 1111 | + @GwtIncompatible |
| 1112 | + public void testRoundToDouble_negativeTwoToThe54MinusFour() { |
| 1113 | + new RoundToDoubleTester((-1L << 54) - 4).setExpectation(-Math.pow(2, 54) - 4, values()).test(); |
| 1114 | + } |
| 1115 | + |
952 | 1116 | private static void failFormat(String template, Object... args) { |
953 | 1117 | assertWithMessage(template, args).fail(); |
954 | 1118 | } |
|
0 commit comments