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 { 98 for (int i = N-1; i >= 0; i--) 99 golden.add(String.valueOf(i)); 100 } 101 102 static void test2(List<Integer> list) { 103 for (int i = 0; i < N; i++) 104 list.add(i); 105 Collections.shuffle(list); 106 Collections.sort(list, Collections.reverseOrder(null)); 107 equal(list, golden2); 108 } 109 110 private static final List<Integer> golden2 = new ArrayList<>(N); 111 static { 112 for (int i = N-1; i >= 0; i--) 113 golden2.add(i); 114 } 115 116 //--------------------- Infrastructure --------------------------- 117 static volatile int passed = 0, failed = 0; 118 static void pass() {passed++;} 119 static void fail() {failed++; Thread.dumpStack();} 120 static void fail(String msg) {System.out.println(msg); fail();} 121 static void unexpected(Throwable t) {failed++; t.printStackTrace();} 122 static void check(boolean cond) {if (cond) pass(); else fail();} 123 static void equal(Object x, Object y) { 124 if (x == null ? y == null : x.equals(y)) pass(); 125 else fail(x + " not equal to " + y);} 126 public static void main(String[] args) throws Throwable { 127 try {realMain(args);} catch (Throwable t) {unexpected(t);} 128 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); 129 if (failed > 0) throw new AssertionError("Some tests failed");} 130 static byte[] serializedForm(Object obj) { 131 try { 132 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 133 new ObjectOutputStream(baos).writeObject(obj); 134 return baos.toByteArray(); 135 } catch (IOException e) {throw new RuntimeException(e);}} 136 static Object readObject(byte[] bytes) 137 throws IOException, ClassNotFoundException { 138 InputStream is = new ByteArrayInputStream(bytes); 139 return new ObjectInputStream(is).readObject();} 140 @SuppressWarnings("unchecked") 141 static <T> T reincarnate(T obj) { 142 try {return (T) readObject(serializedForm(obj));} 143 catch (Exception e) {throw new RuntimeException(e);}} 144 } --- EOF ---