ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jtreg/util/Collection/MOAT.java
(Generate patch)

Comparing jsr166/src/test/jtreg/util/Collection/MOAT.java (file contents):
Revision 1.13 by jsr166, Sat Mar 26 19:08:15 2016 UTC vs.
Revision 1.14 by jsr166, Fri Sep 23 22:07:43 2016 UTC

# Line 112 | Line 112 | public class MOAT {
112          testCollection(Arrays.asList(1,2,3));
113          testCollection(nCopies(25,1));
114          testImmutableList(nCopies(25,1));
115        testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
115  
116          testMap(new HashMap<Integer,Integer>());
117          testMap(new LinkedHashMap<Integer,Integer>());
# Line 134 | Line 133 | public class MOAT {
133          testMap(Collections.synchronizedSortedMap(new TreeMap<Integer,Integer>()));
134          testMap(Collections.synchronizedNavigableMap(new TreeMap<Integer,Integer>()));
135  
136 +        // Unmodifiable wrappers
137 +        testImmutableSet(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
138 +        testImmutableList(unmodifiableList(Arrays.asList(1,2,3)));
139 +        testImmutableMap(unmodifiableMap(Collections.singletonMap(1,2)));
140 +        testCollMutatorsAlwaysThrow(unmodifiableSet(new HashSet<>(Arrays.asList(1,2,3))));
141 +        testCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
142 +        testEmptyCollMutatorsAlwaysThrow(unmodifiableSet(Collections.emptySet()));
143 +        testListMutatorsAlwaysThrow(unmodifiableList(Arrays.asList(1,2,3)));
144 +        testListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
145 +        testEmptyListMutatorsAlwaysThrow(unmodifiableList(Collections.emptyList()));
146 +        testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.singletonMap(1,2)));
147 +        testMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
148 +        testEmptyMapMutatorsAlwaysThrow(unmodifiableMap(Collections.emptyMap()));
149 +
150          // Empty collections
151          final List<Integer> emptyArray = Arrays.asList(new Integer[]{});
152          testCollection(emptyArray);
# Line 196 | Line 209 | public class MOAT {
209  
210          // Immutable List
211          testEmptyList(List.of());
212 +        testListMutatorsAlwaysThrow(List.of());
213 +        testEmptyListMutatorsAlwaysThrow(List.of());
214          for (List<Integer> list : Arrays.asList(
215                  List.<Integer>of(),
216                  List.of(1),
# Line 211 | Line 226 | public class MOAT {
226                  List.of(integerArray))) {
227              testCollection(list);
228              testImmutableList(list);
229 +            testListMutatorsAlwaysThrow(list);
230          }
231  
232          // Immutable Set
233          testEmptySet(Set.of());
234 +        testCollMutatorsAlwaysThrow(Set.of());
235 +        testEmptyCollMutatorsAlwaysThrow(Set.of());
236          for (Set<Integer> set : Arrays.asList(
237                  Set.<Integer>of(),
238                  Set.of(1),
# Line 230 | Line 248 | public class MOAT {
248                  Set.of(integerArray))) {
249              testCollection(set);
250              testImmutableSet(set);
251 +            testCollMutatorsAlwaysThrow(set);
252          }
253  
254          // Immutable Map
# Line 241 | Line 260 | public class MOAT {
260          }
261  
262          testEmptyMap(Map.of());
263 +        testMapMutatorsAlwaysThrow(Map.of());
264 +        testEmptyMapMutatorsAlwaysThrow(Map.of());
265          for (Map<Integer,Integer> map : Arrays.asList(
266                  Map.<Integer,Integer>of(),
267                  Map.of(1, 101),
# Line 256 | Line 277 | public class MOAT {
277                  Map.ofEntries(ea))) {
278              testMap(map);
279              testImmutableMap(map);
280 +            testMapMutatorsAlwaysThrow(map);
281          }
282      }
283  
# Line 358 | Line 380 | public class MOAT {
380                             it.remove(); });
381      }
382  
383 +    /**
384 +     * Test that calling a mutator always throws UOE, even if the mutator
385 +     * wouldn't actually do anything, given its arguments.
386 +     *
387 +     * @param c the collection instance to test
388 +     */
389 +    private static void testCollMutatorsAlwaysThrow(Collection<Integer> c) {
390 +        THROWS(UnsupportedOperationException.class,
391 +                () -> c.addAll(Collections.emptyList()),
392 +                () -> c.remove(ABSENT_VALUE),
393 +                () -> c.removeAll(Collections.emptyList()),
394 +                () -> c.removeIf(x -> false),
395 +                () -> c.retainAll(c));
396 +    }
397 +
398 +    /**
399 +     * Test that calling a mutator always throws UOE, even if the mutator
400 +     * wouldn't actually do anything on an empty collection.
401 +     *
402 +     * @param c the collection instance to test, must be empty
403 +     */
404 +    private static void testEmptyCollMutatorsAlwaysThrow(Collection<Integer> c) {
405 +        if (! c.isEmpty()) {
406 +            fail("collection is not empty");
407 +        }
408 +        THROWS(UnsupportedOperationException.class,
409 +                () -> c.clear());
410 +    }
411 +
412 +    /**
413 +     * As above, for a list.
414 +     *
415 +     * @param c the list instance to test
416 +     */
417 +    private static void testListMutatorsAlwaysThrow(List<Integer> c) {
418 +        testCollMutatorsAlwaysThrow(c);
419 +        THROWS(UnsupportedOperationException.class,
420 +                () -> c.addAll(0, Collections.emptyList()));
421 +    }
422 +
423 +    /**
424 +     * As above, for an empty list.
425 +     *
426 +     * @param c the list instance to test, must be empty
427 +     */
428 +    private static void testEmptyListMutatorsAlwaysThrow(List<Integer> c) {
429 +        if (! c.isEmpty()) {
430 +            fail("list is not empty");
431 +        }
432 +        testEmptyCollMutatorsAlwaysThrow(c);
433 +        THROWS(UnsupportedOperationException.class,
434 +                () -> c.replaceAll(x -> x),
435 +                () -> c.sort(null));
436 +    }
437 +
438 +    /**
439 +     * As above, for a map.
440 +     *
441 +     * @param m the map instance to test
442 +     */
443 +    private static void testMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
444 +        THROWS(UnsupportedOperationException.class,
445 +                () -> m.compute(ABSENT_VALUE, (k, v) -> null),
446 +                () -> m.computeIfAbsent(ABSENT_VALUE, k -> null),
447 +                () -> m.computeIfPresent(ABSENT_VALUE, (k, v) -> null),
448 +                () -> m.merge(ABSENT_VALUE, 0, (k, v) -> null),
449 +                () -> m.putAll(Collections.emptyMap()),
450 +                () -> m.remove(ABSENT_VALUE),
451 +                () -> m.remove(ABSENT_VALUE, 0),
452 +                () -> m.replace(ABSENT_VALUE, 0),
453 +                () -> m.replace(ABSENT_VALUE, 0, 1));
454 +    }
455 +
456 +    /**
457 +     * As above, for an empty map.
458 +     *
459 +     * @param map the map instance to test, must be empty
460 +     */
461 +    private static void testEmptyMapMutatorsAlwaysThrow(Map<Integer,Integer> m) {
462 +        if (! m.isEmpty()) {
463 +            fail("map is not empty");
464 +        }
465 +        THROWS(UnsupportedOperationException.class,
466 +                () -> m.clear(),
467 +                () -> m.replaceAll((k, v) -> v));
468 +    }
469 +
470      private static void clear(Collection<Integer> c) {
471          try { c.clear(); }
472          catch (Throwable t) { unexpected(t); }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines