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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.openjdk.tests.java.util.stream; 24 25 import org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider; 26 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider; 27 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers; 28 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider; 29 import org.openjdk.testlib.java.util.stream.OpTestCase; 30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider; 31 import org.openjdk.testlib.java.util.stream.TestData; 32 33 import org.testng.annotations.Test; 34 35 import java.util.Spliterator; 36 import java.util.stream.BaseStream; 37 import java.util.stream.Stream; 38 import java.util.stream.IntStream; 39 import java.util.stream.LongStream; 40 import java.util.stream.DoubleStream; 41 42 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*; 43 44 /** 45 * MapOpTest 46 * 47 * @author Brian Goetz 48 */ 49 @Test 50 public class MapOpTest extends OpTestCase { 51 testMap()52 public void testMap() { 53 assertCountSum(countTo(0).stream().map(mId), 0, 0); 54 assertCountSum(countTo(10).stream().map(mId), 10, 55); 55 assertCountSum(countTo(10).stream().map(mZero), 10, 0); 56 assertCountSum(countTo(0).stream().map(mDoubler), 0, 0); 57 assertCountSum(countTo(10).stream().map(mDoubler), 10, 110); 58 assertCountSum(countTo(10).stream().map(mDoubler).map(mDoubler), 10, 220); 59 60 exerciseOps(countTo(0), s -> s.map(LambdaTestHelpers.identity()), countTo(0)); 61 exerciseOps(countTo(1000), s -> s.map(LambdaTestHelpers.identity()), countTo(1000)); 62 // @@@ Force cast to integer so output is Stream<Integer> rather an IntStream 63 // this just ensures that no warnings are logged about boxing 64 // when the result is compared with the output 65 exerciseOps(countTo(1000), s -> s.map(e -> (Integer) (1000 + e)), range(1001, 2000)); 66 } 67 testEveryMapShape()68 public void testEveryMapShape() { 69 assertCountSum(countTo(1000).stream() 70 .mapToInt(i -> i - 1) 71 .mapToObj(i -> i + 1) 72 .mapToLong(i -> i - 1) 73 .mapToObj(i -> i + 1) 74 .mapToDouble(i -> i - 1) 75 .mapToObj(i -> i + 1) 76 .mapToInt(i -> (int) (double) i) 77 .mapToLong(i -> i) 78 .mapToDouble(i -> i) 79 .mapToLong(i -> (long) i) 80 .mapToInt(i -> (int) i) 81 .mapToObj(i -> i), 82 1000, countTo(1000).stream().mapToInt(i -> i).sum()); 83 } 84 85 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)86 public void testOps(String name, TestData.OfRef<Integer> data) { 87 exerciseOpsInt(data, s -> s.map(mId), s -> s.map(e -> e), s -> s.map(e -> e), s -> s.map(e -> e)); 88 exerciseOpsInt(data, s -> s.map(mZero), s -> s.map(e -> 0), s -> s.map(e -> 0), s -> s.map(e -> 0)); 89 exerciseOpsInt(data, s -> s.map(mDoubler), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e)); 90 exerciseOpsInt(data, s -> s.map(LambdaTestHelpers.compose(mId, mDoubler)), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e)); 91 exerciseOpsInt(data, s -> s.map(LambdaTestHelpers.compose(mDoubler, mDoubler)), s -> s.map(e -> 4*e), s -> s.map(e -> 4*e), s -> s.map(e -> 4*e)); 92 exerciseOps(data, s -> s.mapToInt(i -> i)); 93 exerciseOps(data, s -> s.mapToLong(i -> i)); 94 exerciseOps(data, s -> s.mapToDouble(i -> i)); 95 } 96 97 // 98 99 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testIntOps(String name, TestData.OfInt data)100 public void testIntOps(String name, TestData.OfInt data) { 101 exerciseOps(data, s -> s.mapToObj(i -> i)); 102 exerciseOps(data, s -> s.map(i -> 0)); 103 exerciseOps(data, s -> s.map(i -> i * 2)); 104 exerciseOps(data, s -> s.asLongStream()); 105 exerciseOps(data, s -> s.asDoubleStream()); 106 exerciseOps(data, s -> s.boxed()); 107 exerciseOps(data, s -> s.mapToObj(Integer::toString)); 108 exerciseOps(data, s -> s.mapToLong(i -> i)); 109 exerciseOps(data, s -> s.mapToDouble(i -> i)); 110 } 111 112 // 113 114 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testLongOps(String name, TestData.OfLong data)115 public void testLongOps(String name, TestData.OfLong data) { 116 exerciseOps(data, s -> s.mapToObj(i -> i)); 117 exerciseOps(data, s -> s.map(i -> 0L)); 118 exerciseOps(data, s -> s.map(i -> i * 2L)); 119 exerciseOps(data, s -> s.asDoubleStream()); 120 exerciseOps(data, s -> s.boxed()); 121 exerciseOps(data, s -> s.mapToObj(e -> Long.toString(e))); 122 exerciseOps(data, s -> s.mapToInt(i -> (int) i)); 123 exerciseOps(data, s -> s.mapToDouble(i -> i)); 124 } 125 126 // 127 128 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleOps(String name, TestData.OfDouble data)129 public void testDoubleOps(String name, TestData.OfDouble data) { 130 exerciseOps(data, s -> s.mapToObj(i -> i)); 131 exerciseOps(data, s -> s.map(i -> 0.0)); 132 exerciseOps(data, s -> s.map(i -> i * 2.0)); 133 exerciseOps(data, s -> s.boxed()); 134 exerciseOps(data, s -> s.mapToObj(e -> Double.toString(e))); 135 exerciseOps(data, s -> s.mapToLong(i -> (long) i)); 136 exerciseOps(data, s -> s.mapToInt(i -> (int) i)); 137 } 138 } 139