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 java.util.function.Consumer; 34 import java.util.function.DoubleConsumer; 35 import java.util.function.IntConsumer; 36 import java.util.function.LongConsumer; 37 import java.util.stream.BaseStream; 38 import java.util.stream.Stream; 39 import java.util.stream.IntStream; 40 import java.util.stream.LongStream; 41 import java.util.stream.DoubleStream; 42 43 import org.testng.annotations.Test; 44 45 import java.util.ArrayList; 46 import java.util.Collections; 47 import java.util.List; 48 49 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*; 50 51 /** 52 * TeeOpTest 53 */ 54 @Test(groups = { "serialization-hostile" }) 55 public class TeeOpTest extends OpTestCase { 56 testTee()57 public void testTee() { 58 List<Integer> copy = new ArrayList<>(); 59 60 assertCountSum(countTo(0).stream().peek(copy::add), 0, 0); 61 assertCountSum(copy.iterator(), 0, 0); 62 63 copy.clear(); 64 assertCountSum(countTo(10).stream().peek(copy::add), 10, 55); 65 assertCountSum(copy.iterator(), 10, 55); 66 67 copy.clear(); 68 assertCountSum(countTo(10).stream().map(mDoubler).peek(copy::add), 10, 110); 69 assertCountSum(copy.iterator(), 10, 110); 70 } 71 72 static class AbstractRecordingConsumer<T> { 73 List<T> list; 74 before(TestData<T, ?> td)75 void before(TestData<T, ?> td) { 76 // Tee block can be called concurrently 77 list = Collections.synchronizedList(new ArrayList<>()); 78 } 79 after(TestData<T, ?> td)80 void after(TestData<T, ?> td) { 81 // No guarantees in parallel tests that calls to tee block will 82 // be in the encounter order, if defined, of the data 83 // @@@ Consider passing more meta-data about evaluation 84 assertContentsUnordered(list, td.into(new ArrayList<T>())); 85 } 86 } 87 88 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, final TestData.OfRef<Integer> data)89 public void testOps(String name, final TestData.OfRef<Integer> data) { 90 class RecordingConsumer extends AbstractRecordingConsumer<Integer> implements Consumer<Integer> { 91 public void accept(Integer t) { 92 list.add(t); 93 } 94 } 95 final RecordingConsumer b = new RecordingConsumer(); 96 97 withData(data) 98 .stream(s -> s.peek(b)) 99 .before(b::before) 100 .after(b::after) 101 .exercise(); 102 } 103 104 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testIntOps(String name, final TestData.OfInt data)105 public void testIntOps(String name, final TestData.OfInt data) { 106 class RecordingConsumer extends AbstractRecordingConsumer<Integer> implements IntConsumer { 107 public void accept(int t) { 108 list.add(t); 109 } 110 } 111 final RecordingConsumer b = new RecordingConsumer(); 112 113 withData(data) 114 .stream(s -> s.peek(b)) 115 .before(b::before) 116 .after(b::after) 117 .exercise(); 118 } 119 120 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testLongOps(String name, final TestData.OfLong data)121 public void testLongOps(String name, final TestData.OfLong data) { 122 class RecordingConsumer extends AbstractRecordingConsumer<Long> implements LongConsumer { 123 public void accept(long t) { 124 list.add(t); 125 } 126 } 127 final RecordingConsumer b = new RecordingConsumer(); 128 129 withData(data) 130 .stream(s -> s.peek(b)) 131 .before(b::before) 132 .after(b::after) 133 .exercise(); 134 } 135 136 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testDoubleOps(String name, final TestData.OfDouble data)137 public void testDoubleOps(String name, final TestData.OfDouble data) { 138 class RecordingConsumer extends AbstractRecordingConsumer<Double> implements DoubleConsumer { 139 public void accept(double t) { 140 list.add(t); 141 } 142 } 143 final RecordingConsumer b = new RecordingConsumer(); 144 145 withData(data) 146 .stream(s -> s.peek(b)) 147 .before(b::before) 148 .after(b::after) 149 .exercise(); 150 } 151 } 152