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.IntStreamTestDataProvider; 26 import org.openjdk.testlib.java.util.stream.OpTestCase; 27 import org.openjdk.testlib.java.util.stream.TestData; 28 29 import java.util.stream.IntStream; 30 import org.testng.annotations.Test; 31 32 import java.util.Arrays; 33 import java.util.OptionalInt; 34 35 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*; 36 37 public class IntReduceTest extends OpTestCase { testReduce()38 public void testReduce() { 39 int[] a = IntStream.range(1, 11).toArray(); 40 41 assertEquals(55, Arrays.stream(a).reduce(irPlus).getAsInt()); 42 assertEquals(55, Arrays.stream(a).reduce(0, irPlus)); 43 assertEquals(10, Arrays.stream(a).reduce(irMax).getAsInt()); 44 assertEquals(1, Arrays.stream(a).reduce(irMin).getAsInt()); 45 46 assertEquals(0, IntStream.empty().reduce(0, irPlus)); 47 assertFalse(IntStream.empty().reduce(irPlus).isPresent()); 48 49 assertEquals(110, Arrays.stream(a).map(irDoubler).reduce(irPlus).getAsInt()); 50 assertEquals(20, Arrays.stream(a).map(irDoubler).reduce(irMax).getAsInt()); 51 assertEquals(2, Arrays.stream(a).map(irDoubler).reduce(irMin).getAsInt()); 52 } 53 54 @Test(dataProvider = "IntStreamTestData", dataProviderClass = IntStreamTestDataProvider.class) testOps(String name, TestData.OfInt data)55 public void testOps(String name, TestData.OfInt data) { 56 assertEquals(0, (int) exerciseTerminalOps(data, s -> s.filter(ipFalse), s -> s.reduce(0, irPlus))); 57 58 OptionalInt seedless = exerciseTerminalOps(data, s -> s.reduce(irPlus)); 59 int folded = exerciseTerminalOps(data, s -> s.reduce(0, irPlus)); 60 assertEquals(folded, seedless.orElse(0)); 61 62 seedless = exerciseTerminalOps(data, s -> s.reduce(irMin)); 63 folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MAX_VALUE, irMin)); 64 assertEquals(folded, seedless.orElse(Integer.MAX_VALUE)); 65 66 seedless = exerciseTerminalOps(data, s -> s.reduce(irMax)); 67 folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MIN_VALUE, irMax)); 68 assertEquals(folded, seedless.orElse(Integer.MIN_VALUE)); 69 70 seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irPlus)); 71 folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(0, irPlus)); 72 assertEquals(folded, seedless.orElse(0)); 73 74 seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irMin)); 75 folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(Integer.MAX_VALUE, irMin)); 76 assertEquals(folded, seedless.orElse(Integer.MAX_VALUE)); 77 78 seedless = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(irMax)); 79 folded = exerciseTerminalOps(data, s -> s.map(irDoubler), s -> s.reduce(Integer.MIN_VALUE, irMax)); 80 assertEquals(folded, seedless.orElse(Integer.MIN_VALUE)); 81 } 82 } 83