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 {
|
1 /*
2 * Copyright (c) 2003, 2026, 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 * @library /test/lib
30 */
31
32 import jdk.test.lib.valueclass.VClass;
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.Arrays;
41 import java.util.Collections;
42 import java.util.Comparator;
43 import java.util.LinkedList;
44 import java.util.List;
45
46 public class ReverseOrder2 {
47 static final int N = 100;
48
49 static void realMain(String[] args) throws Throwable {
50 check(Collections.reverseOrder()
51 == Collections.reverseOrder(null));
52
53 check(Collections.reverseOrder()
54 == reincarnate(Collections.reverseOrder()));
55
56 check(Collections.reverseOrder(Collections.reverseOrder(cmp))
57 == cmp);
58
59 equal(Collections.reverseOrder(cmp),
60 Collections.reverseOrder(cmp));
61
62 equal(Collections.reverseOrder(cmp).hashCode(),
63 Collections.reverseOrder(cmp).hashCode());
64
65 check(Collections.reverseOrder(cmp).hashCode() !=
66 cmp.hashCode());
67
68 test(new ArrayList<String>());
69 test(new LinkedList<String>());
70 test2(new ArrayList<Integer>());
71 test2(new LinkedList<Integer>());
72
73 List<VClass> values = new ArrayList<>(Arrays.asList(new VClass(0, new int[] { 0 }), new VClass(1, new int[] { 1 }), new VClass(2, new int[] { 2 })));
74 Collections.shuffle(values);
75 Collections.sort(values, Collections.reverseOrder());
76 if (!values.equals(Arrays.asList(new VClass(2, new int[] { 2 }), new VClass(1, new int[] { 1 }), new VClass(0, new int[] { 0 }))))
77 throw new RuntimeException("value reverseOrder2 failed");
78 }
79
80 static void test(List<String> list) {
81 for (int i = 0; i < N; i++)
82 list.add(String.valueOf(i));
83 Collections.shuffle(list);
84 Collections.sort(list, Collections.reverseOrder(cmp));
85 equal(list, golden);
86 }
87
88 private static Comparator<String> cmp = new Comparator<>() {
89 public int compare(String s1, String s2) {
90 int i1 = Integer.parseInt(s1);
91 int i2 = Integer.parseInt(s2);
92 return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));
93 }
94 };
95
96 private static final List<String> golden = new ArrayList<>(N);
97 static {
|