1 /*
2  * Copyright (c) 2005, 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 6325535
28  * @summary Test for the rounding behavior of negate(MathContext)
29  * @author Joseph D. Darcy
30  */
31 
32 import java.math.*;
33 
34 import org.testng.Assert;
35 import org.testng.annotations.Test;
36 
37 // Android-changed: Replace error counting with asserts.
38 public class NegateTests {
39 
negateThenRound(BigDecimal bd, MathContext mc)40     static BigDecimal negateThenRound(BigDecimal bd, MathContext mc) {
41         return bd.negate().plus(mc);
42     }
43 
44 
absThenRound(BigDecimal bd, MathContext mc)45     static BigDecimal absThenRound(BigDecimal bd, MathContext mc) {
46         return bd.abs().plus(mc);
47     }
48 
49 
negateTest(BigDecimal[][] testCases, MathContext mc)50     static void negateTest(BigDecimal[][] testCases,  MathContext mc) {
51         for (BigDecimal [] testCase : testCases) {
52 
53             BigDecimal bd = testCase[0];
54             BigDecimal neg1 = bd.negate(mc);
55             BigDecimal neg2 = negateThenRound(bd, mc);
56             BigDecimal expected = testCase[1];
57 
58             Assert.assertEquals(neg1, expected, "(" + bd + ").negate(" + mc + ") => " +
59                                    neg1 + " != expected " + expected);
60 
61             Assert.assertEquals(neg1, neg2, "(" + bd + ").negate(" + mc + ")  => " +
62                                    neg1 + " != ntr " + neg2);
63 
64             // Test abs consistency
65             BigDecimal abs = bd.abs(mc);
66             BigDecimal expectedAbs = absThenRound(bd,mc);
67             Assert.assertEquals(abs, expectedAbs, "(" + bd + ").abs(" + mc + ")  => " +
68                                    abs + " != atr " +  expectedAbs);
69         }
70     }
71 
72     @Test
negateTests()73     public void negateTests() {
74         BigDecimal [][] testCasesCeiling = {
75             {new BigDecimal("1.3"),     new BigDecimal("-1")},
76             {new BigDecimal("-1.3"),    new BigDecimal("2")},
77         };
78 
79         negateTest(testCasesCeiling, new MathContext(1, RoundingMode.CEILING));
80 
81         BigDecimal [][] testCasesFloor = {
82             {new BigDecimal("1.3"),     new BigDecimal("-2")},
83             {new BigDecimal("-1.3"),    new BigDecimal("1")},
84         };
85 
86         negateTest(testCasesFloor, new MathContext(1, RoundingMode.FLOOR));
87     }
88 }
89