1 /*
2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8211936
27  * @summary Tests of BigDecimal.intValueExact
28  */
29 package test.java.math.BigDecimal;
30 
31 import java.math.*;
32 import java.util.List;
33 import java.util.Map;
34 import static java.util.Map.entry;
35 
36 public class IntValueExactTests {
main(String... args)37     public static void main(String... args) {
38         int failures = 0;
39 
40         failures += intValueExactSuccessful();
41         failures += intValueExactExceptional();
42 
43         if (failures > 0) {
44             throw new RuntimeException("Incurred " + failures +
45                                        " failures while testing intValueExact.");
46         }
47     }
48 
simpleIntValueExact(BigDecimal bd)49     private static int simpleIntValueExact(BigDecimal bd) {
50         return bd.toBigIntegerExact().intValue();
51     }
52 
intValueExactSuccessful()53     private static int intValueExactSuccessful() {
54         int failures = 0;
55 
56         // Strings used to create BigDecimal instances on which invoking
57         // intValueExact() will succeed.
58         Map<BigDecimal, Integer> successCases =
59             Map.ofEntries(entry(new BigDecimal("2147483647"),    Integer.MAX_VALUE), // 2^31 -1
60                           entry(new BigDecimal("2147483647.0"),  Integer.MAX_VALUE),
61                           entry(new BigDecimal("2147483647.00"), Integer.MAX_VALUE),
62 
63                           entry(new BigDecimal("-2147483648"),   Integer.MIN_VALUE), // -2^31
64                           entry(new BigDecimal("-2147483648.0"), Integer.MIN_VALUE),
65                           entry(new BigDecimal("-2147483648.00"),Integer.MIN_VALUE),
66 
67                           entry(new BigDecimal("1e0"),    1),
68                           entry(new BigDecimal(BigInteger.ONE, -9),   1_000_000_000),
69 
70                           entry(new BigDecimal("0e13"),   0), // Fast path zero
71                           entry(new BigDecimal("0e32"),   0),
72                           entry(new BigDecimal("0e512"), 0),
73 
74                           entry(new BigDecimal("10.000000000000000000000000000000000"), 10));
75 
76         for (var testCase : successCases.entrySet()) {
77             BigDecimal bd = testCase.getKey();
78             int expected = testCase.getValue();
79             try {
80                 int intValueExact = bd.intValueExact();
81                 if (expected != intValueExact ||
82                     intValueExact != simpleIntValueExact(bd)) {
83                     failures++;
84                     System.err.println("Unexpected intValueExact result " + intValueExact +
85                                        " on " + bd);
86                 }
87             } catch (Exception e) {
88                 failures++;
89                 System.err.println("Error on " + bd + "\tException message:" + e.getMessage());
90             }
91         }
92         return failures;
93     }
94 
intValueExactExceptional()95     private static int intValueExactExceptional() {
96         int failures = 0;
97         List<BigDecimal> exceptionalCases =
98             List.of(new BigDecimal("2147483648"), // Integer.MAX_VALUE + 1
99                     new BigDecimal("2147483648.0"),
100                     new BigDecimal("2147483648.00"),
101                     new BigDecimal("-2147483649"), // Integer.MIN_VALUE - 1
102                     new BigDecimal("-2147483649.1"),
103                     new BigDecimal("-2147483649.01"),
104 
105                     new BigDecimal("9999999999999999999999999999999"),
106                     new BigDecimal("10000000000000000000000000000000"),
107 
108                     new BigDecimal("0.99"),
109                     new BigDecimal("0.999999999999999999999"));
110 
111         for (BigDecimal bd : exceptionalCases) {
112             try {
113                 int intValueExact = bd.intValueExact();
114                 failures++;
115                 System.err.println("Unexpected non-exceptional intValueExact on " + bd);
116             } catch (ArithmeticException e) {
117                 // Success;
118             }
119         }
120         return failures;
121     }
122 }
123