Skip to content

Commit 35387d5

Browse files
rgiuliettijddarcy
authored andcommitted
8289260: BigDecimal movePointLeft() and movePointRight() do not follow their API spec
Reviewed-by: darcy
1 parent 9f37ba4 commit 35387d5

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

src/java.base/share/classes/java/math/BigDecimal.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,7 @@ public BigDecimal setScale(int newScale) {
29932993
* @throws ArithmeticException if scale overflows.
29942994
*/
29952995
public BigDecimal movePointLeft(int n) {
2996-
if (n == 0) return this;
2996+
if (n == 0 && scale >= 0) return this;
29972997

29982998
// Cannot use movePointRight(-n) in case of n==Integer.MIN_VALUE
29992999
int newScale = checkScale((long)scale + n);
@@ -3017,7 +3017,7 @@ public BigDecimal movePointLeft(int n) {
30173017
* @throws ArithmeticException if scale overflows.
30183018
*/
30193019
public BigDecimal movePointRight(int n) {
3020-
if (n == 0) return this;
3020+
if (n == 0 && scale >= 0) return this;
30213021

30223022
// Cannot use movePointLeft(-n) in case of n==Integer.MIN_VALUE
30233023
int newScale = checkScale((long)scale - n);
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright (c) 2022, 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 8289260
27+
*/
28+
29+
import java.math.BigDecimal;
30+
31+
public class MovePointTests {
32+
33+
public static void main(String argv[]) {
34+
BigDecimal bd;
35+
36+
bd = BigDecimal.valueOf(1, -3);
37+
checkNotIdentical(bd, bd.movePointLeft(0));
38+
checkNotIdentical(bd, bd.movePointRight(0));
39+
40+
bd = BigDecimal.valueOf(1, 0);
41+
checkIdentical(bd, bd.movePointLeft(0));
42+
checkIdentical(bd, bd.movePointRight(0));
43+
44+
bd = BigDecimal.valueOf(1, 3);
45+
checkIdentical(bd, bd.movePointLeft(0));
46+
checkIdentical(bd, bd.movePointRight(0));
47+
48+
bd = BigDecimal.valueOf(1, -3);
49+
checkNotEquals(bd, bd.movePointLeft(1));
50+
checkNotEquals(bd, bd.movePointLeft(-1));
51+
checkNotEquals(bd, bd.movePointRight(1));
52+
checkNotEquals(bd, bd.movePointRight(-1));
53+
54+
bd = BigDecimal.valueOf(1, 0);
55+
checkNotEquals(bd, bd.movePointLeft(1));
56+
checkNotEquals(bd, bd.movePointLeft(-1));
57+
checkNotEquals(bd, bd.movePointRight(1));
58+
checkNotEquals(bd, bd.movePointRight(-1));
59+
60+
bd = BigDecimal.valueOf(1, 3);
61+
checkNotEquals(bd, bd.movePointLeft(1));
62+
checkNotEquals(bd, bd.movePointLeft(-1));
63+
checkNotEquals(bd, bd.movePointRight(1));
64+
checkNotEquals(bd, bd.movePointRight(-1));
65+
66+
bd = BigDecimal.valueOf(1, -3);
67+
checkNotEquals(bd, bd.movePointLeft(10));
68+
checkNotEquals(bd, bd.movePointLeft(-10));
69+
checkNotEquals(bd, bd.movePointRight(10));
70+
checkNotEquals(bd, bd.movePointRight(-10));
71+
72+
bd = BigDecimal.valueOf(1, 0);
73+
checkNotEquals(bd, bd.movePointLeft(10));
74+
checkNotEquals(bd, bd.movePointLeft(-10));
75+
checkNotEquals(bd, bd.movePointRight(10));
76+
checkNotEquals(bd, bd.movePointRight(-10));
77+
78+
bd = BigDecimal.valueOf(1, 3);
79+
checkNotEquals(bd, bd.movePointLeft(10));
80+
checkNotEquals(bd, bd.movePointLeft(-10));
81+
checkNotEquals(bd, bd.movePointRight(10));
82+
checkNotEquals(bd, bd.movePointRight(-10));
83+
}
84+
85+
private static void checkIdentical(BigDecimal bd, BigDecimal res) {
86+
if (res != bd) { // intentionally !=
87+
throw new RuntimeException("Unexpected result " +
88+
bd + " != " + res);
89+
}
90+
}
91+
92+
private static void checkNotIdentical(BigDecimal bd, BigDecimal res) {
93+
if (res == bd) { // intentionally ==
94+
throw new RuntimeException("Unexpected result " +
95+
bd + " == " + res);
96+
}
97+
}
98+
99+
private static void checkNotEquals(BigDecimal bd, BigDecimal res) {
100+
if (res.equals(bd)) {
101+
throw new RuntimeException("Unexpected result " +
102+
bd + ".equals(" + res + ")");
103+
}
104+
}
105+
106+
}

0 commit comments

Comments
 (0)