• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.OpTestCase;
26 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
27 import org.openjdk.testlib.java.util.stream.TestData;
28 
29 import java.util.stream.Stream;
30 import org.testng.annotations.Test;
31 
32 import java.util.List;
33 import java.util.Optional;
34 
35 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
36 
37 /**
38  * ReduceOpTest
39  *
40  * @author Brian Goetz
41  */
42 @Test
43 public class ReduceTest extends OpTestCase {
testReduce()44     public void testReduce() {
45         List<Integer> list = countTo(10);
46 
47         assertEquals(55, (int) list.stream().reduce(rPlus).get());
48         assertEquals(55, (int) list.stream().reduce(0, rPlus));
49         assertEquals(10, (int) list.stream().reduce(rMax).get());
50         assertEquals(1, (int) list.stream().reduce(rMin).get());
51 
52         assertEquals(0, (int) countTo(0).stream().reduce(0, rPlus));
53         assertTrue(!countTo(0).stream().reduce(rPlus).isPresent());
54 
55         assertEquals(110, (int) list.stream().map(mDoubler).reduce(rPlus).get());
56         assertEquals(20, (int) list.stream().map(mDoubler).reduce(rMax).get());
57         assertEquals(2, (int) list.stream().map(mDoubler).reduce(rMin).get());
58     }
59 
60     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOps(String name, TestData.OfRef<Integer> data)61     public void testOps(String name, TestData.OfRef<Integer> data) {
62         assertEquals(0, (int) exerciseTerminalOps(data, s -> s.filter(pFalse), s -> s.reduce(0, rPlus, rPlus)));
63 
64         Optional<Integer> seedless = exerciseTerminalOps(data, s -> s.reduce(rPlus));
65         Integer folded = exerciseTerminalOps(data, s -> s.reduce(0, rPlus, rPlus));
66         assertEquals(folded, seedless.orElse(0));
67 
68         seedless = exerciseTerminalOps(data, s -> s.reduce(rMin));
69         folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MAX_VALUE, rMin, rMin));
70         assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));
71 
72         seedless = exerciseTerminalOps(data, s -> s.reduce(rMax));
73         folded = exerciseTerminalOps(data, s -> s.reduce(Integer.MIN_VALUE, rMax, rMax));
74         assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
75 
76         seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rPlus));
77         folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(0, rPlus, rPlus));
78         assertEquals(folded, seedless.orElse(0));
79 
80         seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rMin));
81         folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(Integer.MAX_VALUE, rMin, rMin));
82         assertEquals(folded, seedless.orElse(Integer.MAX_VALUE));
83 
84         seedless = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(rMax));
85         folded = exerciseTerminalOps(data, s -> s.map(mDoubler), s -> s.reduce(Integer.MIN_VALUE, rMax, rMax));
86         assertEquals(folded, seedless.orElse(Integer.MIN_VALUE));
87     }
88 }
89