Path: blob/master/test/jdk/java/util/Collections/ReverseOrder2.java
41149 views
/*1* Copyright (c) 2003, 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 4809442 6366832 4974878 6372554 4890211 648312526* @summary Basic test for Collections.reverseOrder27* @author Josh Bloch, Martin Buchholz28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.IOException;33import java.io.InputStream;34import java.io.ObjectInputStream;35import java.io.ObjectOutputStream;36import java.util.ArrayList;37import java.util.Collections;38import java.util.Comparator;39import java.util.LinkedList;40import java.util.List;4142public class ReverseOrder2 {43static final int N = 100;4445static void realMain(String[] args) throws Throwable {46check(Collections.reverseOrder()47== Collections.reverseOrder(null));4849check(Collections.reverseOrder()50== reincarnate(Collections.reverseOrder()));5152check(Collections.reverseOrder(Collections.reverseOrder(cmp))53== cmp);5455equal(Collections.reverseOrder(cmp),56Collections.reverseOrder(cmp));5758equal(Collections.reverseOrder(cmp).hashCode(),59Collections.reverseOrder(cmp).hashCode());6061check(Collections.reverseOrder(cmp).hashCode() !=62cmp.hashCode());6364test(new ArrayList<String>());65test(new LinkedList<String>());66test2(new ArrayList<Integer>());67test2(new LinkedList<Integer>());68}6970static void test(List<String> list) {71for (int i = 0; i < N; i++)72list.add(String.valueOf(i));73Collections.shuffle(list);74Collections.sort(list, Collections.reverseOrder(cmp));75equal(list, golden);76}7778private static Comparator<String> cmp = new Comparator<>() {79public int compare(String s1, String s2) {80int i1 = Integer.parseInt(s1);81int i2 = Integer.parseInt(s2);82return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));83}84};8586private static final List<String> golden = new ArrayList<>(N);87static {88for (int i = N-1; i >= 0; i--)89golden.add(String.valueOf(i));90}9192static void test2(List<Integer> list) {93for (int i = 0; i < N; i++)94list.add(i);95Collections.shuffle(list);96Collections.sort(list, Collections.reverseOrder(null));97equal(list, golden2);98}99100private static final List<Integer> golden2 = new ArrayList<>(N);101static {102for (int i = N-1; i >= 0; i--)103golden2.add(i);104}105106//--------------------- Infrastructure ---------------------------107static volatile int passed = 0, failed = 0;108static void pass() {passed++;}109static void fail() {failed++; Thread.dumpStack();}110static void fail(String msg) {System.out.println(msg); fail();}111static void unexpected(Throwable t) {failed++; t.printStackTrace();}112static void check(boolean cond) {if (cond) pass(); else fail();}113static void 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 {117try {realMain(args);} catch (Throwable t) {unexpected(t);}118System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);119if (failed > 0) throw new AssertionError("Some tests failed");}120static byte[] serializedForm(Object obj) {121try {122ByteArrayOutputStream baos = new ByteArrayOutputStream();123new ObjectOutputStream(baos).writeObject(obj);124return baos.toByteArray();125} catch (IOException e) {throw new RuntimeException(e);}}126static Object readObject(byte[] bytes)127throws IOException, ClassNotFoundException {128InputStream is = new ByteArrayInputStream(bytes);129return new ObjectInputStream(is).readObject();}130@SuppressWarnings("unchecked")131static <T> T reincarnate(T obj) {132try {return (T) readObject(serializedForm(obj));}133catch (Exception e) {throw new RuntimeException(e);}}134}135136137