jdk/test/java/util/Collections/ReverseOrder2.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 5506 202f599c92aa
child 43522 f9c6f543c4db
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2003, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug     4809442 6366832 4974878 6372554 4890211 6483125
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Basic test for Collections.reverseOrder
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Josh Bloch, Martin Buchholz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class ReverseOrder2 {
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    35
    static final int N = 100;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static void realMain(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        check(Collections.reverseOrder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
              == Collections.reverseOrder(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        check(Collections.reverseOrder()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
              == reincarnate(Collections.reverseOrder()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        check(Collections.reverseOrder(Collections.reverseOrder(cmp))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
              == cmp);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        equal(Collections.reverseOrder(cmp),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
              Collections.reverseOrder(cmp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        equal(Collections.reverseOrder(cmp).hashCode(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
              Collections.reverseOrder(cmp).hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        check(Collections.reverseOrder(cmp).hashCode() !=
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
              cmp.hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        test(new ArrayList<String>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        test(new LinkedList<String>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        test2(new ArrayList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        test2(new LinkedList<Integer>());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static void test(List<String> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        for (int i = 0; i < N; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            list.add(String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        Collections.shuffle(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        Collections.sort(list, Collections.reverseOrder(cmp));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        equal(list, golden);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static Comparator<String> cmp = new Comparator<String> () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        public int compare(String s1, String s2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int i1 = Integer.parseInt(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            int i2 = Integer.parseInt(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return (i1 < i2 ? Integer.MIN_VALUE : (i1 == i2 ? 0 : 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    78
    private static final List<String> golden = new ArrayList<String>(N);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (int i = N-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            golden.add(String.valueOf(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static void test2(List<Integer> list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        for (int i = 0; i < N; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            list.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Collections.shuffle(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        Collections.sort(list, Collections.reverseOrder(null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        equal(list, golden2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    92
    private static final List<Integer> golden2 = new ArrayList<Integer>(N);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        for (int i = N-1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            golden2.add(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    //--------------------- Infrastructure ---------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    static volatile int passed = 0, failed = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    static void pass() {passed++;}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void fail() {failed++; Thread.dumpStack();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static void fail(String msg) {System.out.println(msg); fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    static void unexpected(Throwable t) {failed++; t.printStackTrace();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    static void check(boolean cond) {if (cond) pass(); else fail();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void equal(Object x, Object y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (x == null ? y == null : x.equals(y)) pass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        else fail(x + " not equal to " + y);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public static void main(String[] args) throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        try {realMain(args);} catch (Throwable t) {unexpected(t);}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (failed > 0) throw new AssertionError("Some tests failed");}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    static byte[] serializedForm(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            new ObjectOutputStream(baos).writeObject(obj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            return baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        } catch (IOException e) {throw new RuntimeException(e);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static Object readObject(byte[] bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        InputStream is = new ByteArrayInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return new ObjectInputStream(is).readObject();}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    @SuppressWarnings("unchecked")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    static <T> T reincarnate(T obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {return (T) readObject(serializedForm(obj));}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        catch (Exception e) {throw new RuntimeException(e);}}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}