void ensureNotDirectlyModifiable(SortedSet<Integer> unmod) { try { unmod.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.remove(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.addAll(Collections.singleton(4)); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { Iterator<Integer> iterator = unmod.iterator(); iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } }
public void testComplementOfEmptySetWithoutTypeDoesntWork() { Set<SomeEnum> set = Collections.emptySet(); try { Sets.complementOf(set); fail(); } catch (IllegalArgumentException expected) { } }
public void testNewSetFromMapIllegal() { Map<Integer, Boolean> map = new LinkedHashMap<>(); map.put(2, true); try { Sets.newSetFromMap(map); fail(); } catch (IllegalArgumentException expected) { } }
public void testImmutableEnumSet() { Set<SomeEnum> units = Sets.immutableEnumSet(SomeEnum.D, SomeEnum.B); assertThat(units).containsExactly(SomeEnum.B, SomeEnum.D).inOrder(); try { units.remove(SomeEnum.B); fail("ImmutableEnumSet should throw an exception on remove()"); } catch (UnsupportedOperationException expected) { } try { units.add(SomeEnum.C); fail("ImmutableEnumSet should throw an exception on add()"); } catch (UnsupportedOperationException expected) { } }
public void testPowerSetCreationErrors() { try { Set<Set<Character>> unused = powerSet( newHashSet( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5')); fail(); } catch (IllegalArgumentException expected) { } try { Set<Set<Integer>> unused = powerSet(ContiguousSet.closed(0, Integer.MAX_VALUE / 2)); fail(); } catch (IllegalArgumentException expected) { } try { powerSet(singleton(null)); fail(); } catch (NullPointerException expected) { } }
try { unmod.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) {
@SuppressWarnings("unchecked") // varargs! public void testCartesianProductTooBig() { Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers()); try { Sets.cartesianProduct(set, set, set, set, set); fail("Expected IAE"); } catch (IllegalArgumentException expected) { } }
public void testPowerSetIteration_manual() { ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3); Set<Set<Integer>> powerSet = powerSet(elements); // The API doesn't promise this iteration order, but it's convenient here. Iterator<Set<Integer>> i = powerSet.iterator(); assertEquals(ImmutableSet.of(), i.next()); assertEquals(ImmutableSet.of(1), i.next()); assertEquals(ImmutableSet.of(2), i.next()); assertEquals(ImmutableSet.of(2, 1), i.next()); assertEquals(ImmutableSet.of(3), i.next()); assertEquals(ImmutableSet.of(3, 1), i.next()); assertEquals(ImmutableSet.of(3, 2), i.next()); assertEquals(ImmutableSet.of(3, 2, 1), i.next()); assertFalse(i.hasNext()); try { i.next(); fail(); } catch (NoSuchElementException expected) { } }
try { reverse.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) {
@GwtIncompatible // NavigableSet public void testSubSet_unnaturalOrdering() { ImmutableSortedSet<Integer> set = ImmutableSortedSet.<Integer>reverseOrder().add(2, 4, 6, 8, 10).build(); try { Sets.subSet(set, Range.closed(4, 8)); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException expected) { } // These results are all incorrect, but there's no way (short of iterating over the result) // to verify that with an arbitrary ordering or comparator. assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.atLeast(4))); assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.atMost(8))); assertEquals(ImmutableSortedSet.of(2, 4, 6, 8, 10), Sets.subSet(set, Range.<Integer>all())); } }
public void testComplementOfEmptySetWithoutTypeDoesntWork() { Set<SomeEnum> set = Collections.emptySet(); try { Sets.complementOf(set); fail(); } catch (IllegalArgumentException expected) {} }
public void testNewSetFromMapIllegal() { Map<Integer, Boolean> map = new LinkedHashMap<>(); map.put(2, true); try { Sets.newSetFromMap(map); fail(); } catch (IllegalArgumentException expected) {} }
void ensureNotDirectlyModifiable(SortedSet<Integer> unmod) { try { unmod.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.remove(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { unmod.addAll(Collections.singleton(4)); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } try { Iterator<Integer> iterator = unmod.iterator(); iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { } }
public void testImmutableEnumSet() { Set<SomeEnum> units = Sets.immutableEnumSet(SomeEnum.D, SomeEnum.B); assertThat(units).containsExactly(SomeEnum.B, SomeEnum.D).inOrder(); try { units.remove(SomeEnum.B); fail("ImmutableEnumSet should throw an exception on remove()"); } catch (UnsupportedOperationException expected) {} try { units.add(SomeEnum.C); fail("ImmutableEnumSet should throw an exception on add()"); } catch (UnsupportedOperationException expected) {} }
public void testPowerSetCreationErrors() { try { Set<Set<Character>> unused = powerSet( newHashSet( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5')); fail(); } catch (IllegalArgumentException expected) { } try { powerSet(singleton(null)); fail(); } catch (NullPointerException expected) { } }
try { unmod.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { iterator.next(); iterator.remove(); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) {
@SuppressWarnings("unchecked") // varargs! public void testCartesianProductTooBig() { Set<Integer> set = ContiguousSet.create(Range.closed(0, 10000), DiscreteDomain.integers()); try { Sets.cartesianProduct(set, set, set, set, set); fail("Expected IAE"); } catch (IllegalArgumentException expected) { } }
public void testPowerSetIteration_manual() { ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3); Set<Set<Integer>> powerSet = powerSet(elements); // The API doesn't promise this iteration order, but it's convenient here. Iterator<Set<Integer>> i = powerSet.iterator(); assertEquals(ImmutableSet.of(), i.next()); assertEquals(ImmutableSet.of(1), i.next()); assertEquals(ImmutableSet.of(2), i.next()); assertEquals(ImmutableSet.of(2, 1), i.next()); assertEquals(ImmutableSet.of(3), i.next()); assertEquals(ImmutableSet.of(3, 1), i.next()); assertEquals(ImmutableSet.of(3, 2), i.next()); assertEquals(ImmutableSet.of(3, 2, 1), i.next()); assertFalse(i.hasNext()); try { i.next(); fail(); } catch (NoSuchElementException expected) { } }
try { reverse.add(4); fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) { fail("UnsupportedOperationException expected"); } catch (UnsupportedOperationException expected) {
@GwtIncompatible // NavigableSet public void testSubSet_unnaturalOrdering() { ImmutableSortedSet<Integer> set = ImmutableSortedSet.<Integer>reverseOrder().add(2, 4, 6, 8, 10).build(); try { Sets.subSet(set, Range.closed(4, 8)); fail("IllegalArgumentException expected"); } catch (IllegalArgumentException expected) { } // These results are all incorrect, but there's no way (short of iterating over the result) // to verify that with an arbitrary ordering or comparator. assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.atLeast(4))); assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.atMost(8))); assertEquals(ImmutableSortedSet.of(2, 4, 6, 8, 10), Sets.subSet(set, Range.<Integer>all())); } }