1 /* 2 * Copyright (c) 2012, 2017, 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 8178117 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.ThrowableHelper.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).stream().mapToInt(i -> i).collect(() -> new IntSummaryStatistics(0, -1, 1001, 2), 95 IntSummaryStatistics::accept, 96 IntSummaryStatistics::combine)); 97 instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingInt(i -> i))); 98 instances.add(countTo(1000).parallelStream().mapToInt(i -> i).summaryStatistics()); 99 instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(IntSummaryStatistics::new, 100 IntSummaryStatistics::accept, 101 IntSummaryStatistics::combine)); 102 instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(() -> new IntSummaryStatistics(0, -1, 1001, 2), 103 IntSummaryStatistics::accept, 104 IntSummaryStatistics::combine)); 105 IntSummaryStatistics original = instances.get(0); 106 instances.add(new IntSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum())); 107 108 for (IntSummaryStatistics stats : instances) { 109 assertEquals(stats.getCount(), 1000); 110 assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum()); 111 assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount()); 112 assertEquals(stats.getMax(), 1000); 113 assertEquals(stats.getMin(), 1); 114 } 115 116 expectThrows(IllegalArgumentException.class, () -> new IntSummaryStatistics(-1, 0, 0, 0)); 117 expectThrows(IllegalArgumentException.class, () -> new IntSummaryStatistics(1, 3, 2, 0)); 118 } 119 120 testLongStatistics()121 public void testLongStatistics() { 122 List<LongSummaryStatistics> instances = new ArrayList<>(); 123 instances.add(countTo(1000).stream().collect(Collectors.summarizingLong(i -> i))); 124 instances.add(countTo(1000).stream().mapToLong(i -> i).summaryStatistics()); 125 instances.add(countTo(1000).stream().mapToLong(i -> i).collect(LongSummaryStatistics::new, 126 LongSummaryStatistics::accept, 127 LongSummaryStatistics::combine)); 128 instances.add(countTo(1000).stream().mapToInt(i -> i).collect(() -> new LongSummaryStatistics(0, -1, 1001, 2), 129 LongSummaryStatistics::accept, 130 LongSummaryStatistics::combine)); 131 instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingLong(i -> i))); 132 instances.add(countTo(1000).parallelStream().mapToLong(i -> i).summaryStatistics()); 133 instances.add(countTo(1000).parallelStream().mapToLong(i -> i).collect(LongSummaryStatistics::new, 134 LongSummaryStatistics::accept, 135 LongSummaryStatistics::combine)); 136 instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(() -> new LongSummaryStatistics(0, -1, 1001, 2), 137 LongSummaryStatistics::accept, 138 LongSummaryStatistics::combine)); 139 LongSummaryStatistics original = instances.get(0); 140 instances.add(new LongSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum())); 141 142 for (LongSummaryStatistics stats : instances) { 143 assertEquals(stats.getCount(), 1000); 144 assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum()); 145 assertEquals(stats.getAverage(), (double) stats.getSum() / stats.getCount()); 146 assertEquals(stats.getMax(), 1000L); 147 assertEquals(stats.getMin(), 1L); 148 } 149 150 expectThrows(IllegalArgumentException.class, () -> new LongSummaryStatistics(-1, 0, 0, 0)); 151 expectThrows(IllegalArgumentException.class, () -> new LongSummaryStatistics(1, 3, 2, 0)); 152 } 153 testDoubleStatistics()154 public void testDoubleStatistics() { 155 List<DoubleSummaryStatistics> instances = new ArrayList<>(); 156 instances.add(countTo(1000).stream().collect(Collectors.summarizingDouble(i -> i))); 157 instances.add(countTo(1000).stream().mapToDouble(i -> i).summaryStatistics()); 158 instances.add(countTo(1000).stream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, 159 DoubleSummaryStatistics::accept, 160 DoubleSummaryStatistics::combine)); 161 instances.add(countTo(1000).stream().mapToInt(i -> i).collect(() -> new DoubleSummaryStatistics(0, -1, 1001, 2), 162 DoubleSummaryStatistics::accept, 163 DoubleSummaryStatistics::combine)); 164 instances.add(countTo(1000).parallelStream().collect(Collectors.summarizingDouble(i -> i))); 165 instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).summaryStatistics()); 166 instances.add(countTo(1000).parallelStream().mapToDouble(i -> i).collect(DoubleSummaryStatistics::new, 167 DoubleSummaryStatistics::accept, 168 DoubleSummaryStatistics::combine)); 169 instances.add(countTo(1000).parallelStream().mapToInt(i -> i).collect(() -> new DoubleSummaryStatistics(0, -1, 1001, 2), 170 DoubleSummaryStatistics::accept, 171 DoubleSummaryStatistics::combine)); 172 DoubleSummaryStatistics original = instances.get(0); 173 instances.add(new DoubleSummaryStatistics(original.getCount(), original.getMin(), original.getMax(), original.getSum())); 174 175 for (DoubleSummaryStatistics stats : instances) { 176 assertEquals(stats.getCount(), 1000); 177 assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum()); 178 assertEquals(stats.getAverage(), stats.getSum() / stats.getCount()); 179 assertEquals(stats.getMax(), 1000.0); 180 assertEquals(stats.getMin(), 1.0); 181 } 182 183 expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(-1, 0, 0, 0)); 184 expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(1, 3, 2, 0)); 185 double[] values = {1.0, Double.NaN}; 186 for (var min : values) { 187 for (var max : values) { 188 for (var sum : values) { 189 if (Double.isNaN(min) && Double.isNaN(max) && Double.isNaN(sum)) continue; 190 if (!Double.isNaN(min) && !Double.isNaN(max) && !Double.isNaN(sum)) continue; 191 expectThrows(IllegalArgumentException.class, () -> new DoubleSummaryStatistics(1, min, max, sum)); 192 } 193 } 194 } 195 } 196 } 197