|
Java example source code file (SliceOpTest.java)
The SliceOpTest.java Java example source code/* * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package org.openjdk.tests.java.util.stream; import org.testng.annotations.Test; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LambdaTestHelpers; import java.util.stream.LongStream; import java.util.stream.OpTestCase; import java.util.stream.Stream; import java.util.stream.StreamSupport; import java.util.stream.StreamTestDataProvider; import java.util.stream.TestData; import static java.util.stream.LambdaTestHelpers.*; /** * SliceOpTest * * @author Brian Goetz */ @Test public class SliceOpTest extends OpTestCase { public void testSkip() { assertCountSum(countTo(0).stream().skip(0), 0, 0); assertCountSum(countTo(0).stream().skip(4), 0, 0); assertCountSum(countTo(4).stream().skip(4), 0, 0); assertCountSum(countTo(4).stream().skip(2), 2, 7); assertCountSum(countTo(4).stream().skip(0), 4, 10); assertCountSum(countTo(0).parallelStream().skip(0), 0, 0); assertCountSum(countTo(0).parallelStream().skip(4), 0, 0); assertCountSum(countTo(4).parallelStream().skip(4), 0, 0); assertCountSum(countTo(4).parallelStream().skip(2), 2, 7); assertCountSum(countTo(4).parallelStream().skip(0), 4, 10); exerciseOps(Collections.emptyList(), s -> s.skip(0), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(10), Collections.emptyList()); exerciseOps(countTo(1), s -> s.skip(0), countTo(1)); exerciseOps(countTo(1), s -> s.skip(1), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(0), countTo(100)); exerciseOps(countTo(100), s -> s.skip(10), range(11, 100)); exerciseOps(countTo(100), s -> s.skip(100), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200), Collections.emptyList()); } public void testLimit() { assertCountSum(countTo(0).stream().limit(4), 0, 0); assertCountSum(countTo(2).stream().limit(4), 2, 3); assertCountSum(countTo(4).stream().limit(4), 4, 10); assertCountSum(countTo(8).stream().limit(4), 4, 10); assertCountSum(countTo(0).parallelStream().limit(4), 0, 0); assertCountSum(countTo(2).parallelStream().limit(4), 2, 3); assertCountSum(countTo(4).parallelStream().limit(4), 4, 10); assertCountSum(countTo(8).parallelStream().limit(4), 4, 10); exerciseOps(Collections.emptyList(), s -> s.limit(0), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.limit(10), Collections.emptyList()); exerciseOps(countTo(1), s -> s.limit(0), Collections.emptyList()); exerciseOps(countTo(1), s -> s.limit(1), countTo(1)); exerciseOps(countTo(100), s -> s.limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.limit(10), countTo(10)); exerciseOps(countTo(100), s -> s.limit(10).limit(10), countTo(10)); exerciseOps(countTo(100), s -> s.limit(100), countTo(100)); exerciseOps(countTo(100), s -> s.limit(100).limit(10), countTo(10)); exerciseOps(countTo(100), s -> s.limit(200), countTo(100)); } public void testSkipLimit() { exerciseOps(Collections.emptyList(), s -> s.skip(0).limit(0), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(0).limit(10), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(10).limit(0), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(10).limit(10), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(0).limit(100), countTo(100)); exerciseOps(countTo(100), s -> s.skip(0).limit(10), countTo(10)); exerciseOps(countTo(100), s -> s.skip(0).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(10).limit(100), range(11, 100)); exerciseOps(countTo(100), s -> s.skip(10).limit(10), range(11, 20)); exerciseOps(countTo(100), s -> s.skip(10).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(100), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(10), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(100), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(10), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(0), Collections.emptyList()); } public void testSlice() { exerciseOps(Collections.emptyList(), s -> s.skip(0).limit(0), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(0).limit(10), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(10).limit(10), Collections.emptyList()); exerciseOps(Collections.emptyList(), s -> s.skip(10).limit(20), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(0).limit(100), countTo(100)); exerciseOps(countTo(100), s -> s.skip(0).limit(10), countTo(10)); exerciseOps(countTo(100), s -> s.skip(0).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(10).limit(100), range(11, 100)); exerciseOps(countTo(100), s -> s.skip(10).limit(10), range(11, 20)); exerciseOps(countTo(100), s -> s.skip(10).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(100), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(10), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(100).limit(0), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(100), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(10), Collections.emptyList()); exerciseOps(countTo(100), s -> s.skip(200).limit(0), Collections.emptyList()); } private int sliceSize(int dataSize, int skip, int limit) { int size = Math.max(0, dataSize - skip); if (limit >= 0) size = Math.min(size, limit); return size; } private int sliceSize(int dataSize, int skip) { return Math.max(0, dataSize - skip); } @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" }) public void testSkipOps(String name, TestData.OfRef<Integer> data) { List<Integer> skips = sizes(data.size()); for (int s : skips) { setContext("skip", s); testSliceMulti(data, sliceSize(data.size(), s), st -> st.skip(s), st -> st.skip(s), st -> st.skip(s), st -> st.skip(s)); testSliceMulti(data, sliceSize(sliceSize(data.size(), s), s/2), st -> st.skip(s).skip(s / 2), st -> st.skip(s).skip(s / 2), st -> st.skip(s).skip(s / 2), st -> st.skip(s).skip(s / 2)); } } @Test(dataProvider = "StreamTestData<Integer>", dataProviderClass = StreamTestDataProvider.class, groups = { "serialization-hostile" }) public void testSkipLimitOps(String name, TestData.OfRef<Integer> data) { List<Integer> skips = sizes(data.size()); List<Integer> limits = skips; for (int s : skips) { setContext("skip", s); for (int l : limits) { setContext("limit", l); testSliceMulti(data, sliceSize(sliceSize(data.size(), s), 0, l), st -> st.skip(s).limit(l), st -> st.skip(s).limit(l), st -> st.skip(s).limit(l), st -> st.skip(s).limit(l)); } } } public void testSkipLimitOpsWithNonSplittingSpliterator() { class NonSplittingNotSubsizedOrderedSpliterator<T> implements Spliterator Other Java examples (source code examples)Here is a short list of links related to this Java SliceOpTest.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.