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