public Test testsForArraysAsList() { return ListTestSuiteBuilder.using( new TestStringListGenerator() { @Override public List<String> create(String[] elements) { return Arrays.asList(elements.clone()); } }) .named("Arrays.asList") .withFeatures( ListFeature.SUPPORTS_SET, CollectionFeature.SERIALIZABLE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY) .suppressing(suppressForArraysAsList()) .createTestSuite(); }
@CollectionSize.Require(absent = CollectionSize.ZERO) @CollectionFeature.Require(ALLOWS_NULL_VALUES) public void testHashCode_containingNull() { Collection<E> elements = getSampleElements(getNumElements() - 1); int expectedHashCode = 0; for (E element : elements) { expectedHashCode += ((element == null) ? 0 : element.hashCode()); } elements.add(null); collection = getSubjectGenerator().create(elements.toArray()); assertEquals( "A Set's hashCode() should be the sum of those of its elements (with " + "a null element counting as having a hash of zero).", expectedHashCode, getSet().hashCode()); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_ADD) public void testConflictingBounds() { testEmptyRangeSubMultiset( sortedMultiset.subMultiset(a.getElement(), CLOSED, a.getElement(), OPEN)); testEmptyRangeSubMultiset( sortedMultiset.subMultiset(a.getElement(), OPEN, a.getElement(), OPEN)); testEmptyRangeSubMultiset( sortedMultiset.subMultiset(a.getElement(), OPEN, a.getElement(), CLOSED)); testEmptyRangeSubMultiset( sortedMultiset.subMultiset(b.getElement(), CLOSED, a.getElement(), CLOSED)); testEmptyRangeSubMultiset( sortedMultiset.subMultiset(b.getElement(), CLOSED, a.getElement(), OPEN)); testEmptyRangeSubMultiset( sortedMultiset.subMultiset(b.getElement(), OPEN, a.getElement(), OPEN)); }
public Test testsForConcurrentSkipListSetWithComparator() { return SetTestSuiteBuilder.using( new TestStringSortedSetGenerator() { @Override public SortedSet<String> create(String[] elements) { SortedSet<String> set = new ConcurrentSkipListSet<String>(arbitraryNullFriendlyComparator()); Collections.addAll(set, elements); return set; } }) .named("ConcurrentSkipListSet, with comparator") .withFeatures( SetFeature.GENERAL_PURPOSE, CollectionFeature.SERIALIZABLE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .suppressing(suppressForConcurrentSkipListSetWithComparator()) .createTestSuite(); }
public Test testsForCheckedNavigableMap() { return SortedMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected NavigableMap<String, String> create(Entry<String, String>[] entries) { NavigableMap<String, String> map = populate(new TreeMap<String, String>(), entries); return Collections.checkedNavigableMap(map, String.class, String.class); } }) .named("checkedNavigableMap/TreeMap, natural") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, MapFeature.RESTRICTS_KEYS, MapFeature.RESTRICTS_VALUES, CollectionFeature.KNOWN_ORDER, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForCheckedNavigableMap()) .createTestSuite(); }
private static Set<Feature<?>> computeInverseFeatures(Set<Feature<?>> mapFeatures) { Set<Feature<?>> inverseFeatures = new HashSet<Feature<?>>(mapFeatures); boolean nullKeys = inverseFeatures.remove(MapFeature.ALLOWS_NULL_KEYS); boolean nullValues = inverseFeatures.remove(MapFeature.ALLOWS_NULL_VALUES); if (nullKeys) { inverseFeatures.add(MapFeature.ALLOWS_NULL_VALUES); } if (nullValues) { inverseFeatures.add(MapFeature.ALLOWS_NULL_KEYS); } inverseFeatures.add(NoRecurse.INVERSE); inverseFeatures.remove(CollectionFeature.KNOWN_ORDER); inverseFeatures.add(MapFeature.REJECTS_DUPLICATES_AT_CREATION); return inverseFeatures; }
public static Test suite() { return SetTestSuiteBuilder.using( new TestStringSetGenerator() { @Override protected Set<String> create(String[] elements) { TestSet<String> inner = new TestSet<String>(new HashSet<String>(), null); Set<String> outer = Synchronized.set(inner, null); inner.mutex = outer; Collections.addAll(outer, elements); return outer; } }) .named("Synchronized.set") .withFeatures( CollectionFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY, CollectionFeature.SERIALIZABLE) .createTestSuite(); }
@CollectionSize.Require(ONE) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testEntrySet_iteratorRemovePropagates() { Iterator<Multiset.Entry<E>> iterator = getMultiset().entrySet().iterator(); assertTrue( "non-empty multiset.entrySet() iterator.hasNext() returned false", iterator.hasNext()); assertEquals( "multiset.entrySet() iterator.next() returned incorrect entry", Multisets.immutableEntry(e0(), 1), iterator.next()); assertFalse( "size 1 multiset.entrySet() iterator.hasNext() returned true after next()", iterator.hasNext()); iterator.remove(); assertTrue( "multiset isn't empty after multiset.entrySet() iterator.remove()", getMultiset().isEmpty()); }
public Test testsForUnmodifiableMap() { return MapTestSuiteBuilder.using( new TestStringMapGenerator() { @Override protected Map<String, String> create(Entry<String, String>[] entries) { return Collections.unmodifiableMap(toHashMap(entries)); } }) .named("unmodifiableMap/HashMap") .withFeatures( MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, MapFeature.ALLOWS_ANY_NULL_QUERIES, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForUnmodifiableMap()) .createTestSuite(); }
public Test testsForUnmodifiableNavigableMap() { return MapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected NavigableMap<String, String> create(Entry<String, String>[] entries) { return Collections.unmodifiableNavigableMap(populate(new TreeMap<>(), entries)); } }) .named("unmodifiableNavigableMap/TreeMap, natural") .withFeatures( MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForUnmodifiableNavigableMap()) .createTestSuite(); }
public Test testsForConcurrentSkipListMapNatural() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { return populate(new ConcurrentSkipListMap<String, String>(), entries); } }) .named("ConcurrentSkipListMap, natural") .withFeatures( MapFeature.GENERAL_PURPOSE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForConcurrentSkipListMap()) .createTestSuite(); }
public static Test suite() { return CollectionTestSuiteBuilder .using(new TestStringCollectionGenerator() { @Override public Collection<String> create(String[] elements) { // TODO: MinimalCollection should perhaps throw for (Object element : elements) { if (element == null) { throw new NullPointerException(); } } return MinimalCollection.of(elements); } }) .named("MinimalCollection") .withFeatures( CollectionFeature.NONE, CollectionSize.ANY) .createTestSuite(); }
/** * Tests regular NavigableMap behavior of synchronizedNavigableMap(treeMap); * does not test the fact that it's synchronized. */ public Test testsForSynchronizedNavigableMap() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { NavigableMap<String, String> delegate = populate(new TreeMap<>(), entries); return Collections.synchronizedNavigableMap(delegate); } }) .named("synchronizedNavigableMap/TreeMap, natural") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForSynchronizedNavigableMap()) .createTestSuite(); }
public Test testsForCheckedList() { return ListTestSuiteBuilder.using( new TestStringListGenerator() { @Override public List<String> create(String[] elements) { List<String> innerList = new ArrayList<String>(); Collections.addAll(innerList, elements); return Collections.checkedList(innerList, String.class); } }) .named("checkedList/ArrayList") .withFeatures( ListFeature.GENERAL_PURPOSE, CollectionFeature.SERIALIZABLE, CollectionFeature.RESTRICTS_ELEMENTS, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ANY) .suppressing(suppressForCheckedList()) .createTestSuite(); }
public Test testsForCheckedSet() { return SetTestSuiteBuilder.using( new TestStringSetGenerator() { @Override public Set<String> create(String[] elements) { Set<String> innerSet = new HashSet<String>(); Collections.addAll(innerSet, elements); return Collections.checkedSet(innerSet, String.class); } }) .named("checkedSet/HashSet") .withFeatures( SetFeature.GENERAL_PURPOSE, CollectionFeature.SERIALIZABLE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.RESTRICTS_ELEMENTS, CollectionSize.ANY) .suppressing(suppressForCheckedSet()) .createTestSuite(); }
@GwtIncompatible // suite private static Test testsForFilter() { return SetTestSuiteBuilder.using(new TestStringSetGenerator() { @Override public Set<String> create(String[] elements) { Set<String> unfiltered = Sets.newLinkedHashSet(); unfiltered.add("yyy"); Collections.addAll(unfiltered, elements); unfiltered.add("zzz"); return Sets.filter(unfiltered, Collections2Test.NOT_YYY_ZZZ); } }) .named("Sets.filter") .withFeatures( CollectionFeature.SUPPORTS_ADD, CollectionFeature.SUPPORTS_REMOVE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .createTestSuite(); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) @MultisetFeature.Require(ENTRIES_ARE_VIEWS) public void testEntryReflectsIteratorRemove() { initThreeCopies(); assertEquals(3, getMultiset().count(e0())); Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); assertEquals(3, entry.getCount()); Iterator<E> itr = getMultiset().iterator(); itr.next(); itr.remove(); assertEquals(2, entry.getCount()); itr.next(); itr.remove(); itr.next(); itr.remove(); assertEquals(0, entry.getCount()); }
@GwtIncompatible // suite private static Test testsForFilter() { return CollectionTestSuiteBuilder.using( new TestStringCollectionGenerator() { @Override public Collection<String> create(String[] elements) { List<String> unfiltered = newArrayList(); unfiltered.add("yyy"); Collections.addAll(unfiltered, elements); unfiltered.add("zzz"); return Collections2.filter(unfiltered, NOT_YYY_ZZZ); } }) .named("Collections2.filter") .withFeatures( CollectionFeature.SUPPORTS_ADD, CollectionFeature.SUPPORTS_REMOVE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .createTestSuite(); }
public Test testsForSingletonSet() { return SetTestSuiteBuilder.using( new TestStringSetGenerator() { @Override public Set<String> create(String[] elements) { return Collections.singleton(elements[0]); } }) .named("singleton") .withFeatures( CollectionFeature.SERIALIZABLE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ONE) .suppressing(suppressForSingletonSet()) .createTestSuite(); }
public Test testsForLinkedList() { return ListTestSuiteBuilder.using( new TestStringListGenerator() { @Override public List<String> create(String[] elements) { return new LinkedList<String>(MinimalCollection.of(elements)); } }) .named("LinkedList") .withFeatures( ListFeature.GENERAL_PURPOSE, CollectionFeature.SERIALIZABLE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, CollectionSize.ANY) .suppressing(suppressForLinkedList()) .createTestSuite(); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testEntrySet_removeAllPresent() { assertTrue( "multiset.entrySet.removeAll(presentEntry) returned false", getMultiset() .entrySet() .removeAll(Collections.singleton(Multisets.immutableEntry(e0(), 1)))); assertFalse("multiset contains element after removing its entry", getMultiset().contains(e0())); }
@CollectionFeature.Require(SUPPORTS_REMOVE) public void testClear() { collection.clear(); assertTrue("After clear(), a collection should be empty.", collection.isEmpty()); assertEquals(0, collection.size()); assertFalse(collection.iterator().hasNext()); }
@SuppressWarnings("unchecked") @CollectionFeature.Require(absent = {SUPPORTS_ITERATOR_REMOVE, KNOWN_ORDER}) public void testIteratorUnknownOrder() { new IteratorTester<E>( 4, UNMODIFIABLE, Arrays.asList(e0(), e1(), e1(), e2()), IteratorTester.KnownOrder.UNKNOWN_ORDER) { @Override protected Iterator<E> newTargetIterator() { return getSubjectGenerator().create(e0(), e1(), e1(), e2()).iterator(); } }.test(); }
@CollectionFeature.Require(KNOWN_ORDER) public void testForEachEntryOrdered() { List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet()); List<Entry<E>> actual = new ArrayList<>(); getMultiset() .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count))); assertEquals(expected, actual); }
@CollectionFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) @CollectionSize.Require(absent = ZERO) public void testRemoveAll_containsNullYes() { initCollectionWithNullElement(); assertTrue( "removeAll(containsNull) should return true", collection.removeAll(Collections.singleton(null))); // TODO: make this work with MinimalCollection }
@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION}) @CollectionSize.Require(absent = ZERO) public void testAddConcurrentWithIteration() { try { Iterator<E> iterator = collection.iterator(); assertTrue(collection.add(e3())); iterator.next(); fail("Expected ConcurrentModificationException"); } catch (ConcurrentModificationException expected) { // success } }
@CollectionSize.Require(ONE) @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) public void testToString_size1() { assertEquals( "size1Collection.toString should return [{element}]", "[" + e0() + "]", collection.toString()); }
@CollectionFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = {ZERO, ONE}) public void testRemoveAll_duplicate() { ArrayWithDuplicate<E> arrayAndDuplicate = createArrayWithDuplicateElement(); collection = getSubjectGenerator().create(arrayAndDuplicate.elements); E duplicate = arrayAndDuplicate.duplicate; assertTrue( "removeAll(intersectingCollection) should return true", getList().removeAll(MinimalCollection.of(duplicate))); assertFalse( "after removeAll(e), a collection should not contain e even " + "if it initially contained e more than once.", getList().contains(duplicate)); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testRemoveAllIgnoresCount() { initThreeCopies(); assertTrue(getMultiset().removeAll(Collections.singleton(e0()))); assertEmpty(getMultiset()); }
@CollectionFeature.Require(value = {SUPPORTS_ADD, ALLOWS_NULL_VALUES}) @CollectionSize.Require(absent = ZERO) public void testAdd_supportedNullPresent() { E[] array = createArrayWithNullElement(); collection = getSubjectGenerator().create(array); assertFalse("add(nullPresent) should return false", getSet().add(null)); expectContents(array); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(ALLOWS_NULL_VALUES) public void testSetCount_existingNoNopNull_nullSupported() { initCollectionWithNullElement(); try { assertSetCount(null, 1); } catch (UnsupportedOperationException tolerated) { } }
@CollectionFeature.Require(SUPPORTS_REMOVE) public void testSpliteratorNotImmutable_CollectionAllowsRemove() { // If remove is supported, verify that IMMUTABLE is not reported. synchronized (collection) { // for Collections.synchronized assertFalse(collection.spliterator().hasCharacteristics(Spliterator.IMMUTABLE)); } }
@CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) @CollectionSize.Require(absent = {ZERO, ONE}) public void testCreateWithDuplicates_nonNullDuplicatesNotRejected() { E[] array = createSamplesArray(); array[1] = e0(); collection = getSubjectGenerator().create(array); List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements()); expectContents(expectedWithDuplicateRemoved); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testElementSetRemovePropagatesToMultiset() { Set<E> elementSet = getMultiset().elementSet(); assertTrue(elementSet.remove(e0())); assertFalse(getMultiset().contains(e0())); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testKeySetIteratorRemove() { int key0Count = multimap().get(k0()).size(); Iterator<K> keyItr = multimap().keySet().iterator(); while (keyItr.hasNext()) { if (keyItr.next().equals(k0())) { keyItr.remove(); } } assertEquals(getNumElements() - key0Count, multimap().size()); assertGet(k0()); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testClearTailClosed() { List<Entry<E>> expected = copyToList(sortedMultiset.headMultiset(b.getElement(), OPEN).entrySet()); sortedMultiset.tailMultiset(b.getElement(), CLOSED).clear(); assertEquals(expected, copyToList(sortedMultiset.entrySet())); }
@CollectionFeature.Require(value = ALLOWS_NULL_VALUES, absent = REJECTS_DUPLICATES_AT_CREATION) @CollectionSize.Require(absent = {ZERO, ONE}) public void testCreateWithDuplicates_nullDuplicatesNotRejected() { E[] array = createArrayWithNullElement(); array[0] = null; collection = getSubjectGenerator().create(array); List<E> expectedWithDuplicateRemoved = Arrays.asList(array).subList(1, getNumElements()); expectContents(expectedWithDuplicateRemoved); }