author | jbachorik |
Tue, 21 Jan 2014 13:04:55 +0100 | |
changeset 22353 | d09e3ff5fd63 |
parent 5506 | 202f599c92aa |
child 30046 | cf2c86e1819e |
permissions | -rw-r--r-- |
3943
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
1 |
/* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
2 |
* Copyright 2009 Google, Inc. All Rights Reserved. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
4 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
8 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
13 |
* accompanied this code). |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
14 |
* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
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. |
|
3943
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
22 |
*/ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
23 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
24 |
/* |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
25 |
* @test |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
26 |
* @bug 6582946 |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
27 |
* @summary Test the primitive wrappers compare and compareTo methods |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
28 |
*/ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
29 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
30 |
import java.util.Random; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
31 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
32 |
public class Compare { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
33 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
34 |
final Random rnd = new Random(); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
35 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
36 |
boolean toBoolean(long x) { return x > 0; } |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
37 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
38 |
void compareAll(long x, long y) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
39 |
check(Double.compare(x, y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
40 |
Double.valueOf(x).compareTo(Double.valueOf(y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
41 |
check(Float.compare(x, y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
42 |
Float.valueOf(x).compareTo(Float.valueOf(y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
43 |
check(Long.compare(x, y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
44 |
Long.valueOf(x).compareTo(Long.valueOf(y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
45 |
check(Integer.compare((int) x, (int) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
46 |
Integer.valueOf((int) x).compareTo(Integer.valueOf((int) y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
47 |
check(Short.compare((short) x, (short) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
48 |
Short.valueOf((short) x).compareTo(Short.valueOf((short) y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
49 |
check(Character.compare((char) x, (char) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
50 |
Character.valueOf((char) x).compareTo(Character.valueOf((char) y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
51 |
check(Byte.compare((byte) x, (byte) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
52 |
Byte.valueOf((byte) x).compareTo(Byte.valueOf((byte) y))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
53 |
check(Boolean.compare(toBoolean(x), toBoolean(y)) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
54 |
Boolean.valueOf(toBoolean(x)).compareTo(Boolean.valueOf(toBoolean(y)))); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
55 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
56 |
check(Double.compare(x, y) == -Double.compare(y, x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
57 |
check(Float.compare(x, y) == -Float.compare(y, x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
58 |
check(Long.compare(x, y) == -Long.compare(y, x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
59 |
check(Integer.compare((int) x, (int) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
60 |
-Integer.compare((int) y, (int) x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
61 |
check(Short.compare((short) x, (short) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
62 |
-Short.compare((short) y, (short) x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
63 |
check(Character.compare((char) x, (char) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
64 |
-Character.compare((char) y, (char) x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
65 |
check(Byte.compare((byte) x, (byte) y) == |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
66 |
-Byte.compare((byte) y, (byte) x)); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
67 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
68 |
equal(Long.compare(x, y), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
69 |
x < y ? -1 : x > y ? 1 : 0); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
70 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
71 |
{ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
72 |
int a = (int) x, b = (int) y; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
73 |
equal(Integer.compare(a, b), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
74 |
a < b ? -1 : a > b ? 1 : 0); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
75 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
76 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
77 |
{ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
78 |
short a = (short) x, b = (short) y; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
79 |
equal(Short.compare(a, b), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
80 |
a - b); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
81 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
82 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
83 |
{ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
84 |
char a = (char) x, b = (char) y; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
85 |
equal(Character.compare(a, b), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
86 |
a - b); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
87 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
88 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
89 |
{ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
90 |
byte a = (byte) x, b = (byte) y; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
91 |
equal(Byte.compare(a, b), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
92 |
a - b); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
93 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
94 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
95 |
{ |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
96 |
boolean a = toBoolean(x), b = toBoolean(y); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
97 |
equal(Boolean.compare(a, b), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
98 |
a == b ? 0 : a ? 1 : -1); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
99 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
100 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
101 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
102 |
void test(String args[]) throws Exception { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
103 |
long[] longs = { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
104 |
Long.MIN_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
105 |
Integer.MIN_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
106 |
Short.MIN_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
107 |
Character.MIN_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
108 |
Byte.MIN_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
109 |
-1, 0, 1, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
110 |
Byte.MAX_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
111 |
Character.MAX_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
112 |
Short.MAX_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
113 |
Integer.MAX_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
114 |
Long.MAX_VALUE, |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
115 |
rnd.nextLong(), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
116 |
rnd.nextInt(), |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
117 |
}; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
118 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
119 |
for (long x : longs) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
120 |
for (long y : longs) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
121 |
compareAll(x, y); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
122 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
123 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
124 |
} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
125 |
|
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
126 |
//--------------------- Infrastructure --------------------------- |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
127 |
volatile int passed = 0, failed = 0; |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
128 |
void pass() {passed++;} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
129 |
void fail() {failed++; Thread.dumpStack();} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
130 |
void fail(String msg) {System.err.println(msg); fail();} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
131 |
void unexpected(Throwable t) {failed++; t.printStackTrace();} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
132 |
void check(boolean cond) {if (cond) pass(); else fail();} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
133 |
void equal(Object x, Object y) { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
134 |
if (x == null ? y == null : x.equals(y)) pass(); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
135 |
else fail(x + " not equal to " + y);} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
136 |
public static void main(String[] args) throws Throwable { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
137 |
new Compare().instanceMain(args);} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
138 |
public void instanceMain(String[] args) throws Throwable { |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
139 |
try {test(args);} catch (Throwable t) {unexpected(t);} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
140 |
System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
141 |
if (failed > 0) throw new AssertionError("Some tests failed");} |
11abf5578222
6582946: Add suite of compare(T, T) methods for ints, longs etc
martin
parents:
diff
changeset
|
142 |
} |