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 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(); }
@CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) @CollectionSize.Require(absent = ZERO) public void testIterator_removeAffectsBackingCollection() { int originalSize = collection.size(); Iterator<E> iterator = collection.iterator(); Object element = iterator.next(); // If it's an Entry, it may become invalid once it's removed from the Map. Copy it. if (element instanceof Entry) { Entry<?, ?> entry = (Entry<?, ?>) element; element = mapEntry(entry.getKey(), entry.getValue()); } assertTrue(collection.contains(element)); // sanity check iterator.remove(); assertFalse(collection.contains(element)); assertEquals(originalSize - 1, collection.size()); }
public Test testsForSingletonList() { return ListTestSuiteBuilder.using( new TestStringListGenerator() { @Override public List<String> create(String[] elements) { return Collections.singletonList(elements[0]); } }) .named("singletonList") .withFeatures( CollectionFeature.SERIALIZABLE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionSize.ONE) .suppressing(suppressForSingletonList()) .createTestSuite(); }
public Test testsForTreeMapWithComparator() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { return populate( new TreeMap<String, String>(arbitraryNullFriendlyComparator()), entries); } }) .named("TreeMap, with comparator") .withFeatures( MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_NULL_VALUES, MapFeature.ALLOWS_ANY_NULL_QUERIES, MapFeature.FAILS_FAST_ON_CONCURRENT_MODIFICATION, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForTreeMapWithComparator()) .createTestSuite(); }
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(); }
public Test testsForConcurrentSkipListMapWithComparator() { return NavigableMapTestSuiteBuilder.using( new TestStringSortedMapGenerator() { @Override protected SortedMap<String, String> create(Entry<String, String>[] entries) { return populate( new ConcurrentSkipListMap<String, String>(arbitraryNullFriendlyComparator()), entries); } }) .named("ConcurrentSkipListMap, with comparator") .withFeatures( MapFeature.GENERAL_PURPOSE, CollectionFeature.SUPPORTS_ITERATOR_REMOVE, CollectionFeature.KNOWN_ORDER, CollectionFeature.SERIALIZABLE, CollectionSize.ANY) .suppressing(suppressForConcurrentSkipListMap()) .createTestSuite(); }
public Test testsForArrayDeque() { return QueueTestSuiteBuilder.using( new TestStringQueueGenerator() { @Override public Queue<String> create(String[] elements) { return new ArrayDeque<String>(MinimalCollection.of(elements)); } }) .named("ArrayDeque") .withFeatures( CollectionFeature.GENERAL_PURPOSE, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .suppressing(suppressForArrayDeque()) .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(); }
public Test testsForEnumSet() { return SetTestSuiteBuilder.using( new TestEnumSetGenerator() { @Override public Set<AnEnum> create(AnEnum[] elements) { return (elements.length == 0) ? EnumSet.noneOf(AnEnum.class) : EnumSet.copyOf(MinimalCollection.of(elements)); } }) .named("EnumSet") .withFeatures( SetFeature.GENERAL_PURPOSE, CollectionFeature.SERIALIZABLE, CollectionFeature.KNOWN_ORDER, CollectionFeature.RESTRICTS_ELEMENTS, CollectionSize.ANY) .suppressing(suppressForEnumSet()) .createTestSuite(); }
@MapFeature.Require({SUPPORTS_PUT, SUPPORTS_REMOVE}) @CollectionSize.Require(absent = ZERO) public void testCompute_presentToAbsent() { assertNull( "Map.compute(present, functionReturningNull) should return null", getMap() .compute( k0(), (k, v) -> { assertEquals(k0(), k); assertEquals(v0(), v); return null; })); expectMissing(e0()); expectMissingKeys(k0()); assertEquals(getNumElements() - 1, getMap().size()); }
public Test testsForLinkedList() { return QueueTestSuiteBuilder.using( new TestStringQueueGenerator() { @Override public Queue<String> create(String[] elements) { return new LinkedList<String>(MinimalCollection.of(elements)); } }) .named("LinkedList") .withFeatures( CollectionFeature.GENERAL_PURPOSE, CollectionFeature.ALLOWS_NULL_VALUES, CollectionFeature.KNOWN_ORDER, CollectionSize.ANY) .skipCollectionTests() // already covered in TestsForListsInJavaUtil .suppressing(suppressForLinkedList()) .createTestSuite(); }
@MapFeature.Require({SUPPORTS_PUT, ALLOWS_NULL_KEYS}) @CollectionSize.Require(absent = ZERO) public void testComputeIfPresent_nullKeySupportedPresent() { initMapWithNullKey(); assertEquals( "computeIfPresent(null, function) should return new value", v3(), getMap() .computeIfPresent( null, (k, v) -> { assertNull(k); assertEquals(getValueForNullKey(), v); return v3(); })); Entry<K, V>[] expected = createArrayWithNullKey(); expected[getNullLocation()] = entry(null, v3()); expectContents(expected); }
@ListFeature.Require(SUPPORTS_REMOVE_WITH_INDEX) @CollectionSize.Require(absent = ZERO) public void testSubList_subListRemoveAffectsOriginal() { List<E> subList = getList().subList(0, 1); subList.remove(0); List<E> expected = Arrays.asList(createSamplesArray()).subList(1, getNumElements()); expectContents(expected); }
@SuppressWarnings("unchecked") @MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(ZERO) public void testInversePut() { getMap().put(k0(), v0()); getMap().inverse().put(v1(), k1()); expectAdded(e0(), e1()); }
@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_REMOVE}) @CollectionSize.Require(SEVERAL) public void testClearConcurrentWithEntrySetIteration() { try { Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); getMap().clear(); iterator.next(); fail("Expected ConcurrentModificationException"); } catch (ConcurrentModificationException expected) { // success } }
@SuppressWarnings("unchecked") @MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(ZERO) public void testForcePutOverwritesOldValueEntry() { getMap().put(k0(), v0()); getMap().forcePut(k1(), v0()); // verify that the bimap is unchanged expectAdded(Helpers.mapEntry(k1(), v0())); }
@CollectionFeature.Require(absent = REJECTS_DUPLICATES_AT_CREATION) @CollectionSize.Require(absent = {ZERO, ONE}) public void testIndexOf_duplicate() { E[] array = createSamplesArray(); array[getNumElements() / 2] = e0(); collection = getSubjectGenerator().create(array); assertEquals( "indexOf(duplicate) should return index of first occurrence", 0, getList().indexOf(e0())); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_REMOVE) @MultisetFeature.Require(ENTRIES_ARE_VIEWS) public void testEntryReflectsClear() { initThreeCopies(); assertEquals(3, getMultiset().count(e0())); Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); assertEquals(3, entry.getCount()); getMultiset().clear(); assertEquals(0, entry.getCount()); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_REMOVE) @MultisetFeature.Require(ENTRIES_ARE_VIEWS) public void testEntryReflectsEntrySetClear() { initThreeCopies(); assertEquals(3, getMultiset().count(e0())); Multiset.Entry<E> entry = Iterables.getOnlyElement(getMultiset().entrySet()); assertEquals(3, entry.getCount()); getMultiset().entrySet().clear(); assertEquals(0, entry.getCount()); }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testClearPropagatesToGet() { for (K key : sampleKeys()) { resetContainer(); Collection<V> collection = multimap().get(key); multimap().clear(); assertEmpty(collection); } }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testEntrySet_removeAllAbsent() { assertFalse( "multiset.entrySet.remove(missingEntry) returned true", getMultiset() .entrySet() .removeAll(Collections.singleton(Multisets.immutableEntry(e0(), 2)))); assertTrue( "multiset didn't contain element after removing a missing entry", getMultiset().contains(e0())); }
@ListFeature.Require(absent = SUPPORTS_ADD_WITH_INDEX) @CollectionSize.Require(absent = ZERO) public void testAddAllAtIndex_unsupportedSomePresent() { try { getList().addAll(0, MinimalCollection.of(e0(), e3())); fail("addAll(n, allPresent) should throw"); } catch (UnsupportedOperationException expected) { } expectUnchanged(); expectMissing(e3()); }
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES) @CollectionSize.Require(absent = ZERO) public void testReplaceEntry_presentNullValueUnsupported() { try { getMap().replace(k0(), v0(), null); fail("Expected NullPointerException"); } catch (NullPointerException expected) {} expectUnchanged(); }
@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); }
@CollectionSize.Require(absent = ZERO) @CollectionFeature.Require(absent = NON_STANDARD_TOSTRING) @MapFeature.Require(ALLOWS_NULL_VALUES) public void testToStringWithNullValue() { initMultimapWithNullValue(); testToStringMatchesAsMap(); }
@SuppressWarnings("unchecked") @CollectionFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(SEVERAL) public void testRetainAll_duplicatesRemoved() { E[] array = createSamplesArray(); array[1] = e0(); collection = getSubjectGenerator().create(array); assertTrue( "containsDuplicates.retainAll(subset) should return true", collection.retainAll(MinimalCollection.of(e2()))); expectContents(e2()); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(ALLOWS_NULL_VALUES) public void testEqualsMultimapWithNullValue() { Multimap<K, V> original = multimap(); initMultimapWithNullValue(); Multimap<K, V> withNull = multimap(); new EqualsTester() .addEqualityGroup(original) .addEqualityGroup( withNull, getSubjectGenerator().create((Object[]) createArrayWithNullValue())) .testEquals(); }
@MapFeature.Require(absent = SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testReplaceEntry_unsupportedPresent() { try { getMap().replace(k0(), v0(), v3()); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException expected) {} expectUnchanged(); }
@MapFeature.Require({FAILS_FAST_ON_CONCURRENT_MODIFICATION, SUPPORTS_PUT}) @CollectionSize.Require(absent = ZERO) public void testPutAllSomePresentConcurrentWithEntrySetIteration() { try { Iterator<Entry<K, V>> iterator = getMap().entrySet().iterator(); putAll(MinimalCollection.of(e3(), e0())); iterator.next(); fail("Expected ConcurrentModificationException"); } catch (ConcurrentModificationException expected) { // success } }
public Test testsForEmptyNavigableSet() { return SetTestSuiteBuilder.using( new TestStringSortedSetGenerator() { @Override public NavigableSet<String> create(String[] elements) { return Collections.emptyNavigableSet(); } }) .named("emptyNavigableSet") .withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO) .suppressing(suppressForEmptyNavigableSet()) .createTestSuite(); }
@CollectionSize.Require(SEVERAL) public void testLower() { resetWithHole(); assertEquals(null, sortedMultiset.headMultiset(a.getElement(), OPEN).lastEntry()); assertEquals(a, sortedMultiset.headMultiset(b.getElement(), OPEN).lastEntry()); assertEquals(a, sortedMultiset.headMultiset(c.getElement(), OPEN).lastEntry()); }
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUE_QUERIES) @CollectionSize.Require(absent = ZERO) public void testReplaceEntry_wrongValueNullValueUnsupported() { try { assertFalse(getMap().replace(k0(), v3(), null)); } catch (NullPointerException tolerated) { // the operation would be a no-op, so exceptions are allowed but not required } expectUnchanged(); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(SUPPORTS_REMOVE) public void testRemoveAllPropagatesToGet() { Collection<V> getResult = multimap().get(k0()); multimap().removeAll(k0()); assertEmpty(getResult); expectMissing(e0()); }
@SuppressWarnings("unchecked") @MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(SEVERAL) public void testMultimapRemoveDeletesFirstOccurrence() { resetContainer(mapEntry(k0(), v0()), mapEntry(k0(), v1()), mapEntry(k0(), v0())); List<V> list = multimap().get(k0()); multimap().remove(k0(), v0()); assertContentsInOrder(list, v1(), v0()); }
@CollectionSize.Require(SEVERAL) @MapFeature.Require(SUPPORTS_REMOVE) public void testKeysEntrySetRemove() { resetContainer( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0())); assertTrue(multimap().keys().entrySet().remove(Multisets.immutableEntry(k0(), 2))); assertEquals(1, multimap().size()); assertTrue(multimap().containsEntry(k1(), v0())); }
public Test testsForEmptySet() { return SetTestSuiteBuilder.using( new TestStringSetGenerator() { @Override public Set<String> create(String[] elements) { return Collections.emptySet(); } }) .named("emptySet") .withFeatures(CollectionFeature.SERIALIZABLE, CollectionSize.ZERO) .suppressing(suppressForEmptySet()) .createTestSuite(); }