use of java8.util.stream.LongStream in project streamsupport by stefan-zobel.
the class WhileOpTest method testLongDefaultClose.
@Test(groups = { "serialization-hostile" })
public void testLongDefaultClose() {
AtomicBoolean isClosed = new AtomicBoolean();
LongStream s = LongStreams.of(1, 2, 3).onClose(() -> isClosed.set(true));
LongStream ds = null;
try {
ds = DefaultMethodStreams.delegateTo(s).takeWhile(e -> e < 3);
ds.count();
} finally {
if (ds != null) {
ds.close();
}
}
assertTrue(isClosed.get());
}
use of java8.util.stream.LongStream in project streamsupport by stefan-zobel.
the class SplittableRandomTest method longsDataProvider.
@DataProvider(name = "longs")
public static Object[][] longsDataProvider() {
List<Object[]> data = new ArrayList<>();
// Function to create a stream using a RandomBoxedSpliterator
Function<Function<SplittableRandom, Long>, LongStream> rbsf = sf -> StreamSupport.stream(new RandomBoxedSpliterator<>(new SplittableRandom(), 0, SIZE, sf), false).mapToLong(i -> i);
// Unbounded
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs().limit(%d)", SIZE), () -> new SplittableRandom().longs().limit(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d)", SIZE), () -> new SplittableRandom().longs(SIZE)), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong())", SIZE), () -> rbsf.apply(sr -> sr.nextLong())), randomAsserter(SIZE, Long.MAX_VALUE, 0L) });
for (int b : BOUNDS) {
for (int o : ORIGINS) {
final long origin = o;
final long bound = b;
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d).limit(%d)", origin, bound, SIZE), () -> new SplittableRandom().longs(origin, bound).limit(SIZE)), randomAsserter(SIZE, origin, bound) });
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new SplittableRandom().longs(%d, %d, %d)", SIZE, origin, bound), () -> new SplittableRandom().longs(SIZE, origin, bound)), randomAsserter(SIZE, origin, bound) });
if (origin == 0) {
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d))", SIZE, bound), () -> rbsf.apply(sr -> sr.nextLong(bound))), randomAsserter(SIZE, origin, bound) });
}
data.add(new Object[] { TestData.Factory.ofLongSupplier(String.format("new RandomBoxedSpliterator(0, %d, sr -> sr.nextLong(%d, %d))", SIZE, origin, bound), () -> rbsf.apply(sr -> sr.nextLong(origin, bound))), randomAsserter(SIZE, origin, bound) });
}
}
return data.toArray(new Object[0][]);
}
use of java8.util.stream.LongStream in project streamsupport by stefan-zobel.
the class StreamSpliteratorTest method testLongSplitting.
//
public void testLongSplitting() {
List<Consumer<LongStream>> terminalOps = Arrays.asList(s -> s.toArray(), s -> s.forEach(e -> {
}), s -> s.reduce(java8.lang.Longs::sum));
List<UnaryOperator<LongStream>> 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++) {
Consumer<LongStream> terminalOp = terminalOps.get(i);
setContext("termOpIndex", i);
for (int j = 0; j < intermediateOps.size(); j++) {
setContext("intOpIndex", j);
UnaryOperator<LongStream> 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.OfLong sp = intermediateOp.apply(LongStreams.range(0, 1000)).spliterator();
ProxyNoExactSizeSpliterator.OfLong psp = new ProxyNoExactSizeSpliterator.OfLong(sp, proxyEstimateSize);
LongStream s = StreamSupport.longStream(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.LongStream 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.LongStream in project streamsupport by stefan-zobel.
the class FindAnyOpTest method exerciseLongStream.
void exerciseLongStream(TestData.OfLong data, Function<LongStream, LongStream> fs) {
OptionalLong or = withData(data).terminal(fs, s -> s.findAny()).equalator(LONG_VALID_ANSWER).exercise();
if (or.isPresent()) {
long r = or.getAsLong();
PrimitiveIterator.OfLong it = fs.apply(data.stream()).iterator();
boolean contained = false;
while (!contained && it.hasNext()) {
contained = r == it.nextLong();
}
assertTrue(contained);
} else {
assertFalse(fs.apply(data.stream()).iterator().hasNext());
}
}
Aggregations