view j2se/test/java/util/Arrays/FloatDoubleOrder.java @ 1:193df1943809 trunk

[svn] Load openjdk/jdk7/b13 into jdk/trunk.
author xiomara
date Fri, 25 May 2007 00:49:14 +0000
parents a4ed3fb96592
children
line wrap: on
line source

/*
 * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/**
 * @test 1.6 07/05/15
 * @bug 4143272 6548425
 * @summary The natural ordering on Float and Double was not even a partial
 *	    order (i.e., it violated the contract of Comparable.compareTo).
 *	    Now it's a total ordering.  Arrays.sort(double[])
 *	    and Arrays.sort(double[]) reflect the new ordering.  Also,
 *	    Arrays.equals(double[], double[]) and
 *	    Arrays.equals(float[], float[]) reflect the definition of
 *	    equality used by Float and Double.
 */

import java.util.*;

public class FloatDoubleOrder {
    public static final void main(String args[]) {
        double[] unsortedDbl = new double[] {1.0d, 3.7d, Double.NaN, -2.0d,
           Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, 0.0d, -0.0d};

        double[] sortedDbl = new double[] {Double.NEGATIVE_INFINITY, -2.0d,
           -0.0d, 0.0d, 1.0d, 3.7d, Double.POSITIVE_INFINITY, Double.NaN};

        List list = new ArrayList();
        for (int i=0; i<unsortedDbl.length; i++)
            list.add(new Double(unsortedDbl[i]));
        Collections.sort(list);

        List sortedList = new ArrayList();
        for (int i=0; i<sortedDbl.length; i++)
            sortedList.add(new Double(sortedDbl[i]));

        if (!list.equals(sortedList))
            throw new RuntimeException("Double List failed");

        Arrays.sort(unsortedDbl);
        if (!Arrays.equals(unsortedDbl, sortedDbl))
            throw new RuntimeException("Double Array failed");


        float[] unsortedFlt = new float[] {1.0f, 3.7f, Float.NaN, -2.0f,
           Float.POSITIVE_INFINITY, Float.NEGATIVE_INFINITY, 0.0f, -0.0f};

        float[] sortedFlt = new float[] {Float.NEGATIVE_INFINITY, -2.0f,
           -0.0f, 0.0f, 1.0f, 3.7f, Float.POSITIVE_INFINITY, Float.NaN};

        list.clear();
        for (int i=0; i<unsortedFlt.length; i++)
            list.add(new Float(unsortedFlt[i]));
        Collections.sort(list);

        sortedList.clear();
        for (int i=0; i<sortedFlt.length; i++)
            sortedList.add(new Float(sortedFlt[i]));

        if (!list.equals(sortedList))
            throw new RuntimeException("Float List failed");

        Arrays.sort(unsortedFlt);
        if (!Arrays.equals(unsortedFlt, sortedFlt))
            throw new RuntimeException("Float Array failed");


	double[] da = {-0.0d, -0.0d, 0.0d, -0.0d};
	Arrays.sort(da, 1, 4);
        if (!Arrays.equals(da, new double[] {-0.0d, -0.0d, -0.0d, 0.0d}))
            throw new RuntimeException("6548425");

	float[] fa = {-0.0f, -0.0f, 0.0f, -0.0f};
	Arrays.sort(fa, 1, 4);
        if (!Arrays.equals(fa, new float[] {-0.0f, -0.0f, -0.0f, 0.0f}))
            throw new RuntimeException("6548425");
  }
}