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