test/jdk/java/util/Arrays/FloatDoubleOrder.java
author pli
Tue, 16 Jul 2019 00:57:00 +0000
changeset 55689 8c5c9d86e1d6
parent 47216 71c04702a3d5
permissions -rw-r--r--
8227512: [TESTBUG] Fix JTReg javac test failures with Graal Reviewed-by: mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4143272 6548425
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary The natural ordering on Float and Double was not even a partial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          order (i.e., it violated the contract of Comparable.compareTo).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          Now it's a total ordering.  Arrays.sort(double[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          and Arrays.sort(double[]) reflect the new ordering.  Also,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *          Arrays.equals(double[], double[]) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *          Arrays.equals(float[], float[]) reflect the definition of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *          equality used by Float and Double.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
@SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class FloatDoubleOrder {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    void test(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        double[] unsortedDbl = new double[] {1.0d, 3.7d, Double.NaN, -2.0d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
           Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0d, -0.0d};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        double[] sortedDbl = new double[] {Double.NEGATIVE_INFINITY, -2.0d,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
           -0.0d, 0.0d, 1.0d, 3.7d, Double.POSITIVE_INFINITY, Double.NaN};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        List list = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        for (int i=0; i<unsortedDbl.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            list.add(new Double(unsortedDbl[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        Collections.sort(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        List sortedList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        for (int i=0; i<sortedDbl.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            sortedList.add(new Double(sortedDbl[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        check(list.equals(sortedList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Arrays.sort(unsortedDbl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        check(Arrays.equals(unsortedDbl, sortedDbl));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        double negNan = Double.longBitsToDouble(0xfff8000000000000L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        for (int i = 0; i < sortedDbl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            equal(Arrays.binarySearch(sortedDbl, sortedDbl[i]), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (Double.isNaN(sortedDbl[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                equal(Arrays.binarySearch(sortedDbl, negNan), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        float[] unsortedFlt = new float[] {1.0f, 3.7f, Float.NaN, -2.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
           Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0f, -0.0f};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        float[] sortedFlt = new float[] {Float.NEGATIVE_INFINITY, -2.0f,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
           -0.0f, 0.0f, 1.0f, 3.7f, Float.POSITIVE_INFINITY, Float.NaN};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        list.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for (int i=0; i<unsortedFlt.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            list.add(new Float(unsortedFlt[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        Collections.sort(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        sortedList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (int i=0; i<sortedFlt.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            sortedList.add(new Float(sortedFlt[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        check(list.equals(sortedList));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        Arrays.sort(unsortedFlt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        check(Arrays.equals(unsortedFlt, sortedFlt));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        float negNaN = Float.intBitsToFloat(0xFfc00000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        for (int i = 0; i < sortedDbl.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            equal(Arrays.binarySearch(sortedFlt, sortedFlt[i]), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (Float.isNaN(sortedFlt[i]))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                equal(Arrays.binarySearch(sortedFlt, negNaN), i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        // 6548425: Arrays.sort incorrectly sorts a double array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // containing negative zeros
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        double[] da = {-0.0d, -0.0d, 0.0d, -0.0d};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        Arrays.sort(da, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        check(Arrays.equals(da, new double[] {-0.0d, -0.0d, -0.0d, 0.0d}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        float[] fa = {-0.0f, -0.0f, 0.0f, -0.0f};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        Arrays.sort(fa, 1, 4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        check(Arrays.equals(fa, new float[] {-0.0f, -0.0f, -0.0f, 0.0f}));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    void fail(String msg) {System.err.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        new FloatDoubleOrder().instanceMain(args);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    void instanceMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        try {test(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
}