Path: blob/master/test/jdk/java/util/Collections/ReverseOrder.java
41152 views
/*1* Copyright (c) 2002, 2013, 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 4593209 800166726* @summary Reverse comparator was subtly broken27* @author Josh Bloch28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.util.Arrays;35import java.util.Collections;36import java.util.Comparator;37import java.util.List;3839public class ReverseOrder {40static byte[] serialBytes(Object o) {41try {42ByteArrayOutputStream bos = new ByteArrayOutputStream();43ObjectOutputStream oos = new ObjectOutputStream(bos);44oos.writeObject(o);45oos.flush();46oos.close();47return bos.toByteArray();48} catch (Throwable t) {49throw new Error(t);50}51}5253@SuppressWarnings("unchecked")54static <T> T serialClone(T o) {55try {56ObjectInputStream ois = new ObjectInputStream57(new ByteArrayInputStream(serialBytes(o)));58T clone = (T) ois.readObject();59return clone;60} catch (Throwable t) {61throw new Error(t);62}63}6465public static void main(String[] args) throws Exception {66Foo[] a = { new Foo(2), new Foo(3), new Foo(1) };67List list = Arrays.asList(a);68Comparator cmp = Collections.reverseOrder();69Collections.sort(list, cmp);7071Foo[] golden = { new Foo(3), new Foo(2), new Foo(1) };72List goldenList = Arrays.asList(golden);73if (!list.equals(goldenList))74throw new Exception(list.toString());7576Comparator clone = serialClone(cmp);77List list2 = Arrays.asList(a);78Collections.sort(list2, clone);79if (!list2.equals(goldenList))80throw new Exception(list.toString());81}82}8384class Foo implements Comparable {85int val;86Foo(int i) { val = i; }8788public int compareTo(Object o) {89Foo f = (Foo)o;90return (val < f.val ? Integer.MIN_VALUE : (val == f.val ? 0 : 1));91}9293public boolean equals(Object o) {94return o instanceof Foo && ((Foo)o).val == val;95}9697public int hashCode() { return val; }9899public String toString() { return Integer.toString(val); }100}101102103