1 /* 2 * Copyright (c) 1998, 2007, Oracle and/or its affiliates. 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 4143272 6548425 27 * @summary The natural ordering on Float and Double was not even a partial 28 * order (i.e., it violated the contract of Comparable.compareTo). 29 * Now it's a total ordering. Arrays.sort(double[]) 30 * and Arrays.sort(double[]) reflect the new ordering. Also, 31 * Arrays.equals(double[], double[]) and 32 * Arrays.equals(float[], float[]) reflect the definition of 33 * equality used by Float and Double. 34 */ 35 36 package test.java.util.Arrays; 37 38 import java.util.*; 39 40 @SuppressWarnings("unchecked") 41 public class FloatDoubleOrder { test(String[] args)42 void test(String[] args) throws Throwable { 43 double[] unsortedDbl = new double[] {1.0d, 3.7d, Double.NaN, -2.0d, 44 Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0d, -0.0d}; 45 46 double[] sortedDbl = new double[] {Double.NEGATIVE_INFINITY, -2.0d, 47 -0.0d, 0.0d, 1.0d, 3.7d, Double.POSITIVE_INFINITY, Double.NaN}; 48 49 List list = new ArrayList(); 50 for (int i=0; i<unsortedDbl.length; i++) 51 list.add(new Double(unsortedDbl[i])); 52 Collections.sort(list); 53 54 List sortedList = new ArrayList(); 55 for (int i=0; i<sortedDbl.length; i++) 56 sortedList.add(new Double(sortedDbl[i])); 57 58 check(list.equals(sortedList)); 59 60 Arrays.sort(unsortedDbl); 61 check(Arrays.equals(unsortedDbl, sortedDbl)); 62 63 double negNan = Double.longBitsToDouble(0xfff8000000000000L); 64 for (int i = 0; i < sortedDbl.length; i++) { 65 equal(Arrays.binarySearch(sortedDbl, sortedDbl[i]), i); 66 if (Double.isNaN(sortedDbl[i])) 67 equal(Arrays.binarySearch(sortedDbl, negNan), i); 68 } 69 70 float[] unsortedFlt = new float[] {1.0f, 3.7f, Float.NaN, -2.0f, 71 Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0f, -0.0f}; 72 73 float[] sortedFlt = new float[] {Float.NEGATIVE_INFINITY, -2.0f, 74 -0.0f, 0.0f, 1.0f, 3.7f, Float.POSITIVE_INFINITY, Float.NaN}; 75 76 list.clear(); 77 for (int i=0; i<unsortedFlt.length; i++) 78 list.add(new Float(unsortedFlt[i])); 79 Collections.sort(list); 80 81 sortedList.clear(); 82 for (int i=0; i<sortedFlt.length; i++) 83 sortedList.add(new Float(sortedFlt[i])); 84 85 check(list.equals(sortedList)); 86 87 Arrays.sort(unsortedFlt); 88 check(Arrays.equals(unsortedFlt, sortedFlt)); 89 90 float negNaN = Float.intBitsToFloat(0xFfc00000); 91 for (int i = 0; i < sortedDbl.length; i++) { 92 equal(Arrays.binarySearch(sortedFlt, sortedFlt[i]), i); 93 if (Float.isNaN(sortedFlt[i])) 94 equal(Arrays.binarySearch(sortedFlt, negNaN), i); 95 } 96 97 98 // 6548425: Arrays.sort incorrectly sorts a double array 99 // containing negative zeros 100 double[] da = {-0.0d, -0.0d, 0.0d, -0.0d}; 101 Arrays.sort(da, 1, 4); 102 check(Arrays.equals(da, new double[] {-0.0d, -0.0d, -0.0d, 0.0d})); 103 104 float[] fa = {-0.0f, -0.0f, 0.0f, -0.0f}; 105 Arrays.sort(fa, 1, 4); 106 check(Arrays.equals(fa, new float[] {-0.0f, -0.0f, -0.0f, 0.0f})); 107 } 108 109 //--------------------- Infrastructure --------------------------- 110 volatile int passed = 0, failed = 0; pass()111 void pass() {passed++;} fail()112 void fail() {failed++; Thread.dumpStack();} fail(String msg)113 void fail(String msg) {System.err.println(msg); fail();} unexpected(Throwable t)114 void unexpected(Throwable t) {failed++; t.printStackTrace();} check(boolean cond)115 void check(boolean cond) {if (cond) pass(); else fail();} equal(Object x, Object y)116 void equal(Object x, Object y) { 117 if (x == null ? y == null : x.equals(y)) pass(); 118 else fail(x + " not equal to " + y);} main(String[] args)119 public static void main(String[] args) throws Throwable { 120 new FloatDoubleOrder().instanceMain(args);} instanceMain(String[] args)121 void instanceMain(String[] args) throws Throwable { 122 try {test(args);} catch (Throwable t) {unexpected(t);} 123 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 124 if (failed > 0) throw new AssertionError("Some tests failed");} 125 } 126