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 @ElementTypesAreNonnullByDefault 33 public class CollectSpliteratorsTest extends TestCase { testMap()34 public void testMap() { 35 SpliteratorTester.of( 36 () -> 37 CollectSpliterators.map( 38 Arrays.spliterator(new String[] {"a", "b", "c", "d", "e"}), Ascii::toUpperCase)) 39 .expect("A", "B", "C", "D", "E"); 40 } 41 testFlatMap()42 public void testFlatMap() { 43 SpliteratorTester.of( 44 () -> 45 CollectSpliterators.flatMap( 46 Arrays.spliterator(new String[] {"abc", "", "de", "f", "g", ""}), 47 (String str) -> Lists.charactersOf(str).spliterator(), 48 Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL, 49 7)) 50 .expect('a', 'b', 'c', 'd', 'e', 'f', 'g'); 51 } 52 testFlatMap_nullStream()53 public void testFlatMap_nullStream() { 54 SpliteratorTester.of( 55 () -> 56 CollectSpliterators.flatMap( 57 Arrays.spliterator(new String[] {"abc", "", "de", "f", "g", ""}), 58 (String str) -> str.isEmpty() ? null : Lists.charactersOf(str).spliterator(), 59 Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL, 60 7)) 61 .expect('a', 'b', 'c', 'd', 'e', 'f', 'g'); 62 } 63 testFlatMapToInt_nullStream()64 public void testFlatMapToInt_nullStream() { 65 SpliteratorTester.ofInt( 66 () -> 67 CollectSpliterators.flatMapToInt( 68 Arrays.spliterator(new Integer[] {1, 0, 1, 2, 3}), 69 (Integer i) -> i == 0 ? null : IntStream.of(i).spliterator(), 70 Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL, 71 4)) 72 .expect(1, 1, 2, 3); 73 } 74 testFlatMapToLong_nullStream()75 public void testFlatMapToLong_nullStream() { 76 SpliteratorTester.ofLong( 77 () -> 78 CollectSpliterators.flatMapToLong( 79 Arrays.spliterator(new Long[] {1L, 0L, 1L, 2L, 3L}), 80 (Long i) -> i == 0L ? null : LongStream.of(i).spliterator(), 81 Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL, 82 4)) 83 .expect(1L, 1L, 2L, 3L); 84 } 85 testFlatMapToDouble_nullStream()86 public void testFlatMapToDouble_nullStream() { 87 SpliteratorTester.ofDouble( 88 () -> 89 CollectSpliterators.flatMapToDouble( 90 Arrays.spliterator(new Double[] {1.0, 0.0, 1.0, 2.0, 3.0}), 91 (Double i) -> i == 0.0 ? null : DoubleStream.of(i).spliterator(), 92 Spliterator.SIZED | Spliterator.DISTINCT | Spliterator.NONNULL, 93 4)) 94 .expect(1.0, 1.0, 2.0, 3.0); 95 } 96 testMultisetsSpliterator()97 public void testMultisetsSpliterator() { 98 Multiset<String> multiset = TreeMultiset.create(); 99 multiset.add("a", 3); 100 multiset.add("b", 1); 101 multiset.add("c", 2); 102 103 List<String> actualValues = Lists.newArrayList(); 104 multiset.spliterator().forEachRemaining(actualValues::add); 105 assertThat(multiset).containsExactly("a", "a", "a", "b", "c", "c").inOrder(); 106 } 107 } 108