1 /*
2  * Copyright (C) 2015 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 
15 package com.google.common.collect;
16 
17 import static com.google.common.truth.Truth.assertThat;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.base.Ascii;
21 import com.google.common.collect.testing.SpliteratorTester;
22 import java.util.Arrays;
23 import java.util.List;
24 import java.util.Spliterator;
25 import java.util.stream.DoubleStream;
26 import java.util.stream.IntStream;
27 import java.util.stream.LongStream;
28 import junit.framework.TestCase;
29 
30 /** Tests for {@code CollectSpliterators}. */
31 @GwtCompatible
32 public class CollectSpliteratorsTest extends TestCase {
testMap()33   public void testMap() {
34     SpliteratorTester.of(
35             () ->
36                 CollectSpliterators.map(
37                     Arrays.spliterator(new String[] {"a", "b", "c", "d", "e"}), Ascii::toUpperCase))
38         .expect("A", "B", "C", "D", "E");
39   }
40 
testFlatMap()41   public void testFlatMap() {
42     SpliteratorTester.of(
43             () ->
44                 CollectSpliterators.flatMap(
45                     Arrays.spliterator(new String[] {"abc", "", "de", "f", "g", ""}),
46                     (String str) -> Lists.charactersOf(str).spliterator(),
47                     Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL,
48                     7))
49         .expect('a', 'b', 'c', 'd', 'e', 'f', 'g');
50   }
51 
testFlatMap_nullStream()52   public void testFlatMap_nullStream() {
53     SpliteratorTester.of(
54             () ->
55                 CollectSpliterators.flatMap(
56                     Arrays.spliterator(new String[] {"abc", "", "de", "f", "g", ""}),
57                     (String str) -> str.isEmpty() ? null : Lists.charactersOf(str).spliterator(),
58                     Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL,
59                     7))
60         .expect('a', 'b', 'c', 'd', 'e', 'f', 'g');
61   }
62 
testFlatMapToInt_nullStream()63   public void testFlatMapToInt_nullStream() {
64     SpliteratorTester.ofInt(
65             () ->
66                 CollectSpliterators.flatMapToInt(
67                     Arrays.spliterator(new Integer[] {1, 0, 1, 2, 3}),
68                     (Integer i) -> i == 0 ? null : IntStream.of(i).spliterator(),
69                     Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL,
70                     4))
71         .expect(1, 1, 2, 3);
72   }
73 
testFlatMapToLong_nullStream()74   public void testFlatMapToLong_nullStream() {
75     SpliteratorTester.ofLong(
76             () ->
77                 CollectSpliterators.flatMapToLong(
78                     Arrays.spliterator(new Long[] {1L, 0L, 1L, 2L, 3L}),
79                     (Long i) -> i == 0L ? null : LongStream.of(i).spliterator(),
80                     Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL,
81                     4))
82         .expect(1L, 1L, 2L, 3L);
83   }
84 
testFlatMapToDouble_nullStream()85   public void testFlatMapToDouble_nullStream() {
86     SpliteratorTester.ofDouble(
87             () ->
88                 CollectSpliterators.flatMapToDouble(
89                     Arrays.spliterator(new Double[] {1.0, 0.0, 1.0, 2.0, 3.0}),
90                     (Double i) -> i == 0.0 ? null : DoubleStream.of(i).spliterator(),
91                     Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL,
92                     4))
93         .expect(1.0, 1.0, 2.0, 3.0);
94   }
95 
testMultisetsSpliterator()96   public void testMultisetsSpliterator() {
97     Multiset<String> multiset = TreeMultiset.create();
98     multiset.add("a", 3);
99     multiset.add("b", 1);
100     multiset.add("c", 2);
101 
102     List<String> actualValues = Lists.newArrayList();
103     multiset.spliterator().forEachRemaining(actualValues::add);
104     assertThat(multiset).containsExactly("a", "a", "a", "b", "c", "c").inOrder();
105   }
106 }
107