Skip to content

Commit a1c9d04

Browse files
testlib: add tests for Map.entrySet().stream() and Spliterator characteristics #7856
R
1 parent 92cca15 commit a1c9d04

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.google.common.collect.testing;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import com.google.common.collect.ImmutableMap;
6+
import junit.framework.TestCase;
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.Spliterator;
11+
import java.util.stream.Collectors;
12+
13+
public class EntrySetStreamTest extends TestCase {
14+
15+
public void testEntrySetStreamToList_BasicImmutableMap() {
16+
Map<String, Integer> map = ImmutableMap.of("a", 1, "b", 2);
17+
18+
assertThat(map.entrySet().stream().collect(Collectors.toList()))
19+
.containsExactlyElementsIn(map.entrySet())
20+
.inOrder();
21+
}
22+
23+
public void testEntrySetStreamToList_WithNullValueInHashMap() {
24+
Map<String, String> map = new HashMap<>();
25+
map.put("x", null);
26+
map.put("y", "yes");
27+
28+
assertThat(map.entrySet().stream().collect(Collectors.toList()))
29+
.containsExactlyElementsIn(map.entrySet())
30+
.inOrder();
31+
}
32+
33+
public void testSpliteratorCharacteristics_ImmutableMap() {
34+
Map<String, String> map = ImmutableMap.of("a", "1", "b", "2");
35+
36+
Spliterator<Map.Entry<String, String>> spliterator = map.entrySet().spliterator();
37+
38+
// NONNULL should be set since ImmutableMap does not allow nulls
39+
assertTrue((spliterator.characteristics() & Spliterator.NONNULL) != 0);
40+
}
41+
42+
public void testSpliteratorCharacteristics_HashMapWithNullValue() {
43+
Map<String, String> map = new HashMap<>();
44+
map.put("a", "1");
45+
map.put("b", null);
46+
47+
Spliterator<Map.Entry<String, String>> spliterator = map.entrySet().spliterator();
48+
49+
// NONNULL should NOT be set because value is null
50+
assertFalse((spliterator.characteristics() & Spliterator.NONNULL) != 0);
51+
}
52+
}

0 commit comments

Comments
 (0)