Set<Feature<?>> computeMultimapGetFeatures(Set<Feature<?>> multimapFeatures) { Set<Feature<?>> derivedFeatures = Helpers.copyToSet(multimapFeatures); for (Map.Entry<Feature<?>, Feature<?>> entry : GET_FEATURE_MAP.entries()) { if (derivedFeatures.contains(entry.getKey())) { derivedFeatures.add(entry.getValue()); } } if (derivedFeatures.remove(MultimapFeature.VALUE_COLLECTIONS_SUPPORT_ITERATOR_REMOVE)) { derivedFeatures.add(CollectionFeature.SUPPORTS_ITERATOR_REMOVE); } if (!derivedFeatures.contains(CollectionFeature.SERIALIZABLE_INCLUDING_VIEWS)) { derivedFeatures.remove(CollectionFeature.SERIALIZABLE); } derivedFeatures.removeAll(GET_FEATURE_MAP.keySet()); return derivedFeatures; }
@MapFeature.Require(SUPPORTS_REMOVE) @CollectionSize.Require(absent = ZERO) public void testRemovePropagatesToAsMap() { 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().asMap().get(key); assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().remove(key, value); expectedCollection.remove(value); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(!expectedCollection.isEmpty(), multimap().containsKey(key)); } }
public void testArbitrary_withCollisions() { List<Integer> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(i); } Ordering<Object> arbitrary = new ArbitraryOrdering() { @Override int identityHashCode(Object object) { return ((Integer) object) % 5; // fake tons of collisions! } }; // Don't let the elements be in such a predictable order list = shuffledCopy(list, new Random(1)); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); }
@MapFeature.Require(SUPPORTS_PUT) @CollectionSize.Require(absent = ZERO) public void testPutPresentKeyPropagatesToAsMapGet() { List<K> keys = Helpers.copyToList(multimap().keySet()); for (K key : keys) { resetContainer(); int size = getNumElements(); Collection<V> collection = multimap().asMap().get(key); assertNotNull(collection); Collection<V> expectedCollection = Helpers.copyToList(collection); multimap().put(key, v3()); expectedCollection.add(v3()); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(size + 1, multimap().size()); } }
@CollectionSize.Require(SEVERAL) public void testOrderingDoesntAffectEqualsComparisons() { SetMultimap<K, V> multimap1 = getSubjectGenerator() .create( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k0(), v4())); SetMultimap<K, V> multimap2 = getSubjectGenerator() .create( Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v4())); new EqualsTester().addEqualityGroup(multimap1, multimap2).testEquals(); }
@SuppressWarnings("unchecked") // dang varargs public void testLexicographical() { Comparator<String> comparator = Ordering.natural(); Comparator<Iterable<String>> lexy = Comparators.lexicographical(comparator); ImmutableList<String> empty = ImmutableList.of(); ImmutableList<String> a = ImmutableList.of("a"); ImmutableList<String> aa = ImmutableList.of("a", "a"); ImmutableList<String> ab = ImmutableList.of("a", "b"); ImmutableList<String> b = ImmutableList.of("b"); Helpers.testComparator(lexy, empty, a, aa, ab, b); new EqualsTester() .addEqualityGroup(lexy, Comparators.lexicographical(comparator)) .addEqualityGroup(Comparators.lexicographical(String.CASE_INSENSITIVE_ORDER)) .addEqualityGroup(Ordering.natural()) .testEquals(); }
/** * Verify thread safety by using a collection whose size() may be inconsistent * with the actual number of elements. Tests using this method might fail in * GWT because the GWT emulations might count on size() during copy. It is * safe to do so in GWT because javascript is single-threaded. */ // 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)); } } }
@Override public void setUp() throws Exception { super.setUp(); navigableSet = (NavigableSet<E>) getSet(); values = Helpers.copyToList( getSubjectGenerator() .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); Collections.sort(values, navigableSet.comparator()); // some tests assume SEVERAL == 3 if (values.size() >= 1) { a = values.get(0); if (values.size() >= 3) { b = values.get(1); c = values.get(2); } } }
@CollectionSize.Require(ONE) @CollectionFeature.Require(SUPPORTS_ITERATOR_REMOVE) public void testIteratorRemovePropagatesToMultimap() { Iterator<Entry<K, V>> iterator = multimap().entries().iterator(); assertEquals(Helpers.mapEntry(k0(), v0()), iterator.next()); iterator.remove(); assertTrue(multimap().isEmpty()); }
public void testLexicographicalComparator() { List<double[]> ordered = Arrays.asList( new double[] {}, new double[] {LEAST}, new double[] {LEAST, LEAST}, new double[] {LEAST, (double) 1}, new double[] {(double) 1}, new double[] {(double) 1, LEAST}, new double[] {GREATEST, Double.MAX_VALUE}, new double[] {GREATEST, GREATEST}, new double[] {GREATEST, GREATEST, GREATEST}); Comparator<double[]> comparator = Doubles.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); }
public void testForEachEntry() { List<Entry<E>> expected = new ArrayList<>(getMultiset().entrySet()); List<Entry<E>> actual = new ArrayList<>(); getMultiset() .forEachEntry((element, count) -> actual.add(Multisets.immutableEntry(element, count))); Helpers.assertEqualIgnoringOrder(expected, actual); }
public void testArbitrary_withoutCollisions() { List<Object> list = Lists.newArrayList(); for (int i = 0; i < 50; i++) { list.add(new Object()); } Ordering<Object> arbitrary = Ordering.arbitrary(); Collections.sort(list, arbitrary); // Now we don't care what order it's put the list in, only that // comparing any pair of elements gives the answer we expect. Helpers.testComparator(arbitrary, list); assertEquals("Ordering.arbitrary()", arbitrary.toString()); }
@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, v3()); expectedCollection.add(v3()); assertEqualIgnoringOrder(expectedCollection, collection); assertEquals(size + 1, multimap().size()); } }
public void testLexicographicalComparator() { List<long[]> ordered = Arrays.asList( new long[] {}, new long[] {MIN_VALUE}, new long[] {MIN_VALUE, MIN_VALUE}, new long[] {MIN_VALUE, (long) 1}, new long[] {(long) 1}, new long[] {(long) 1, MIN_VALUE}, new long[] {MAX_VALUE, MAX_VALUE - (long) 1}, new long[] {MAX_VALUE, MAX_VALUE}, new long[] {MAX_VALUE, MAX_VALUE, MAX_VALUE}); Comparator<long[]> comparator = Longs.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); }
public void testToArray_threadSafe() { for (int delta : new int[] { +1, 0, -1 }) { for (int i = 0; i < VALUES.length; i++) { List<Long> list = Longs.asList(VALUES).subList(0, i); Collection<Long> misleadingSize = Helpers.misleadingSizeCollection(delta); misleadingSize.addAll(list); long[] arr = Longs.toArray(misleadingSize); assertEquals(i, arr.length); for (int j = 0; j < i; j++) { assertEquals(VALUES[j], arr[j]); } } } }
@Override protected List<Class<? extends AbstractTester>> getTesters() { List<Class<? extends AbstractTester>> testers = Helpers.copyToList(super.getTesters()); testers.add(ListMultimapAsMapTester.class); testers.add(ListMultimapEqualsTester.class); testers.add(ListMultimapPutTester.class); testers.add(ListMultimapPutAllTester.class); testers.add(ListMultimapRemoveTester.class); testers.add(ListMultimapReplaceValuesTester.class); return testers; }
@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")); }
@CollectionFeature.Require(absent = KNOWN_ORDER) public void testStreamToArrayUnknownOrder() { synchronized (collection) { // to allow Collections.synchronized* tests to pass Helpers.assertEqualIgnoringOrder( getSampleElements(), Arrays.asList(collection.stream().toArray())); } }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(SUPPORTS_PUT) public void testPutPresentKeyPropagatesToEntries() { Collection<Entry<K, V>> entries = multimap().entries(); assertFalse(entries.contains(Helpers.mapEntry(k0(), v3()))); multimap().put(k0(), v3()); assertContains(entries, Helpers.mapEntry(k0(), v3())); }
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(); }
/** * Returns {@link Method} instances for the tests that assume multisets support duplicates so that * the test of {@code Multisets.forSet()} can suppress them. */ @GwtIncompatible // reflection public static List<Method> getIteratorDuplicateInitializingMethods() { return Arrays.asList( Helpers.getMethod(MultisetIteratorTester.class, "testIteratorKnownOrder"), Helpers.getMethod(MultisetIteratorTester.class, "testIteratorUnknownOrder"), Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorKnownOrder"), Helpers.getMethod(MultisetIteratorTester.class, "testRemovingIteratorUnknownOrder")); }
@CollectionSize.Require(SEVERAL) @CollectionFeature.Require(SUPPORTS_REMOVE) public void testRetainAllIgnoresCount() { initThreeCopies(); List<E> contents = Helpers.copyToList(getMultiset()); assertFalse(getMultiset().retainAll(Collections.singleton(e0()))); expectContents(contents); }
public void testImmutableEnumMapOrdering() { ImmutableMap<AnEnum, String> map = Maps.immutableEnumMap( ImmutableMap.of(AnEnum.C, "c", AnEnum.A, "a", AnEnum.E, "e")); assertThat(map.entrySet()).containsExactly( Helpers.mapEntry(AnEnum.A, "a"), Helpers.mapEntry(AnEnum.C, "c"), Helpers.mapEntry(AnEnum.E, "e")).inOrder(); }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require({SUPPORTS_REMOVE, ALLOWS_NULL_KEYS}) public void testRemoveNullKeyPresent() { initMultimapWithNullKey(); assertTrue(multimap().remove(null, getValueForNullKey())); expectMissing(Helpers.mapEntry((K) null, getValueForNullKey())); assertGet(getKeyForNullValue(), ImmutableList.<V>of()); }
@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(), ImmutableList.<V>of()); }
@CollectionSize.Require(SEVERAL) public void testKeys() { resetContainer( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k0(), v1()), Helpers.mapEntry(k1(), v0())); Multiset<K> keys = multimap().keys(); assertEquals(2, keys.count(k0())); assertEquals(1, keys.count(k1())); assertEquals(3, keys.size()); assertContainsAllOf(keys, k0(), k1()); assertContainsAllOf( keys.entrySet(), Multisets.immutableEntry(k0(), 2), Multisets.immutableEntry(k1(), 1)); }
@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 void testIterator() { List<E> iteratorElements = new ArrayList<E>(); for (E element : collection) { // uses iterator() iteratorElements.add(element); } Helpers.assertEqualIgnoringOrder(Arrays.asList(createSamplesArray()), iteratorElements); }
@CollectionSize.Require(SEVERAL) @MapFeature.Require(SUPPORTS_REMOVE) public void testValuesRemove() { resetContainer( Helpers.mapEntry(k0(), v0()), Helpers.mapEntry(k1(), v0()), Helpers.mapEntry(k0(), v3())); assertTrue(multimap().asMap().values().remove(Collections.singletonList(v0()))); assertEquals(2, multimap().size()); assertEquals( Collections.singletonMap(k0(), Lists.newArrayList(v0(), v3())), multimap().asMap()); }
@MapFeature.Require(ALLOWS_NULL_VALUES) @CollectionSize.Require(absent = ZERO) public void testForEach_nullValues() { initMapWithNullValue(); List<Entry<K, V>> expectedEntries = Arrays.asList(createArrayWithNullValue()); List<Entry<K, V>> entries = new ArrayList<>(); getMap().forEach((k, v) -> entries.add(entry(k, v))); Helpers.assertEqualIgnoringOrder(expectedEntries, entries); }
public void testHeadMap() { List<Entry<K, V>> entries = Helpers.copyToList( getSubjectGenerator() .getSampleElements(getSubjectGenerator().getCollectionSize().getNumElements())); Collections.sort(entries, Helpers.<K, V>entryComparator(navigableMap.comparator())); for (int i = 0; i < entries.size(); i++) { assertEqualInOrder( entries.subList(0, i), navigableMap.headMap(entries.get(i).getKey()).entrySet()); } }
@ListFeature.Require(SUPPORTS_SET) @CollectionSize.Require(absent = {ZERO, ONE}) public void testSubList_subListSetAffectsOriginalLargeList() { List<E> subList = getList().subList(1, 2); subList.set(0, e3()); List<E> expected = Helpers.copyToList(createSamplesArray()); expected.set(1, e3()); expectContents(expected); }
public void testLexicographicalComparator() { List<int[]> ordered = Arrays.asList( new int[] {}, new int[] {LEAST}, new int[] {LEAST, LEAST}, new int[] {LEAST, (int) 1}, new int[] {(int) 1}, new int[] {(int) 1, LEAST}, new int[] {GREATEST, GREATEST - (int) 1}, new int[] {GREATEST, GREATEST}, new int[] {GREATEST, GREATEST, GREATEST}); Comparator<int[]> comparator = Ints.lexicographicalComparator(); Helpers.testComparator(comparator, ordered); }
@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())); }
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)); } } }
public void testCompound_instance() { Comparator<String> comparator = byCharAt(1).compound(byCharAt(0)); Helpers.testComparator(comparator, ImmutableList.of( "red", "yellow", "violet", "blue", "indigo", "green", "orange")); }
public void testContainsEntryWithIncomparableKey() { try { assertFalse(getMap().entrySet().contains(Helpers.mapEntry(IncomparableType.INSTANCE, v0()))); } catch (ClassCastException acceptable) { // allowed by the spec } }
@CollectionSize.Require(absent = ZERO) @MapFeature.Require(SUPPORTS_REMOVE) public void testRemovePropagatesToMultimap() { assertTrue(multimap().entries().remove(Helpers.mapEntry(k0(), v0()))); expectMissing(Helpers.mapEntry(k0(), v0())); assertEquals(getNumElements() - 1, multimap().size()); assertFalse(multimap().containsEntry(k0(), v0())); }