ReverseOrder2.java revision 12745:f068a4ffddd2
1/*
2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug     4809442 6366832 4974878 6372554 4890211 6483125
27 * @summary Basic test for Collections.reverseOrder
28 * @author  Josh Bloch, Martin Buchholz
29 */
30
31import java.util.*;
32import java.io.*;
33
34public class ReverseOrder2 {
35    static final int N = 100;
36
37    static void realMain(String[] args) throws Throwable {
38        check(Collections.reverseOrder()
39              == Collections.reverseOrder(null));
40
41        check(Collections.reverseOrder()
42              == reincarnate(Collections.reverseOrder()));
43
44        check(Collections.reverseOrder(Collections.reverseOrder(cmp))
45              == cmp);
46
47        equal(Collections.reverseOrder(cmp),
48              Collections.reverseOrder(cmp));
49
50        equal(Collections.reverseOrder(cmp).hashCode(),
51              Collections.reverseOrder(cmp).hashCode());
52
53        check(Collections.reverseOrder(cmp).hashCode() !=
54              cmp.hashCode());
55
56        test(new ArrayList<String>());
57        test(new LinkedList<String>());
58        test2(new ArrayList<Integer>());
59        test2(new LinkedList<Integer>());
60    }
61
62    static void test(List<String> list) {
63        for (int i = 0; i < N; i++)
64            list.add(String.valueOf(i));
65        Collections.shuffle(list);
66        Collections.sort(list, Collections.reverseOrder(cmp));
67        equal(list, golden);
68    }
69
70    private static Comparator<String> cmp = new Comparator<String> () {
71        public int compare(String s1, String s2) {
72            int i1 = Integer.parseInt(s1);
73            int i2 = Integer.parseInt(s2);
74            return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));
75        }
76    };
77
78    private static final List<String> golden = new ArrayList<String>(N);
79    static {
80        for (int i = N-1; i >= 0; i--)
81            golden.add(String.valueOf(i));
82    }
83
84    static void test2(List<Integer> list) {
85        for (int i = 0; i < N; i++)
86            list.add(i);
87        Collections.shuffle(list);
88        Collections.sort(list, Collections.reverseOrder(null));
89        equal(list, golden2);
90    }
91
92    private static final List<Integer> golden2 = new ArrayList<Integer>(N);
93    static {
94        for (int i = N-1; i >= 0; i--)
95            golden2.add(i);
96    }
97
98    //--------------------- Infrastructure ---------------------------
99    static volatile int passed = 0, failed = 0;
100    static void pass() {passed++;}
101    static void fail() {failed++; Thread.dumpStack();}
102    static void fail(String msg) {System.out.println(msg); fail();}
103    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
104    static void check(boolean cond) {if (cond) pass(); else fail();}
105    static void equal(Object x, Object y) {
106        if (x == null ? y == null : x.equals(y)) pass();
107        else fail(x + " not equal to " + y);}
108    public static void main(String[] args) throws Throwable {
109        try {realMain(args);} catch (Throwable t) {unexpected(t);}
110        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
111        if (failed > 0) throw new AssertionError("Some tests failed");}
112    static byte[] serializedForm(Object obj) {
113        try {
114            ByteArrayOutputStream baos = new ByteArrayOutputStream();
115            new ObjectOutputStream(baos).writeObject(obj);
116            return baos.toByteArray();
117        } catch (IOException e) {throw new RuntimeException(e);}}
118    static Object readObject(byte[] bytes)
119        throws IOException, ClassNotFoundException {
120        InputStream is = new ByteArrayInputStream(bytes);
121        return new ObjectInputStream(is).readObject();}
122    @SuppressWarnings("unchecked")
123    static <T> T reincarnate(T obj) {
124        try {return (T) readObject(serializedForm(obj));}
125        catch (Exception e) {throw new RuntimeException(e);}}
126}
127