1 /* |
1 /* |
2 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
115 |
115 |
116 // Empty collections |
116 // Empty collections |
117 final List<Integer> emptyArray = Arrays.asList(new Integer[]{}); |
117 final List<Integer> emptyArray = Arrays.asList(new Integer[]{}); |
118 testCollection(emptyArray); |
118 testCollection(emptyArray); |
119 testEmptyList(emptyArray); |
119 testEmptyList(emptyArray); |
120 THROWS(IndexOutOfBoundsException.class, |
120 THROWS(IndexOutOfBoundsException.class, () -> emptyArray.set(0,1)); |
121 new Fun(){void f(){ emptyArray.set(0,1); }}); |
121 THROWS(UnsupportedOperationException.class, () -> emptyArray.add(0,1)); |
122 THROWS(UnsupportedOperationException.class, |
|
123 new Fun(){void f(){ emptyArray.add(0,1); }}); |
|
124 |
122 |
125 List<Integer> noOne = nCopies(0,1); |
123 List<Integer> noOne = nCopies(0,1); |
126 testCollection(noOne); |
124 testCollection(noOne); |
127 testEmptyList(noOne); |
125 testEmptyList(noOne); |
128 testImmutableList(noOne); |
126 testImmutableList(noOne); |
202 |
200 |
203 static <T> void testEmptyIterator(final Iterator<T> it) { |
201 static <T> void testEmptyIterator(final Iterator<T> it) { |
204 if (rnd.nextBoolean()) |
202 if (rnd.nextBoolean()) |
205 check(! it.hasNext()); |
203 check(! it.hasNext()); |
206 |
204 |
207 THROWS(NoSuchElementException.class, |
205 THROWS(NoSuchElementException.class, () -> it.next()); |
208 new Fun(){void f(){ it.next(); }}); |
|
209 |
206 |
210 try { it.remove(); } |
207 try { it.remove(); } |
211 catch (IllegalStateException ignored) { pass(); } |
208 catch (IllegalStateException ignored) { pass(); } |
212 catch (UnsupportedOperationException ignored) { pass(); } |
209 catch (UnsupportedOperationException ignored) { pass(); } |
213 catch (Throwable t) { unexpected(t); } |
210 catch (Throwable t) { unexpected(t); } |
230 testEmptyIterator(((NavigableSet<T>)c).descendingIterator()); |
227 testEmptyIterator(((NavigableSet<T>)c).descendingIterator()); |
231 } |
228 } |
232 |
229 |
233 private static void testImmutableCollection(final Collection<Integer> c) { |
230 private static void testImmutableCollection(final Collection<Integer> c) { |
234 THROWS(UnsupportedOperationException.class, |
231 THROWS(UnsupportedOperationException.class, |
235 new Fun(){void f(){ c.add(99); }}, |
232 () -> c.add(99), |
236 new Fun(){void f(){ c.addAll(singleton(99)); }}); |
233 () -> c.addAll(singleton(99))); |
237 if (! c.isEmpty()) { |
234 if (! c.isEmpty()) { |
238 final Integer first = c.iterator().next(); |
235 final Integer first = c.iterator().next(); |
239 THROWS(UnsupportedOperationException.class, |
236 THROWS(UnsupportedOperationException.class, |
240 new Fun(){void f(){ c.clear(); }}, |
237 () -> c.clear(), |
241 new Fun(){void f(){ c.remove(first); }}, |
238 () -> c.remove(first), |
242 new Fun(){void f(){ c.removeAll(singleton(first)); }}, |
239 () -> c.removeAll(singleton(first)), |
243 new Fun(){void f(){ c.retainAll(emptyList()); }} |
240 () -> c.retainAll(emptyList())); |
244 ); |
|
245 } |
241 } |
246 } |
242 } |
247 |
243 |
248 private static void testImmutableSet(final Set<Integer> c) { |
244 private static void testImmutableSet(final Set<Integer> c) { |
249 testImmutableCollection(c); |
245 testImmutableCollection(c); |
251 |
247 |
252 private static void testImmutableList(final List<Integer> c) { |
248 private static void testImmutableList(final List<Integer> c) { |
253 testList(c); |
249 testList(c); |
254 testImmutableCollection(c); |
250 testImmutableCollection(c); |
255 THROWS(UnsupportedOperationException.class, |
251 THROWS(UnsupportedOperationException.class, |
256 new Fun(){void f(){ c.set(0,42); }}, |
252 () -> c.set(0,42), |
257 new Fun(){void f(){ c.add(0,42); }}, |
253 () -> c.add(0,42), |
258 new Fun(){void f(){ c.addAll(0,singleton(86)); }}); |
254 () -> c.addAll(0,singleton(86))); |
259 if (! c.isEmpty()) |
255 if (! c.isEmpty()) |
260 THROWS(UnsupportedOperationException.class, |
256 THROWS(UnsupportedOperationException.class, |
261 new Fun(){void f(){ |
257 () -> { Iterator<Integer> it = c.iterator(); |
262 Iterator<Integer> it = c.iterator(); |
258 it.next(); |
263 it.next(); it.remove();}}, |
259 it.remove(); }, |
264 new Fun(){void f(){ |
260 () -> { ListIterator<Integer> it = c.listIterator(); |
265 ListIterator<Integer> it = c.listIterator(); |
261 it.next(); |
266 it.next(); it.remove();}}); |
262 it.remove(); }); |
267 } |
263 } |
268 |
264 |
269 private static void clear(Collection<Integer> c) { |
265 private static void clear(Collection<Integer> c) { |
270 try { c.clear(); } |
266 try { c.clear(); } |
271 catch (Throwable t) { unexpected(t); } |
267 catch (Throwable t) { unexpected(t); } |
288 check(! m.containsKey(1)); |
284 check(! m.containsKey(1)); |
289 } |
285 } |
290 |
286 |
291 private static void testImmutableMap(final Map<Integer,Integer> m) { |
287 private static void testImmutableMap(final Map<Integer,Integer> m) { |
292 THROWS(UnsupportedOperationException.class, |
288 THROWS(UnsupportedOperationException.class, |
293 new Fun(){void f(){ m.put(1,1); }}, |
289 () -> m.put(1,1), |
294 new Fun(){void f(){ m.putAll(singletonMap(1,1)); }}); |
290 () -> m.putAll(singletonMap(1,1))); |
295 if (! m.isEmpty()) { |
291 if (! m.isEmpty()) { |
296 final Integer first = m.keySet().iterator().next(); |
292 final Integer first = m.keySet().iterator().next(); |
297 THROWS(UnsupportedOperationException.class, |
293 THROWS(UnsupportedOperationException.class, |
298 new Fun(){void f(){ m.remove(first); }}, |
294 () -> m.remove(first), |
299 new Fun(){void f(){ m.clear(); }}); |
295 () -> m.clear()); |
300 final Map.Entry<Integer,Integer> me |
296 final Map.Entry<Integer,Integer> me |
301 = m.entrySet().iterator().next(); |
297 = m.entrySet().iterator().next(); |
302 Integer key = me.getKey(); |
298 Integer key = me.getKey(); |
303 Integer val = me.getValue(); |
299 Integer val = me.getValue(); |
304 THROWS(UnsupportedOperationException.class, |
300 THROWS(UnsupportedOperationException.class, |
305 new Fun(){void f(){ me.setValue(3); }}); |
301 () -> me.setValue(3)); |
306 equal(key, me.getKey()); |
302 equal(key, me.getKey()); |
307 equal(val, me.getValue()); |
303 equal(val, me.getValue()); |
308 } |
304 } |
309 testImmutableSet(m.keySet()); |
305 testImmutableSet(m.keySet()); |
310 testImmutableCollection(m.values()); |
306 testImmutableCollection(m.values()); |
490 final List<Integer> singleton = Collections.singletonList(e); |
486 final List<Integer> singleton = Collections.singletonList(e); |
491 |
487 |
492 // insert, query, remove element at head |
488 // insert, query, remove element at head |
493 if (isEmpty) { |
489 if (isEmpty) { |
494 THROWS(NoSuchElementException.class, |
490 THROWS(NoSuchElementException.class, |
495 new Fun(){void f(){ deq.getFirst(); }}, |
491 () -> deq.getFirst(), |
496 new Fun(){void f(){ deq.element(); }}, |
492 () -> deq.element(), |
497 new Fun(){void f(){ deq.iterator().next(); }}); |
493 () -> deq.iterator().next()); |
498 check(deq.peekFirst() == null); |
494 check(deq.peekFirst() == null); |
499 check(deq.peek() == null); |
495 check(deq.peek() == null); |
500 } else { |
496 } else { |
501 check(deq.getFirst() != e); |
497 check(deq.getFirst() != e); |
502 check(deq.element() != e); |
498 check(deq.element() != e); |
544 case 10: asList.subList(0, 1).clear(); break; |
540 case 10: asList.subList(0, 1).clear(); break; |
545 default: throw new AssertionError(); |
541 default: throw new AssertionError(); |
546 } |
542 } |
547 if (isEmpty) { |
543 if (isEmpty) { |
548 THROWS(NoSuchElementException.class, |
544 THROWS(NoSuchElementException.class, |
549 new Fun(){void f(){ deq.getFirst(); }}, |
545 () -> deq.getFirst(), |
550 new Fun(){void f(){ deq.element(); }}, |
546 () -> deq.element(), |
551 new Fun(){void f(){ deq.iterator().next(); }}); |
547 () -> deq.iterator().next()); |
552 check(deq.peekFirst() == null); |
548 check(deq.peekFirst() == null); |
553 check(deq.peek() == null); |
549 check(deq.peek() == null); |
554 } else { |
550 } else { |
555 check(deq.getFirst() != e); |
551 check(deq.getFirst() != e); |
556 check(deq.element() != e); |
552 check(deq.element() != e); |
569 equal(new ArrayList<Integer>(deq), originalContents); |
565 equal(new ArrayList<Integer>(deq), originalContents); |
570 |
566 |
571 // insert, query, remove element at tail |
567 // insert, query, remove element at tail |
572 if (isEmpty) { |
568 if (isEmpty) { |
573 check(deq.peekLast() == null); |
569 check(deq.peekLast() == null); |
574 THROWS(NoSuchElementException.class, |
570 THROWS(NoSuchElementException.class, () -> deq.getLast()); |
575 new Fun(){void f(){ deq.getLast(); }}); |
|
576 } else { |
571 } else { |
577 check(deq.peekLast() != e); |
572 check(deq.peekLast() != e); |
578 check(deq.getLast() != e); |
573 check(deq.getLast() != e); |
579 } |
574 } |
580 switch (rnd.nextInt(isList ? 6 : 4)) { |
575 switch (rnd.nextInt(isList ? 6 : 4)) { |
613 break; |
608 break; |
614 default: throw new AssertionError(); |
609 default: throw new AssertionError(); |
615 } |
610 } |
616 if (isEmpty) { |
611 if (isEmpty) { |
617 check(deq.peekLast() == null); |
612 check(deq.peekLast() == null); |
618 THROWS(NoSuchElementException.class, |
613 THROWS(NoSuchElementException.class, () -> deq.getLast()); |
619 new Fun(){void f(){ deq.getLast(); }}); |
|
620 } else { |
614 } else { |
621 check(deq.peekLast() != e); |
615 check(deq.peekLast() != e); |
622 check(deq.getLast() != e); |
616 check(deq.getLast() != e); |
623 } |
617 } |
624 check(!deq.contains(e)); |
618 check(!deq.contains(e)); |
647 testEmptyCollection(deq); |
641 testEmptyCollection(deq); |
648 check(!deq.iterator().hasNext()); |
642 check(!deq.iterator().hasNext()); |
649 if (isList) { |
643 if (isList) { |
650 check(!asList.listIterator().hasPrevious()); |
644 check(!asList.listIterator().hasPrevious()); |
651 THROWS(NoSuchElementException.class, |
645 THROWS(NoSuchElementException.class, |
652 new Fun(){void f(){ asList.listIterator().previous(); }}); |
646 () -> asList.listIterator().previous()); |
653 } |
647 } |
654 THROWS(NoSuchElementException.class, |
648 THROWS(NoSuchElementException.class, |
655 new Fun(){void f(){ deq.iterator().next(); }}, |
649 () -> deq.iterator().next(), |
656 new Fun(){void f(){ deq.element(); }}, |
650 () -> deq.element(), |
657 new Fun(){void f(){ deq.getFirst(); }}, |
651 () -> deq.getFirst(), |
658 new Fun(){void f(){ deq.getLast(); }}, |
652 () -> deq.getLast(), |
659 new Fun(){void f(){ deq.pop(); }}, |
653 () -> deq.pop(), |
660 new Fun(){void f(){ deq.remove(); }}, |
654 () -> deq.remove(), |
661 new Fun(){void f(){ deq.removeFirst(); }}, |
655 () -> deq.removeFirst(), |
662 new Fun(){void f(){ deq.removeLast(); }}); |
656 () -> deq.removeLast()); |
663 |
657 |
664 check(deq.poll() == null); |
658 check(deq.poll() == null); |
665 check(deq.pollFirst() == null); |
659 check(deq.pollFirst() == null); |
666 check(deq.pollLast() == null); |
660 check(deq.pollLast() == null); |
667 check(deq.peek() == null); |
661 check(deq.peek() == null); |
726 l.iterator(); |
720 l.iterator(); |
727 l.listIterator(); |
721 l.listIterator(); |
728 l.listIterator(0); |
722 l.listIterator(0); |
729 l.listIterator(l.size()); |
723 l.listIterator(l.size()); |
730 THROWS(IndexOutOfBoundsException.class, |
724 THROWS(IndexOutOfBoundsException.class, |
731 new Fun(){void f(){l.listIterator(-1);}}, |
725 () -> l.listIterator(-1), |
732 new Fun(){void f(){l.listIterator(l.size() + 1);}}); |
726 () -> l.listIterator(l.size() + 1)); |
733 |
727 |
734 if (l instanceof AbstractList) { |
728 if (l instanceof AbstractList) { |
735 try { |
729 try { |
736 int size = l.size(); |
730 int size = l.size(); |
737 AbstractList<Integer> abList = (AbstractList<Integer>) l; |
731 AbstractList<Integer> abList = (AbstractList<Integer>) l; |
1002 m.clear(); |
996 m.clear(); |
1003 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap) |
997 final ConcurrentMap<T,Integer> cm = (m instanceof ConcurrentMap) |
1004 ? (ConcurrentMap<T,Integer>) m |
998 ? (ConcurrentMap<T,Integer>) m |
1005 : null; |
999 : null; |
1006 List<Fun> fs = new ArrayList<Fun>(); |
1000 List<Fun> fs = new ArrayList<Fun>(); |
1007 fs.add(new Fun(){void f(){ check(! m.containsKey(null));}}); |
1001 fs.add(() -> check(! m.containsKey(null))); |
1008 fs.add(new Fun(){void f(){ equal(m.remove(null), null);}}); |
1002 fs.add(() -> equal(m.remove(null), null)); |
1009 fs.add(new Fun(){void f(){ equal(m.get(null), null);}}); |
1003 fs.add(() -> equal(m.get(null), null)); |
1010 if (cm != null) { |
1004 if (cm != null) |
1011 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}});} |
1005 fs.add(() -> check(! cm.remove(null,null))); |
1012 throwsConsistently(NullPointerException.class, fs); |
1006 throwsConsistently(NullPointerException.class, fs); |
1013 |
1007 |
1014 fs.clear(); |
1008 fs.clear(); |
1015 final Map<T,Integer> sm = singletonMap(null,1); |
1009 final Map<T,Integer> sm = singletonMap(null,1); |
1016 fs.add(new Fun(){void f(){ equal(m.put(null,1), null); m.clear();}}); |
1010 fs.add(() -> { equal(m.put(null,1), null); m.clear();}); |
1017 fs.add(new Fun(){void f(){ m.putAll(sm); m.clear();}}); |
1011 fs.add(() -> { m.putAll(sm); m.clear();}); |
1018 if (cm != null) { |
1012 if (cm != null) { |
1019 fs.add(new Fun(){void f(){ check(! cm.remove(null,null));}}); |
1013 fs.add(() -> check(! cm.remove(null,null))); |
1020 fs.add(new Fun(){void f(){ equal(cm.putIfAbsent(null,1), 1);}}); |
1014 fs.add(() -> equal(cm.putIfAbsent(null,1), 1)); |
1021 fs.add(new Fun(){void f(){ equal(cm.replace(null,1), null);}}); |
1015 fs.add(() -> equal(cm.replace(null,1), null)); |
1022 fs.add(new Fun(){void f(){ equal(cm.replace(null,1, 1), 1);}}); |
1016 fs.add(() -> equal(cm.replace(null,1, 1), 1)); |
1023 } |
1017 } |
1024 throwsConsistently(NullPointerException.class, fs); |
1018 throwsConsistently(NullPointerException.class, fs); |
1025 } |
1019 } |
1026 |
1020 |
1027 //---------------------------------------------------------------- |
1021 //---------------------------------------------------------------- |
1178 m.navigableKeySet().descendingIterator()}) { |
1172 m.navigableKeySet().descendingIterator()}) { |
1179 equalNext(it, 5); |
1173 equalNext(it, 5); |
1180 equalNext(it, 3); |
1174 equalNext(it, 3); |
1181 equalNext(it, 1); |
1175 equalNext(it, 1); |
1182 check(! it.hasNext()); |
1176 check(! it.hasNext()); |
1183 THROWS(NoSuchElementException.class, |
1177 THROWS(NoSuchElementException.class, () -> it.next()); |
1184 new Fun(){void f(){it.next();}}); |
|
1185 } |
1178 } |
1186 |
1179 |
1187 { |
1180 { |
1188 final Iterator<Map.Entry<Integer,Integer>> it |
1181 final Iterator<Map.Entry<Integer,Integer>> it |
1189 = m.descendingMap().entrySet().iterator(); |
1182 = m.descendingMap().entrySet().iterator(); |
1190 check(it.hasNext()); equal(it.next().getKey(), 5); |
1183 check(it.hasNext()); equal(it.next().getKey(), 5); |
1191 check(it.hasNext()); equal(it.next().getKey(), 3); |
1184 check(it.hasNext()); equal(it.next().getKey(), 3); |
1192 check(it.hasNext()); equal(it.next().getKey(), 1); |
1185 check(it.hasNext()); equal(it.next().getKey(), 1); |
1193 check(! it.hasNext()); |
1186 check(! it.hasNext()); |
1194 THROWS(NoSuchElementException.class, |
1187 THROWS(NoSuchElementException.class, () -> it.next()); |
1195 new Fun(){void f(){it.next();}}); |
|
1196 } |
1188 } |
1197 |
1189 |
1198 prepMapForDescItrTests(m); |
1190 prepMapForDescItrTests(m); |
1199 checkDescItrRmFirst(m.keySet(), m.navigableKeySet().descendingIterator()); |
1191 checkDescItrRmFirst(m.keySet(), m.navigableKeySet().descendingIterator()); |
1200 prepMapForDescItrTests(m); |
1192 prepMapForDescItrTests(m); |
1260 s.descendingSet().iterator()}) { |
1252 s.descendingSet().iterator()}) { |
1261 equalNext(it, 5); |
1253 equalNext(it, 5); |
1262 equalNext(it, 3); |
1254 equalNext(it, 3); |
1263 equalNext(it, 1); |
1255 equalNext(it, 1); |
1264 check(! it.hasNext()); |
1256 check(! it.hasNext()); |
1265 THROWS(NoSuchElementException.class, |
1257 THROWS(NoSuchElementException.class, () -> it.next()); |
1266 new Fun(){void f(){it.next();}}); |
|
1267 } |
1258 } |
1268 |
1259 |
1269 prepSetForDescItrTests(s); |
1260 prepSetForDescItrTests(s); |
1270 checkDescItrRmFirst(s, s.descendingIterator()); |
1261 checkDescItrRmFirst(s, s.descendingIterator()); |
1271 prepSetForDescItrTests(s); |
1262 prepSetForDescItrTests(s); |
1363 try { realMain(args); } catch (Throwable t) { unexpected(t); } |
1354 try { realMain(args); } catch (Throwable t) { unexpected(t); } |
1364 |
1355 |
1365 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
1356 System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed); |
1366 if (failed > 0) throw new Exception("Some tests failed"); |
1357 if (failed > 0) throw new Exception("Some tests failed"); |
1367 } |
1358 } |
1368 private static abstract class Fun {abstract void f() throws Throwable;} |
1359 interface Fun {void f() throws Throwable;} |
1369 private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
1360 private static void THROWS(Class<? extends Throwable> k, Fun... fs) { |
1370 for (Fun f : fs) |
1361 for (Fun f : fs) |
1371 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
1362 try { f.f(); fail("Expected " + k.getName() + " not thrown"); } |
1372 catch (Throwable t) { |
1363 catch (Throwable t) { |
1373 if (k.isAssignableFrom(t.getClass())) pass(); |
1364 if (k.isAssignableFrom(t.getClass())) pass(); |