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