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