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