Path: blob/master/test/jdk/java/util/Arrays/FloatDoubleOrder.java
41152 views
/*1* Copyright (c) 1998, 2007, Oracle and/or its affiliates. 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 4143272 654842526* @summary The natural ordering on Float and Double was not even a partial27* order (i.e., it violated the contract of Comparable.compareTo).28* Now it's a total ordering. Arrays.sort(double[])29* and Arrays.sort(double[]) reflect the new ordering. Also,30* Arrays.equals(double[], double[]) and31* Arrays.equals(float[], float[]) reflect the definition of32* equality used by Float and Double.33*/3435import java.util.*;3637@SuppressWarnings("unchecked")38public class FloatDoubleOrder {39void test(String[] args) throws Throwable {40double[] unsortedDbl = new double[] {1.0d, 3.7d, Double.NaN, -2.0d,41Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0d, -0.0d};4243double[] sortedDbl = new double[] {Double.NEGATIVE_INFINITY, -2.0d,44-0.0d, 0.0d, 1.0d, 3.7d, Double.POSITIVE_INFINITY, Double.NaN};4546List list = new ArrayList();47for (int i=0; i<unsortedDbl.length; i++)48list.add(new Double(unsortedDbl[i]));49Collections.sort(list);5051List sortedList = new ArrayList();52for (int i=0; i<sortedDbl.length; i++)53sortedList.add(new Double(sortedDbl[i]));5455check(list.equals(sortedList));5657Arrays.sort(unsortedDbl);58check(Arrays.equals(unsortedDbl, sortedDbl));5960double negNan = Double.longBitsToDouble(0xfff8000000000000L);61for (int i = 0; i < sortedDbl.length; i++) {62equal(Arrays.binarySearch(sortedDbl, sortedDbl[i]), i);63if (Double.isNaN(sortedDbl[i]))64equal(Arrays.binarySearch(sortedDbl, negNan), i);65}6667float[] unsortedFlt = new float[] {1.0f, 3.7f, Float.NaN, -2.0f,68Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0f, -0.0f};6970float[] sortedFlt = new float[] {Float.NEGATIVE_INFINITY, -2.0f,71-0.0f, 0.0f, 1.0f, 3.7f, Float.POSITIVE_INFINITY, Float.NaN};7273list.clear();74for (int i=0; i<unsortedFlt.length; i++)75list.add(new Float(unsortedFlt[i]));76Collections.sort(list);7778sortedList.clear();79for (int i=0; i<sortedFlt.length; i++)80sortedList.add(new Float(sortedFlt[i]));8182check(list.equals(sortedList));8384Arrays.sort(unsortedFlt);85check(Arrays.equals(unsortedFlt, sortedFlt));8687float negNaN = Float.intBitsToFloat(0xFfc00000);88for (int i = 0; i < sortedDbl.length; i++) {89equal(Arrays.binarySearch(sortedFlt, sortedFlt[i]), i);90if (Float.isNaN(sortedFlt[i]))91equal(Arrays.binarySearch(sortedFlt, negNaN), i);92}939495// 6548425: Arrays.sort incorrectly sorts a double array96// containing negative zeros97double[] da = {-0.0d, -0.0d, 0.0d, -0.0d};98Arrays.sort(da, 1, 4);99check(Arrays.equals(da, new double[] {-0.0d, -0.0d, -0.0d, 0.0d}));100101float[] fa = {-0.0f, -0.0f, 0.0f, -0.0f};102Arrays.sort(fa, 1, 4);103check(Arrays.equals(fa, new float[] {-0.0f, -0.0f, -0.0f, 0.0f}));104}105106//--------------------- Infrastructure ---------------------------107volatile int passed = 0, failed = 0;108void pass() {passed++;}109void fail() {failed++; Thread.dumpStack();}110void fail(String msg) {System.err.println(msg); fail();}111void unexpected(Throwable t) {failed++; t.printStackTrace();}112void check(boolean cond) {if (cond) pass(); else fail();}113void equal(Object x, Object y) {114if (x == null ? y == null : x.equals(y)) pass();115else fail(x + " not equal to " + y);}116public static void main(String[] args) throws Throwable {117new FloatDoubleOrder().instanceMain(args);}118void instanceMain(String[] args) throws Throwable {119try {test(args);} catch (Throwable t) {unexpected(t);}120System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);121if (failed > 0) throw new AssertionError("Some tests failed");}122}123124125