/** * Returns a nonempty contiguous set containing all {@code int} values from {@code lower} * (inclusive) to {@code upper} (inclusive). (These are the same values contained in {@code * Range.closed(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Integer> closed(int lower, int upper) { return create(Range.closed(lower, upper), DiscreteDomain.integers()); }
/** * Returns a contiguous set containing all {@code int} values from {@code lower} (inclusive) to * {@code upper} (exclusive). If the endpoints are equal, an empty set is returned. (These are the * same values contained in {@code Range.closedOpen(lower, upper)}.) * * @throws IllegalArgumentException if {@code lower} is greater than {@code upper} * @since 23.0 */ @Beta public static ContiguousSet<Integer> closedOpen(int lower, int upper) { return create(Range.closedOpen(lower, upper), DiscreteDomain.integers()); }
public void testAsList() { ImmutableList<Integer> list = ContiguousSet.create(Range.closed(1, 3), integers()).asList(); for (int i = 0; i < 3; i++) { assertEquals(i + 1, list.get(i).intValue()); } assertEquals(ImmutableList.of(1, 2, 3), ImmutableList.copyOf(list.iterator())); assertEquals(ImmutableList.of(1, 2, 3), ImmutableList.copyOf(list.toArray(new Integer[0]))); }
public PackageSanityTests() { publicApiOnly(); // Many package-private classes are tested through the public API. setDefault(DiscreteDomain.class, DiscreteDomain.integers()); } }
public void testIntersection() { ContiguousSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertEquals( ImmutableSet.of(1, 2, 3), ContiguousSet.create(Range.open(-1, 4), integers()).intersection(set)); assertEquals( ImmutableSet.of(1, 2, 3), set.intersection(ContiguousSet.create(Range.open(-1, 4), integers()))); assertEquals( ImmutableSet.of(3), set.intersection(ContiguousSet.create(Range.closed(3, 5), integers()))); }
@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 testSubSet_outOfOrder() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); try { set.subSet(3, 2); fail(); } catch (IllegalArgumentException expected) { } }
public void testCreate_empty() { assertEquals(ImmutableSet.of(), ContiguousSet.create(Range.closedOpen(1, 1), integers())); assertEquals(ImmutableSet.of(), ContiguousSet.closedOpen(1, 1)); assertEquals(ImmutableSet.of(), ContiguousSet.create(Range.openClosed(5, 5), integers())); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.lessThan(Integer.MIN_VALUE), integers())); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.greaterThan(Integer.MAX_VALUE), integers())); }
public void testFirst() { assertEquals(1, ContiguousSet.create(Range.closed(1, 3), integers()).first().intValue()); assertEquals(1, ContiguousSet.create(Range.open(0, 4), integers()).first().intValue()); assertEquals( Integer.MIN_VALUE, ContiguousSet.create(Range.<Integer>all(), integers()).first().intValue()); }
public void testLast() { assertEquals(3, ContiguousSet.create(Range.closed(1, 3), integers()).last().intValue()); assertEquals(3, ContiguousSet.create(Range.open(0, 4), integers()).last().intValue()); assertEquals( Integer.MAX_VALUE, ContiguousSet.create(Range.<Integer>all(), integers()).last().intValue()); }
public void testContainsAll() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertTrue(set.containsAll(subset)); } for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertFalse(set.containsAll(Sets.union(subset, ImmutableSet.of(9)))); } assertFalse(set.containsAll((Collection<?>) ImmutableSet.of("blah"))); }
public void testIntersection_empty() { ContiguousSet<Integer> set = ContiguousSet.closed(1, 3); ContiguousSet<Integer> emptySet = ContiguousSet.closedOpen(2, 2); assertEquals(ImmutableSet.of(), set.intersection(emptySet)); assertEquals(ImmutableSet.of(), emptySet.intersection(set)); assertEquals( ImmutableSet.of(), ContiguousSet.create(Range.closed(-5, -1), integers()) .intersection(ContiguousSet.create(Range.open(3, 64), integers()))); }
public void testSerialization() { reserializeAndAssert(DiscreteDomain.integers()); reserializeAndAssert(DiscreteDomain.longs()); reserializeAndAssert(DiscreteDomain.bigIntegers()); }
@Override public Set<Integer> create(Object... elements) { ImmutableRangeSet.Builder<Integer> builder = ImmutableRangeSet.builder(); for (Object o : elements) { Integer i = (Integer) o; builder.add(Range.singleton(i)); } return builder.build().asSet(DiscreteDomain.integers()); } }
public void testTailSet() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); assertThat(set.tailSet(Integer.MIN_VALUE)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(2)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(3)).containsExactly(3).inOrder(); assertThat(set.tailSet(Integer.MIN_VALUE, false)).containsExactly(1, 2, 3).inOrder(); assertThat(set.tailSet(1, false)).containsExactly(2, 3).inOrder(); assertThat(set.tailSet(2, false)).containsExactly(3).inOrder(); assertThat(set.tailSet(3, false)).isEmpty(); }
public void testAsSet() { ImmutableSortedSet<Integer> expectedSet = ImmutableSortedSet.of(2, 3, 4, 8, 9, 16, 17); ImmutableSortedSet<Integer> asSet = RANGE_SET_ONE.asSet(DiscreteDomain.integers()); assertEquals(expectedSet, asSet); assertThat(asSet).containsExactlyElementsIn(expectedSet).inOrder(); assertTrue(asSet.containsAll(expectedSet)); SerializableTester.reserializeAndAssert(asSet); }
public void testAsSetHeadSet() { ImmutableSortedSet<Integer> expectedSet = ImmutableSortedSet.of(2, 3, 4, 8, 9, 16, 17); ImmutableSortedSet<Integer> asSet = RANGE_SET_ONE.asSet(DiscreteDomain.integers()); for (int i = 0; i <= 20; i++) { assertEquals(asSet.headSet(i, false), expectedSet.headSet(i, false)); assertEquals(asSet.headSet(i, true), expectedSet.headSet(i, true)); } }
public void testAsSetTailSet() { ImmutableSortedSet<Integer> expectedSet = ImmutableSortedSet.of(2, 3, 4, 8, 9, 16, 17); ImmutableSortedSet<Integer> asSet = RANGE_SET_ONE.asSet(DiscreteDomain.integers()); for (int i = 0; i <= 20; i++) { assertEquals(asSet.tailSet(i, false), expectedSet.tailSet(i, false)); assertEquals(asSet.tailSet(i, true), expectedSet.tailSet(i, true)); } }