1 /*
2  * Copyright (c) 2012, 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 primtive stream collection with summary statistics
27  * @bug 8044047
28  */
29 
30 package org.openjdk.tests.java.util.stream;
31 
32 import org.testng.annotations.Test;
33 
34 import java.util.ArrayList;
35 import java.util.DoubleSummaryStatistics;
36 import java.util.IntSummaryStatistics;
37 import java.util.List;
38 import java.util.LongSummaryStatistics;
39 import java.util.stream.Collectors;
40 import java.util.stream.DoubleStream;
41 import java.util.stream.IntStream;
42 import java.util.stream.LongStream;
43 import java.util.stream.OpTestCase;
44 
45 import static java.util.stream.LambdaTestHelpers.countTo;
46 import static java.util.stream.ThowableHelper.checkNPE;
47 
48 @Test
49 public class CollectAndSummaryStatisticsTest extends OpTestCase {
50 
testIntCollectNull()51     public void testIntCollectNull() {
52         checkNPE(() -> IntStream.of(1).collect(null,
53                                                IntSummaryStatistics::accept,
54                                                IntSummaryStatistics::combine));
55         checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
56                                                null,
57                                                IntSummaryStatistics::combine));
58         checkNPE(() -> IntStream.of(1).collect(IntSummaryStatistics::new,
59                                                IntSummaryStatistics::accept,
60                                                null));
61     }
62 
testLongCollectNull()63     public void testLongCollectNull() {
64         checkNPE(() -> LongStream.of(1).collect(null,
65                                                LongSummaryStatistics::accept,
66                                                LongSummaryStatistics::combine));
67         checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new,
68                                                 null,
69                                                 LongSummaryStatistics::combine));
70         checkNPE(() -> LongStream.of(1).collect(LongSummaryStatistics::new,
71                                                 LongSummaryStatistics::accept,
72                                                 null));
73     }
74 
testDoubleCollectNull()75     public void testDoubleCollectNull() {
76         checkNPE(() -> DoubleStream.of(1).collect(null,
77                                                 DoubleSummaryStatistics::accept,
78                                                 DoubleSummaryStatistics::combine));
79         checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
80                                                 null,
81                                                 DoubleSummaryStatistics::combine));
82         checkNPE(() -> DoubleStream.of(1).collect(DoubleSummaryStatistics::new,
83                                                   DoubleSummaryStatistics::accept,
84                                                   null));
85     }
86 
testIntStatistics()87     public void testIntStatistics() {
88         List<IntSummaryStatistics> instances = new ArrayList<>();
89         instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
90         instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
91         instances.add(countTo(1000).stream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
92                                                                       IntSummaryStatistics::accept,
93                                                                       IntSummaryStatistics::combine));
94         instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
95         instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
96         instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new,
97                                                                               IntSummaryStatistics::accept,
98                                                                               IntSummaryStatistics::combine));
99 
100         for (IntSummaryStatistics stats : instances) {
101             assertEquals(stats.getCount(), 1000);
102             assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
103             assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
104             assertEquals(stats.getMax(), 1000);
105             assertEquals(stats.getMin(), 1);
106         }
107     }
108 
109 
testLongStatistics()110     public void testLongStatistics() {
111         List<LongSummaryStatistics> instances = new ArrayList<>();
112         instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i)));
113         instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics());
114         instances.add(countTo(1000).stream().mapToLong(i -> i).collect(LongSummaryStatistics::new,
115                                                                        LongSummaryStatistics::accept,
116                                                                        LongSummaryStatistics::combine));
117         instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i)));
118         instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics());
119         instances.add(countTo(1000).parallelStream().mapToLong(i -> i).collect(LongSummaryStatistics::new,
120                                                                                LongSummaryStatistics::accept,
121                                                                                LongSummaryStatistics::combine));
122 
123         for (LongSummaryStatistics stats : instances) {
124             assertEquals(stats.getCount(), 1000);
125             assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum());
126             assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount());
127             assertEquals(stats.getMax(), 1000L);
128             assertEquals(stats.getMin(), 1L);
129         }
130     }
131 
testDoubleStatistics()132     public void testDoubleStatistics() {
133         List<DoubleSummaryStatistics> instances = new ArrayList<>();
134         instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
135         instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
136         instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
137                                                                          DoubleSummaryStatistics::accept,
138                                                                          DoubleSummaryStatistics::combine));
139         instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
140         instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
141         instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new,
142                                                                                  DoubleSummaryStatistics::accept,
143                                                                                  DoubleSummaryStatistics::combine));
144 
145         for (DoubleSummaryStatistics stats : instances) {
146             assertEquals(stats.getCount(), 1000);
147             assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
148             assertEquals(stats.getAverage(), stats.getSum() / stats.getCount());
149             assertEquals(stats.getMax(), 1000.0);
150             assertEquals(stats.getMin(), 1.0);
151         }
152     }
153 }
154