ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collections/ReverseOrder2.java
Revision: 1.3
Committed: Mon Sep 27 19:15:15 2010 UTC (13 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.2: +3 -3 lines
Log Message:
use blessed declaration modifier order

File Contents

# User Rev Content
1 jsr166 1.1 /*
2 jsr166 1.2 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
3 jsr166 1.1 * 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 jsr166 1.2 * 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 jsr166 1.1 */
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.util.*;
32     import java.io.*;
33    
34     public class ReverseOrder2 {
35 jsr166 1.3 static final int N = 100;
36 jsr166 1.1
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 jsr166 1.3 private static final List<String> golden = new ArrayList<String>(N);
79 jsr166 1.1 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 jsr166 1.3 private static final List<Integer> golden2 = new ArrayList<Integer>(N);
93 jsr166 1.1 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     }