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