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. 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.openjdk.testlib.java.util.stream.LambdaTestHelpers; 26 import org.openjdk.testlib.java.util.stream.OpTestCase; 27 import org.openjdk.testlib.java.util.stream.StreamTestDataProvider; 28 import org.openjdk.testlib.java.util.stream.TestData; 29 30 import java.util.Arrays; 31 import java.util.Collection; 32 import java.util.HashSet; 33 import java.util.Iterator; 34 import java.util.List; 35 import java.util.Map; 36 import java.util.Objects; 37 import java.util.Set; 38 import java.util.function.Function; 39 import java.util.stream.Collector; 40 import java.util.stream.Collectors; 41 import java.util.stream.Stream; 42 43 import org.testng.annotations.Test; 44 45 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.countTo; 46 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mDoubler; 47 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mId; 48 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.mZero; 49 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pEven; 50 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pFalse; 51 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pOdd; 52 import static org.openjdk.testlib.java.util.stream.LambdaTestHelpers.pTrue; 53 54 /** 55 * GroupByOpTest 56 * 57 */ 58 @Test 59 public class GroupByOpTest extends OpTestCase { 60 testBypassCollect()61 public void testBypassCollect() { 62 @SuppressWarnings("unchecked") 63 Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>> collector 64 = (Collector<Integer, Map<Boolean, List<Integer>>, Map<Boolean, List<Integer>>>) Collectors.groupingBy(LambdaTestHelpers.forPredicate(pEven, true, false)); 65 66 Map<Boolean, List<Integer>> m = collector.supplier().get(); 67 int[] ints = countTo(10).stream().mapToInt(e -> (int) e).toArray(); 68 for (int i : ints) 69 collector.accumulator().accept(m, i); 70 71 assertEquals(2, m.keySet().size()); 72 for(Collection<Integer> group : m.values()) { 73 int count = 0; 74 Stream<Integer> stream = group.stream(); 75 Iterator<Integer> it = stream.iterator(); 76 while (it.hasNext()) { 77 it.next(); 78 ++count; 79 } 80 assertEquals(5, count); 81 } 82 } 83 testGroupBy()84 public void testGroupBy() { 85 Map<Boolean,List<Integer>> result = countTo(10).stream().collect(Collectors.groupingBy(LambdaTestHelpers.forPredicate(pEven, true, false))); 86 87 assertEquals(2, result.keySet().size()); 88 for(Collection<Integer> group : result.values()) { 89 int count = 0; 90 Stream<Integer> stream = group.stream(); 91 Iterator<Integer> it = stream.iterator(); 92 while (it.hasNext()) { 93 it.next(); 94 ++count; 95 } 96 assertEquals(5, count); 97 } 98 } 99 100 static class MapperData<T, K> { 101 Function<T, K> m; 102 int expectedSize; 103 MapperData(Function<T, K> m, int expectedSize)104 MapperData(Function<T, K> m, int expectedSize) { 105 this.m = m; 106 this.expectedSize = expectedSize; 107 } 108 } 109 getMapperData(TestData.OfRef<Integer> data)110 List<MapperData<Integer, ?>> getMapperData(TestData.OfRef<Integer> data) { 111 int uniqueSize = data.into(new HashSet<>()).size(); 112 113 return Arrays.asList( 114 new MapperData<>(mId, uniqueSize), 115 new MapperData<>(mZero, Math.min(1, data.size())), 116 new MapperData<>(mDoubler, uniqueSize), 117 new MapperData<>(LambdaTestHelpers.compose(mId, mDoubler), uniqueSize), 118 new MapperData<>(LambdaTestHelpers.compose(mDoubler, mDoubler), uniqueSize), 119 120 new MapperData<>(LambdaTestHelpers.forPredicate(pFalse, true, false), Math.min(1, uniqueSize)), 121 new MapperData<>(LambdaTestHelpers.forPredicate(pTrue, true, false), Math.min(1, uniqueSize)), 122 new MapperData<>(LambdaTestHelpers.forPredicate(pEven, true, false), Math.min(2, uniqueSize)), 123 new MapperData<>(LambdaTestHelpers.forPredicate(pOdd, true, false), Math.min(2, uniqueSize)) 124 ); 125 } 126 127 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class) testOps(String name, TestData.OfRef<Integer> data)128 public void testOps(String name, TestData.OfRef<Integer> data) { 129 // @@@ More things to test here: 130 // - Every value in data is present in right bucket 131 // - Total number of values equals size of data 132 133 for (MapperData<Integer, ?> md : getMapperData(data)) { 134 Collector<Integer, ?, Map<Object, List<Integer>>> tab = Collectors.groupingBy(md.m); 135 Map<Object, List<Integer>> result = 136 withData(data) 137 .terminal(s -> s, s -> s.collect(tab)) 138 .resultAsserter((act, exp, ord, par) -> { 139 if (par & !ord) { 140 GroupByOpTest.assertMultiMapEquals(act, exp); 141 } 142 else { 143 GroupByOpTest.assertObjectEquals(act, exp); 144 } 145 }) 146 .exercise(); 147 assertEquals(result.keySet().size(), md.expectedSize); 148 } 149 } 150 assertObjectEquals(Object a, Object b)151 static void assertObjectEquals(Object a, Object b) { 152 assertTrue(Objects.equals(a, b)); 153 } 154 assertMultiMapEquals(Map<K, ? extends Collection<V>> a, Map<K, ? extends Collection<V>> b)155 static <K, V> void assertMultiMapEquals(Map<K, ? extends Collection<V>> a, Map<K, ? extends Collection<V>> b) { 156 assertTrue(multiMapEquals(a, b)); 157 } 158 multiMapEquals(Map<K, ? extends Collection<V>> a, Map<K, ? extends Collection<V>> b)159 static<K, V> boolean multiMapEquals(Map<K, ? extends Collection<V>> a, Map<K, ? extends Collection<V>> b) { 160 if (!Objects.equals(a.keySet(), b.keySet())) { 161 return false; 162 } 163 164 for (K k : a.keySet()) { 165 Set<V> as = new HashSet<>(a.get(k)); 166 Set<V> bs = new HashSet<>(b.get(k)); 167 if (!Objects.equals(as, bs)) { 168 return false; 169 } 170 } 171 172 return true; 173 } 174 } 175