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