changeset 7830:92cd244a11db

Add tests to fill coverage gaps
author briangoetz
date Fri, 05 Apr 2013 17:44:17 -0400
parents 215d9ddc8762
children 52923cc16dc4
files test-ng/bootlib/java/util/stream/OpTestCase.java test-ng/bootlib/java/util/stream/StreamTestDataProvider.java test-ng/tests/org/openjdk/tests/java/util/stream/MapOpTest.java test-ng/tests/org/openjdk/tests/java/util/stream/SliceOpTest.java test-ng/tests/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java
diffstat 5 files changed, 198 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/test-ng/bootlib/java/util/stream/OpTestCase.java	Fri Apr 05 16:09:34 2013 -0400
+++ b/test-ng/bootlib/java/util/stream/OpTestCase.java	Fri Apr 05 17:44:17 2013 -0400
@@ -78,25 +78,47 @@
 
         boolean isParallel();
 
-        abstract <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> void run(TestData<T, S_IN> data, Consumer<U> b, Function<S_IN, S_OUT> m);
+        abstract <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
+        void run(TestData<T, S_IN> data, Consumer<U> b, Function<S_IN, S_OUT> m);
     }
 
-    public <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>> Collection<U> exerciseOps(TestData<T, S_IN> data, Function<S_IN, S_OUT> m) {
+    public <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
+    Collection<U> exerciseOps(TestData<T, S_IN> data, Function<S_IN, S_OUT> m) {
         return withData(data).stream(m).exercise();
     }
 
-    public <T, U, S_OUT extends BaseStream<U, S_OUT>> Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m) {
+    // Run multiple versions of exercise(), returning the result of the first, and asserting that others return the same result
+    // If the first version is s -> s.foo(), can be used with s -> s.mapToInt(i -> i).foo().mapToObj(i -> i) to test all shape variants
+    public <T, U, S_IN extends BaseStream<T, S_IN>, S_OUT extends BaseStream<U, S_OUT>>
+    Collection<U> exerciseOpsMulti(TestData<T, S_IN> data,
+                                   Function<S_IN, S_OUT>... ms) {
+        Collection<U> result = null;
+        for (Function<S_IN, S_OUT> m : ms) {
+            if (result == null)
+                result = withData(data).stream(m).exercise();
+            else {
+                Collection<U> r2 = withData(data).stream(m).exercise();
+                assertEquals(result, r2);
+            }
+        }
+        return result;
+    }
+
+    public <T, U, S_OUT extends BaseStream<U, S_OUT>>
+    Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m) {
         StreamTestData.CollectionData<T> data1 = new StreamTestData.CollectionData<>("Collection of type " + data.getClass().getName(), data);
         return withData(data1).stream(m).exercise();
     }
 
-    public <T, U, S_OUT extends BaseStream<U, S_OUT>, I extends Iterable<U>> Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m, I expected) {
+    public <T, U, S_OUT extends BaseStream<U, S_OUT>, I extends Iterable<U>>
+    Collection<U> exerciseOps(Collection<T> data, Function<Stream<T>, S_OUT> m, I expected) {
         StreamTestData.CollectionData<T> data1 = new StreamTestData.CollectionData<>("Collection of type " + data.getClass().getName(), data);
         return withData(data1).stream(m).expectedResult(expected).exercise();
     }
 
     @SuppressWarnings("unchecked")
-    public <U, S_OUT extends BaseStream<U, S_OUT>> Collection<U> exerciseOps(int[] data, Function<IntStream, S_OUT> m) {
+    public <U, S_OUT extends BaseStream<U, S_OUT>>
+    Collection<U> exerciseOps(int[] data, Function<IntStream, S_OUT> m) {
         return withData(new IntStreamTestData.ArrayData("int array", data)).stream(m).exercise();
     }
 
@@ -118,7 +140,8 @@
             this.data = Objects.requireNonNull(data);
         }
 
-        public <U, S_OUT extends BaseStream<U, S_OUT>> ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> ops(IntermediateTestOp... ops) {
+        public <U, S_OUT extends BaseStream<U, S_OUT>>
+        ExerciseDataStreamBuilder<T, U, S_IN, S_OUT> ops(IntermediateTestOp... ops) {
             return new ExerciseDataStreamBuilder<>(data, (S_IN s) -> (S_OUT) chain(s, ops));
         }
 
@@ -272,6 +295,15 @@
 
         // Build method
 
+        private long count(StreamShape shape, BaseStream s) {
+            switch (shape) {
+                case REFERENCE:    return ((Stream) s).count();
+                case INT_VALUE:    return ((IntStream) s).count();
+                case LONG_VALUE:   return ((LongStream) s).count();
+                case DOUBLE_VALUE: return ((DoubleStream) s).count();
+                default: throw new IllegalStateException("Unknown shape: " + shape);
+            }
+        }
 
         public Collection<U> exercise() {
             if (refResult == null) {
@@ -281,6 +313,9 @@
                 Node<U> refNodeResult = ((AbstractPipeline<?, U, ?>) sOut).evaluateToArrayNode(size -> (U[]) new Object[size]);
                 refResult = LambdaTestHelpers.toBoxedList(refNodeResult.spliterator());
                 after.accept(data);
+                S_OUT anotherCopy = m.apply(data.stream());
+                long count = count(((AbstractPipeline) anotherCopy).getOutputShape(), anotherCopy);
+                assertEquals(count, refNodeResult.count());
             }
 
             List<Error> errors = new ArrayList<>();
--- a/test-ng/bootlib/java/util/stream/StreamTestDataProvider.java	Fri Apr 05 16:09:34 2013 -0400
+++ b/test-ng/bootlib/java/util/stream/StreamTestDataProvider.java	Fri Apr 05 17:44:17 2013 -0400
@@ -91,25 +91,19 @@
                 final Integer[] ints = (Integer[])data[1];
                 final List<Integer> intsAsList = Arrays.asList(ints);
 
-                list.add(arrayDataDescr("array:" + name,
-                                        ints));
-                list.add(collectionDataDescr("ArrayList.asList:" + name,
-                                             intsAsList));
-                list.add(collectionDataDescr("ArrayList:" + name,
-                                             new ArrayList<>(intsAsList)));
+                list.add(arrayDataDescr("array:" + name, ints));
+                list.add(collectionDataDescr("ArrayList.asList:" + name, intsAsList));
+                list.add(collectionDataDescr("ArrayList:" + name, new ArrayList<>(intsAsList)));
+                list.add(streamDataDescr("DelegatingStream(ArrayList):" + name,
+                                         () -> new ArrayList<>(intsAsList).stream()));
                 List<Integer> aList = new ArrayList<>(intsAsList);
                 list.add(collectionDataDescr("ArrayList.Sublist:" + name,
                                              (ints.length) <= 1 ? aList.subList(0, 0) : aList.subList(1, ints.length / 2)));
-                list.add(collectionDataDescr("LinkedList:" + name,
-                                             new LinkedList<>(intsAsList)));
-                list.add(collectionDataDescr("HashSet:" + name,
-                                             new HashSet<>(intsAsList)));
-                list.add(collectionDataDescr("LinkedHashSet:" + name,
-                                             new LinkedHashSet<>(intsAsList)));
-                list.add(collectionDataDescr("TreeSet:" + name,
-                                             new TreeSet<>(intsAsList)));
-                list.add(sbDataDescr("SpinedBuffer:" + name,
-                                     new SpinedBuffer<>(intsAsList)));
+                list.add(collectionDataDescr("LinkedList:" + name, new LinkedList<>(intsAsList)));
+                list.add(collectionDataDescr("HashSet:" + name, new HashSet<>(intsAsList)));
+                list.add(collectionDataDescr("LinkedHashSet:" + name, new LinkedHashSet<>(intsAsList)));
+                list.add(collectionDataDescr("TreeSet:" + name, new TreeSet<>(intsAsList)));
+                list.add(sbDataDescr("SpinedBuffer:" + name, new SpinedBuffer<>(intsAsList)));
 
                 // @@@ Add more
             }
@@ -161,6 +155,10 @@
         return new Object[] { description, new StreamTestData.ArrayData<>(description, data) };
     }
 
+    static <T> Object[] streamDataDescr(String description, Supplier<Stream<T>> supplier) {
+        return new Object[] { description, new StreamTestData.StreamSupplierData<>(description, supplier) };
+    }
+
     static <T> Object[] collectionDataDescr(String description, Collection<T> data) {
         return new Object[] { description, new StreamTestData.CollectionData<>(description, data) };
     }
--- a/test-ng/tests/org/openjdk/tests/java/util/stream/MapOpTest.java	Fri Apr 05 16:09:34 2013 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/MapOpTest.java	Fri Apr 05 17:44:17 2013 -0400
@@ -26,7 +26,13 @@
 
 import org.testng.annotations.Test;
 
+import java.util.function.DoubleToIntFunction;
+import java.util.function.DoubleToLongFunction;
 import java.util.function.Functions;
+import java.util.function.IntToDoubleFunction;
+import java.util.function.IntToLongFunction;
+import java.util.function.LongToDoubleFunction;
+import java.util.function.LongToIntFunction;
 import java.util.function.ToDoubleFunction;
 import java.util.function.ToIntFunction;
 import java.util.function.ToLongFunction;
@@ -58,6 +64,23 @@
         exerciseOps(countTo(1000), s -> s.map(e -> (Integer) (1000 + e)), range(1001, 2000));
     }
 
+    public void testEveryMapShape() {
+        assertCountSum(countTo(1000).stream()
+                               .mapToInt(i -> i - 1)
+                               .mapToObj(i -> i + 1)
+                               .mapToLong(i -> i - 1)
+                               .mapToObj(i -> i + 1)
+                               .mapToDouble(i -> i - 1)
+                               .mapToObj(i -> i + 1)
+                               .mapToInt(i -> (int) (double) i)
+                               .mapToLong(i -> i)
+                               .mapToDouble(i -> i)
+                               .mapToLong(i -> (long) i)
+                               .mapToInt(i -> (int) i)
+                               .mapToObj(i -> i),
+                       1000, countTo(1000).stream().mapToInt(i -> i).sum());
+    }
+
     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
     public void testOps(String name, StreamTestData<Integer> data) {
         exerciseOps(data, s -> s.map(mId));
@@ -81,6 +104,8 @@
         exerciseOps(data, s -> s.doubles());
         exerciseOps(data, s -> s.boxed());
         exerciseOps(data, s -> s.mapToObj(e -> Integer.toString(e)));
+        exerciseOps(data, s -> s.mapToLong((IntToLongFunction) i -> (long) i));
+        exerciseOps(data, s -> s.mapToDouble((IntToDoubleFunction) i -> (double) i));
     }
 
     //
@@ -93,6 +118,8 @@
         exerciseOps(data, s -> s.doubles());
         exerciseOps(data, s -> s.boxed());
         exerciseOps(data, s -> s.mapToObj(e -> Long.toString(e)));
+        exerciseOps(data, s -> s.mapToInt((LongToIntFunction) i -> (int) i));
+        exerciseOps(data, s -> s.mapToDouble((LongToDoubleFunction) i -> (double) i));
     }
 
     //
@@ -104,5 +131,7 @@
         exerciseOps(data, s -> s.map(i -> i * 2.0));
         exerciseOps(data, s -> s.boxed());
         exerciseOps(data, s -> s.mapToObj(e -> Double.toString(e)));
+        exerciseOps(data, s -> s.mapToLong((DoubleToLongFunction) i -> (long) i));
+        exerciseOps(data, s -> s.mapToInt((DoubleToIntFunction) i -> (int) i));
     }
 }
--- a/test-ng/tests/org/openjdk/tests/java/util/stream/SliceOpTest.java	Fri Apr 05 16:09:34 2013 -0400
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/SliceOpTest.java	Fri Apr 05 17:44:17 2013 -0400
@@ -146,10 +146,18 @@
         List<Integer> skips = sizes(data.size());
 
         for (int s : skips) {
-            Collection<Integer> sr = exerciseOps(data, st -> st.substream(s));
+            Collection<Integer> sr = exerciseOpsMulti(data,
+                                                      st -> st.substream(s),
+                                                      st -> st.mapToInt(i -> i).substream(s).mapToObj(i -> i),
+                                                      st -> st.mapToLong(i -> i).substream(s).mapToObj(i -> (int) i),
+                                                      st -> st.mapToDouble(i -> i).substream(s).mapToObj(i -> (int) i));
             assertEquals(sr.size(), sliceSize(data.size(), s));
 
-            sr = exerciseOps(data, st -> st.substream(s).substream(s / 2));
+            sr = exerciseOpsMulti(data,
+                                  st -> st.substream(s).substream(s / 2),
+                                  st -> st.mapToInt(i -> i).substream(s).substream(s / 2).mapToObj(i -> i),
+                                  st -> st.mapToLong(i -> i).substream(s).substream(s / 2).mapToObj(i -> (int) i),
+                                  st -> st.mapToDouble(i -> i).substream(s).substream(s / 2).mapToObj(i -> (int) i));
             assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), s/2));
         }
     }
@@ -161,10 +169,18 @@
 
         for (int s : skips) {
             for (int limit : limits) {
-                Collection<Integer> sr = exerciseOps(data, st -> st.substream(s).limit(limit));
+                Collection<Integer> sr = exerciseOpsMulti(data,
+                                                          st -> st.substream(s).limit(limit),
+                                                          st -> st.mapToInt(i -> i).substream(s).limit(limit).mapToObj(i -> i),
+                                                          st -> st.mapToLong(i -> i).substream(s).limit(limit).mapToObj(i -> (int) i),
+                                                          st -> st.mapToDouble(i -> i).substream(s).limit(limit).mapToObj(i -> (int) i));
                 assertEquals(sr.size(), sliceSize(sliceSize(data.size(), s), 0, limit));
 
-                sr = exerciseOps(data, st -> st.substream(s, limit+s));
+                sr = exerciseOpsMulti(data,
+                                      st -> st.substream(s, limit+s),
+                                      st -> st.mapToInt(i -> i).substream(s, limit+s).mapToObj(i -> i),
+                                      st -> st.mapToLong(i -> i).substream(s, limit+s).mapToObj(i -> (int) i),
+                                      st -> st.mapToDouble(i -> i).substream(s, limit+s).mapToObj(i -> (int) i));
                 assertEquals(sr.size(), sliceSize(data.size(), s, limit));
             }
         }
@@ -175,10 +191,18 @@
         List<Integer> limits = sizes(data.size());
 
         for (int limit : limits) {
-            Collection<Integer> sr = exerciseOps(data, st -> st.limit(limit));
+            Collection<Integer> sr = exerciseOpsMulti(data,
+                                                      st -> st.limit(limit),
+                                                      st -> st.mapToInt(i -> i).limit(limit).mapToObj(i -> i),
+                                                      st -> st.mapToLong(i -> i).limit(limit).mapToObj(i -> (int) i),
+                                                      st -> st.mapToDouble(i -> i).limit(limit).mapToObj(i -> (int) i));
             assertEquals(sr.size(), sliceSize(data.size(), 0, limit));
 
-            sr = exerciseOps(data, st -> st.limit(limit).limit(limit / 2));
+            sr = exerciseOpsMulti(data,
+                                  st -> st.limit(limit).limit(limit / 2),
+                                  st -> st.mapToInt(i -> i).limit(limit).limit(limit / 2).mapToObj(i -> i),
+                                  st -> st.mapToLong(i -> i).limit(limit).limit(limit / 2).mapToObj(i -> (int) i),
+                                  st -> st.mapToDouble(i -> i).limit(limit).limit(limit / 2).mapToObj(i -> (int) i));
             assertEquals(sr.size(), sliceSize(sliceSize(data.size(), 0, limit), 0, limit/2));
         }
     }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/SummaryStatisticsTest.java	Fri Apr 05 17:44:17 2013 -0400
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package org.openjdk.tests.java.util.stream;
+
+import java.util.DoubleSummaryStatistics;
+import java.util.IntSummaryStatistics;
+import java.util.LongSummaryStatistics;
+import java.util.stream.Collectors;
+import java.util.stream.OpTestCase;
+import java.util.stream.Stream;
+import java.util.stream.StreamTestData;
+
+import static java.util.stream.LambdaTestHelpers.countTo;
+
+/**
+ * TestSummaryStatistics
+ *
+ * @author Brian Goetz
+ */
+public class SummaryStatisticsTest extends OpTestCase {
+    public void testIntStatistics() {
+        IntSummaryStatistics stats = countTo(1000).stream().collect(Collectors.toIntSummaryStatistics(i -> i));
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000);
+        assertEquals(stats.getMin(), 1);
+
+        stats = countTo(1000).stream().mapToInt(i -> i).summaryStatistics();
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000);
+        assertEquals(stats.getMin(), 1);
+    }
+
+    public void testLongStatistics() {
+        LongSummaryStatistics stats = countTo(1000).stream().mapToLong(i -> i).boxed().collect(Collectors.toLongSummaryStatistics(i -> i));
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000L);
+        assertEquals(stats.getMin(), 1L);
+
+        stats = countTo(1000).stream().mapToLong(i -> i).summaryStatistics();
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), (long) countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000L);
+        assertEquals(stats.getMin(), 1L);
+    }
+
+    public void testDoubleStatistics() {
+        DoubleSummaryStatistics stats = countTo(1000).stream().mapToDouble(i -> i).boxed().collect(Collectors.toDoubleSummaryStatistics(i -> i));
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000.0);
+        assertEquals(stats.getMin(), 1.0);
+
+        stats = countTo(1000).stream().mapToDouble(i -> i).summaryStatistics();
+        assertEquals(stats.getCount(), 1000);
+        assertEquals(stats.getSum(), (double) countTo(1000).stream().mapToInt(i -> i).sum());
+        assertEquals(stats.getMax(), 1000.0);
+        assertEquals(stats.getMin(), 1.0);
+    }
+}