1701N/A/*
1701N/A * Copyright 2009 Google, Inc. All Rights Reserved.
1701N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1701N/A *
1701N/A * This code is free software; you can redistribute it and/or modify it
1701N/A * under the terms of the GNU General Public License version 2 only, as
1701N/A * published by the Free Software Foundation.
1701N/A *
1701N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1701N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1701N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1701N/A * version 2 for more details (a copy is included in the LICENSE file that
1701N/A * accompanied this code).
1701N/A *
1701N/A * You should have received a copy of the GNU General Public License version
1701N/A * 2 along with this work; if not, write to the Free Software Foundation,
1701N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1701N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1701N/A */
1701N/A
1701N/A/*
1701N/A * @test
1701N/A * @bug 6582946
1701N/A * @summary Test the primitive wrappers compare and compareTo methods
1701N/A */
1701N/A
1701N/Aimport java.util.Random;
1701N/A
1701N/Apublic class Compare {
1701N/A
1701N/A final Random rnd = new Random();
1701N/A
1701N/A boolean toBoolean(long x) { return x > 0; }
1701N/A
1701N/A void compareAll(long x, long y) {
1701N/A check(Double.compare(x, y) ==
1701N/A Double.valueOf(x).compareTo(Double.valueOf(y)));
1701N/A check(Float.compare(x, y) ==
1701N/A Float.valueOf(x).compareTo(Float.valueOf(y)));
1701N/A check(Long.compare(x, y) ==
1701N/A Long.valueOf(x).compareTo(Long.valueOf(y)));
1701N/A check(Integer.compare((int) x, (int) y) ==
1701N/A Integer.valueOf((int) x).compareTo(Integer.valueOf((int) y)));
1701N/A check(Short.compare((short) x, (short) y) ==
1701N/A Short.valueOf((short) x).compareTo(Short.valueOf((short) y)));
1701N/A check(Character.compare((char) x, (char) y) ==
1701N/A Character.valueOf((char) x).compareTo(Character.valueOf((char) y)));
1701N/A check(Byte.compare((byte) x, (byte) y) ==
1701N/A Byte.valueOf((byte) x).compareTo(Byte.valueOf((byte) y)));
1701N/A check(Boolean.compare(toBoolean(x), toBoolean(y)) ==
1701N/A Boolean.valueOf(toBoolean(x)).compareTo(Boolean.valueOf(toBoolean(y))));
1701N/A
1701N/A check(Double.compare(x, y) == -Double.compare(y, x));
1701N/A check(Float.compare(x, y) == -Float.compare(y, x));
1701N/A check(Long.compare(x, y) == -Long.compare(y, x));
1701N/A check(Integer.compare((int) x, (int) y) ==
1701N/A -Integer.compare((int) y, (int) x));
1701N/A check(Short.compare((short) x, (short) y) ==
1701N/A -Short.compare((short) y, (short) x));
1701N/A check(Character.compare((char) x, (char) y) ==
1701N/A -Character.compare((char) y, (char) x));
1701N/A check(Byte.compare((byte) x, (byte) y) ==
1701N/A -Byte.compare((byte) y, (byte) x));
1701N/A
1701N/A equal(Long.compare(x, y),
1701N/A x < y ? -1 : x > y ? 1 : 0);
1701N/A
1701N/A {
1701N/A int a = (int) x, b = (int) y;
1701N/A equal(Integer.compare(a, b),
1701N/A a < b ? -1 : a > b ? 1 : 0);
1701N/A }
1701N/A
1701N/A {
1701N/A short a = (short) x, b = (short) y;
1701N/A equal(Short.compare(a, b),
1701N/A a - b);
1701N/A }
1701N/A
1701N/A {
1701N/A char a = (char) x, b = (char) y;
1701N/A equal(Character.compare(a, b),
1701N/A a - b);
1701N/A }
1701N/A
1701N/A {
1701N/A byte a = (byte) x, b = (byte) y;
1701N/A equal(Byte.compare(a, b),
1701N/A a - b);
1701N/A }
1701N/A
1701N/A {
1701N/A boolean a = toBoolean(x), b = toBoolean(y);
1701N/A equal(Boolean.compare(a, b),
1701N/A a == b ? 0 : a ? 1 : -1);
1701N/A }
1701N/A }
1701N/A
1701N/A void test(String args[]) throws Exception {
1701N/A long[] longs = {
1701N/A Long.MIN_VALUE,
1701N/A Integer.MIN_VALUE,
1701N/A Short.MIN_VALUE,
1701N/A Character.MIN_VALUE,
1701N/A Byte.MIN_VALUE,
1701N/A -1, 0, 1,
1701N/A Byte.MAX_VALUE,
1701N/A Character.MAX_VALUE,
1701N/A Short.MAX_VALUE,
1701N/A Integer.MAX_VALUE,
1701N/A Long.MAX_VALUE,
1701N/A rnd.nextLong(),
1701N/A rnd.nextInt(),
1701N/A };
1701N/A
1701N/A for (long x : longs) {
1701N/A for (long y : longs) {
1701N/A compareAll(x, y);
1701N/A }
1701N/A }
1701N/A }
1701N/A
1701N/A //--------------------- Infrastructure ---------------------------
1701N/A volatile int passed = 0, failed = 0;
1701N/A void pass() {passed++;}
1701N/A void fail() {failed++; Thread.dumpStack();}
1701N/A void fail(String msg) {System.err.println(msg); fail();}
1701N/A void unexpected(Throwable t) {failed++; t.printStackTrace();}
1701N/A void check(boolean cond) {if (cond) pass(); else fail();}
1701N/A void equal(Object x, Object y) {
1701N/A if (x == null ? y == null : x.equals(y)) pass();
1701N/A else fail(x + " not equal to " + y);}
1701N/A public static void main(String[] args) throws Throwable {
1701N/A new Compare().instanceMain(args);}
1701N/A public void instanceMain(String[] args) throws Throwable {
1701N/A try {test(args);} catch (Throwable t) {unexpected(t);}
1701N/A System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1701N/A if (failed > 0) throw new AssertionError("Some tests failed");}
1701N/A}