public void testPowerSetEmpty() { ImmutableSet<Integer> elements = ImmutableSet.of(); Set<Set<Integer>> powerSet = powerSet(elements); assertEquals(1, powerSet.size()); assertEquals(ImmutableSet.of(ImmutableSet.of()), powerSet); assertEquals(0, powerSet.hashCode()); }
/** * Utility method that verifies that the given set is equal to and hashes identically to a set * constructed with the elements in the given iterable. */ private static <E> void verifySetContents(Set<E> set, Iterable<E> contents) { Set<E> expected = null; if (contents instanceof Set) { expected = (Set<E>) contents; } else { expected = new HashSet<E>(); for (E element : contents) { expected.add(element); } } assertEquals(expected, set); }
private static void assertPowerSetSize(int i, Object... elements) { assertEquals(i, powerSet(newHashSet(elements)).size()); }
public void testNewEnumSet_empty() { EnumSet<SomeEnum> copy = newEnumSet(Collections.<SomeEnum>emptySet(), SomeEnum.class); assertEquals(EnumSet.noneOf(SomeEnum.class), copy); }
public void testNewEnumSet_enumSet() { EnumSet<SomeEnum> set = EnumSet.of(SomeEnum.A, SomeEnum.D); assertEquals(set, newEnumSet(set, SomeEnum.class)); }
/** * Utility method to verify that the given LinkedHashSet is equal to and hashes identically to a * set constructed with the elements in the given collection. Also verifies that the ordering in * the set is the same as the ordering of the given contents. */ private static <E> void verifyLinkedHashSetContents( LinkedHashSet<E> set, Collection<E> contents) { assertEquals( "LinkedHashSet should have preserved order for iteration", new ArrayList<E>(set), new ArrayList<E>(contents)); verifySetContents(set, contents); }
public void testNewEnumSet_collection() { Set<SomeEnum> set = ImmutableSet.of(SomeEnum.B, SomeEnum.C); assertEquals(set, newEnumSet(set, SomeEnum.class)); }
private static void assertPowerSetHashCode(int expected, Set<?> elements) { assertEquals(expected, powerSet(elements).hashCode()); }
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) { } }
private static void checkHashCode(Set<?> set) { assertEquals(Sets.newHashSet(set).hashCode(), set.hashCode()); }
private static void assertEmpty(Set<? extends List<?>> set) { assertTrue(set.isEmpty()); assertEquals(0, set.size()); assertFalse(set.iterator().hasNext()); }
public void testNewEnumSet_iterable() { Set<SomeEnum> set = ImmutableSet.of(SomeEnum.A, SomeEnum.B, SomeEnum.C); assertEquals(set, newEnumSet(unmodifiableIterable(set), SomeEnum.class)); }
public void testNewIdentityHashSet() { Set<Integer> set = Sets.newIdentityHashSet(); Integer value1 = new Integer(12357); Integer value2 = new Integer(12357); assertTrue(set.add(value1)); assertFalse(set.contains(value2)); assertTrue(set.contains(value1)); assertTrue(set.add(value2)); assertEquals(2, set.size()); }
@GwtIncompatible // java serialization not supported in GWT. public void testImmutableEnumSet_deserializationMakesDefensiveCopy() throws Exception { ImmutableSet<SomeEnum> original = Sets.immutableEnumSet(SomeEnum.A, SomeEnum.B); int handleOffset = 6; byte[] serializedForm = serializeWithBackReference(original, handleOffset); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(serializedForm)); ImmutableSet<?> deserialized = (ImmutableSet<?>) in.readObject(); EnumSet<?> delegate = (EnumSet<?>) in.readObject(); assertEquals(original, deserialized); assertTrue(delegate.remove(SomeEnum.A)); assertTrue(deserialized.contains(SomeEnum.A)); }
@GwtIncompatible // NavigableSet public void testSubSet_boundedRange() { ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10); ImmutableSortedSet<Integer> empty = ImmutableSortedSet.of(); assertEquals(set, Sets.subSet(set, Range.closed(0, 12))); assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.closed(0, 4))); assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.closed(2, 6))); assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.closed(3, 7))); assertEquals(empty, Sets.subSet(set, Range.closed(20, 30))); assertEquals(set, Sets.subSet(set, Range.open(0, 12))); assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.open(0, 4))); assertEquals(ImmutableSortedSet.of(4), Sets.subSet(set, Range.open(2, 6))); assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.open(3, 7))); assertEquals(empty, Sets.subSet(set, Range.open(20, 30))); assertEquals(set, Sets.subSet(set, Range.openClosed(0, 12))); assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.openClosed(0, 4))); assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.openClosed(2, 6))); assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.openClosed(3, 7))); assertEquals(empty, Sets.subSet(set, Range.openClosed(20, 30))); assertEquals(set, Sets.subSet(set, Range.closedOpen(0, 12))); assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.closedOpen(0, 4))); assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.closedOpen(2, 6))); assertEquals(ImmutableSortedSet.of(4, 6), Sets.subSet(set, Range.closedOpen(3, 7))); assertEquals(empty, Sets.subSet(set, Range.closedOpen(20, 30))); }
@GwtIncompatible // NavigableSet public void testSubSet_halfBoundedRange() { ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10); ImmutableSortedSet<Integer> empty = ImmutableSortedSet.of(); assertEquals(set, Sets.subSet(set, Range.atLeast(0))); assertEquals(ImmutableSortedSet.of(4, 6, 8, 10), Sets.subSet(set, Range.atLeast(4))); assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.atLeast(7))); assertEquals(empty, Sets.subSet(set, Range.atLeast(20))); assertEquals(set, Sets.subSet(set, Range.greaterThan(0))); assertEquals(ImmutableSortedSet.of(6, 8, 10), Sets.subSet(set, Range.greaterThan(4))); assertEquals(ImmutableSortedSet.of(8, 10), Sets.subSet(set, Range.greaterThan(7))); assertEquals(empty, Sets.subSet(set, Range.greaterThan(20))); assertEquals(empty, Sets.subSet(set, Range.lessThan(0))); assertEquals(ImmutableSortedSet.of(2), Sets.subSet(set, Range.lessThan(4))); assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.lessThan(7))); assertEquals(set, Sets.subSet(set, Range.lessThan(20))); assertEquals(empty, Sets.subSet(set, Range.atMost(0))); assertEquals(ImmutableSortedSet.of(2, 4), Sets.subSet(set, Range.atMost(4))); assertEquals(ImmutableSortedSet.of(2, 4, 6), Sets.subSet(set, Range.atMost(7))); assertEquals(set, Sets.subSet(set, Range.atMost(20))); }
public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() { ImmutableList<Integer> allElements = ImmutableList.of( 4233352, 3284593, 3794208, 3849533, 4013967, 2902658, 1886275, 2131109, 985872, 1843868); for (int i = 0; i < allElements.size(); i++) { Set<Integer> elements = newHashSet(allElements.subList(0, i)); Set<Set<Integer>> powerSet1 = powerSet(elements); Set<Set<Integer>> powerSet2 = powerSet(elements); new EqualsTester() .addEqualityGroup(powerSet1, powerSet2, toHashSets(powerSet1)) .addEqualityGroup(ImmutableSet.of()) .addEqualityGroup(ImmutableSet.of(9999999)) .addEqualityGroup("notASet") .testEquals(); assertEquals(toHashSets(powerSet1).hashCode(), powerSet1.hashCode()); } }
@GwtIncompatible // NavigableSet public void testSubSet_unboundedRange() { ImmutableSortedSet<Integer> set = ImmutableSortedSet.of(2, 4, 6, 8, 10); assertEquals(set, Sets.subSet(set, Range.<Integer>all())); }
public void testPowerSetShowOff() { Set<Object> zero = ImmutableSet.of(); Set<Set<Object>> one = powerSet(zero); Set<Set<Set<Object>>> two = powerSet(one); Set<Set<Set<Set<Object>>>> four = powerSet(two); Set<Set<Set<Set<Set<Object>>>>> sixteen = powerSet(four); Set<Set<Set<Set<Set<Set<Object>>>>>> sixtyFiveThousandish = powerSet(sixteen); assertEquals(1 << 16, sixtyFiveThousandish.size()); assertTrue(powerSet(makeSetOfZeroToTwentyNine()).contains(makeSetOfZeroToTwentyNine())); assertFalse(powerSet(makeSetOfZeroToTwentyNine()).contains(ImmutableSet.of(30))); }
@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())); } }