1 /* 2 * Copyright (c) 2012, 2013, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.tests.java.util.stream; 26 27 import org.testng.annotations.Test; 28 29 import java.util.ArrayList; 30 import java.util.Collection; 31 import java.util.Comparator; 32 import java.util.List; 33 import java.util.Spliterator; 34 import java.util.Spliterators; 35 import java.util.stream.*; 36 37 import static java.util.stream.LambdaTestHelpers.*; 38 39 /** 40 * DistinctOpTest 41 */ 42 @Test 43 public class DistinctOpTest extends OpTestCase { 44 45 public void testUniqOp() { 46 assertCountSum(repeat(0, 10).stream().distinct(), 1, 0); 47 assertCountSum(repeat(1, 10).stream().distinct(), 1, 1); 48 assertCountSum(countTo(0).stream().distinct(), 0, 0); 49 assertCountSum(countTo(10).stream().distinct(), 10, 55); 50 assertCountSum(countTo(10).stream().distinct(), 10, 55); 51 } 52 53 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 54 public void testOp(String name, TestData.OfRef<Integer> data) { 55 Collection<Integer> result = exerciseOpsInt(data, Stream::distinct, IntStream::distinct, LongStream::distinct, DoubleStream::distinct); 56 57 assertUnique(result); 58 assertTrue((data.size() > 0) ? result.size() > 0 : result.size() == 0); 59 assertTrue(result.size() <= data.size()); 60 } 61 62 @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 63 public void testOpWithNull(String name, TestData.OfRef<Integer> data) { 64 Collection<Integer> node = exerciseOps(data, Stream::distinct); 65 assertUnique(node); 66 67 node = withData(data). 68 stream(s -> s.unordered().distinct()). 69 parallelEqualityAsserter(LambdaTestHelpers::assertContentsUnordered). 70 exercise(); 71 assertUnique(node); 72 73 node = exerciseOps(data, s -> s.distinct().distinct()); 74 assertUnique(node); 75 } 76 77 @Test(dataProvider = "withNull:StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 78 public void testOpWithNullSorted(String name, TestData.OfRef<Integer> data) { 79 List<Integer> l = new ArrayList<>(); 80 data.into(l).sort(cNullInteger); 81 // Need to inject SORTED into the sorted list source since 82 // sorted() with a comparator ironically clears SORTED 83 Collection<Integer> node = exerciseOps(new SortedTestData<>(l), Stream::distinct); 84 assertUnique(node); 85 assertSorted(node, cNullInteger); 86 } 87 88 @SuppressWarnings("serial") 89 static class SortedTestData<T> extends TestData.AbstractTestData.RefTestData<T, List<T>> { 90 SortedTestData(List<T> coll) { 91 super("SortedTestData", coll, 92 c -> StreamSupport.stream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED)), 93 c -> StreamSupport.parallelStream(Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED)), 94 c -> Spliterators.spliterator(c.toArray(), Spliterator.ORDERED | Spliterator.SORTED), 95 List::size); 96 } 97 } 98 99 public static final Comparator<Integer> cNullInteger = (a, b) -> { 100 if (a == null && b == null) { 101 return 0; 102 } 103 else if (a == null) { 104 return -1; 105 } 106 else if (b == null) { 107 return 1; 108 } 109 else { 110 return Integer.compare(a, b); 111 } 112 }; 113 114 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 115 public void testDistinctDistinct(String name, TestData.OfRef<Integer> data) { 116 Collection<Integer> result = withData(data) 117 .stream(s -> s.distinct().distinct(), new CollectorOps.TestParallelSizedOp<>()) 118 .exercise(); 119 assertUnique(result); 120 } 121 122 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 123 public void testDistinctSorted(String name, TestData.OfRef<Integer> data) { 124 Collection<Integer> result = withData(data) 125 .stream(s -> s.distinct().sorted(), 126 new CollectorOps.TestParallelSizedOp<>()) 127 .exercise(); 128 assertUnique(result); 129 assertSorted(result); 130 } 131 132 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) 133 public void testSortedDistinct(String name, TestData.OfRef<Integer> data) { 134 Collection<Integer> result = withData(data) 135 .stream(s -> s.sorted().distinct(), 136 new CollectorOps.TestParallelSizedOp<>()) 137 .exercise(); 138 assertUnique(result); 139 assertSorted(result); 140 } 141 }