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