• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2012, 2015, 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 org.testng.annotations.Test;
26 
27 import java.util.Spliterator;
28 import org.openjdk.testlib.java.util.stream.CollectorOps;
29 import org.openjdk.testlib.java.util.stream.OpTestCase;
30 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider;
31 import org.openjdk.testlib.java.util.stream.TestData;
32 
33 import java.util.ArrayList;
34 import java.util.Collection;
35 import java.util.Comparator;
36 import java.util.List;
37 import java.util.Optional;
38 import java.util.Spliterators;
39 import java.util.concurrent.ThreadLocalRandom;
40 import java.util.stream.Collectors;
41 import java.util.stream.DoubleStream;
42 import java.util.stream.IntStream;
43 import java.util.stream.LongStream;
44 import java.util.stream.Stream;
45 import java.util.stream.StreamSupport;
46 
47 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.*;
48 
49 /**
50  * DistinctOpTest
51  */
52 @Test
53 public class DistinctOpTest extends OpTestCase {
54 
testUniqOp()55     public void testUniqOp() {
56         assertCountSum(repeat(0, 10).stream().distinct(), 1, 0);
57         assertCountSum(repeat(1, 10).stream().distinct(), 1, 1);
58         assertCountSum(countTo(0).stream().distinct(), 0, 0);
59         assertCountSum(countTo(10).stream().distinct(), 10, 55);
60         assertCountSum(countTo(10).stream().distinct(), 10, 55);
61     }
62 
testWithUnorderedInfiniteStream()63     public void testWithUnorderedInfiniteStream() {
64         // These tests should short-circuit, otherwise will fail with a time-out
65         // or an OOME
66 
67         // Note that since the streams are unordered and any element is requested
68         // (a non-deterministic process) the only assertion that can be made is
69         // that an element should be found
70 
71         Optional<Integer> oi = Stream.iterate(1, i -> i + 1).unordered().parallel().distinct().findAny();
72         assertTrue(oi.isPresent());
73 
74         oi = ThreadLocalRandom.current().ints().boxed().parallel().distinct().findAny();
75         assertTrue(oi.isPresent());
76     }
77 
78     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOp(String name, TestData.OfRef<Integer> data)79     public void testOp(String name, TestData.OfRef<Integer> data) {
80         Collection<Integer> result = exerciseOpsInt(
81                 data,
82                 Stream::distinct,
83                 IntStream::distinct,
84                 LongStream::distinct,
85                 DoubleStream::distinct);
86 
87         assertUnique(result);
88         assertTrue((data.size() > 0) ? result.size() > 0 : result.size() == 0);
89         assertTrue(result.size() <= data.size());
90     }
91 
92     @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOpWithNull(String name, TestData.OfRef<Integer> data)93     public void testOpWithNull(String name, TestData.OfRef<Integer> data) {
94         Collection<Integer> node = exerciseOps(data, Stream::distinct);
95         assertUnique(node);
96 
97         node = withData(data).
98                 stream(s -> s.unordered().distinct()).
99                 exercise();
100         assertUnique(node);
101 
102         node = exerciseOps(data, s -> s.distinct().distinct());
103         assertUnique(node);
104     }
105 
106     @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testOpWithNullSorted(String name, TestData.OfRef<Integer> data)107     public void testOpWithNullSorted(String name, TestData.OfRef<Integer> data) {
108         List<Integer> l = new ArrayList<>();
109         data.into(l).sort(cNullInteger);
110         // Need to inject SORTED into the sorted list source since
111         // sorted() with a comparator ironically clears SORTED
112         Collection<Integer> node = exerciseOps(new SortedTestData<>(l), Stream::distinct);
113         assertUnique(node);
114         assertSorted(node, cNullInteger);
115     }
116 
117     @SuppressWarnings("serial")
118     static class SortedTestData<T> extends TestData.AbstractTestData.RefTestData<T, List<T>> {
SortedTestData(List<T> coll)119         SortedTestData(List<T> coll) {
120             super("SortedTestData", coll,
121                   c -> StreamSupport.stream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED), false),
122                   c -> StreamSupport.stream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED), true),
123                   c -> Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED),
124                   List::size);
125         }
126     }
127 
128     public static final Comparator<Integer> cNullInteger = (a, b) -> {
129         if (a == null && b == null) {
130             return 0;
131         }
132         else if (a == null) {
133             return -1;
134         }
135         else if (b == null) {
136             return 1;
137         }
138         else {
139             return Integer.compare(a, b);
140         }
141     };
142 
143     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testDistinctDistinct(String name, TestData.OfRef<Integer> data)144     public void testDistinctDistinct(String name, TestData.OfRef<Integer> data) {
145         Collection<Integer> result = exerciseOpsInt(
146                 data,
147                 s -> s.distinct().distinct(),
148                 s -> s.distinct().distinct(),
149                 s -> s.distinct().distinct(),
150                 s -> s.distinct().distinct());
151 
152         assertUnique(result);
153     }
154 
155     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testDistinctSorted(String name, TestData.OfRef<Integer> data)156     public void testDistinctSorted(String name, TestData.OfRef<Integer> data) {
157         Collection<Integer> result = withData(data)
158                 .stream(s -> s.distinct().sorted(),
159                         new CollectorOps.TestParallelSizedOp<>())
160                 .exercise();
161         assertUnique(result);
162         assertSorted(result);
163     }
164 
165     @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
testSortedDistinct(String name, TestData.OfRef<Integer> data)166     public void testSortedDistinct(String name, TestData.OfRef<Integer> data) {
167         Collection<Integer> result = withData(data)
168                 .stream(s -> s.sorted().distinct(),
169                         new CollectorOps.TestParallelSizedOp<>())
170                 .exercise();
171         assertUnique(result);
172         assertSorted(result);
173     }
174 
175     @Test(groups = { "serialization-hostile" })
testStable()176     public void testStable() {
177         // Create N instances of Integer all with the same value
178         List<Integer> input = IntStream.rangeClosed(0, 1000)
179                 .mapToObj(i -> new Integer(1000)) // explicit construction
180                 .collect(Collectors.toList());
181         Integer expectedElement = input.get(0);
182         TestData<Integer, Stream<Integer>> data = TestData.Factory.ofCollection(
183                 "1000 instances of Integer with the same value", input);
184 
185         withData(data)
186                 .stream(Stream::distinct)
187                 .resultAsserter((actual, expected, isOrdered, isParallel) -> {
188                     List<Integer> l = new ArrayList<>();
189                     actual.forEach(l::add);
190 
191                     // Assert stability
192                     // The single result element should be equal in identity to
193                     // the first input element
194                     assertEquals(l.size(), 1);
195                     assertEquals(System.identityHashCode(l.get(0)),
196                                  System.identityHashCode(expectedElement));
197 
198                 })
199                 .exercise();
200     }
201 }
202