Path: blob/master/test/jdk/java/lang/Compare.java
41145 views
/*1* Copyright 2009 Google, Inc. All Rights Reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 658294626* @summary Test the primitive wrappers compare and compareTo methods27* @key randomness28*/2930import java.util.Random;3132public class Compare {3334final Random rnd = new Random();3536boolean toBoolean(long x) { return x > 0; }3738void compareAll(long x, long y) {39check(Double.compare(x, y) ==40Double.valueOf(x).compareTo(Double.valueOf(y)));41check(Float.compare(x, y) ==42Float.valueOf(x).compareTo(Float.valueOf(y)));43check(Long.compare(x, y) ==44Long.valueOf(x).compareTo(Long.valueOf(y)));45check(Integer.compare((int) x, (int) y) ==46Integer.valueOf((int) x).compareTo(Integer.valueOf((int) y)));47check(Short.compare((short) x, (short) y) ==48Short.valueOf((short) x).compareTo(Short.valueOf((short) y)));49check(Character.compare((char) x, (char) y) ==50Character.valueOf((char) x).compareTo(Character.valueOf((char) y)));51check(Byte.compare((byte) x, (byte) y) ==52Byte.valueOf((byte) x).compareTo(Byte.valueOf((byte) y)));53check(Boolean.compare(toBoolean(x), toBoolean(y)) ==54Boolean.valueOf(toBoolean(x)).compareTo(Boolean.valueOf(toBoolean(y))));5556check(Double.compare(x, y) == -Double.compare(y, x));57check(Float.compare(x, y) == -Float.compare(y, x));58check(Long.compare(x, y) == -Long.compare(y, x));59check(Integer.compare((int) x, (int) y) ==60-Integer.compare((int) y, (int) x));61check(Short.compare((short) x, (short) y) ==62-Short.compare((short) y, (short) x));63check(Character.compare((char) x, (char) y) ==64-Character.compare((char) y, (char) x));65check(Byte.compare((byte) x, (byte) y) ==66-Byte.compare((byte) y, (byte) x));6768equal(Long.compare(x, y),69x < y ? -1 : x > y ? 1 : 0);7071{72int a = (int) x, b = (int) y;73equal(Integer.compare(a, b),74a < b ? -1 : a > b ? 1 : 0);75}7677{78short a = (short) x, b = (short) y;79equal(Short.compare(a, b),80a - b);81}8283{84char a = (char) x, b = (char) y;85equal(Character.compare(a, b),86a - b);87}8889{90byte a = (byte) x, b = (byte) y;91equal(Byte.compare(a, b),92a - b);93}9495{96boolean a = toBoolean(x), b = toBoolean(y);97equal(Boolean.compare(a, b),98a == b ? 0 : a ? 1 : -1);99}100}101102void test(String args[]) throws Exception {103long[] longs = {104Long.MIN_VALUE,105Integer.MIN_VALUE,106Short.MIN_VALUE,107Character.MIN_VALUE,108Byte.MIN_VALUE,109-1, 0, 1,110Byte.MAX_VALUE,111Character.MAX_VALUE,112Short.MAX_VALUE,113Integer.MAX_VALUE,114Long.MAX_VALUE,115rnd.nextLong(),116rnd.nextInt(),117};118119for (long x : longs) {120for (long y : longs) {121compareAll(x, y);122}123}124}125126//--------------------- Infrastructure ---------------------------127volatile int passed = 0, failed = 0;128void pass() {passed++;}129void fail() {failed++; Thread.dumpStack();}130void fail(String msg) {System.err.println(msg); fail();}131void unexpected(Throwable t) {failed++; t.printStackTrace();}132void check(boolean cond) {if (cond) pass(); else fail();}133void equal(Object x, Object y) {134if (x == null ? y == null : x.equals(y)) pass();135else fail(x + " not equal to " + y);}136public static void main(String[] args) throws Throwable {137new Compare().instanceMain(args);}138public void instanceMain(String[] args) throws Throwable {139try {test(args);} catch (Throwable t) {unexpected(t);}140System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);141if (failed > 0) throw new AssertionError("Some tests failed");}142}143144145