@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemovePropagatesToAsMapEntrySet() { List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); for (Entry<K, V> entry : entries) { resetContainer(); K key = entry.getKey(); V value = entry.getValue(); Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); Collection<V> collection = null; while (asMapItr.hasNext()) { Entry<K, Collection<V>> asMapEntry = asMapItr.next(); if (key.equals(asMapEntry.getKey())) { collection = asMapEntry.getValue(); break; } } assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().remove(key, value); expectedCollection.remove(value); ASSERT.that(collection).hasContentsAnyOrder(expectedCollection.toArray()); assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); } }
/** * Returns the {@link Method} instances for the test methods in this class which call {@code * hashCode()} on the set values so that set tests on unhashable objects can suppress it with * {@code FeatureSpecificTestSuiteBuilder.suppressing()}. */ @GwtIncompatible("reflection") public static Method[] getHashCodeMethods() { return new Method[] { Helpers.getMethod(SetHashCodeTester.class, "testHashCode"), Helpers.getMethod(SetHashCodeTester.class, "testHashCode_containingNull") }; }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testPutPresentKeyPropagatesToAsMapEntrySet() { List<K> keys = Helpers.copyToList(multimap().keySet()); for (K key : keys) { resetContainer(); int size = getNumElements(); Iterator<Entry<K, Collection<V>>> asMapItr = multimap().asMap().entrySet().iterator(); Collection<V> collection = null; while (asMapItr.hasNext()) { Entry<K, Collection<V>> asMapEntry = asMapItr.next(); if (key.equals(asMapEntry.getKey())) { collection = asMapEntry.getValue(); break; } } assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().put(key, sampleValues().e3); expectedCollection.add(sampleValues().e3); assertThat(collection).has().exactlyAs(expectedCollection); assertEquals(size + 1, multimap().size()); } }
@MapFeature.Require(SUPPORTS_PUT) public void testPutNotPresentKeyPropagatesToEntries() { Collection<Entry<K, V>> entries = multimap().entries(); assertFalse(entries.contains(Helpers.mapEntry(sampleKeys().e3, sampleValues().e3))); multimap().put(sampleKeys().e3, sampleValues().e3); assertThat(entries).has().allOf(Helpers.mapEntry(sampleKeys().e3, sampleValues().e3)); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(SUPPORTS_REMOVE) public void testRemovePropagatesToMultimap() { assertTrue(multimap().entries().remove(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0))); expectMissing(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0)); assertEquals(getNumElements() - 1, multimap().size()); assertFalse(multimap().containsEntry(sampleKeys().e0, sampleValues().e0)); }
@Override public SampleElements<Entry<AnEnum, String>> samples() { return new SampleElements<Entry<AnEnum, String>>( Helpers.mapEntry(AnEnum.A, "January"), Helpers.mapEntry(AnEnum.B, "February"), Helpers.mapEntry(AnEnum.C, "March"), Helpers.mapEntry(AnEnum.D, "April"), Helpers.mapEntry(AnEnum.E, "May")); }
@Override public SampleElements<Map.Entry<String, String>> samples() { return new SampleElements<Map.Entry<String, String>>( Helpers.mapEntry("one", "January"), Helpers.mapEntry("two", "February"), Helpers.mapEntry("three", "March"), Helpers.mapEntry("four", "April"), Helpers.mapEntry("five", "May")); }
@Override public SampleElements<Entry<Country, Currency>> samples() { return new SampleElements<Entry<Country, Currency>>( Helpers.mapEntry(Country.CANADA, Currency.DOLLAR), Helpers.mapEntry(Country.CHILE, Currency.PESO), Helpers.mapEntry(Country.UK, Currency.POUND), Helpers.mapEntry(Country.JAPAN, Currency.YEN), Helpers.mapEntry(Country.SWITZERLAND, Currency.FRANC)); }
public static <K, V> SampleElements<Map.Entry<K, V>> mapEntries( SampleElements<K> keys, SampleElements<V> values) { return new SampleElements<Map.Entry<K, V>>( Helpers.mapEntry(keys.e0, values.e0), Helpers.mapEntry(keys.e1, values.e1), Helpers.mapEntry(keys.e2, values.e2), Helpers.mapEntry(keys.e3, values.e3), Helpers.mapEntry(keys.e4, values.e4)); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(SUPPORTS_REMOVE) public void testRetainAllPropagatesToMultimap() { multimap() .entries() .retainAll(Collections.singleton(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0))); assertEquals( getSubjectGenerator().create(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0)), multimap()); assertEquals(1, multimap().size()); assertTrue(multimap().containsEntry(sampleKeys().e0, sampleValues().e0)); }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(SEVERAL) public void testPollLast() { assertEquals(c, navigableMap.pollLastEntry()); assertEquals( entries.subList(0, entries.size() - 1), Helpers.copyToList(navigableMap.entrySet())); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(ALLOWS_NULL_VALUES) public void testContainsEntryWithNullValuePresent() { initMultimapWithNullValue(); // TODO(user): Change this back to Truth assertTrue(multimap().entries().contains(Helpers.mapEntry(getKeyForNullValue(), (V) null))); }
public void testIterator() { List<E> iteratorElements = new ArrayList<E>(); for (E element : collection) { // uses iterator() iteratorElements.add(element); } Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements); }
private static Set<Feature<?>> computeReserializedMultimapFeatures( Set<Feature<?>> multimapFeatures) { Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures); derivedFeatures.remove(CollectionFeature.SERIALIZABLE); derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS); return derivedFeatures; }
@CollectionSize.Require(ONE) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testIteratorRemovePropagatesToMultimap() { Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); assertEquals(Helpers.mapEntry(sampleKeys().e0, sampleValues().e0), iterator.next()); iterator.remove(); assertTrue(multimap().isEmpty()); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testPutPresentKeyPropagatesToGet() { List<K> keys = Helpers.copyToList(multimap().keySet()); for (K key : keys) { resetContainer(); int size = getNumElements(); Collection<V> collection = multimap().get(key); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().put(key, sampleValues().e3); expectedCollection.add(sampleValues().e3); assertThat(collection).has().exactlyAs(expectedCollection); assertEquals(size + 1, multimap().size()); } }
@CollectionFeature.Require(KNOWN_ORDER) public void testIterationOrdering() { List<E> iteratorElements = new ArrayList<E>(); for (E element : collection) { // uses iterator() iteratorElements.add(element); } List<E> expected = Helpers.copyToList(getOrderedElements()); assertEquals("Different ordered iteration", expected, iteratorElements); }
Set<Feature<?>> computeMultimapAsMapGetFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> derivedFeatures = Helpers.copyToSet(computeMultimapGetFeatures(multimapFeatures)); if (derivedFeatures.remove(CollectionSize.ANY)) { derivedFeatures.addAll(CollectionSize.ANY.getImpliedFeatures()); } derivedFeatures.remove(CollectionSize.ZERO); return derivedFeatures; }
public void testNullsLast() { Ordering<Integer> ordering = Ordering.natural().nullsLast(); Helpers.testComparator(ordering, 0, 1, Integer.MAX_VALUE, null); new EqualsTester() .addEqualityGroup(ordering, Ordering.natural().nullsLast()) .addEqualityGroup(numberOrdering.nullsLast()) .addEqualityGroup(Ordering.natural()) .testEquals(); }
@Override public void setUp() throws Exception { super.setUp(); navigableMap = (NavigableMap<K, V>) getMap(); entries = Helpers.copyToList( getSubjectGenerator() .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); // some tests assume SEVERAL == 3 if (entries.size() >= 1) { a = entries.get(0); if (entries.size() >= 3) { b = entries.get(1); c = entries.get(2); } } }
public void testReverse() { Ordering<Number> reverseOrder = numberOrdering.reverse(); Helpers.testComparator(reverseOrder, Integer.MAX_VALUE, 1, 0, -1, Integer.MIN_VALUE); new EqualsTester() .addEqualityGroup(reverseOrder, numberOrdering.reverse()) .addEqualityGroup(Ordering.natural().reverse()) .addEqualityGroup(Collections.reverseOrder()) .testEquals(); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_VALUES}) public void testRemoveNullValuePresent() { initMultimapWithNullValue(); assertTrue(multimap().remove(getKeyForNullValue(), null)); expectMissing(Helpers.mapEntry(getKeyForNullValue(), (V) null)); assertGet(getKeyForNullValue(), EMPTY); }
private void replaceValue(List<Entry<K, V>> expected, Entry<K, V> newEntry) { for (ListIterator<Entry<K, V>> i = expected.listIterator(); i.hasNext(); ) { if (Helpers.equal(i.next().getKey(), newEntry.getKey())) { i.set(newEntry); return; } } throw new IllegalArgumentException( Platform.format("key %s not found in entries %s", newEntry.getKey(), expected)); }
private static Set<Feature<?>> computeAsMapFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures); derivedFeatures.remove(MapFeature.GENERAL_PURPOSE); derivedFeatures.remove(MapFeature.SUPPORTS_PUT); derivedFeatures.remove(MapFeature.ALLOWS_NULL_VALUES); derivedFeatures.add(MapFeature.REJECTS_DUPLICATES_AT_CREATION); if (!derivedFeatures.contains(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) { derivedFeatures.remove(CollectionFeature.SERIALIZABLE); } return derivedFeatures; }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemovePropagatesToGet() { List<Entry<K, V>> entries = Helpers.copyToList(multimap().entries()); for (Entry<K, V> entry : entries) { resetContainer(); K key = entry.getKey(); V value = entry.getValue(); Collection<V> collection = multimap().get(key); assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().remove(key, value); expectedCollection.remove(value); ASSERT.that(collection).hasContentsAnyOrder(expectedCollection.toArray()); assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); } }
@Override protected List<Class<? extends AbstractTester>> getTesters() { List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); testers.add(CollectionSerializationEqualTester.class); testers.add(MultisetReadsTester.class); testers.add(MultisetSetCountConditionallyTester.class); testers.add(MultisetSetCountUnconditionallyTester.class); testers.add(MultisetWritesTester.class); testers.add(MultisetIteratorTester.class); testers.add(MultisetSerializationTester.class); return testers; }
public void testCopyOf_concurrentlyMutating() { List<String> sample = Lists.newArrayList("a", "b", "c"); for (int delta : new int[] {-1, 0, 1}) { for (int i = 0; i < sample.size(); i++) { Collection<String> misleading = Helpers.misleadingSizeCollection(delta); List<String> expected = sample.subList(0, i); misleading.addAll(expected); assertEquals(expected, ImmutableList.copyOf(misleading)); assertEquals(expected, ImmutableList.copyOf((Iterable<String>) misleading)); } } }
static Set<Feature<?>> computeDerivedCollectionFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures); if (!derivedFeatures.remove(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) { derivedFeatures.remove(CollectionFeature.SERIALIZABLE); } if (derivedFeatures.remove(MapFeature.ALLOWS_NULL_QUERIES)) { derivedFeatures.add(CollectionFeature.ALLOWS_NULL_QUERIES); } if (derivedFeatures.remove(MapFeature.SUPPORTS_REMOVE)) { derivedFeatures.add(CollectionFeature.SUPPORTS_REMOVE); } return derivedFeatures; }
public void testToArray_threadSafe() { for (int delta : new int[] {+1, 0, -1}) { for (int i = 0; i < VALUES.length; i++) { List<Float> list = Floats.asList(VALUES).subList(0, i); Collection<Float> misleadingSize = Helpers.misleadingSizeCollection(delta); misleadingSize.addAll(list); float[] arr = Floats.toArray(misleadingSize); assertEquals(i, arr.length); for (int j = 0; j < i; j++) { assertEquals(VALUES[j], arr[j]); } } } }
// TODO(benyu): turn this into a test once all copyOf(Collection) are // thread-safe @GwtIncompatible("GWT is single threaded") void verifyThreadSafe() { List<String> sample = Lists.newArrayList("a", "b", "c"); for (int delta : new int[] {-1, 0, 1}) { for (int i = 0; i < sample.size(); i++) { Collection<String> misleading = Helpers.misleadingSizeCollection(delta); List<String> expected = sample.subList(0, i); misleading.addAll(expected); assertEquals( "delta: " + delta + " sample size: " + i, Sets.newHashSet(expected), copyOf(misleading)); } } }