Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/ReverseOrder2.java
38821 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.util.*;31import java.io.*;3233public class ReverseOrder2 {34final static int N = 100;3536static void realMain(String[] args) throws Throwable {37check(Collections.reverseOrder()38== Collections.reverseOrder(null));3940check(Collections.reverseOrder()41== reincarnate(Collections.reverseOrder()));4243check(Collections.reverseOrder(Collections.reverseOrder(cmp))44== cmp);4546equal(Collections.reverseOrder(cmp),47Collections.reverseOrder(cmp));4849equal(Collections.reverseOrder(cmp).hashCode(),50Collections.reverseOrder(cmp).hashCode());5152check(Collections.reverseOrder(cmp).hashCode() !=53cmp.hashCode());5455test(new ArrayList<String>());56test(new LinkedList<String>());57test2(new ArrayList<Integer>());58test2(new LinkedList<Integer>());59}6061static void test(List<String> list) {62for (int i = 0; i < N; i++)63list.add(String.valueOf(i));64Collections.shuffle(list);65Collections.sort(list, Collections.reverseOrder(cmp));66equal(list, golden);67}6869private static Comparator<String> cmp = new Comparator<String> () {70public int compare(String s1, String s2) {71int i1 = Integer.parseInt(s1);72int i2 = Integer.parseInt(s2);73return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));74}75};7677private final static List<String> golden = new ArrayList<String>(N);78static {79for (int i = N-1; i >= 0; i--)80golden.add(String.valueOf(i));81}8283static void test2(List<Integer> list) {84for (int i = 0; i < N; i++)85list.add(i);86Collections.shuffle(list);87Collections.sort(list, Collections.reverseOrder(null));88equal(list, golden2);89}9091private final static List<Integer> golden2 = new ArrayList<Integer>(N);92static {93for (int i = N-1; i >= 0; i--)94golden2.add(i);95}9697//--------------------- Infrastructure ---------------------------98static volatile int passed = 0, failed = 0;99static void pass() {passed++;}100static void fail() {failed++; Thread.dumpStack();}101static void fail(String msg) {System.out.println(msg); fail();}102static void unexpected(Throwable t) {failed++; t.printStackTrace();}103static void check(boolean cond) {if (cond) pass(); else fail();}104static void equal(Object x, Object y) {105if (x == null ? y == null : x.equals(y)) pass();106else fail(x + " not equal to " + y);}107public static void main(String[] args) throws Throwable {108try {realMain(args);} catch (Throwable t) {unexpected(t);}109System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);110if (failed > 0) throw new AssertionError("Some tests failed");}111static byte[] serializedForm(Object obj) {112try {113ByteArrayOutputStream baos = new ByteArrayOutputStream();114new ObjectOutputStream(baos).writeObject(obj);115return baos.toByteArray();116} catch (IOException e) {throw new RuntimeException(e);}}117static Object readObject(byte[] bytes)118throws IOException, ClassNotFoundException {119InputStream is = new ByteArrayInputStream(bytes);120return new ObjectInputStream(is).readObject();}121@SuppressWarnings("unchecked")122static <T> T reincarnate(T obj) {123try {return (T) readObject(serializedForm(obj));}124catch (Exception e) {throw new RuntimeException(e);}}125}126127128