jdk/test/java/util/Collections/CheckedListBash.java
author mduigou
Fri, 12 Jul 2013 11:11:30 -0700
changeset 18818 a9ceff754226
parent 5506 202f599c92aa
child 30046 cf2c86e1819e
permissions -rw-r--r--
7129185: Add Collections.{checked|empty|unmodifiable}Navigable{Map|Set} Reviewed-by: dmocek, martin, smarks
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, 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     4904067
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Unit test for Collections.checkedList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author  Josh Bloch
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class CheckedListBash {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    static Random rnd = new Random();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        int numItr = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        int listSize = 100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        for (int i=0; i<numItr; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            List s1 = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            AddRandoms(s1, listSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            List s2 = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            AddRandoms(s2, listSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            List intersection = clone(s1); intersection.retainAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            List diff1 = clone(s1); diff1.removeAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            List diff2 = clone(s2); diff2.removeAll(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            List union = clone(s1); union.addAll(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            if (diff1.removeAll(diff2))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                fail("List algebra identity 2 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            if (diff1.removeAll(intersection))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                fail("List algebra identity 3 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            if (diff2.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                fail("List algebra identity 4 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            if (diff2.removeAll(intersection))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                fail("List algebra identity 5 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            if (intersection.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                fail("List algebra identity 6 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            if (intersection.removeAll(diff1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                fail("List algebra identity 7 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            intersection.addAll(diff1); intersection.addAll(diff2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            if (!(intersection.containsAll(union) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                  union.containsAll(intersection)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                fail("List algebra identity 1 failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            Iterator e = union.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            while (e.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                intersection.remove(e.next());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (!intersection.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                fail("Copy nonempty after deleting all elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            e = union.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            while (e.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Object o = e.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                if (!union.contains(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                    fail("List doesn't contain one of its elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                e.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            if (!union.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                fail("List nonempty after deleting all elements.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            s1.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (s1.size() != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                fail("Clear didn't reduce size to zero.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            s1.addAll(0, s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (!(s1.equals(s2) && s2.equals(s1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                fail("addAll(int, Collection) doesn't work.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // Reverse List
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            for (int j=0, n=s1.size(); j<n; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                s1.set(j, s1.set(n-j-1, s1.get(j)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            // Reverse it again
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            for (int j=0, n=s1.size(); j<n; j++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                s1.set(j, s1.set(n-j-1, s1.get(j)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            if (!(s1.equals(s2) && s2.equals(s1)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                fail("set(int, Object) doesn't work");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        List s = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (int i=0; i<listSize; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            s.add(new Integer(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        if (s.size() != listSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            fail("Size of [0..n-1] != n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        List even = clone(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        Iterator it = even.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        while(it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if(((Integer)it.next()).intValue() % 2 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        it = even.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        while(it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            if(((Integer)it.next()).intValue() % 2 == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                fail("Failed to remove all odd nubmers.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        List odd = clone(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        for (int i=0; i<(listSize/2); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            odd.remove(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        for (int i=0; i<(listSize/2); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if(((Integer)odd.get(i)).intValue() % 2 != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                fail("Failed to remove all even nubmers.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        List all = clone(odd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int i=0; i<(listSize/2); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            all.add(2*i, even.get(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (!all.equals(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            fail("Failed to reconstruct ints from odds and evens.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        all = clone(odd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        ListIterator itAll = all.listIterator(all.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        ListIterator itEven = even.listIterator(even.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        while (itEven.hasPrevious()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            itAll.previous();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            itAll.add(itEven.previous());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            itAll.previous(); // ???
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        itAll = all.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        while (itAll.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            Integer i = (Integer)itAll.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            itAll.set(new Integer(i.intValue()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        itAll = all.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        it = s.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        while(it.hasNext())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            if(it.next()==itAll.next())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                fail("Iterator.set failed to change value.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (!all.equals(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            fail("Failed to reconstruct ints with ListIterator.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        it = all.listIterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        int i=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            Object o = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            if (all.indexOf(o) != all.lastIndexOf(o))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                fail("Apparent duplicate detected.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (all.subList(i,   all.size()).indexOf(o) != 0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                all.subList(i+1, all.size()).indexOf(o) != -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                fail("subList/indexOf is screwy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (all.subList(0,i+1).lastIndexOf(o) != i)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                fail("subList/lastIndexOf is screwy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        List l = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        AddRandoms(l, listSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        Integer[] ia = (Integer[]) l.toArray(new Integer[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        if (!l.equals(Arrays.asList(ia)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            fail("toArray(Object[]) is hosed (1)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        ia = new Integer[listSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        Integer[] ib = (Integer[]) l.toArray(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (ia != ib || !l.equals(Arrays.asList(ia)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            fail("toArray(Object[]) is hosed (2)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        ia = new Integer[listSize+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        ia[listSize] = new Integer(69);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        ib = (Integer[]) l.toArray(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (ia != ib || ia[listSize] != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            || !l.equals(Arrays.asList(ia).subList(0, listSize)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            fail("toArray(Object[]) is hosed (3)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    // Done inefficiently so as to exercise toArray
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    static List clone(List s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        List a = Arrays.asList(s.toArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (s.hashCode() != a.hashCode())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            fail("Incorrect hashCode computation.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        List clone = newList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        clone.addAll(a);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (!s.equals(clone))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            fail("List not equal to copy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (!s.containsAll(clone))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            fail("List does not contain copy.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (!clone.containsAll(s))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            fail("Copy does not contain list.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        return clone;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    static List newList() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        List s =  Collections.checkedList(new ArrayList(), Integer.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (!s.isEmpty())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            fail("New instance non empty.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    static void AddRandoms(List s, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        for (int i=0; i<n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            int r = rnd.nextInt() % n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            Integer e = new Integer(r < 0 ? -r : r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            int preSize = s.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (!s.add(e))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                fail ("Add failed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            int postSize = s.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            if (postSize-preSize != 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                fail ("Add didn't increase size by 1.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    static void fail(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        throw new RuntimeException(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
}