1 /*
   2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package org.openjdk.tests.java.util.stream;
  26 
  27 import org.testng.annotations.Test;
  28 
  29 import java.util.Arrays;
  30 import java.util.Collection;
  31 import java.util.Collections;
  32 import java.util.function.Function;
  33 import java.util.stream.*;
  34 
  35 import static java.util.stream.LambdaTestHelpers.*;
  36 
  37 /**
  38  * ExplodeOpTest
  39  *
  40  * @author Brian Goetz
  41  */
  42 @Test
  43 public class ExplodeOpTest extends OpTestCase {
  44 
  45     static final Function<Integer, Stream<Integer>> integerRangeMapper
  46             = e -> IntStream.range(0, e).boxed();
  47 
  48     public void testFlatMap() {
  49         String[] stringsArray = {"hello", "there", "", "yada"};
  50         Stream<String> strings = Arrays.asList(stringsArray).stream();
  51         assertConcat(strings.flatMap(flattenChars).iterator(), "hellothereyada");
  52 
  53         assertCountSum(countTo(10).stream().flatMap(mfId), 10, 55);
  54         assertCountSum(countTo(10).stream().flatMap(mfNull), 0, 0);
  55         assertCountSum(countTo(3).stream().flatMap(mfLt), 6, 4);
  56 
  57         exerciseOps(TestData.Factory.ofArray("stringsArray", stringsArray), s -> s.flatMap(flattenChars));
  58         exerciseOps(TestData.Factory.ofArray("LONG_STRING", new String[] {LONG_STRING}), s -> s.flatMap(flattenChars));
  59     }
  60 
  61     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
  62     public void testOps(String name, TestData.OfRef<Integer> data) {
  63         Collection<Integer> result = exerciseOps(data, s -> s.flatMap(mfId));
  64         assertEquals(data.size(), result.size());
  65 
  66         result = exerciseOps(data, s -> s.flatMap(mfNull));
  67         assertEquals(0, result.size());
  68 
  69         exerciseOps(data, s -> s.flatMap(mfLt));
  70         exerciseOps(data, s -> s.flatMap(integerRangeMapper));
  71         exerciseOps(data, s -> s.flatMap((Integer e) -> IntStream.range(0, e).boxed().limit(10)));
  72     }
  73 
  74     //
  75 
  76     @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class)
  77     public void testIntOps(String name, TestData.OfInt data) {
  78         Collection<Integer> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToInt(j -> j)));
  79         assertEquals(data.size(), result.size());
  80         assertContents(data, result);
  81 
  82         result = exerciseOps(data, s -> s.flatMap(i -> IntStream.empty()));
  83         assertEquals(0, result.size());
  84 
  85         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e)));
  86         exerciseOps(data, s -> s.flatMap(e -> IntStream.range(0, e).limit(10)));
  87     }
  88 
  89     //
  90 
  91     @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class)
  92     public void testLongOps(String name, TestData.OfLong data) {
  93         Collection<Long> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToLong(j -> j)));
  94         assertEquals(data.size(), result.size());
  95         assertContents(data, result);
  96 
  97         result = exerciseOps(data, s -> LongStream.empty());
  98         assertEquals(0, result.size());
  99 
 100         exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e)));
 101         exerciseOps(data, s -> s.flatMap(e -> LongStream.range(0, e).limit(10)));
 102     }
 103 
 104     //
 105 
 106     @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class)
 107     public void testDoubleOps(String name, TestData.OfDouble data) {
 108         Collection<Double> result = exerciseOps(data, s -> s.flatMap(i -> Collections.singleton(i).stream().mapToDouble(j -> j)));
 109         assertEquals(data.size(), result.size());
 110         assertContents(data, result);
 111 
 112         result = exerciseOps(data, s -> DoubleStream.empty());
 113         assertEquals(0, result.size());
 114 
 115         exerciseOps(data, s -> s.flatMap(e -> DoubleStream.range(0, e)));
 116         exerciseOps(data, s -> s.flatMap(e -> DoubleStream.range(0, e).limit(10)));
 117     }
 118 }