changeset 7638:cf2551af0226

Add test data for supplier of a stream.
author psandoz
date Thu, 14 Mar 2013 10:56:14 +0100
parents 247a89d3853f
children c61c4cfa4bd2
files test-ng/bootlib/java/util/stream/StreamTestData.java test-ng/tests/org/openjdk/tests/java/util/stream/RangeTest.java
diffstat 2 files changed, 51 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/test-ng/bootlib/java/util/stream/StreamTestData.java	Thu Mar 14 10:47:08 2013 +0100
+++ b/test-ng/bootlib/java/util/stream/StreamTestData.java	Thu Mar 14 10:56:14 2013 +0100
@@ -28,6 +28,7 @@
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Spliterator;
+import java.util.function.Supplier;
 
 @SuppressWarnings("serial")
 public abstract class StreamTestData<T> implements OpTestCase.TestData<T, Stream<T>>, Serializable {
@@ -147,6 +148,41 @@
         }
     }
 
+    public static class StreamSupplierData<T> extends StreamTestData<T> {
+        protected final String name;
+        protected final Supplier<Stream<T>> s;
+
+        public StreamSupplierData(String name, Supplier<Stream<T>> s) {
+            this.name = name;
+            this.s = s;
+        }
+
+        @Override
+        public Stream<T> stream() {
+            return s.get();
+        }
+
+        @Override
+        public Stream<T> parallelStream() {
+            return s.get().parallel();
+        }
+
+        @Override
+        public Spliterator<T> spliterator() {
+            return s.get().spliterator();
+        }
+
+        @Override
+        public int size() {
+            return (int) s.get().spliterator().getExactSizeIfKnown();
+        }
+
+        @Override
+        public String toString() {
+            return getClass().getSimpleName() + "[" + name + "]";
+        }
+    }
+
     public static class NodeData<T> extends StreamTestData<T> {
         protected final String name;
         protected final Node<T> node;
--- a/test-ng/tests/org/openjdk/tests/java/util/stream/RangeTest.java	Thu Mar 14 10:47:08 2013 +0100
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/RangeTest.java	Thu Mar 14 10:56:14 2013 +0100
@@ -25,7 +25,9 @@
 package org.openjdk.tests.java.util.stream;
 
 import java.util.Arrays;
+import java.util.Optional;
 import java.util.stream.OpTestCase;
+import java.util.stream.StreamTestData;
 import java.util.stream.Streams;
 
 /**
@@ -35,6 +37,19 @@
  */
 public class RangeTest extends OpTestCase {
 
+    public void testInfiniteRangeFindFirst() {
+        Integer first = Streams.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().get();
+        assertEquals(first, Streams.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().get());
+
+        // Limit is required to transform the infinite stream to a finite stream
+        // since the exercising requires a finite stream
+        withData(new StreamTestData.StreamSupplierData<Integer>(
+                "", () -> Streams.iterate(0, i -> i + 1).filter(i -> i > 10000).limit(20000))).
+                terminal(s->s.findFirst()).expectedResult(Optional.of(10001)).exercise();
+    }
+
+    //
+
     public void testIntRangeErrors() {
         executeAndCatch(() -> Streams.intRange(1, 1, 0));
         executeAndCatch(() -> Streams.intRange(1, 10, 0));
@@ -119,11 +134,6 @@
         assertEquals(sum, Streams.iterateInt(0, i -> i + 1).parallel().limit(10000).reduce(0, Integer::sum));
     }
 
-    public void testInfiniteRangeFindFirst() {
-        Integer first = Streams.iterate(0, i -> i + 1).filter(i -> i > 10000).findFirst().get();
-        assertEquals(first, Streams.iterate(0, i -> i + 1).parallel().filter(i -> i > 10000).findFirst().get());
-    }
-    
     //
 
     public void testLongRangeErrors() {