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 
31 package test.java.util.Collections;
32 
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.ObjectInputStream;
38 import java.io.ObjectOutputStream;
39 import java.util.ArrayList;
40 import java.util.Collections;
41 import java.util.Comparator;
42 import java.util.LinkedList;
43 import java.util.List;
44 
45 public class ReverseOrder2 {
46     static final int N = 100;
47 
realMain(String[] args)48     static void realMain(String[] args) throws Throwable {
49         check(Collections.reverseOrder()
50               == Collections.reverseOrder(null));
51 
52         check(Collections.reverseOrder()
53               == reincarnate(Collections.reverseOrder()));
54 
55         check(Collections.reverseOrder(Collections.reverseOrder(cmp))
56               == cmp);
57 
58         equal(Collections.reverseOrder(cmp),
59               Collections.reverseOrder(cmp));
60 
61         equal(Collections.reverseOrder(cmp).hashCode(),
62               Collections.reverseOrder(cmp).hashCode());
63 
64         check(Collections.reverseOrder(cmp).hashCode() !=
65               cmp.hashCode());
66 
67         test(new ArrayList<String>());
68         test(new LinkedList<String>());
69         test2(new ArrayList<Integer>());
70         test2(new LinkedList<Integer>());
71     }
72 
test(List<String> list)73     static void test(List<String> list) {
74         for (int i = 0; i < N; i++)
75             list.add(String.valueOf(i));
76         Collections.shuffle(list);
77         Collections.sort(list, Collections.reverseOrder(cmp));
78         equal(list, golden);
79     }
80 
81     private static Comparator<String> cmp = new Comparator<>() {
82         public int compare(String s1, String s2) {
83             int i1 = Integer.parseInt(s1);
84             int i2 = Integer.parseInt(s2);
85             return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));
86         }
87     };
88 
89     private static final List<String> golden = new ArrayList<>(N);
90     static {
91         for (int i = N-1; i >= 0; i--)
String.valueOf(i)92             golden.add(String.valueOf(i));
93     }
94 
test2(List<Integer> list)95     static void test2(List<Integer> list) {
96         for (int i = 0; i < N; i++)
97             list.add(i);
98         Collections.shuffle(list);
99         Collections.sort(list, Collections.reverseOrder(null));
100         equal(list, golden2);
101     }
102 
103     private static final List<Integer> golden2 = new ArrayList<>(N);
104     static {
105         for (int i = N-1; i >= 0; i--)
106             golden2.add(i);
107     }
108 
109     //--------------------- Infrastructure ---------------------------
110     static volatile int passed = 0, failed = 0;
pass()111     static void pass() {passed++;}
fail()112     static void fail() {failed++; Thread.dumpStack();}
fail(String msg)113     static void fail(String msg) {System.out.println(msg); fail();}
unexpected(Throwable t)114     static void unexpected(Throwable t) {failed++; t.printStackTrace();}
check(boolean cond)115     static void check(boolean cond) {if (cond) pass(); else fail();}
equal(Object x, Object y)116     static void equal(Object x, Object y) {
117         if (x == null ? y == null : x.equals(y)) pass();
118         else fail(x + " not equal to " + y);}
main(String[] args)119     public static void main(String[] args) throws Throwable {
120         try {realMain(args);} catch (Throwable t) {unexpected(t);}
121         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
122         if (failed > 0) throw new AssertionError("Some tests failed");}
serializedForm(Object obj)123     static byte[] serializedForm(Object obj) {
124         try {
125             ByteArrayOutputStream baos = new ByteArrayOutputStream();
126             new ObjectOutputStream(baos).writeObject(obj);
127             return baos.toByteArray();
128         } catch (IOException e) {throw new RuntimeException(e);}}
readObject(byte[] bytes)129     static Object readObject(byte[] bytes)
130         throws IOException, ClassNotFoundException {
131         InputStream is = new ByteArrayInputStream(bytes);
132         return new ObjectInputStream(is).readObject();}
133     @SuppressWarnings("unchecked")
reincarnate(T obj)134     static <T> T reincarnate(T obj) {
135         try {return (T) readObject(serializedForm(obj));}
136         catch (Exception e) {throw new RuntimeException(e);}}
137 }
138