author | malenkov |
Fri, 16 May 2014 15:51:57 +0400 | |
changeset 25088 | 8d4b058368f0 |
parent 23010 | 6dadb192ad81 |
permissions | -rw-r--r-- |
1826 | 1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
19198
diff
changeset
|
2 |
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved. |
1826 | 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 |
* |
|
5506 | 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. |
|
1826 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @bug 6473768 |
|
27 |
* @summary Tests of BigDecimal.compareTo |
|
28 |
* @author Joseph D. Darcy |
|
29 |
*/ |
|
30 |
import java.math.*; |
|
31 |
import static java.math.BigDecimal.*; |
|
32 |
||
33 |
public class CompareToTests { |
|
34 |
private static int compareToTests() { |
|
35 |
int failures = 0; |
|
36 |
||
37 |
final BigDecimal MINUS_ONE = BigDecimal.ONE.negate(); |
|
38 |
||
39 |
// First operand, second operand, expected compareTo result |
|
40 |
BigDecimal [][] testCases = { |
|
41 |
// Basics |
|
42 |
{valueOf(0), valueOf(0), ZERO}, |
|
43 |
{valueOf(0), valueOf(1), MINUS_ONE}, |
|
44 |
{valueOf(1), valueOf(2), MINUS_ONE}, |
|
45 |
{valueOf(2), valueOf(1), ONE}, |
|
46 |
{valueOf(10), valueOf(10), ZERO}, |
|
47 |
||
48 |
// Significands would compare differently than scaled value |
|
49 |
{valueOf(2,1), valueOf(2), MINUS_ONE}, |
|
50 |
{valueOf(2,-1), valueOf(2), ONE}, |
|
51 |
{valueOf(1,1), valueOf(2), MINUS_ONE}, |
|
52 |
{valueOf(1,-1), valueOf(2), ONE}, |
|
53 |
{valueOf(5,-1), valueOf(2), ONE}, |
|
54 |
||
55 |
// Boundary and near boundary values |
|
19198
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
56 |
{valueOf(Long.MAX_VALUE), valueOf(Long.MAX_VALUE), ZERO}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
57 |
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
58 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
59 |
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MAX_VALUE), MINUS_ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
60 |
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MAX_VALUE), MINUS_ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
61 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
62 |
{valueOf(Long.MIN_VALUE), valueOf(Long.MAX_VALUE), MINUS_ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
63 |
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MAX_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
64 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
65 |
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MAX_VALUE), MINUS_ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
66 |
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MAX_VALUE), ZERO}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
67 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
68 |
{valueOf(Long.MAX_VALUE), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
69 |
{valueOf(Long.MAX_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
70 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
71 |
{valueOf(Long.MAX_VALUE-1), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
72 |
{valueOf(Long.MAX_VALUE-1).negate(), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
73 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
74 |
{valueOf(Long.MIN_VALUE), valueOf(Long.MIN_VALUE), ZERO}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
75 |
{valueOf(Long.MIN_VALUE).negate(), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
76 |
|
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
77 |
{valueOf(Long.MIN_VALUE+1), valueOf(Long.MIN_VALUE), ONE}, |
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
78 |
{valueOf(Long.MIN_VALUE+1).negate(), valueOf(Long.MIN_VALUE), ONE}, |
1826 | 79 |
}; |
80 |
||
81 |
for (BigDecimal[] testCase : testCases) { |
|
82 |
BigDecimal a = testCase[0]; |
|
83 |
BigDecimal a_negate = a.negate(); |
|
84 |
BigDecimal b = testCase[1]; |
|
85 |
BigDecimal b_negate = b.negate(); |
|
86 |
int expected = testCase[2].intValue(); |
|
87 |
||
88 |
failures += compareToTest(a, b, expected); |
|
89 |
failures += compareToTest(a_negate, b_negate, -expected); |
|
90 |
} |
|
91 |
||
92 |
||
93 |
return failures; |
|
94 |
} |
|
95 |
||
96 |
private static int compareToTest(BigDecimal a, BigDecimal b, int expected) { |
|
97 |
int result = a.compareTo(b); |
|
98 |
int failed = (result==expected) ? 0 : 1; |
|
19198
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
99 |
if (failed == 1) { |
1826 | 100 |
System.err.println("(" + a + ").compareTo(" + b + ") => " + result + |
101 |
"\n\tExpected " + expected); |
|
102 |
} |
|
19198
513b533ed31a
8022094: BigDecimal/CompareToTests and BigInteger/CompareToTests are incorrect
bpb
parents:
5506
diff
changeset
|
103 |
return failed; |
1826 | 104 |
} |
105 |
||
106 |
public static void main(String argv[]) { |
|
107 |
int failures = 0; |
|
108 |
||
109 |
failures += compareToTests(); |
|
110 |
||
111 |
if (failures > 0) { |
|
112 |
throw new RuntimeException("Incurred " + failures + |
|
113 |
" failures while testing exact compareTo."); |
|
114 |
} |
|
115 |
} |
|
116 |
} |