changeset 7841:3a84da0da6dc

Update distinct op null tests to work correctly when upstream is sorted and contains null values.
author psandoz
date Mon, 08 Apr 2013 18:52:46 +0200
parents 4317c86ff03e
children 2bf9b8b3f119
files test-ng/tests/org/openjdk/tests/java/util/stream/DistinctOpTest.java test-ng/tests/org/openjdk/tests/java/util/stream/UniqOpTest.java
diffstat 2 files changed, 195 insertions(+), 118 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test-ng/tests/org/openjdk/tests/java/util/stream/DistinctOpTest.java	Mon Apr 08 18:52:46 2013 +0200
@@ -0,0 +1,195 @@
+/*
+ * Copyright (c) 2012, 2013, 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 org.testng.annotations.Test;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Comparators;
+import java.util.HashSet;
+import java.util.List;
+import java.util.SortedSet;
+import java.util.Spliterator;
+import java.util.TreeSet;
+import java.util.function.Consumer;
+import java.util.stream.*;
+
+import static java.util.stream.LambdaTestHelpers.*;
+
+/**
+ * DistinctOpTest
+ */
+@Test
+public class DistinctOpTest extends OpTestCase {
+
+    public void testUniqOp() {
+        assertCountSum(repeat(0, 10).stream().distinct(), 1, 0);
+        assertCountSum(repeat(1, 10).stream().distinct(), 1, 1);
+        assertCountSum(countTo(0).stream().distinct(), 0, 0);
+        assertCountSum(countTo(10).stream().distinct(), 10, 55);
+        assertCountSum(countTo(10).stream().distinct(), 10, 55);
+    }
+
+    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    public void testOp(String name, StreamTestData<Integer> data) {
+        Collection<Integer> result = exerciseOps(data, s -> s.distinct());
+
+        assertUnique(result);
+        assertTrue((data.size() > 0) ? result.size() > 0 : result.size() == 0);
+        assertTrue(result.size() <= data.size());
+    }
+
+    @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    public void testOpWithNull(String name, StreamTestData<Integer> data) {
+        Collection<Integer> node = exerciseOps(data, s -> s.distinct());
+        assertUnique(node);
+
+        List<Integer> l = new ArrayList<>();
+        data.into(l).sort(cNullInteger);
+        // Need to inject SORTED into the sorted list source since
+        // sorted() with a comparator ironically clears SORTED
+        node = exerciseOps(new SortedTestData<>(l), s -> s.distinct());
+        assertUnique(node);
+        assertSorted(node, cNullInteger);
+
+        node = exerciseOps(data, s -> s.distinct().distinct());
+        assertUnique(node);
+    }
+
+    @SuppressWarnings("serial")
+    static class SortedTestData<T> extends StreamTestData<T> {
+        List<T> c;
+
+        SortedTestData(List<T> c) {
+            this.c = c;
+        }
+
+        @Override
+        public int size() {
+            return c.size();
+        }
+
+        @Override
+        public Spliterator<T> spliterator() {
+            class SortedSpliterator implements Spliterator<T> {
+
+                final Spliterator<T> s;
+
+                SortedSpliterator(Spliterator<T> s) {
+                    this.s = s;
+                }
+
+                @Override
+                public boolean tryAdvance(Consumer<? super T> action) {
+                    return s.tryAdvance(action);
+                }
+
+                @Override
+                public void forEachRemaining(Consumer<? super T> action) {
+                    s.forEachRemaining(action);
+                }
+
+                @Override
+                public Spliterator<T> trySplit() {
+                    Spliterator<T> split = s.trySplit();
+                    if (split == null) return null;
+                    return new SortedSpliterator(split);
+                }
+
+                @Override
+                public long estimateSize() {
+                    return s.estimateSize();
+                }
+
+                @Override
+                public int characteristics() {
+                    return s.characteristics() | Spliterator.SORTED;
+                }
+
+                @Override
+                public Comparator<? super T> getComparator() {
+                    return null;
+                }
+            }
+            return new SortedSpliterator(c.spliterator());
+        }
+
+        @Override
+        public Stream<T> stream() {
+            return Streams.stream(spliterator());
+        }
+
+        @Override
+        public Stream<T> parallelStream() {
+            return stream().parallel();
+        }
+    }
+
+    public static final Comparator<Integer> cNullInteger = (a, b) -> {
+        if (a == null && b == null) {
+            return 0;
+        }
+        else if (a == null) {
+            return -1;
+        }
+        else if (b == null) {
+            return 1;
+        }
+        else {
+            return Integer.compare(a, b);
+        }
+    };
+
+    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    public void testDistinctDistinct(String name, StreamTestData<Integer> data) {
+        Collection<Integer> result = withData(data)
+                .stream(s -> s.distinct().distinct(), new CollectorOps.TestParallelSizedOp<Integer>())
+                .exercise();
+        assertUnique(result);
+    }
+
+    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    public void testDistinctSorted(String name, StreamTestData<Integer> data) {
+        Collection<Integer> result = withData(data)
+                .stream(s -> s.distinct().sorted(),
+                        new CollectorOps.TestParallelSizedOp<Integer>())
+                .exercise();
+        assertUnique(result);
+        assertSorted(result);
+    }
+
+    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
+    public void testSortedDistinct(String name, StreamTestData<Integer> data) {
+        Collection<Integer> result = withData(data)
+                .stream(s -> s.sorted().distinct(),
+                        new CollectorOps.TestParallelSizedOp<Integer>())
+                .exercise();
+        assertUnique(result);
+        assertSorted(result);
+    }
+}
--- a/test-ng/tests/org/openjdk/tests/java/util/stream/UniqOpTest.java	Mon Apr 08 12:45:45 2013 -0400
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,118 +0,0 @@
-/*
- * Copyright (c) 2012, 2013, 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 org.testng.annotations.Test;
-
-import java.util.Collection;
-import java.util.Comparator;
-import java.util.Comparators;
-import java.util.stream.*;
-
-import static java.util.stream.LambdaTestHelpers.*;
-
-/**
- * UniqOpTest
- */
-@Test
-public class UniqOpTest extends OpTestCase {
-
-    public void testUniqOp() {
-        assertCountSum(repeat(0, 10).stream().distinct(), 1, 0);
-        assertCountSum(repeat(1, 10).stream().distinct(), 1, 1);
-        assertCountSum(countTo(0).stream().distinct(), 0, 0);
-        assertCountSum(countTo(10).stream().distinct(), 10, 55);
-        assertCountSum(countTo(10).stream().distinct(), 10, 55);
-    }
-
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testOp(String name, StreamTestData<Integer> data) {
-        Collection<Integer> result = exerciseOps(data, s -> s.distinct());
-
-        assertUnique(result);
-        assertTrue((data.size() > 0) ? result.size() > 0 : result.size() == 0);
-        assertTrue(result.size() <= data.size());
-    }
-
-    @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testOpWithNull(String name, StreamTestData<Integer> data) {
-        Collection<Integer> node = exerciseOps(data, s -> s.distinct());
-        assertUnique(node);
-
-        node = exerciseOps(data, s -> s.sorted(cNullInteger).distinct());
-        assertUnique(node);
-        assertSorted(node, cNullInteger);
-
-        node = withData(data).stream(s -> s.distinct()).
-                parallelEqualityAsserter(LambdaTestHelpers::assertContentsUnordered).exercise();
-        assertUnique(node);
-
-        node = exerciseOps(data, s -> s.distinct().distinct());
-        assertUnique(node);
-    }
-
-    public static final Comparator<Integer> cNullInteger = (a, b) -> {
-        if (a == null && b == null) {
-            return 0;
-        }
-        else if (a == null) {
-            return -1;
-        }
-        else if (b == null) {
-            return 1;
-        }
-        else {
-            return Integer.compare(a, b);
-        }
-    };
-
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testUniqUniq(String name, StreamTestData<Integer> data) {
-        Collection<Integer> result = withData(data)
-                .stream(s -> s.distinct().distinct(), new CollectorOps.TestParallelSizedOp<Integer>())
-                .exercise();
-        assertUnique(result);
-    }
-
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testUniqSort(String name, StreamTestData<Integer> data) {
-        Collection<Integer> result = withData(data)
-                .stream(s -> s.distinct().sorted(Comparators.naturalOrder()),
-                        new CollectorOps.TestParallelSizedOp<Integer>())
-                .exercise();
-        assertUnique(result);
-        assertSorted(result);
-    }
-
-    @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
-    public void testSortUniq(String name, StreamTestData<Integer> data) {
-        Collection<Integer> result = withData(data)
-                .stream(s -> s.sorted(Comparators.naturalOrder()).distinct(),
-                        new CollectorOps.TestParallelSizedOp<Integer>())
-                .exercise();
-        assertUnique(result);
-        assertSorted(result);
-    }
-}