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 */
23package org.openjdk.tests.java.util.stream;
24
25import org.openjdk.testlib.java.util.stream.OpTestCase;
26
27import java.util.ArrayList;
28import java.util.DoubleSummaryStatistics;
29import java.util.IntSummaryStatistics;
30import java.util.List;
31import java.util.LongSummaryStatistics;
32import java.util.stream.Collectors;
33
34import org.testng.annotations.Test;
35
36import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.countTo;
37
38/**
39 * TestSummaryStatistics
40 *
41 * @author Brian Goetz
42 */
43@Test
44public class SummaryStatisticsTest extends OpTestCase {
45    public void testIntStatistics() {
46        List<IntSummaryStatistics> instances = new ArrayList<>();
47        instances.add(countTo(1000).stream().collect(Collectors.summarizingInt(i -> i)));
48        instances.add(countTo(1000).stream().mapToInt(i -> i).summaryStatistics());
49        instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i)));
50        instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics());
51
52        for (IntSummaryStatistics stats : instances) {
53            assertEquals(stats.getCount(), 1000);
54            assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
55            assertEquals(stats.getMax(), 1000);
56            assertEquals(stats.getMin(), 1);
57        }
58    }
59
60    public void testLongStatistics() {
61        List<LongSummaryStatistics> instances = new ArrayList<>();
62        instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i)));
63        instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics());
64        instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i)));
65        instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics());
66
67        for (LongSummaryStatistics stats : instances) {
68            assertEquals(stats.getCount(), 1000);
69            assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum());
70            assertEquals(stats.getMax(), 1000L);
71            assertEquals(stats.getMin(), 1L);
72        }
73    }
74
75    public void testDoubleStatistics() {
76        List<DoubleSummaryStatistics> instances = new ArrayList<>();
77        instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i)));
78        instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics());
79        instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i)));
80        instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics());
81
82        for (DoubleSummaryStatistics stats : instances) {
83            assertEquals(stats.getCount(), 1000);
84            assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
85            assertEquals(stats.getMax(), 1000.0);
86            assertEquals(stats.getMin(), 1.0);
87        }
88    }
89}
90