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