1 /* 2 * Copyright (c) 2014, 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 24 /** 25 * @test 26 * @summary Tests counting of streams 27 * @bug 8031187 28 */ 29 30 package org.openjdk.tests.java.util.stream; 31 32 import org.openjdk.testlib.java.util.stream.DoubleStreamTestDataProvider; 33 import org.openjdk.testlib.java.util.stream.IntStreamTestDataProvider; 34 import org.openjdk.testlib.java.util.stream.LambdaTestHelpers; 35 import org.openjdk.testlib.java.util.stream.LongStreamTestDataProvider; 36 import org.openjdk.testlib.java.util.stream.OpTestCase; 37 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider; 38 import org.openjdk.testlib.java.util.stream.TestData; 39 40 import java.util.concurrent.atomic.AtomicLong; 41 import java.util.stream.DoubleStream; 42 import java.util.stream.IntStream; 43 import java.util.stream.LongStream; 44 import java.util.stream.Stream; 45 46 import org.testng.annotations.Test; 47 48 public class CountTest extends OpTestCase { 49 50 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)51 public void testOps(String name, TestData.OfRef<Integer> data) { 52 AtomicLong expectedCount = new AtomicLong(); 53 data.stream().forEach(e -> expectedCount.incrementAndGet()); 54 55 withData(data). 56 terminal(Stream::count). 57 expectedResult(expectedCount.get()). 58 exercise(); 59 } 60 61 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testOps(String name, TestData.OfInt data)62 public void testOps(String name, TestData.OfInt data) { 63 AtomicLong expectedCount = new AtomicLong(); 64 data.stream().forEach(e -> expectedCount.incrementAndGet()); 65 66 withData(data). 67 terminal(IntStream::count). 68 expectedResult(expectedCount.get()). 69 exercise(); 70 } 71 72 @Test(dataProvider = "LongStreamTestData", dataProviderClass = LongStreamTestDataProvider.class) testOps(String name, TestData.OfLong data)73 public void testOps(String name, TestData.OfLong data) { 74 AtomicLong expectedCount = new AtomicLong(); 75 data.stream().forEach(e -> expectedCount.incrementAndGet()); 76 77 withData(data). 78 terminal(LongStream::count). 79 expectedResult(expectedCount.get()). 80 exercise(); 81 } 82 83 @Test(dataProvider = "DoubleStreamTestData", dataProviderClass = DoubleStreamTestDataProvider.class) testOps(String name, TestData.OfDouble data)84 public void testOps(String name, TestData.OfDouble data) { 85 AtomicLong expectedCount = new AtomicLong(); 86 data.stream().forEach(e -> expectedCount.incrementAndGet()); 87 88 withData(data). 89 terminal(DoubleStream::count). 90 expectedResult(expectedCount.get()). 91 exercise(); 92 } 93 } 94