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