1 /* 2 * Copyright 2009 Google, Inc. 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 6582946 27 * @summary Test the primitive wrappers compare and compareTo methods 28 * @key randomness 29 */ 30 31 import java.util.Random; 32 33 public class Compare { 34 35 final Random rnd = new Random(); 36 37 boolean toBoolean(long x) { return x > 0; } 38 39 void compareAll(long x, long y) { 40 check(Double.compare(x, y) == 41 Double.valueOf(x).compareTo(Double.valueOf(y))); 42 check(Float.compare(x, y) == 43 Float.valueOf(x).compareTo(Float.valueOf(y))); 44 check(Long.compare(x, y) == 45 Long.valueOf(x).compareTo(Long.valueOf(y))); 46 check(Integer.compare((int) x, (int) y) == 47 Integer.valueOf((int) x).compareTo(Integer.valueOf((int) y))); 48 check(Short.compare((short) x, (short) y) == 49 Short.valueOf((short) x).compareTo(Short.valueOf((short) y))); 50 check(Character.compare((char) x, (char) y) == 51 Character.valueOf((char) x).compareTo(Character.valueOf((char) y))); 52 check(Byte.compare((byte) x, (byte) y) == 53 Byte.valueOf((byte) x).compareTo(Byte.valueOf((byte) y))); 54 check(Boolean.compare(toBoolean(x), toBoolean(y)) == 55 Boolean.valueOf(toBoolean(x)).compareTo(Boolean.valueOf(toBoolean(y)))); 56 57 check(Double.compare(x, y) == -Double.compare(y, x)); 58 check(Float.compare(x, y) == -Float.compare(y, x)); 59 check(Long.compare(x, y) == -Long.compare(y, x)); 60 check(Integer.compare((int) x, (int) y) == 61 -Integer.compare((int) y, (int) x)); 62 check(Short.compare((short) x, (short) y) == 63 -Short.compare((short) y, (short) x)); 64 check(Character.compare((char) x, (char) y) == 65 -Character.compare((char) y, (char) x)); 66 check(Byte.compare((byte) x, (byte) y) == 67 -Byte.compare((byte) y, (byte) x)); 68 69 equal(Long.compare(x, y), 70 x < y ? -1 : x > y ? 1 : 0); 71 72 { 73 int a = (int) x, b = (int) y; 74 equal(Integer.compare(a, b), 75 a < b ? -1 : a > b ? 1 : 0); 76 } 77 78 { 79 short a = (short) x, b = (short) y; 80 equal(Short.compare(a, b), 81 a - b); 82 } 83 84 { 85 char a = (char) x, b = (char) y; 86 equal(Character.compare(a, b), 87 a - b); 88 } 89 90 { 91 byte a = (byte) x, b = (byte) y; 92 equal(Byte.compare(a, b), 93 a - b); 94 } 95 96 { 97 boolean a = toBoolean(x), b = toBoolean(y); 98 equal(Boolean.compare(a, b), 99 a == b ? 0 : a ? 1 : -1); 100 } 101 } 102 103 void test(String args[]) throws Exception { 104 long[] longs = { 105 Long.MIN_VALUE, 106 Integer.MIN_VALUE, 107 Short.MIN_VALUE, 108 Character.MIN_VALUE, 109 Byte.MIN_VALUE, 110 -1, 0, 1, 111 Byte.MAX_VALUE, 112 Character.MAX_VALUE, 113 Short.MAX_VALUE, 114 Integer.MAX_VALUE, 115 Long.MAX_VALUE, 116 rnd.nextLong(), 117 rnd.nextInt(), 118 }; 119 120 for (long x : longs) { 121 for (long y : longs) { 122 compareAll(x, y); 123 } 124 } 125 } 126 127 //--------------------- Infrastructure --------------------------- 128 volatile int passed = 0, failed = 0; 129 void pass() {passed++;} 130 void fail() {failed++; Thread.dumpStack();} 131 void fail(String msg) {System.err.println(msg); fail();} 132 void unexpected(Throwable t) {failed++; t.printStackTrace();} 133 void check(boolean cond) {if (cond) pass(); else fail();} 134 void equal(Object x, Object y) { 135 if (x == null ? y == null : x.equals(y)) pass(); 136 else fail(x + " not equal to " + y);} 137 public static void main(String[] args) throws Throwable { 138 new Compare().instanceMain(args);} 139 public void instanceMain(String[] args) throws Throwable { 140 try {test(args);} catch (Throwable t) {unexpected(t);} 141 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 142 if (failed > 0) throw new AssertionError("Some tests failed");} 143 }