Skip to content

Commit

Permalink
Collection tests: Add explicit type parameters for J2KT and enable J2…
Browse files Browse the repository at this point in the history
…KT tests

- The J2KT type inference often cannot infer `@Nullable` on type arguments
- Kotlin doesn’t allow infering a function’s type arguments from the return type
  alone. J2KT errs on the side of printing anything that can be inferred in
  Java, but there are some situations where it backs off (`this.of()` in this cl)

PiperOrigin-RevId: 613629330
  • Loading branch information
martinkretzschmar authored and Google Java Core Libraries committed Mar 7, 2024
1 parent 1bb3c43 commit 0b1c477
Show file tree
Hide file tree
Showing 52 changed files with 326 additions and 258 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ protected abstract <E extends Comparable<? super E>> Set<E> copyOf(
public void testCreation_noArgs() {
Set<String> set = of();
assertEquals(Collections.<String>emptySet(), set);
assertSame(of(), set);
assertSame(this.<String>of(), set);
}

public void testCreation_oneElement() {
Expand Down Expand Up @@ -122,7 +122,7 @@ public void testCopyOf_emptyArray() {
String[] array = new String[0];
Set<String> set = copyOf(array);
assertEquals(Collections.<String>emptySet(), set);
assertSame(of(), set);
assertSame(this.<String>of(), set);
}

public void testCopyOf_arrayOfOneElement() {
Expand Down Expand Up @@ -153,7 +153,7 @@ public void testCopyOf_collection_empty() {
Collection<String> c = MinimalCollection.<String>of();
Set<String> set = copyOf(c);
assertEquals(Collections.<String>emptySet(), set);
assertSame(of(), set);
assertSame(this.<String>of(), set);
}

public void testCopyOf_collection_oneElement() {
Expand Down Expand Up @@ -203,7 +203,7 @@ public void testCopyOf_iterator_empty() {
Iterator<String> iterator = Iterators.emptyIterator();
Set<String> set = copyOf(iterator);
assertEquals(Collections.<String>emptySet(), set);
assertSame(of(), set);
assertSame(this.<String>of(), set);
}

public void testCopyOf_iterator_oneElement() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,12 @@ public void testTransformEntrySetContains() {

Set<Entry<@Nullable String, @Nullable Boolean>> entries = map.entrySet();
assertTrue(entries.contains(Maps.immutableEntry("a", true)));
assertTrue(entries.contains(Maps.immutableEntry("b", (Boolean) null)));
assertTrue(entries.contains(Maps.immutableEntry((String) null, (Boolean) null)));
assertTrue(entries.contains(Maps.<String, @Nullable Boolean>immutableEntry("b", null)));
assertTrue(
entries.contains(Maps.<@Nullable String, @Nullable Boolean>immutableEntry(null, null)));

assertFalse(entries.contains(Maps.immutableEntry("c", (Boolean) null)));
assertFalse(entries.contains(Maps.immutableEntry((String) null, true)));
assertFalse(entries.contains(Maps.<String, @Nullable Boolean>immutableEntry("c", null)));
assertFalse(entries.contains(Maps.<@Nullable String, Boolean>immutableEntry(null, true)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ForwardingSortedMapImplementsMapTest() {
@Override
protected SortedMap<String, Integer> makeEmptyMap() {
return new SimpleForwardingSortedMap<>(
new TreeMap<String, Integer>(Ordering.natural().nullsFirst()));
new TreeMap<String, Integer>(Ordering.<String>natural().nullsFirst()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
@GwtCompatible(emulated = true)
@ElementTypesAreNonnullByDefault
public class GeneralRangeTest extends TestCase {
private static final Ordering<@Nullable Integer> ORDERING = Ordering.natural().nullsFirst();
private static final Ordering<@Nullable Integer> ORDERING =
Ordering.<Integer>natural().<Integer>nullsFirst();

private static final List<@Nullable Integer> IN_ORDER_VALUES = Arrays.asList(null, 1, 2, 3, 4, 5);

Expand Down Expand Up @@ -150,7 +151,7 @@ public void testIntersectAgainstBiggerRange() {

assertEquals(
GeneralRange.range(ORDERING, 2, CLOSED, 4, OPEN),
range.intersect(GeneralRange.range(ORDERING, null, OPEN, 5, CLOSED)));
range.intersect(GeneralRange.<@Nullable Integer>range(ORDERING, null, OPEN, 5, CLOSED)));

assertEquals(
GeneralRange.range(ORDERING, 2, OPEN, 4, OPEN),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ public void testCopyOfNullKey() {
}

public void testCopyOfNullValue() {
ArrayListMultimap<String, Integer> input = ArrayListMultimap.create();
input.putAll("foo", Arrays.asList(1, null, 3));
ArrayListMultimap<String, @Nullable Integer> input = ArrayListMultimap.create();
input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3));
try {
ImmutableListMultimap.copyOf(input);
ImmutableListMultimap.copyOf((ArrayListMultimap<String, Integer>) input);
fail();
} catch (NullPointerException expected) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,9 +635,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() {
}

builder = ImmutableList.builder();
Iterator<String> iteratorWithNulls = asList("a", null, "b").iterator();
Iterator<@Nullable String> iteratorWithNulls =
Arrays.<@Nullable String>asList("a", null, "b").iterator();
try {
builder.addAll(iteratorWithNulls);
builder.addAll((Iterator<String>) iteratorWithNulls);
fail("expected NullPointerException");
} catch (NullPointerException expected) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.google.common.testing.NullPointerTester;
import com.google.common.testing.SerializableTester;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
Expand Down Expand Up @@ -264,9 +265,10 @@ public void testCopyOf_multiset_general() {
}

public void testCopyOf_multisetContainingNull() {
Multiset<String> c = HashMultiset.create(asList("a", null, "b"));
Multiset<@Nullable String> c =
HashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b"));
try {
ImmutableMultiset.copyOf(c);
ImmutableMultiset.copyOf((Multiset<String>) c);
fail();
} catch (NullPointerException expected) {
}
Expand All @@ -291,9 +293,10 @@ public void testCopyOf_iterator_general() {
}

public void testCopyOf_iteratorContainingNull() {
Iterator<String> iterator = asList("a", null, "b").iterator();
Iterator<@Nullable String> iterator =
Arrays.<@Nullable String>asList("a", null, "b").iterator();
try {
ImmutableMultiset.copyOf(iterator);
ImmutableMultiset.copyOf((Iterator<String>) iterator);
fail();
} catch (NullPointerException expected) {
}
Expand Down Expand Up @@ -430,9 +433,10 @@ public void testBuilderAddAllHandlesNullsCorrectly() {
}

builder = ImmutableMultiset.builder();
Multiset<String> multisetWithNull = LinkedHashMultiset.create(asList("a", null, "b"));
Multiset<@Nullable String> multisetWithNull =
LinkedHashMultiset.create(Arrays.<@Nullable String>asList("a", null, "b"));
try {
builder.addAll(multisetWithNull);
builder.addAll((Multiset<String>) multisetWithNull);
fail("expected NullPointerException");
} catch (NullPointerException expected) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,10 @@ public void testCopyOfNullKey() {
}

public void testCopyOfNullValue() {
HashMultimap<String, Integer> input = HashMultimap.create();
input.putAll("foo", Arrays.asList(1, null, 3));
HashMultimap<String, @Nullable Integer> input = HashMultimap.create();
input.putAll("foo", Arrays.<@Nullable Integer>asList(1, null, 3));
try {
ImmutableSetMultimap.copyOf(input);
ImmutableSetMultimap.copyOf((Multimap<String, Integer>) input);
fail();
} catch (NullPointerException expected) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ public void testCopyOf() {
}

public void testCopyOfExplicitComparator() {
Comparator<String> comparator = Ordering.natural().reverse();
Comparator<String> comparator = Ordering.<String>natural().reverse();
Map<String, Integer> original = new LinkedHashMap<>();
original.put("one", 1);
original.put("two", 2);
Expand All @@ -524,7 +524,7 @@ public void testCopyOfExplicitComparator() {
}

public void testCopyOfImmutableSortedSetDifferentComparator() {
Comparator<String> comparator = Ordering.natural().reverse();
Comparator<String> comparator = Ordering.<String>natural().reverse();
Map<String, Integer> original = ImmutableSortedMap.of("one", 1, "two", 2, "three", 3);
ImmutableSortedMap<String, Integer> copy = ImmutableSortedMap.copyOf(original, comparator);
assertMapEquals(copy, "two", 2, "three", 3, "one", 1);
Expand All @@ -545,7 +545,7 @@ public void testCopyOfSortedNatural() {
}

public void testCopyOfSortedExplicit() {
Comparator<String> comparator = Ordering.natural().reverse();
Comparator<String> comparator = Ordering.<String>natural().reverse();
SortedMap<String, Integer> original = Maps.newTreeMap(comparator);
original.put("one", 1);
original.put("two", 2);
Expand Down Expand Up @@ -613,11 +613,11 @@ public void testBuilderReverseOrder() {
.put("five", 5)
.build();
assertMapEquals(map, "two", 2, "three", 3, "one", 1, "four", 4, "five", 5);
assertEquals(Ordering.natural().reverse(), map.comparator());
assertEquals(Ordering.<String>natural().reverse(), map.comparator());
}

public void testBuilderComparator() {
Comparator<String> comparator = Ordering.natural().reverse();
Comparator<String> comparator = Ordering.<String>natural().reverse();
ImmutableSortedMap<String, Integer> map =
new ImmutableSortedMap.Builder<String, Integer>(comparator)
.put("one", 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,26 +293,26 @@ public void testSingle_headSet() {
SortedSet<String> set = of("e");
assertTrue(set.headSet("g") instanceof ImmutableSortedSet);
assertThat(set.headSet("g")).contains("e");
assertSame(of(), set.headSet("c"));
assertSame(of(), set.headSet("e"));
assertSame(this.<String>of(), set.headSet("c"));
assertSame(this.<String>of(), set.headSet("e"));
}

public void testSingle_tailSet() {
SortedSet<String> set = of("e");
assertTrue(set.tailSet("c") instanceof ImmutableSortedSet);
assertThat(set.tailSet("c")).contains("e");
assertThat(set.tailSet("e")).contains("e");
assertSame(of(), set.tailSet("g"));
assertSame(this.<String>of(), set.tailSet("g"));
}

public void testSingle_subSet() {
SortedSet<String> set = of("e");
assertTrue(set.subSet("c", "g") instanceof ImmutableSortedSet);
assertThat(set.subSet("c", "g")).contains("e");
assertThat(set.subSet("e", "g")).contains("e");
assertSame(of(), set.subSet("f", "g"));
assertSame(of(), set.subSet("c", "e"));
assertSame(of(), set.subSet("c", "d"));
assertSame(this.<String>of(), set.subSet("f", "g"));
assertSame(this.<String>of(), set.subSet("c", "e"));
assertSame(this.<String>of(), set.subSet("c", "d"));
}

public void testSingle_first() {
Expand Down Expand Up @@ -398,26 +398,26 @@ public void testOf_headSet() {
assertTrue(set.headSet("e") instanceof ImmutableSortedSet);
assertThat(set.headSet("e")).containsExactly("b", "c", "d").inOrder();
assertThat(set.headSet("g")).containsExactly("b", "c", "d", "e", "f").inOrder();
assertSame(of(), set.headSet("a"));
assertSame(of(), set.headSet("b"));
assertSame(this.<String>of(), set.headSet("a"));
assertSame(this.<String>of(), set.headSet("b"));
}

public void testOf_tailSet() {
SortedSet<String> set = of("e", "f", "b", "d", "c");
assertTrue(set.tailSet("e") instanceof ImmutableSortedSet);
assertThat(set.tailSet("e")).containsExactly("e", "f").inOrder();
assertThat(set.tailSet("a")).containsExactly("b", "c", "d", "e", "f").inOrder();
assertSame(of(), set.tailSet("g"));
assertSame(this.<String>of(), set.tailSet("g"));
}

public void testOf_subSet() {
SortedSet<String> set = of("e", "f", "b", "d", "c");
assertTrue(set.subSet("c", "e") instanceof ImmutableSortedSet);
assertThat(set.subSet("c", "e")).containsExactly("c", "d").inOrder();
assertThat(set.subSet("a", "g")).containsExactly("b", "c", "d", "e", "f").inOrder();
assertSame(of(), set.subSet("a", "b"));
assertSame(of(), set.subSet("g", "h"));
assertSame(of(), set.subSet("c", "c"));
assertSame(this.<String>of(), set.subSet("a", "b"));
assertSame(this.<String>of(), set.subSet("g", "h"));
assertSame(this.<String>of(), set.subSet("c", "c"));
try {
set.subSet("e", "c");
fail();
Expand Down Expand Up @@ -1138,7 +1138,7 @@ private static ImmutableList<String> sortedNumberNames(int i, int j) {
ImmutableList.of("one", "two", "three", "four", "five", "six", "seven");

private static final ImmutableList<String> SORTED_NUMBER_NAMES =
Ordering.natural().immutableSortedCopy(NUMBER_NAMES);
Ordering.<String>natural().immutableSortedCopy(NUMBER_NAMES);

private static class SelfComparableExample implements Comparable<SelfComparableExample> {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public void testGetOnlyElement_withDefault_empty() {

public void testGetOnlyElement_withDefault_empty_null() {
Iterable<String> iterable = Collections.emptyList();
assertNull(Iterables.getOnlyElement(iterable, null));
assertNull(Iterables.<@Nullable String>getOnlyElement(iterable, null));
}

public void testGetOnlyElement_withDefault_multiple() {
Expand Down Expand Up @@ -479,7 +479,7 @@ public void testPaddedPartition_basic() {
List<Integer> list = asList(1, 2, 3, 4, 5);
Iterable<List<@Nullable Integer>> partitions = Iterables.paddedPartition(list, 2);
assertEquals(3, Iterables.size(partitions));
assertEquals(asList(5, null), Iterables.getLast(partitions));
assertEquals(Arrays.<@Nullable Integer>asList(5, null), Iterables.getLast(partitions));
}

public void testPaddedPartitionRandomAccessInput() {
Expand Down Expand Up @@ -537,7 +537,7 @@ public void testElementsEqual() throws Exception {
assertFalse(Iterables.elementsEqual(a, b));

// null versus non-null.
a = asList(4, 8, 15, null, 23, 42);
a = Arrays.<@Nullable Integer>asList(4, 8, 15, null, 23, 42);
b = asList(4, 8, 15, 16, 23, 42);
assertFalse(Iterables.elementsEqual(a, b));
assertFalse(Iterables.elementsEqual(b, a));
Expand Down Expand Up @@ -824,7 +824,7 @@ public void testGetFirst_withDefault_empty() {

public void testGetFirst_withDefault_empty_null() {
Iterable<String> iterable = Collections.emptyList();
assertNull(Iterables.getFirst(iterable, null));
assertNull(Iterables.<@Nullable String>getFirst(iterable, null));
}

public void testGetFirst_withDefault_multiple() {
Expand Down Expand Up @@ -863,7 +863,7 @@ public void testGetLast_withDefault_empty() {

public void testGetLast_withDefault_empty_null() {
Iterable<String> iterable = Collections.emptyList();
assertNull(Iterables.getLast(iterable, null));
assertNull(Iterables.<@Nullable String>getLast(iterable, null));
}

public void testGetLast_withDefault_multiple() {
Expand Down Expand Up @@ -1350,7 +1350,7 @@ public void testMergeSorted_pyramid() {
list.add(j);
allIntegers.add(j);
}
iterables.add(Ordering.natural().sortedCopy(list));
iterables.add(Ordering.<Integer>natural().sortedCopy(list));
}

verifyMergeSorted(iterables, allIntegers);
Expand All @@ -1367,7 +1367,7 @@ public void testMergeSorted_skipping_pyramid() {
list.add(j * i);
allIntegers.add(j * i);
}
iterables.add(Ordering.natural().sortedCopy(list));
iterables.add(Ordering.<Integer>natural().sortedCopy(list));
}

verifyMergeSorted(iterables, allIntegers);
Expand All @@ -1385,7 +1385,7 @@ public void testIterables_nullCheck() throws Exception {

private static void verifyMergeSorted(
Iterable<Iterable<Integer>> iterables, Iterable<Integer> unsortedExpected) {
Iterable<Integer> expected = Ordering.natural().sortedCopy(unsortedExpected);
Iterable<Integer> expected = Ordering.<Integer>natural().sortedCopy(unsortedExpected);

Iterable<Integer> mergedIterator = Iterables.mergeSorted(iterables, Ordering.natural());

Expand Down
Loading

0 comments on commit 0b1c477

Please sign in to comment.