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.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Iterator;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Optional;
35 import java.util.Set;
36 import java.util.StringJoiner;
37 import java.util.TreeMap;
38 import java.util.concurrent.ConcurrentHashMap;
39 import java.util.concurrent.ConcurrentSkipListMap;
40 import java.util.function.BinaryOperator;
41 import java.util.function.Function;
42 import java.util.function.Predicate;
43 import java.util.function.Supplier;
44 import java.util.stream.Collector;
45 import java.util.stream.Collectors;
46 import java.util.stream.LambdaTestHelpers;
47 import java.util.stream.OpTestCase;
48 import java.util.stream.Stream;
49 import java.util.stream.StreamOpFlagTestHelper;
50 import java.util.stream.StreamTestDataProvider;
51 import java.util.stream.TestData;
52
53 import org.testng.annotations.Test;
54
55 import static java.util.stream.Collectors.groupingBy;
56 import static java.util.stream.Collectors.groupingByConcurrent;
57 import static java.util.stream.Collectors.partitioningBy;
58 import static java.util.stream.Collectors.reducing;
59 import static java.util.stream.Collectors.toCollection;
60 import static java.util.stream.Collectors.toConcurrentMap;
61 import static java.util.stream.Collectors.toList;
62 import static java.util.stream.Collectors.toMap;
63 import static java.util.stream.Collectors.toSet;
64 import static java.util.stream.LambdaTestHelpers.assertContents;
65 import static java.util.stream.LambdaTestHelpers.assertContentsUnordered;
66 import static java.util.stream.LambdaTestHelpers.mDoubler;
67
68 /**
69 * TabulatorsTest
70 *
71 * @author Brian Goetz
72 */
73 @SuppressWarnings({"rawtypes", "unchecked"})
74 public class TabulatorsTest extends OpTestCase {
586 new PartitionAssertion<>(classifier, new ListAssertion<>()));
587 }
588
589 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
590 public void testTwoLevelPartition(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
591 Predicate<Integer> classifier = i -> i % 3 == 0;
592 Predicate<Integer> classifier2 = i -> i % 7 == 0;
593
594 // Two level partition
595 exerciseMapTabulation(data,
596 partitioningBy(classifier, partitioningBy(classifier2)),
597 new PartitionAssertion<>(classifier,
598 new PartitionAssertion(classifier2, new ListAssertion<>())));
599
600 // Two level partition with reduce
601 exerciseMapTabulation(data,
602 partitioningBy(classifier, reducing(0, Integer::sum)),
603 new PartitionAssertion<>(classifier,
604 new ReduceAssertion<>(0, LambdaTestHelpers.identity(), Integer::sum)));
605 }
606 }
|
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.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Comparator;
30 import java.util.HashMap;
31 import java.util.HashSet;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Optional;
36 import java.util.Set;
37 import java.util.StringJoiner;
38 import java.util.TreeMap;
39 import java.util.concurrent.ConcurrentHashMap;
40 import java.util.concurrent.ConcurrentSkipListMap;
41 import java.util.function.BinaryOperator;
42 import java.util.function.Function;
43 import java.util.function.Predicate;
44 import java.util.function.Supplier;
45 import java.util.stream.Collector;
46 import java.util.stream.Collectors;
47 import java.util.stream.LambdaTestHelpers;
48 import java.util.stream.OpTestCase;
49 import java.util.stream.Stream;
50 import java.util.stream.StreamOpFlagTestHelper;
51 import java.util.stream.StreamTestDataProvider;
52 import java.util.stream.TestData;
53
54 import org.testng.annotations.Test;
55
56 import static java.util.stream.Collectors.collectingAndThen;
57 import static java.util.stream.Collectors.groupingBy;
58 import static java.util.stream.Collectors.groupingByConcurrent;
59 import static java.util.stream.Collectors.partitioningBy;
60 import static java.util.stream.Collectors.reducing;
61 import static java.util.stream.Collectors.toCollection;
62 import static java.util.stream.Collectors.toConcurrentMap;
63 import static java.util.stream.Collectors.toList;
64 import static java.util.stream.Collectors.toMap;
65 import static java.util.stream.Collectors.toSet;
66 import static java.util.stream.LambdaTestHelpers.assertContents;
67 import static java.util.stream.LambdaTestHelpers.assertContentsUnordered;
68 import static java.util.stream.LambdaTestHelpers.mDoubler;
69
70 /**
71 * TabulatorsTest
72 *
73 * @author Brian Goetz
74 */
75 @SuppressWarnings({"rawtypes", "unchecked"})
76 public class TabulatorsTest extends OpTestCase {
588 new PartitionAssertion<>(classifier, new ListAssertion<>()));
589 }
590
591 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
592 public void testTwoLevelPartition(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
593 Predicate<Integer> classifier = i -> i % 3 == 0;
594 Predicate<Integer> classifier2 = i -> i % 7 == 0;
595
596 // Two level partition
597 exerciseMapTabulation(data,
598 partitioningBy(classifier, partitioningBy(classifier2)),
599 new PartitionAssertion<>(classifier,
600 new PartitionAssertion(classifier2, new ListAssertion<>())));
601
602 // Two level partition with reduce
603 exerciseMapTabulation(data,
604 partitioningBy(classifier, reducing(0, Integer::sum)),
605 new PartitionAssertion<>(classifier,
606 new ReduceAssertion<>(0, LambdaTestHelpers.identity(), Integer::sum)));
607 }
608
609 @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class)
610 public void testComposeFinisher(String name, TestData.OfRef<Integer> data) throws ReflectiveOperationException {
611 List<Integer> asList = exerciseTerminalOps(data, s -> s.collect(toList()));
612 List<Integer> asImmutableList = exerciseTerminalOps(data, s -> s.collect(collectingAndThen(toList(), Collections::unmodifiableList)));
613 assertEquals(asList, asImmutableList);
614 try {
615 asImmutableList.add(0);
616 fail("Expecting immutable result");
617 }
618 catch (UnsupportedOperationException ignored) { }
619 }
620
621 }
|