1 /*
2  * Copyright (c) 2009, 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 package test.java.math.BigDecimal;
24 
25 /*
26  * @test
27  * @bug 1234567
28  * @summary Test that precision() is computed properly.
29  * @author Joseph D. Darcy
30  */
31 
32 import java.math.*;
33 import static java.math.BigDecimal.*;
34 
35 import org.testng.Assert;
36 import org.testng.annotations.Test;
37 
38 // Android-changed: Replace error counting with asserts.
39 public class PrecisionTests {
40     private static BigDecimal NINE = valueOf(9);
41 
42     @Test
testPrecision()43     public void testPrecision() {
44         // Smallest and largest values of a given length
45         BigDecimal[] testValues = {
46             valueOf(1), valueOf(9),
47         };
48 
49         testPrecision(new BigDecimal(0), 1);
50 
51         for(int i = 1; i < 100; i++) {
52             for(BigDecimal bd : testValues) {
53                 testPrecision(bd, i);
54                 testPrecision(bd.negate(), i);
55             }
56 
57             testValues[0] = testValues[0].multiply(TEN);
58             testValues[1] = testValues[1].multiply(TEN).add(NINE);
59         }
60 
61         // The following test tries to cover testings for precision of long values
62         BigDecimal[] randomTestValues = {
63             valueOf(2147483648L),          // 2^31:       10 digits
64             valueOf(-2147483648L),         // -2^31:      10 digits
65             valueOf(98893745455L),         // random:     11 digits
66             valueOf(3455436789887L),       // random:     13 digits
67             valueOf(140737488355328L),     // 2^47:       15 digits
68             valueOf(-140737488355328L),    // -2^47:      15 digits
69             valueOf(7564232235739573L),    // random:     16 digits
70             valueOf(25335434990002322L),   // random:     17 digits
71             valueOf(9223372036854775807L), // 2^63 - 1:   19 digits
72             valueOf(-9223372036854775807L) // -2^63 + 1:  19 digits
73         };
74         // The array below contains the expected precision of the above numbers
75         int[] expectedPrecision = {10, 10, 11, 13, 15, 15, 16, 17, 19, 19};
76         for (int i = 0; i < randomTestValues.length; i++) {
77             testPrecision(randomTestValues[i], expectedPrecision[i]);
78         }
79     }
80 
testPrecision(BigDecimal bd, int expected)81     private static void testPrecision(BigDecimal bd, int expected) {
82         int precision = bd.precision();
83 
84         Assert.assertEquals(precision, expected,
85                 String.format("For (%s).precision expected %d, got %d%n", bd, expected, precision));
86     }
87 }
88