blob: 4de8ca6ee11e8e78b63bdda23e641471db9e0ae1 [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
ohair2283b9d2010-05-25 15:58:33 -07002 * Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
duke6e45e102007-12-01 00:00:00 +00003 * 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 *
ohair2283b9d2010-05-25 15:58:33 -070019 * 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.
duke6e45e102007-12-01 00:00:00 +000022 */
23
24/*
25 * @test
26 * @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
27 * 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
martin4d0abcd2008-05-10 12:14:53 -070028 * 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
duke6e45e102007-12-01 00:00:00 +000029 * @summary Run many tests on many Collection and Map implementations
30 * @author Martin Buchholz
31 */
32
33/* Mother Of All (Collection) Tests
34 *
35 * Testing of collection classes is often spotty, because many tests
36 * need to be performed on many implementations, but the onus on
37 * writing the tests falls on the engineer introducing the new
38 * implementation.
39 *
40 * The idea of this mega-test is that:
41 *
42 * An engineer adding a new collection implementation could simply add
43 * their new implementation to a list of implementations in this
44 * test's main method. Any general purpose Collection<Integer> or
45 * Map<Integer,Integer> class is appropriate.
46 *
47 * An engineer fixing a regression could add their regression test here and
48 * simultaneously test all other implementations.
49 */
50
51import java.io.*;
52import java.util.*;
53import java.util.concurrent.*;
54import static java.util.Collections.*;
55
56public class MOAT {
57 public static void realMain(String[] args) {
58
59 testCollection(new LinkedHashSet<Integer>());
60 testCollection(new HashSet<Integer>());
61 testCollection(new Vector<Integer>());
62 testCollection(new Vector<Integer>().subList(0,0));
63 testCollection(new ArrayDeque<Integer>());
64 testCollection(new ArrayList<Integer>());
65 testCollection(new ArrayList<Integer>().subList(0,0));
66 testCollection(new LinkedList<Integer>());
67 testCollection(new LinkedList<Integer>().subList(0,0));
68 testCollection(new TreeSet<Integer>());
69
70 testCollection(new CopyOnWriteArrayList<Integer>());
71 testCollection(new CopyOnWriteArrayList<Integer>().subList(0,0));
72 testCollection(new CopyOnWriteArraySet<Integer>());
73 testCollection(new PriorityQueue<Integer>());
74 testCollection(new PriorityBlockingQueue<Integer>());
75 testCollection(new ArrayBlockingQueue<Integer>(20));
76 testCollection(new LinkedBlockingQueue<Integer>(20));
77 testCollection(new LinkedBlockingDeque<Integer>(20));
dlb818e9d2010-09-20 18:05:09 -070078 testCollection(new ConcurrentLinkedDeque<Integer>());
duke6e45e102007-12-01 00:00:00 +000079 testCollection(new ConcurrentLinkedQueue<Integer>());
dl54d3afc2009-11-02 17:25:38 -080080 testCollection(new LinkedTransferQueue<Integer>());
duke6e45e102007-12-01 00:00:00 +000081 testCollection(new ConcurrentSkipListSet<Integer>());
82 testCollection(Arrays.asList(new Integer(42)));
83 testCollection(Arrays.asList(1,2,3));
84 testCollection(nCopies(25,1));
85 testImmutableList(nCopies(25,1));
86 testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
87
88 testMap(new HashMap<Integer,Integer>());
89 testMap(new LinkedHashMap<Integer,Integer>());
90 testMap(new WeakHashMap<Integer,Integer>());
91 testMap(new IdentityHashMap<Integer,Integer>());
92 testMap(new TreeMap<Integer,Integer>());
93 testMap(new Hashtable<Integer,Integer>());
94 testMap(new ConcurrentHashMap<Integer,Integer>(10, 0.5f));
95 testMap(new ConcurrentSkipListMap<Integer,Integer>());
96
97 // Empty collections
98 final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
99 testCollection(emptyArray);
100 testEmptyList(emptyArray);
101 THROWS(IndexOutOfBoundsException.class,
102 new Fun(){void f(){ emptyArray.set(0,1); }});
103 THROWS(UnsupportedOperationException.class,
104 new Fun(){void f(){ emptyArray.add(0,1); }});
105
106 List<Integer> noOne = nCopies(0,1);
107 testCollection(noOne);
108 testEmptyList(noOne);
109 testImmutableList(noOne);
110
111 Set<Integer> emptySet = emptySet();
112 testCollection(emptySet);
113 testEmptySet(emptySet);
114 testEmptySet(EMPTY_SET);
115 testImmutableSet(emptySet);
116
117 List<Integer> emptyList = emptyList();
118 testCollection(emptyList);
119 testEmptyList(emptyList);
120 testEmptyList(EMPTY_LIST);
121 testImmutableList(emptyList);
122
123 Map<Integer,Integer> emptyMap = emptyMap();
124 testMap(emptyMap);
125 testEmptyMap(emptyMap);
126 testEmptyMap(EMPTY_MAP);
127 testImmutableMap(emptyMap);
128
129 // Singleton collections
130 Set<Integer> singletonSet = singleton(1);
131 equal(singletonSet.size(), 1);
132 testCollection(singletonSet);
133 testImmutableSet(singletonSet);
134
135 List<Integer> singletonList = singletonList(1);
136 equal(singletonList.size(), 1);
137 testCollection(singletonList);
138 testImmutableList(singletonList);
139 testImmutableList(singletonList.subList(0,1));
140 testImmutableList(singletonList.subList(0,1).subList(0,1));
141 testEmptyList(singletonList.subList(0,0));
142 testEmptyList(singletonList.subList(0,0).subList(0,0));
143
144 Map<Integer,Integer> singletonMap = singletonMap(1,2);
145 equal(singletonMap.size(), 1);
146 testMap(singletonMap);
147 testImmutableMap(singletonMap);
148 }
149
150 private static void checkContainsSelf(Collection<Integer> c) {
151 check(c.containsAll(c));
152 check(c.containsAll(Arrays.asList(c.toArray())));
153 check(c.containsAll(Arrays.asList(c.toArray(new Integer[0]))));
154 }
155
156 private static void checkContainsEmpty(Collection<Integer> c) {
157 check(c.containsAll(new ArrayList<Integer>()));
158 }
159
martineb76fa62008-05-10 11:49:25 -0700160 private static <T> void testEmptyCollection(Collection<T> c) {
duke6e45e102007-12-01 00:00:00 +0000161 check(c.isEmpty());
162 equal(c.size(), 0);
163 equal(c.toString(),"[]");
164 equal(c.toArray().length, 0);
165 equal(c.toArray(new Object[0]).length, 0);
dlb1e19572009-08-25 19:19:42 -0700166 check(c.toArray(new Object[]{42})[0] == null);
duke6e45e102007-12-01 00:00:00 +0000167
168 Object[] a = new Object[1]; a[0] = Boolean.TRUE;
169 equal(c.toArray(a), a);
170 equal(a[0], null);
martineb76fa62008-05-10 11:49:25 -0700171 testEmptyIterator(c.iterator());
172 }
173
174 static <T> void testEmptyIterator(final Iterator<T> it) {
175 if (rnd.nextBoolean())
176 check(! it.hasNext());
177
178 THROWS(NoSuchElementException.class,
179 new Fun(){void f(){ it.next(); }});
180
181 try { it.remove(); }
182 catch (IllegalStateException _) { pass(); }
183 catch (UnsupportedOperationException _) { pass(); }
184 catch (Throwable t) { unexpected(t); }
185
186 if (rnd.nextBoolean())
187 check(! it.hasNext());
duke6e45e102007-12-01 00:00:00 +0000188 }
189
190 private static void testEmptyList(List<?> c) {
191 testEmptyCollection(c);
192 equal(c.hashCode(), 1);
193 equal2(c, Collections.<Integer>emptyList());
194 }
195
martineb76fa62008-05-10 11:49:25 -0700196 private static <T> void testEmptySet(Set<T> c) {
duke6e45e102007-12-01 00:00:00 +0000197 testEmptyCollection(c);
198 equal(c.hashCode(), 0);
199 equal2(c, Collections.<Integer>emptySet());
martineb76fa62008-05-10 11:49:25 -0700200 if (c instanceof NavigableSet<?>)
201 testEmptyIterator(((NavigableSet<T>)c).descendingIterator());
duke6e45e102007-12-01 00:00:00 +0000202 }
203
204 private static void testImmutableCollection(final Collection<Integer> c) {
205 THROWS(UnsupportedOperationException.class,
206 new Fun(){void f(){ c.add(99); }},
207 new Fun(){void f(){ c.addAll(singleton(99)); }});
208 if (! c.isEmpty()) {
209 final Integer first = c.iterator().next();
210 THROWS(UnsupportedOperationException.class,
211 new Fun(){void f(){ c.clear(); }},
212 new Fun(){void f(){ c.remove(first); }},
213 new Fun(){void f(){ c.removeAll(singleton(first)); }},
214 new Fun(){void f(){ c.retainAll(emptyList()); }}
215 );
216 }
217 }
218
219 private static void testImmutableSet(final Set<Integer> c) {
220 testImmutableCollection(c);
221 }
222
223 private static void testImmutableList(final List<Integer> c) {
224 testList(c);
225 testImmutableCollection(c);
226 THROWS(UnsupportedOperationException.class,
227 new Fun(){void f(){ c.set(0,42); }},
228 new Fun(){void f(){ c.add(0,42); }},
229 new Fun(){void f(){ c.addAll(0,singleton(86)); }});
230 if (! c.isEmpty())
231 THROWS(UnsupportedOperationException.class,
232 new Fun(){void f(){
233 Iterator<Integer> it = c.iterator();
234 it.next(); it.remove();}},
235 new Fun(){void f(){
236 ListIterator<Integer> it = c.listIterator();
237 it.next(); it.remove();}});
238 }
239
240 private static void clear(Collection<Integer> c) {
241 try { c.clear(); }
242 catch (Throwable t) { unexpected(t); }
243 testEmptyCollection(c);
244 }
245
martineb76fa62008-05-10 11:49:25 -0700246 private static <K,V> void testEmptyMap(final Map<K,V> m) {
duke6e45e102007-12-01 00:00:00 +0000247 check(m.isEmpty());
248 equal(m.size(), 0);
249 equal(m.toString(),"{}");
250 testEmptySet(m.keySet());
251 testEmptySet(m.entrySet());
252 testEmptyCollection(m.values());
martin4d0abcd2008-05-10 12:14:53 -0700253
254 try { check(! m.containsValue(null)); }
255 catch (NullPointerException _) { /* OK */ }
256 try { check(! m.containsKey(null)); }
257 catch (NullPointerException _) { /* OK */ }
258 check(! m.containsValue(1));
259 check(! m.containsKey(1));
duke6e45e102007-12-01 00:00:00 +0000260 }
261
262 private static void testImmutableMap(final Map<Integer,Integer> m) {
263 THROWS(UnsupportedOperationException.class,
264 new Fun(){void f(){ m.put(1,1); }},
265 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }});
266 if (! m.isEmpty()) {
267 final Integer first = m.keySet().iterator().next();
268 THROWS(UnsupportedOperationException.class,
269 new Fun(){void f(){ m.remove(first); }},
270 new Fun(){void f(){ m.clear(); }});
271 final Map.Entry<Integer,Integer> me
272 = m.entrySet().iterator().next();
273 Integer key = me.getKey();
274 Integer val = me.getValue();
275 THROWS(UnsupportedOperationException.class,
276 new Fun(){void f(){ me.setValue(3); }});
277 equal(key, me.getKey());
278 equal(val, me.getValue());
279 }
280 testImmutableSet(m.keySet());
281 testImmutableCollection(m.values());
282 //testImmutableSet(m.entrySet());
283 }
284
285 private static void clear(Map<?,?> m) {
286 try { m.clear(); }
287 catch (Throwable t) { unexpected(t); }
288 testEmptyMap(m);
289 }
290
291 private static void oneElement(Collection<Integer> c) {
292 clear(c);
293 try {
294 check(c.add(-42));
295 equal(c.toString(), "[-42]");
296 if (c instanceof Set) check(! c.add(-42));
297 } catch (Throwable t) { unexpected(t); }
298 check(! c.isEmpty()); check(c.size() == 1);
299 }
300
301 private static boolean supportsAdd(Collection<Integer> c) {
302 try { check(c.add(778347983)); }
303 catch (UnsupportedOperationException t) { return false; }
304 catch (Throwable t) { unexpected(t); }
305
306 try {
307 check(c.contains(778347983));
308 check(c.remove(778347983));
309 } catch (Throwable t) { unexpected(t); }
310 return true;
311 }
312
313 private static boolean supportsRemove(Collection<Integer> c) {
314 try { check(! c.remove(19134032)); }
315 catch (UnsupportedOperationException t) { return false; }
316 catch (Throwable t) { unexpected(t); }
317 return true;
318 }
319
320 private static void checkFunctionalInvariants(Collection<Integer> c) {
321 try {
322 checkContainsSelf(c);
323 checkContainsEmpty(c);
324 check(c.size() != 0 ^ c.isEmpty());
325
326 {
327 int size = 0;
328 for (Integer i : c) size++;
329 check(c.size() == size);
330 }
331
332 check(c.toArray().length == c.size());
333 check(c.toArray().getClass() == Object[].class
334 ||
335 // !!!!
336 // 6260652: (coll) Arrays.asList(x).toArray().getClass()
337 // should be Object[].class
338 (c.getClass().getName().equals("java.util.Arrays$ArrayList"))
339 );
340 for (int size : new int[]{0,1,c.size(), c.size()+1}) {
341 Integer[] a = c.toArray(new Integer[size]);
342 check((size > c.size()) || a.length == c.size());
343 int i = 0; for (Integer j : c) check(a[i++] == j);
344 check((size <= c.size()) || (a[c.size()] == null));
345 check(a.getClass() == Integer[].class);
346 }
347
348 check(c.equals(c));
349 if (c instanceof Serializable) {
350 //System.out.printf("Serializing %s%n", c.getClass().getName());
351 try {
352 Object clone = serialClone(c);
353 equal(c instanceof Serializable,
354 clone instanceof Serializable);
355 equal(c instanceof RandomAccess,
356 clone instanceof RandomAccess);
357 if ((c instanceof List) || (c instanceof Set))
358 equal(c, clone);
359 else
360 equal(new HashSet<Integer>(c),
361 new HashSet<Integer>(serialClone(c)));
362 } catch (Error xxx) {
363 if (! (xxx.getCause() instanceof NotSerializableException))
364 throw xxx;
365 }
366 }
367 }
368 catch (Throwable t) { unexpected(t); }
369 }
370
371 //----------------------------------------------------------------
372 // If add(null) succeeds, contains(null) & remove(null) should succeed
373 //----------------------------------------------------------------
374 private static void testNullElement(Collection<Integer> c) {
375 // !!!! 5018849: (coll) TreeSet.contains(null) does not agree with Javadoc
376 if (c instanceof TreeSet) return;
377
378 try {
379 check(c.add(null));
380 if (c.size() == 1)
381 equal(c.toString(), "[null]");
382 try {
383 checkFunctionalInvariants(c);
384 check(c.contains(null));
385 check(c.remove(null));
386 }
387 catch (Throwable t) { unexpected(t); }
388 }
389 catch (NullPointerException e) { /* OK */ }
390 catch (Throwable t) { unexpected(t); }
391 }
392
393
394 //----------------------------------------------------------------
395 // If add("x") succeeds, contains("x") & remove("x") should succeed
396 //----------------------------------------------------------------
397 @SuppressWarnings("unchecked")
398 private static void testStringElement(Collection<Integer> c) {
399 Collection x = (Collection)c; // Make type-unsafe
400 try {
401 check(x.add("x"));
402 try {
403 check(x.contains("x"));
404 check(x.remove("x"));
405 } catch (Throwable t) { unexpected(t); }
406 }
407 catch (ClassCastException e) { /* OK */ }
408 catch (Throwable t) { unexpected(t); }
409 }
410
411 private static void testConcurrentCollection(Collection<Integer> c) {
412 try {
413 c.add(1);
414 Iterator<Integer> it = c.iterator();
415 check(it.hasNext());
416 clear(c);
417 check(it.next() instanceof Integer); // No CME
418 check(c.isEmpty());
419 }
420 catch (Throwable t) { unexpected(t); }
421 }
422
423 private static void testQueue(Queue<Integer> q) {
424 q.clear();
martinffd2d052009-11-05 16:12:45 -0800425 for (int i = 0; i < 5; i++) {
426 testQueueAddRemove(q, null);
427 testQueueAddRemove(q, 537);
duke6e45e102007-12-01 00:00:00 +0000428 q.add(i);
martinffd2d052009-11-05 16:12:45 -0800429 }
duke6e45e102007-12-01 00:00:00 +0000430 equal(q.size(), 5);
431 checkFunctionalInvariants(q);
432 q.poll();
433 equal(q.size(), 4);
434 checkFunctionalInvariants(q);
dlb818e9d2010-09-20 18:05:09 -0700435 if ((q instanceof LinkedBlockingQueue) ||
436 (q instanceof LinkedBlockingDeque) ||
437 (q instanceof ConcurrentLinkedDeque) ||
dl8e56eb52009-07-28 17:17:55 -0700438 (q instanceof ConcurrentLinkedQueue)) {
439 testQueueIteratorRemove(q);
440 }
441 }
442
martinffd2d052009-11-05 16:12:45 -0800443 private static void testQueueAddRemove(final Queue<Integer> q,
444 final Integer e) {
445 final List<Integer> originalContents = new ArrayList<Integer>(q);
446 final boolean isEmpty = q.isEmpty();
447 final boolean isList = (q instanceof List);
448 final List asList = isList ? (List) q : null;
449 check(!q.contains(e));
450 try {
451 q.add(e);
452 } catch (NullPointerException npe) {
453 check(e == null);
454 return; // Null elements not supported
455 }
456 check(q.contains(e));
457 check(q.remove(e));
458 check(!q.contains(e));
459 equal(new ArrayList<Integer>(q), originalContents);
460
461 if (q instanceof Deque<?>) {
462 final Deque<Integer> deq = (Deque<Integer>) q;
463 final List<Integer> singleton = Collections.singletonList(e);
464
465 // insert, query, remove element at head
466 if (isEmpty) {
467 THROWS(NoSuchElementException.class,
468 new Fun(){void f(){ deq.getFirst(); }},
469 new Fun(){void f(){ deq.element(); }},
470 new Fun(){void f(){ deq.iterator().next(); }});
471 check(deq.peekFirst() == null);
472 check(deq.peek() == null);
473 } else {
474 check(deq.getFirst() != e);
475 check(deq.element() != e);
476 check(deq.iterator().next() != e);
477 check(deq.peekFirst() != e);
478 check(deq.peek() != e);
479 }
480 check(!deq.contains(e));
481 check(!deq.removeFirstOccurrence(e));
482 check(!deq.removeLastOccurrence(e));
483 if (isList) {
484 check(asList.indexOf(e) == -1);
485 check(asList.lastIndexOf(e) == -1);
486 }
487 switch (rnd.nextInt(isList ? 4 : 3)) {
488 case 0: deq.addFirst(e); break;
489 case 1: check(deq.offerFirst(e)); break;
490 case 2: deq.push(e); break;
491 case 3: asList.add(0, e); break;
492 default: throw new AssertionError();
493 }
494 check(deq.peekFirst() == e);
495 check(deq.getFirst() == e);
496 check(deq.element() == e);
497 check(deq.peek() == e);
498 check(deq.iterator().next() == e);
499 check(deq.contains(e));
500 if (isList) {
501 check(asList.get(0) == e);
502 check(asList.indexOf(e) == 0);
503 check(asList.lastIndexOf(e) == 0);
504 check(asList.subList(0, 1).equals(singleton));
505 }
506 switch (rnd.nextInt(isList ? 11 : 9)) {
507 case 0: check(deq.pollFirst() == e); break;
508 case 1: check(deq.removeFirst() == e); break;
509 case 2: check(deq.remove() == e); break;
510 case 3: check(deq.pop() == e); break;
511 case 4: check(deq.removeFirstOccurrence(e)); break;
512 case 5: check(deq.removeLastOccurrence(e)); break;
513 case 6: check(deq.remove(e)); break;
514 case 7: check(deq.removeAll(singleton)); break;
515 case 8: Iterator it = deq.iterator(); it.next(); it.remove(); break;
516 case 9: asList.remove(0); break;
517 case 10: asList.subList(0, 1).clear(); break;
518 default: throw new AssertionError();
519 }
520 if (isEmpty) {
521 THROWS(NoSuchElementException.class,
522 new Fun(){void f(){ deq.getFirst(); }},
523 new Fun(){void f(){ deq.element(); }},
524 new Fun(){void f(){ deq.iterator().next(); }});
525 check(deq.peekFirst() == null);
526 check(deq.peek() == null);
527 } else {
528 check(deq.getFirst() != e);
529 check(deq.element() != e);
530 check(deq.iterator().next() != e);
531 check(deq.peekFirst() != e);
532 check(deq.peek() != e);
533 }
534 check(!deq.contains(e));
535 check(!deq.removeFirstOccurrence(e));
536 check(!deq.removeLastOccurrence(e));
537 if (isList) {
538 check(isEmpty || asList.get(0) != e);
539 check(asList.indexOf(e) == -1);
540 check(asList.lastIndexOf(e) == -1);
541 }
542 equal(new ArrayList<Integer>(deq), originalContents);
543
544 // insert, query, remove element at tail
545 if (isEmpty) {
546 check(deq.peekLast() == null);
547 THROWS(NoSuchElementException.class,
548 new Fun(){void f(){ deq.getLast(); }});
549 } else {
550 check(deq.peekLast() != e);
551 check(deq.getLast() != e);
552 }
553 switch (rnd.nextInt(isList ? 6 : 4)) {
554 case 0: deq.addLast(e); break;
555 case 1: check(deq.offerLast(e)); break;
556 case 2: check(deq.add(e)); break;
557 case 3: deq.addAll(singleton); break;
558 case 4: asList.addAll(deq.size(), singleton); break;
559 case 5: asList.add(deq.size(), e); break;
560 default: throw new AssertionError();
561 }
562 check(deq.peekLast() == e);
563 check(deq.getLast() == e);
564 check(deq.contains(e));
565 if (isList) {
566 ListIterator it = asList.listIterator(asList.size());
567 check(it.previous() == e);
568 check(asList.get(asList.size() - 1) == e);
569 check(asList.indexOf(e) == asList.size() - 1);
570 check(asList.lastIndexOf(e) == asList.size() - 1);
571 int size = asList.size();
572 check(asList.subList(size - 1, size).equals(singleton));
573 }
574 switch (rnd.nextInt(isList ? 8 : 6)) {
575 case 0: check(deq.pollLast() == e); break;
576 case 1: check(deq.removeLast() == e); break;
577 case 2: check(deq.removeFirstOccurrence(e)); break;
578 case 3: check(deq.removeLastOccurrence(e)); break;
579 case 4: check(deq.remove(e)); break;
580 case 5: check(deq.removeAll(singleton)); break;
581 case 6: asList.remove(asList.size() - 1); break;
582 case 7:
583 ListIterator it = asList.listIterator(asList.size());
584 it.previous();
585 it.remove();
586 break;
587 default: throw new AssertionError();
588 }
589 if (isEmpty) {
590 check(deq.peekLast() == null);
591 THROWS(NoSuchElementException.class,
592 new Fun(){void f(){ deq.getLast(); }});
593 } else {
594 check(deq.peekLast() != e);
595 check(deq.getLast() != e);
596 }
597 check(!deq.contains(e));
598 equal(new ArrayList<Integer>(deq), originalContents);
599
600 // Test operations on empty deque
601 switch (rnd.nextInt(isList ? 4 : 2)) {
602 case 0: deq.clear(); break;
603 case 1:
604 Iterator it = deq.iterator();
605 while (it.hasNext()) {
606 it.next();
607 it.remove();
608 }
609 break;
610 case 2: asList.subList(0, asList.size()).clear(); break;
611 case 3:
612 ListIterator lit = asList.listIterator(asList.size());
613 while (lit.hasPrevious()) {
614 lit.previous();
615 lit.remove();
616 }
617 break;
618 default: throw new AssertionError();
619 }
620 testEmptyCollection(deq);
621 check(!deq.iterator().hasNext());
622 if (isList) {
623 check(!asList.listIterator().hasPrevious());
624 THROWS(NoSuchElementException.class,
625 new Fun(){void f(){ asList.listIterator().previous(); }});
626 }
627 THROWS(NoSuchElementException.class,
628 new Fun(){void f(){ deq.iterator().next(); }},
629 new Fun(){void f(){ deq.element(); }},
630 new Fun(){void f(){ deq.getFirst(); }},
631 new Fun(){void f(){ deq.getLast(); }},
632 new Fun(){void f(){ deq.pop(); }},
633 new Fun(){void f(){ deq.remove(); }},
634 new Fun(){void f(){ deq.removeFirst(); }},
635 new Fun(){void f(){ deq.removeLast(); }});
636
637 check(deq.poll() == null);
638 check(deq.pollFirst() == null);
639 check(deq.pollLast() == null);
640 check(deq.peek() == null);
641 check(deq.peekFirst() == null);
642 check(deq.peekLast() == null);
643 check(!deq.removeFirstOccurrence(e));
644 check(!deq.removeLastOccurrence(e));
645
646 check(deq.addAll(originalContents) == !isEmpty);
647 equal(new ArrayList<Integer>(deq), originalContents);
648 check(!deq.addAll(Collections.<Integer>emptyList()));
649 equal(new ArrayList<Integer>(deq), originalContents);
650 }
651 }
652
dl8e56eb52009-07-28 17:17:55 -0700653 private static void testQueueIteratorRemove(Queue<Integer> q) {
654 System.err.printf("testQueueIteratorRemove %s%n",
655 q.getClass().getSimpleName());
656 q.clear();
657 for (int i = 0; i < 5; i++)
658 q.add(i);
659 Iterator<Integer> it = q.iterator();
660 check(it.hasNext());
661 for (int i = 3; i >= 0; i--)
662 q.remove(i);
663 equal(it.next(), 0);
664 equal(it.next(), 4);
665
666 q.clear();
667 for (int i = 0; i < 5; i++)
668 q.add(i);
669 it = q.iterator();
670 equal(it.next(), 0);
671 check(it.hasNext());
672 for (int i = 1; i < 4; i++)
673 q.remove(i);
674 equal(it.next(), 1);
675 equal(it.next(), 4);
duke6e45e102007-12-01 00:00:00 +0000676 }
677
678 private static void testList(final List<Integer> l) {
679 //----------------------------------------------------------------
680 // 4802633: (coll) AbstractList.addAll(-1,emptyCollection)
681 // doesn't throw IndexOutOfBoundsException
682 //----------------------------------------------------------------
683 try {
684 l.addAll(-1, Collections.<Integer>emptyList());
685 fail("Expected IndexOutOfBoundsException not thrown");
686 }
687 catch (UnsupportedOperationException _) {/* OK */}
688 catch (IndexOutOfBoundsException _) {/* OK */}
689 catch (Throwable t) { unexpected(t); }
690
691// equal(l instanceof Serializable,
692// l.subList(0,0) instanceof Serializable);
693 if (l.subList(0,0) instanceof Serializable)
694 check(l instanceof Serializable);
695
696 equal(l instanceof RandomAccess,
697 l.subList(0,0) instanceof RandomAccess);
698 }
699
700 private static void testCollection(Collection<Integer> c) {
dl8e56eb52009-07-28 17:17:55 -0700701 try { testCollection1(c); }
702 catch (Throwable t) { unexpected(t); }
703 }
704
705 private static void testCollection1(Collection<Integer> c) {
duke6e45e102007-12-01 00:00:00 +0000706
707 System.out.println("\n==> " + c.getClass().getName());
708
709 checkFunctionalInvariants(c);
710
711 if (! supportsAdd(c)) return;
712 //System.out.println("add() supported");
713
martineb76fa62008-05-10 11:49:25 -0700714 if (c instanceof NavigableSet) {
715 System.out.println("NavigableSet tests...");
716
717 NavigableSet<Integer> ns = (NavigableSet<Integer>)c;
718 testNavigableSet(ns);
719 testNavigableSet(ns.headSet(6, false));
720 testNavigableSet(ns.headSet(5, true));
721 testNavigableSet(ns.tailSet(0, false));
722 testNavigableSet(ns.tailSet(1, true));
723 testNavigableSet(ns.subSet(0, false, 5, true));
724 testNavigableSet(ns.subSet(1, true, 6, false));
725 }
duke6e45e102007-12-01 00:00:00 +0000726
727 if (c instanceof Queue)
728 testQueue((Queue<Integer>)c);
729
730 if (c instanceof List)
731 testList((List<Integer>)c);
732
733 check(supportsRemove(c));
734
735 try {
736 oneElement(c);
737 checkFunctionalInvariants(c);
738 }
739 catch (Throwable t) { unexpected(t); }
740
741 clear(c); testNullElement(c);
742 oneElement(c); testNullElement(c);
743
744 clear(c); testStringElement(c);
745 oneElement(c); testStringElement(c);
746
747 if (c.getClass().getName().matches(".*concurrent.*"))
748 testConcurrentCollection(c);
749
750 //----------------------------------------------------------------
751 // The "all" operations should throw NPE when passed null
752 //----------------------------------------------------------------
753 {
754 oneElement(c);
755 try {
756 c.removeAll(null);
757 fail("Expected NullPointerException");
758 }
759 catch (NullPointerException e) { pass(); }
760 catch (Throwable t) { unexpected(t); }
761
762 oneElement(c);
763 try {
764 c.retainAll(null);
765 fail("Expected NullPointerException");
766 }
767 catch (NullPointerException e) { pass(); }
768 catch (Throwable t) { unexpected(t); }
769
770 oneElement(c);
771 try {
772 c.addAll(null);
773 fail("Expected NullPointerException");
774 }
775 catch (NullPointerException e) { pass(); }
776 catch (Throwable t) { unexpected(t); }
777
778 oneElement(c);
779 try {
780 c.containsAll(null);
781 fail("Expected NullPointerException");
782 }
783 catch (NullPointerException e) { pass(); }
784 catch (Throwable t) { unexpected(t); }
785 }
786 }
787
788 //----------------------------------------------------------------
789 // Map
790 //----------------------------------------------------------------
791 private static void checkFunctionalInvariants(Map<Integer,Integer> m) {
792 check(m.keySet().size() == m.entrySet().size());
793 check(m.keySet().size() == m.size());
794 checkFunctionalInvariants(m.keySet());
795 checkFunctionalInvariants(m.values());
796 check(m.size() != 0 ^ m.isEmpty());
797 }
798
799 private static void testMap(Map<Integer,Integer> m) {
800 System.out.println("\n==> " + m.getClass().getName());
801
802 if (m instanceof ConcurrentMap)
803 testConcurrentMap((ConcurrentMap<Integer,Integer>) m);
804
martineb76fa62008-05-10 11:49:25 -0700805 if (m instanceof NavigableMap) {
806 System.out.println("NavigableMap tests...");
807
808 NavigableMap<Integer,Integer> nm =
809 (NavigableMap<Integer,Integer>) m;
dl0ad61612009-03-24 19:42:23 -0700810 testNavigableMapRemovers(nm);
martineb76fa62008-05-10 11:49:25 -0700811 testNavigableMap(nm);
812 testNavigableMap(nm.headMap(6, false));
813 testNavigableMap(nm.headMap(5, true));
814 testNavigableMap(nm.tailMap(0, false));
815 testNavigableMap(nm.tailMap(1, true));
816 testNavigableMap(nm.subMap(1, true, 6, false));
817 testNavigableMap(nm.subMap(0, false, 5, true));
818 }
duke6e45e102007-12-01 00:00:00 +0000819
820 checkFunctionalInvariants(m);
821
822 if (supportsClear(m)) {
823 try { clear(m); }
824 catch (Throwable t) { unexpected(t); }
825 }
826
827 if (supportsPut(m)) {
828 try {
829 check(m.put(3333, 77777) == null);
830 check(m.put(9134, 74982) == null);
831 check(m.get(9134) == 74982);
832 check(m.put(9134, 1382) == 74982);
833 check(m.get(9134) == 1382);
834 check(m.size() == 2);
835 checkFunctionalInvariants(m);
836 checkNPEConsistency(m);
837 }
838 catch (Throwable t) { unexpected(t); }
839 }
840 }
841
842 private static boolean supportsPut(Map<Integer,Integer> m) {
843 // We're asking for .equals(...) semantics
844 if (m instanceof IdentityHashMap) return false;
845
846 try { check(m.put(778347983,12735) == null); }
847 catch (UnsupportedOperationException t) { return false; }
848 catch (Throwable t) { unexpected(t); }
849
850 try {
851 check(m.containsKey(778347983));
852 check(m.remove(778347983) != null);
853 } catch (Throwable t) { unexpected(t); }
854 return true;
855 }
856
857 private static boolean supportsClear(Map<?,?> m) {
858 try { m.clear(); }
859 catch (UnsupportedOperationException t) { return false; }
860 catch (Throwable t) { unexpected(t); }
861 return true;
862 }
863
864 //----------------------------------------------------------------
865 // ConcurrentMap
866 //----------------------------------------------------------------
867 private static void testConcurrentMap(ConcurrentMap<Integer,Integer> m) {
868 System.out.println("ConcurrentMap tests...");
869
870 try {
871 clear(m);
872
873 check(m.putIfAbsent(18357,7346) == null);
874 check(m.containsKey(18357));
875 check(m.putIfAbsent(18357,8263) == 7346);
876 try { m.putIfAbsent(18357,null); fail("NPE"); }
877 catch (NullPointerException t) { }
878 check(m.containsKey(18357));
879
880 check(! m.replace(18357,8888,7777));
881 check(m.containsKey(18357));
882 try { m.replace(18357,null,7777); fail("NPE"); }
883 catch (NullPointerException t) { }
884 check(m.containsKey(18357));
885 check(m.get(18357) == 7346);
886 check(m.replace(18357,7346,5555));
887 check(m.replace(18357,5555,7346));
888 check(m.get(18357) == 7346);
889
890 check(m.replace(92347,7834) == null);
891 try { m.replace(18357,null); fail("NPE"); }
892 catch (NullPointerException t) { }
893 check(m.replace(18357,7346) == 7346);
894 check(m.replace(18357,5555) == 7346);
895 check(m.get(18357) == 5555);
896 check(m.replace(18357,7346) == 5555);
897 check(m.get(18357) == 7346);
898
899 check(! m.remove(18357,9999));
900 check(m.get(18357) == 7346);
901 check(m.containsKey(18357));
902 check(! m.remove(18357,null)); // 6272521
903 check(m.get(18357) == 7346);
904 check(m.remove(18357,7346));
905 check(m.get(18357) == null);
906 check(! m.containsKey(18357));
907 check(m.isEmpty());
908
909 m.putIfAbsent(1,2);
910 check(m.size() == 1);
911 check(! m.remove(1,null));
912 check(! m.remove(1,null));
913 check(! m.remove(1,1));
914 check(m.remove(1,2));
915 check(m.isEmpty());
916
917 testEmptyMap(m);
918 }
919 catch (Throwable t) { unexpected(t); }
920 }
921
922 private static void throwsConsistently(Class<? extends Throwable> k,
923 Iterable<Fun> fs) {
924 List<Class<? extends Throwable>> threw
925 = new ArrayList<Class<? extends Throwable>>();
926 for (Fun f : fs)
927 try { f.f(); threw.add(null); }
928 catch (Throwable t) {
929 check(k.isAssignableFrom(t.getClass()));
930 threw.add(t.getClass());
931 }
932 if (new HashSet<Object>(threw).size() != 1)
933 fail(threw.toString());
934 }
935
936 private static <T> void checkNPEConsistency(final Map<T,Integer> m) {
937 m.clear();
938 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap)
939 ? (ConcurrentMap<T,Integer>) m
940 : null;
941 List<Fun> fs = new ArrayList<Fun>();
942 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}});
943 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}});
944 fs.add(new Fun(){void f(){ equal(m.get(null), null);}});
945 if (cm != null) {
946 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});}
947 throwsConsistently(NullPointerException.class, fs);
948
949 fs.clear();
950 final Map<T,Integer> sm = singletonMap(null,1);
951 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}});
952 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}});
953 if (cm != null) {
954 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});
955 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}});
956 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}});
957 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}});
958 }
959 throwsConsistently(NullPointerException.class, fs);
960 }
961
962 //----------------------------------------------------------------
963 // NavigableMap
964 //----------------------------------------------------------------
965 private static void
966 checkNavigableMapKeys(NavigableMap<Integer,Integer> m,
967 Integer i,
968 Integer lower,
969 Integer floor,
970 Integer ceiling,
971 Integer higher) {
972 equal(m.lowerKey(i), lower);
973 equal(m.floorKey(i), floor);
974 equal(m.ceilingKey(i), ceiling);
975 equal(m.higherKey(i), higher);
976 }
977
978 private static void
979 checkNavigableSetKeys(NavigableSet<Integer> m,
980 Integer i,
981 Integer lower,
982 Integer floor,
983 Integer ceiling,
984 Integer higher) {
985 equal(m.lower(i), lower);
986 equal(m.floor(i), floor);
987 equal(m.ceiling(i), ceiling);
988 equal(m.higher(i), higher);
989 }
990
991 static final Random rnd = new Random();
992 static void equalNext(final Iterator<?> it, Object expected) {
993 if (rnd.nextBoolean())
994 check(it.hasNext());
995 equal(it.next(), expected);
996 }
997
dl0ad61612009-03-24 19:42:23 -0700998 static void equalMaps(Map m1, Map m2) {
999 equal(m1, m2);
1000 equal(m2, m1);
1001 equal(m1.size(), m2.size());
1002 equal(m1.isEmpty(), m2.isEmpty());
1003 equal(m1.toString(), m2.toString());
1004 check(Arrays.equals(m1.entrySet().toArray(), m2.entrySet().toArray()));
1005 }
1006
1007 @SuppressWarnings({"unchecked", "rawtypes"})
1008 static void testNavigableMapRemovers(NavigableMap m)
1009 {
1010 final Map emptyMap = new HashMap();
1011
1012 final Map singletonMap = new HashMap();
1013 singletonMap.put(1, 2);
1014
1015 abstract class NavigableMapView {
1016 abstract NavigableMap view(NavigableMap m);
1017 }
1018
1019 NavigableMapView[] views = {
1020 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1021 return m; }},
1022 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1023 return m.headMap(99, true); }},
1024 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1025 return m.tailMap(-99, false); }},
1026 new NavigableMapView() { NavigableMap view(NavigableMap m) {
1027 return m.subMap(-99, true, 99, false); }},
1028 };
1029
1030 abstract class Remover {
1031 abstract void remove(NavigableMap m, Object k, Object v);
1032 }
1033
1034 Remover[] removers = {
1035 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1036 equal(m.remove(k), v); }},
1037
1038 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1039 equal(m.descendingMap().remove(k), v); }},
1040 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1041 equal(m.descendingMap().headMap(-86, false).remove(k), v); }},
1042 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1043 equal(m.descendingMap().tailMap(86, true).remove(k), v); }},
1044
1045 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1046 equal(m.headMap(86, true).remove(k), v); }},
1047 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1048 equal(m.tailMap(-86, true).remove(k), v); }},
1049 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1050 equal(m.subMap(-86, false, 86, true).remove(k), v); }},
1051
1052 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1053 check(m.keySet().remove(k)); }},
1054 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1055 check(m.navigableKeySet().remove(k)); }},
1056
1057 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1058 check(m.navigableKeySet().headSet(86, true).remove(k)); }},
1059 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1060 check(m.navigableKeySet().tailSet(-86, false).remove(k)); }},
1061 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1062 check(m.navigableKeySet().subSet(-86, true, 86, false)
1063 .remove(k)); }},
1064
1065 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1066 check(m.descendingKeySet().headSet(-86, false).remove(k)); }},
1067 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1068 check(m.descendingKeySet().tailSet(86, true).remove(k)); }},
1069 new Remover() { void remove(NavigableMap m, Object k, Object v) {
1070 check(m.descendingKeySet().subSet(86, true, -86, false)
1071 .remove(k)); }},
1072 };
1073
1074 for (NavigableMapView view : views) {
1075 for (Remover remover : removers) {
1076 try {
1077 m.clear();
1078 equalMaps(m, emptyMap);
1079 equal(m.put(1, 2), null);
1080 equalMaps(m, singletonMap);
1081 NavigableMap v = view.view(m);
1082 remover.remove(v, 1, 2);
1083 equalMaps(m, emptyMap);
1084 } catch (Throwable t) { unexpected(t); }
1085 }
1086 }
1087 }
1088
duke6e45e102007-12-01 00:00:00 +00001089 private static void testNavigableMap(NavigableMap<Integer,Integer> m)
1090 {
duke6e45e102007-12-01 00:00:00 +00001091 clear(m);
1092 checkNavigableMapKeys(m, 1, null, null, null, null);
1093
1094 equal(m.put(1, 2), null);
1095 equal(m.put(3, 4), null);
1096 equal(m.put(5, 9), null);
1097
1098 equal(m.put(1, 2), 2);
1099 equal(m.put(3, 4), 4);
1100 equal(m.put(5, 6), 9);
1101
1102 checkNavigableMapKeys(m, 0, null, null, 1, 1);
1103 checkNavigableMapKeys(m, 1, null, 1, 1, 3);
1104 checkNavigableMapKeys(m, 2, 1, 1, 3, 3);
1105 checkNavigableMapKeys(m, 3, 1, 3, 3, 5);
1106 checkNavigableMapKeys(m, 5, 3, 5, 5, null);
1107 checkNavigableMapKeys(m, 6, 5, 5, null, null);
1108
martineb76fa62008-05-10 11:49:25 -07001109 for (final Iterator<Integer> it :
1110 (Iterator<Integer>[])
1111 new Iterator<?>[] {
1112 m.descendingKeySet().iterator(),
1113 m.navigableKeySet().descendingIterator()}) {
duke6e45e102007-12-01 00:00:00 +00001114 equalNext(it, 5);
1115 equalNext(it, 3);
1116 equalNext(it, 1);
1117 check(! it.hasNext());
1118 THROWS(NoSuchElementException.class,
1119 new Fun(){void f(){it.next();}});
1120 }
1121
1122 {
1123 final Iterator<Map.Entry<Integer,Integer>> it
1124 = m.descendingMap().entrySet().iterator();
1125 check(it.hasNext()); equal(it.next().getKey(), 5);
1126 check(it.hasNext()); equal(it.next().getKey(), 3);
1127 check(it.hasNext()); equal(it.next().getKey(), 1);
1128 check(! it.hasNext());
1129 THROWS(NoSuchElementException.class,
1130 new Fun(){void f(){it.next();}});
1131 }
1132 }
1133
1134
1135 private static void testNavigableSet(NavigableSet<Integer> s) {
duke6e45e102007-12-01 00:00:00 +00001136 clear(s);
1137 checkNavigableSetKeys(s, 1, null, null, null, null);
1138
1139 check(s.add(1));
1140 check(s.add(3));
1141 check(s.add(5));
1142
1143 check(! s.add(1));
1144 check(! s.add(3));
1145 check(! s.add(5));
1146
1147 checkNavigableSetKeys(s, 0, null, null, 1, 1);
1148 checkNavigableSetKeys(s, 1, null, 1, 1, 3);
1149 checkNavigableSetKeys(s, 2, 1, 1, 3, 3);
1150 checkNavigableSetKeys(s, 3, 1, 3, 3, 5);
1151 checkNavigableSetKeys(s, 5, 3, 5, 5, null);
1152 checkNavigableSetKeys(s, 6, 5, 5, null, null);
1153
martineb76fa62008-05-10 11:49:25 -07001154 for (final Iterator<Integer> it :
1155 (Iterator<Integer>[])
1156 new Iterator<?>[] {
1157 s.descendingIterator(),
1158 s.descendingSet().iterator()}) {
duke6e45e102007-12-01 00:00:00 +00001159 equalNext(it, 5);
1160 equalNext(it, 3);
1161 equalNext(it, 1);
1162 check(! it.hasNext());
1163 THROWS(NoSuchElementException.class,
1164 new Fun(){void f(){it.next();}});
1165 }
1166 }
1167
1168 //--------------------- Infrastructure ---------------------------
1169 static volatile int passed = 0, failed = 0;
1170 static void pass() { passed++; }
1171 static void fail() { failed++; Thread.dumpStack(); }
1172 static void fail(String msg) { System.out.println(msg); fail(); }
1173 static void unexpected(Throwable t) { failed++; t.printStackTrace(); }
1174 static void check(boolean cond) { if (cond) pass(); else fail(); }
1175 static void equal(Object x, Object y) {
1176 if (x == null ? y == null : x.equals(y)) pass();
1177 else {System.out.println(x + " not equal to " + y); fail();}}
1178 static void equal2(Object x, Object y) {equal(x, y); equal(y, x);}
1179 public static void main(String[] args) throws Throwable {
1180 try { realMain(args); } catch (Throwable t) { unexpected(t); }
1181
1182 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
1183 if (failed > 0) throw new Exception("Some tests failed");
1184 }
1185 private static abstract class Fun {abstract void f() throws Throwable;}
1186 private static void THROWS(Class<? extends Throwable> k, Fun... fs) {
1187 for (Fun f : fs)
1188 try { f.f(); fail("Expected " + k.getName() + " not thrown"); }
1189 catch (Throwable t) {
1190 if (k.isAssignableFrom(t.getClass())) pass();
1191 else unexpected(t);}}
1192 static byte[] serializedForm(Object obj) {
1193 try {
1194 ByteArrayOutputStream baos = new ByteArrayOutputStream();
1195 new ObjectOutputStream(baos).writeObject(obj);
1196 return baos.toByteArray();
1197 } catch (IOException e) { throw new Error(e); }}
1198 static Object readObject(byte[] bytes)
1199 throws IOException, ClassNotFoundException {
1200 InputStream is = new ByteArrayInputStream(bytes);
1201 return new ObjectInputStream(is).readObject();}
1202 @SuppressWarnings("unchecked")
1203 static <T> T serialClone(T obj) {
1204 try { return (T) readObject(serializedForm(obj)); }
1205 catch (Exception e) { throw new Error(e); }}
1206}