1 /*
2  * Copyright (c) 2011, 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 6371401
27  * @summary Tests of fooValueExact methods
28  * @author Joseph D. Darcy
29  */
30 package test.java.math.BigInteger;
31 
32 import java.math.BigInteger;
33 
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
36 
37 // Android-changed: Replace error printing with asserts.
38 public class TestValueExact {
39 
40     @Test
testLongValueExact()41     public void testLongValueExact() {
42         BigInteger[] inRange = {
43             BigInteger.valueOf(Long.MIN_VALUE),
44             BigInteger.ZERO,
45             BigInteger.valueOf(Long.MAX_VALUE)
46         };
47 
48         BigInteger[] outOfRange = {
49             BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE),
50             BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE)
51         };
52 
53         for (BigInteger bi : inRange) {
54             Assert.assertEquals(bi.longValueExact(), bi.longValue(),
55                     "Mismatching int conversion for " + bi);
56         }
57 
58         for (BigInteger bi : outOfRange) {
59             try {
60                 long value = bi.longValueExact();
61                 Assert.fail("Failed to get expected exception on " + bi + " got " + value);
62             } catch(ArithmeticException ae) {
63                 ; // Expected
64             }
65         }
66     }
67 
68     @Test
testIntValueExact()69     public void testIntValueExact() {
70         BigInteger[] inRange = {
71             BigInteger.valueOf(Integer.MIN_VALUE),
72             BigInteger.ZERO,
73             BigInteger.ONE,
74             BigInteger.TEN,
75             BigInteger.valueOf(Integer.MAX_VALUE)
76         };
77 
78         BigInteger[] outOfRange = {
79             BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
80             BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
81         };
82 
83         for (BigInteger bi : inRange) {
84             Assert.assertEquals(bi.intValueExact(), bi.intValue(),
85                     "Mismatching int conversion for " + bi);
86         }
87 
88         for (BigInteger bi : outOfRange) {
89             try {
90                 int value = bi.intValueExact();
91                 Assert.fail("Failed to get expected exception on " + bi + " got " + value);
92             } catch(ArithmeticException ae) {
93                 ; // Expected
94             }
95         }
96     }
97 
98     @Test
testShortValueExact()99     public void testShortValueExact() {
100         BigInteger[] inRange = {
101             BigInteger.valueOf(Short.MIN_VALUE),
102             BigInteger.ZERO,
103             BigInteger.ONE,
104             BigInteger.TEN,
105             BigInteger.valueOf(Short.MAX_VALUE)
106         };
107 
108         BigInteger[] outOfRange = {
109             BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
110             BigInteger.valueOf((long)Integer.MIN_VALUE),
111             BigInteger.valueOf(   (int)Short.MIN_VALUE - 1),
112             BigInteger.valueOf(   (int)Short.MAX_VALUE + 1),
113             BigInteger.valueOf((long)Integer.MAX_VALUE),
114             BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
115         };
116 
117         for (BigInteger bi : inRange) {
118             Assert.assertEquals(bi.shortValueExact(), bi.intValue(),
119                     "Mismatching int conversion for " + bi);
120         }
121 
122         for (BigInteger bi : outOfRange) {
123             try {
124                 int value = bi.shortValueExact();
125                 Assert.fail("Failed to get expected exception on " + bi + " got " + value);
126             } catch(ArithmeticException ae) {
127                 ; // Expected
128             }
129         }
130     }
131 
132     @Test
testByteValueExact()133     public void testByteValueExact() {
134         BigInteger[] inRange = {
135             BigInteger.valueOf(Byte.MIN_VALUE),
136             BigInteger.valueOf(0),
137             BigInteger.ONE,
138             BigInteger.TEN,
139             BigInteger.valueOf(Byte.MAX_VALUE)
140         };
141 
142         BigInteger[] outOfRange = {
143             BigInteger.valueOf((long)Integer.MIN_VALUE - 1),
144             BigInteger.valueOf((long)Integer.MIN_VALUE),
145             BigInteger.valueOf(   (int)Short.MIN_VALUE - 1),
146             BigInteger.valueOf(   (int)Short.MIN_VALUE),
147             BigInteger.valueOf(    (int)Byte.MIN_VALUE - 1),
148             BigInteger.valueOf(    (int)Byte.MAX_VALUE + 1),
149             BigInteger.valueOf(   (int)Short.MAX_VALUE + 1),
150             BigInteger.valueOf(   (int)Short.MAX_VALUE),
151             BigInteger.valueOf((long)Integer.MAX_VALUE),
152             BigInteger.valueOf((long)Integer.MAX_VALUE + 1)
153         };
154 
155         for (BigInteger bi : inRange) {
156             Assert.assertEquals(bi.byteValueExact(), bi.intValue(),
157                     "Mismatching int conversion for " + bi);
158         }
159 
160         for (BigInteger bi : outOfRange) {
161             try {
162                 int value = bi.byteValueExact();
163                 Assert.fail("Failed to get expected exception on " + bi + " got " + value);
164             } catch(ArithmeticException ae) {
165                 ; // Expected
166             }
167         }
168     }
169 }
170