use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.
the class ConcatTest method assertIntConcat.
private void assertIntConcat(Stream<Integer> s1, Stream<Integer> s2, boolean parallel, boolean ordered) {
IntStream result = IntStreams.concat(s1.mapToInt(Integer::intValue), s2.mapToInt(Integer::intValue));
assertEquals(result.isParallel(), parallel);
assertConcatContent(result.spliterator(), ordered, StreamSupport.stream(expected).mapToInt(Integer::intValue).spliterator());
}
use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.
the class SplittableRandomTest method intsDataProvider.
@DataProvider(name = "ints")
public static Object[][] intsDataProvider() {
List<Object[]> data = new ArrayList<>();
// Function to create a stream using a RandomBoxedSpliterator
Function<Function<SplittableRandom, Integer>, IntStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToInt(i -> i);
// Unbounded
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints().limit(%d)", SIZE), () -> new SplittableRandom().ints().limit(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d)", SIZE), () -> new SplittableRandom().ints(SIZE)), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt())", SIZE), () -> rbsf.apply(sr -> sr.nextInt())), randomAsserter(SIZE, Integer.MAX_VALUE, 0) });
for (int b : BOUNDS) {
for (int o : ORIGINS) {
final int origin = o;
final int bound = b;
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().ints(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new SplittableRandom().ints(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().ints(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
if (origin == 0) {
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextInt(bound))), randomAsserter(SIZE, origin, bound) });
}
data.add(new Object[] { TestData.Factory.ofIntSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextInt(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextInt(origin, bound))), randomAsserter(SIZE, origin, bound) });
}
}
return data.toArray(new Object[0][]);
}
use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.
the class StreamSpliteratorTest method testIntSplitting.
//
public void testIntSplitting() {
List<Consumer<IntStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
}), s -> s.reduce(java8.lang.Integers::sum));
List<UnaryOperator<IntStream>> intermediateOps = Arrays.asList(s -> s.parallel(), // The following ensures the wrapping spliterator is tested
s -> s.map(i -> i).parallel());
for (int i = 0; i < terminalOps.size(); i++) {
setContext("termOpIndex", i);
Consumer<IntStream> terminalOp = terminalOps.get(i);
for (int j = 0; j < intermediateOps.size(); j++) {
setContext("intOpIndex", j);
UnaryOperator<IntStream> intermediateOp = intermediateOps.get(j);
for (boolean proxyEstimateSize : new boolean[] { false, true }) {
setContext("proxyEstimateSize", proxyEstimateSize);
// Size is assumed to be larger than the target size for no splitting
// @@@ Need way to obtain the target size
Spliterator.OfInt sp = intermediateOp.apply(IntStreams.range(0, 1000)).spliterator();
ProxyNoExactSizeSpliterator.OfInt psp = new ProxyNoExactSizeSpliterator.OfInt(sp, proxyEstimateSize);
IntStream s = StreamSupport.intStream(psp, true);
terminalOp.accept(s);
Assert.assertTrue(psp.splits > 0, String.format("Number of splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.prefixSplits > 0, String.format("Number of non-null prefix splits should be greater that zero when proxyEstimateSize is %s", proxyEstimateSize));
Assert.assertTrue(psp.sizeOnTraversal < 1000, String.format("Size on traversal of last split should be less than the size of the list, %d, when proxyEstimateSize is %s", 1000, proxyEstimateSize));
}
}
}
}
use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.
the class WhileOpStatefulTest method testWhileMulti.
private void testWhileMulti(Map<String, Supplier<Stream<Integer>>> sources, Consumer<Stream<Integer>> mRef, Consumer<IntStream> mInt, Consumer<LongStream> mLong, Consumer<DoubleStream> mDouble) {
Map<String, Function<Stream<Integer>, Stream<Integer>>> transforms = new HashMap<>();
transforms.put("Stream.sequential()", s -> {
BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
return s.peek(e -> {
if (!isWithinExecutionPeriod.getAsBoolean()) {
throw new RuntimeException();
}
});
});
transforms.put("Stream.parallel()", s -> {
BooleanSupplier isWithinExecutionPeriod = within(System.currentTimeMillis(), EXECUTION_TIME_LIMIT);
return s.parallel().peek(e -> {
if (!isWithinExecutionPeriod.getAsBoolean()) {
throw new RuntimeException();
}
});
});
Map<String, Consumer<Stream<Integer>>> actions = new HashMap<>();
actions.put("Ref", mRef);
actions.put("Int", s -> mInt.accept(s.mapToInt(e -> e)));
actions.put("Long", s -> mLong.accept(s.mapToLong(e -> e)));
actions.put("Double", s -> mDouble.accept(s.mapToDouble(e -> e)));
actions.put("Ref using defaults", s -> mRef.accept(DefaultMethodStreams.delegateTo(s)));
actions.put("Int using defaults", s -> mInt.accept(DefaultMethodStreams.delegateTo(s.mapToInt(e -> e))));
actions.put("Long using defaults", s -> mLong.accept(DefaultMethodStreams.delegateTo(s.mapToLong(e -> e))));
actions.put("Double using defaults", s -> mDouble.accept(DefaultMethodStreams.delegateTo(s.mapToDouble(e -> e))));
for (Map.Entry<String, Supplier<Stream<Integer>>> s : sources.entrySet()) {
setContext("source", s.getKey());
for (Map.Entry<String, Function<Stream<Integer>, Stream<Integer>>> t : transforms.entrySet()) {
setContext("transform", t.getKey());
for (Map.Entry<String, Consumer<Stream<Integer>>> a : actions.entrySet()) {
setContext("shape", a.getKey());
Stream<Integer> stream = s.getValue().get();
stream = t.getValue().apply(stream);
a.getValue().accept(stream);
}
}
}
}
use of java8.util.stream.IntStream in project streamsupport by stefan-zobel.
the class WhileOpTest method testIntDefaultClose.
@Test(groups = { "serialization-hostile" })
public void testIntDefaultClose() {
AtomicBoolean isClosed = new AtomicBoolean();
IntStream s = IntStreams.of(1, 2, 3).onClose(() -> isClosed.set(true));
IntStream ds = null;
try {
ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3);
ds.count();
} finally {
if (ds != null) {
ds.close();
}
}
assertTrue(isClosed.get());
}
Aggregations