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.testng.annotations.Test; 26 27 import java.util.stream.*; 28 29 import static java.util.stream.LambdaTestHelpers.*; 30 31 /** 32 * MapOpTest 33 * 34 * @author Brian Goetz 35 */ 36 @Test 37 public class MapOpTest extends OpTestCase { 38 testMap()39 public void testMap() { 40 assertCountSum(countTo(0).stream().map(mId), 0, 0); 41 assertCountSum(countTo(10).stream().map(mId), 10, 55); 42 assertCountSum(countTo(10).stream().map(mZero), 10, 0); 43 assertCountSum(countTo(0).stream().map(mDoubler), 0, 0); 44 assertCountSum(countTo(10).stream().map(mDoubler), 10, 110); 45 assertCountSum(countTo(10).stream().map(mDoubler).map(mDoubler), 10, 220); 46 47 exerciseOps(countTo(0), s -> s.map(LambdaTestHelpers.identity()), countTo(0)); 48 exerciseOps(countTo(1000), s -> s.map(LambdaTestHelpers.identity()), countTo(1000)); 49 // @@@ Force cast to integer so output is Stream<Integer> rather an IntStream 50 // this just ensures that no warnings are logged about boxing 51 // when the result is compared with the output 52 exerciseOps(countTo(1000), s -> s.map(e -> (Integer) (1000 + e)), range(1001, 2000)); 53 } 54 testEveryMapShape()55 public void testEveryMapShape() { 56 assertCountSum(countTo(1000).stream() 57 .mapToInt(i -> i - 1) 58 .mapToObj(i -> i + 1) 59 .mapToLong(i -> i - 1) 60 .mapToObj(i -> i + 1) 61 .mapToDouble(i -> i - 1) 62 .mapToObj(i -> i + 1) 63 .mapToInt(i -> (int) (double) i) 64 .mapToLong(i -> i) 65 .mapToDouble(i -> i) 66 .mapToLong(i -> (long) i) 67 .mapToInt(i -> (int) i) 68 .mapToObj(i -> i), 69 1000, countTo(1000).stream().mapToInt(i -> i).sum()); 70 } 71 72 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)73 public void testOps(String name, TestData.OfRef<Integer> data) { 74 exerciseOpsInt(data, s -> s.map(mId), s -> s.map(e -> e), s -> s.map(e -> e), s -> s.map(e -> e)); 75 exerciseOpsInt(data, s -> s.map(mZero), s -> s.map(e -> 0), s -> s.map(e -> 0), s -> s.map(e -> 0)); 76 exerciseOpsInt(data, s -> s.map(mDoubler), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e), s -> s.map(e -> 2*e)); 77 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)); 78 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)); 79 exerciseOps(data, s -> s.mapToInt(i -> i)); 80 exerciseOps(data, s -> s.mapToLong(i -> i)); 81 exerciseOps(data, s -> s.mapToDouble(i -> i)); 82 } 83 84 // 85 86 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testIntOps(String name, TestData.OfInt data)87 public void testIntOps(String name, TestData.OfInt data) { 88 exerciseOps(data, s -> s.mapToObj(i -> i)); 89 exerciseOps(data, s -> s.map(i -> 0)); 90 exerciseOps(data, s -> s.map(i -> i * 2)); 91 exerciseOps(data, s -> s.asLongStream()); 92 exerciseOps(data, s -> s.asDoubleStream()); 93 exerciseOps(data, s -> s.boxed()); 94 exerciseOps(data, s -> s.mapToObj(Integer::toString)); 95 exerciseOps(data, s -> s.mapToLong(i -> i)); 96 exerciseOps(data, s -> s.mapToDouble(i -> i)); 97 } 98 99 // 100 101 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testLongOps(String name, TestData.OfLong data)102 public void testLongOps(String name, TestData.OfLong data) { 103 exerciseOps(data, s -> s.mapToObj(i -> i)); 104 exerciseOps(data, s -> s.map(i -> 0L)); 105 exerciseOps(data, s -> s.map(i -> i * 2L)); 106 exerciseOps(data, s -> s.asDoubleStream()); 107 exerciseOps(data, s -> s.boxed()); 108 exerciseOps(data, s -> s.mapToObj(e -> Long.toString(e))); 109 exerciseOps(data, s -> s.mapToInt(i -> (int) i)); 110 exerciseOps(data, s -> s.mapToDouble(i -> i)); 111 } 112 113 // 114 115 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleOps(String name, TestData.OfDouble data)116 public void testDoubleOps(String name, TestData.OfDouble data) { 117 exerciseOps(data, s -> s.mapToObj(i -> i)); 118 exerciseOps(data, s -> s.map(i -> 0.0)); 119 exerciseOps(data, s -> s.map(i -> i * 2.0)); 120 exerciseOps(data, s -> s.boxed()); 121 exerciseOps(data, s -> s.mapToObj(e -> Double.toString(e))); 122 exerciseOps(data, s -> s.mapToLong(i -> (long) i)); 123 exerciseOps(data, s -> s.mapToInt(i -> (int) i)); 124 } 125 } 126