下面列出了java.util.stream.LongStreamTestScenario#java.util.stream.LongStream 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testInClauseMaxSize() throws Exception
{
final GenericJDBCMessageStore store = spy((GenericJDBCMessageStore) getStore());
reOpenStoreWithInClauseMaxSize(store, 10);
store.removeMessages(LongStream.rangeClosed(1, 21L).boxed().collect(Collectors.toList()));
verify(store).removeMessagesFromDatabase(any(Connection.class),
eq(LongStream.rangeClosed(1L, 10L)
.boxed()
.collect(Collectors.toList())));
verify(store).removeMessagesFromDatabase(any(Connection.class),
eq(LongStream.rangeClosed(11L, 20L)
.boxed()
.collect(Collectors.toList())));
verify(store).removeMessagesFromDatabase(any(Connection.class), eq(Collections.singletonList(21L)));
}
public void testLongSize() {
assertSized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE / 2),
LongStream.range(0, Long.MAX_VALUE / 2)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.range(0, Long.MAX_VALUE)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.iterate(0, i -> i + 1)));
assertUnsized(LongStream.concat(
LongStream.iterate(0, i -> i + 1),
LongStream.range(0, Long.MAX_VALUE)));
}
public void testLongSize() {
assertSized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE / 2),
LongStream.range(0, Long.MAX_VALUE / 2)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.range(0, Long.MAX_VALUE)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.iterate(0, i -> i + 1)));
assertUnsized(LongStream.concat(
LongStream.iterate(0, i -> i + 1),
LongStream.range(0, Long.MAX_VALUE)));
}
@Override
public void init() {
metricRegistry.register(getClass().getSimpleName(), timer);
super.init();
int commentsSize = 5;
doInJPA(entityManager -> {
LongStream.range(0, 50).forEach(i -> {
Post post = new Post();
post.setId(i);
post.setTitle(String.format("Post nr. %d", i));
LongStream.range(0, commentsSize).forEach(j -> {
PostComment comment = new PostComment();
comment.setId((i * commentsSize) + j);
comment.setReview(String.format("Good review nr. %d", comment.getId()));
post.addComment(comment);
});
entityManager.persist(post);
});
});
}
public boolean isGermanHoliday(LocalDate date) {
if (GERMAN_HOLIDAYS.contains(CustomHoliday.of(date.getDayOfMonth(), date.getMonthValue()))) {
return true;
}
// Easter holidays Good Friday, Easter Monday, Ascension Day, Whit Monday.
long diffFromEasterSunday =
DAYS.between(easterCalculator.getEasterSunday(date.getYear()), date);
Builder builder =
LongStream.builder()
.add(OFFSET_GOOD_FRIDAY)
.add(OFFSET_EASTER_MONDAY)
.add(OFFSET_ASCENSION_DAY)
.add(OFFSET_WHIT_MONDAY);
if (corpusChristiEnabled) {
builder.add(OFFSET_CORPUS_CHRISTI);
}
return builder.build().anyMatch(c -> c == diffFromEasterSunday);
}
public void testLongSize() {
assertSized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE / 2),
LongStream.range(0, Long.MAX_VALUE / 2)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.range(0, Long.MAX_VALUE)));
assertUnsized(LongStream.concat(
LongStream.range(0, Long.MAX_VALUE),
LongStream.iterate(0, i -> i + 1)));
assertUnsized(LongStream.concat(
LongStream.iterate(0, i -> i + 1),
LongStream.range(0, Long.MAX_VALUE)));
}
private void assertCollectionOrder(final String collectionName, final boolean exactOrdering) {
List<Long> expectedIdOrder = LongStream.range(0, 100).boxed().collect(Collectors.toList());
List<Long> idOrder =
getCollection(collectionName).find().sort(Sorts.ascending("_id")).into(new ArrayList<>())
.stream()
.map(d -> d.getLong("id"))
.collect(Collectors.toList());
assertEquals(
new HashSet<>(expectedIdOrder),
new HashSet<>(idOrder),
format("%s missing expected values.", collectionName));
if (exactOrdering) {
assertEquals(expectedIdOrder, idOrder, format("%s is out of order.", collectionName));
} else {
assertNotEquals(
expectedIdOrder, idOrder, format("%s unexpectedly in order.", collectionName));
}
}
private void testSliceMulti(TestData.OfRef<Integer> data,
int expectedSize,
Function<Stream<Integer>, Stream<Integer>> mRef,
Function<IntStream, IntStream> mInt,
Function<LongStream, LongStream> mLong,
Function<DoubleStream, DoubleStream> mDouble) {
@SuppressWarnings({ "rawtypes", "unchecked" })
Function<Stream<Integer>, Stream<Integer>>[] ms = new Function[4];
ms[0] = mRef;
ms[1] = s -> mInt.apply(s.mapToInt(e -> e)).mapToObj(e -> e);
ms[2] = s -> mLong.apply(s.mapToLong(e -> e)).mapToObj(e -> (int) e);
ms[3] = s -> mDouble.apply(s.mapToDouble(e -> e)).mapToObj(e -> (int) e);
testSliceMulti(data, expectedSize, ms);
}
@Test(dataProvider = "LongStream.limit")
public void testLongSubsizedWithRange(String description, UnaryOperator<LongStream> fs) {
// Range is [0, Long.MAX_VALUE), splits are SUBSIZED
// Such a size will induce out of memory errors for incorrect
// slice implementations
withData(longs()).
stream(s -> fs.apply(s)).
without(LongStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
exercise();
}
public void testDoubleStreamMatches() {
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, false, false, false, false);
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, true, true, true);
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, true, true, true, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, false, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, true, false);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, true, true);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, false);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, false, false);
}
public void testDoubleStreamMatches() {
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, false, false, false, false);
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, true, true, true);
assertDoublePredicates(() -> LongStream.range(0, 0).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, true, true, true, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, false, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, true);
assertDoublePredicates(() -> LongStream.range(1, 2).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, true, false);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ANY, DOUBLE_PREDICATES, true, false, true, true);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.ALL, DOUBLE_PREDICATES, true, false, false, false);
assertDoublePredicates(() -> LongStream.range(1, 6).asDoubleStream(), Kind.NONE, DOUBLE_PREDICATES, false, true, false, false);
}
@DataProvider(name = "LongStream.limit")
public static Object[][] longSliceFunctionsDataProvider() {
Function<String, String> f = s -> String.format(s, SKIP_LIMIT_SIZE);
List<Object[]> data = new ArrayList<>();
data.add(new Object[]{f.apply("LongStream.limit(%d)"),
(UnaryOperator<LongStream>) s -> s.limit(SKIP_LIMIT_SIZE)});
data.add(new Object[]{f.apply("LongStream.skip(%1$d).limit(%1$d)"),
(UnaryOperator<LongStream>) s -> s.skip(SKIP_LIMIT_SIZE).limit(SKIP_LIMIT_SIZE)});
return data.toArray(new Object[0][]);
}
private void takeSnapshot() {
final long i;
try {
Timer.Context takeSnapshotTimerContext = stateMachineMetrics.getTakeSnapshotTimer().time();
i = stateMachine.takeSnapshot();
takeSnapshotTimerContext.stop();
final long lastAppliedIndex = getLastAppliedIndex();
if (i > lastAppliedIndex) {
throw new StateMachineException(
"Bug in StateMachine: snapshot index = " + i + " > appliedIndex = " + lastAppliedIndex
+ "; StateMachine class=" + stateMachine.getClass().getName() + ", stateMachine=" + stateMachine);
}
stateMachine.getStateMachineStorage().cleanupOldSnapshots(snapshotRetentionPolicy);
} catch (IOException e) {
LOG.error(name + ": Failed to take snapshot", e);
return;
}
if (i >= 0) {
LOG.info("{}: Took a snapshot at index {}", name, i);
snapshotIndex.updateIncreasingly(i, infoIndexChange);
final long purgeIndex;
if (purgeUptoSnapshotIndex) {
// We can purge up to snapshot index even if all the peers do not have
// commitIndex up to this snapshot index.
purgeIndex = i;
} else {
final LongStream commitIndexStream = server.getCommitInfos().stream().mapToLong(
CommitInfoProto::getCommitIndex);
purgeIndex = LongStream.concat(LongStream.of(i), commitIndexStream).min().orElse(i);
}
raftLog.purge(purgeIndex);
}
}
@Test(dataProvider = "LongStream.limit")
public void testLongSubsizedWithRange(String description, UnaryOperator<LongStream> fs) {
// Range is [0, Long.MAX_VALUE), splits are SUBSIZED
// Such a size will induce out of memory errors for incorrect
// slice implementations
withData(longs()).
stream(s -> fs.apply(s)).
without(LongStreamTestScenario.CLEAR_SIZED_SCENARIOS).
exercise();
}
@Test(dataProvider = "LongStream.limit")
public void testLongUnorderedGenerator(String description, UnaryOperator<LongStream> fs) {
// Source is spliterator of infinite size
TestData.OfLong generator = TestData.Factory.ofLongSupplier(
"[1L, 1L, ...]", () -> LongStream.generate(() -> 1));
withData(generator).
stream(s -> fs.apply(s.filter(i -> true).unordered())).
exercise();
}
@Test(dataProvider = "sizes")
public void testLongAfterBuilding(int size) {
LongStream.Builder sb = LongStream.builder();
LongStream.range(0, size).forEach(sb);
sb.build();
checkISE(() -> sb.accept(1));
checkISE(() -> sb.add(1));
checkISE(() -> sb.build());
}
static void runTestMinMax(LongStream stream) {
final List<Long> list = stream.collect(ArrayList::new, List::add, List::addAll);
final LongMinMax longMinMax = toLongStream(list).collect(LongMinMax::new, LongMinMax::accumulate, LongMinMax::combine);
if (longMinMax.isInitialized()) {
Assert.assertEquals(toLongStream(list).min().getAsLong(), longMinMax.getMin());
Assert.assertEquals(toLongStream(list).max().getAsLong(), longMinMax.getMax());
} else {
Assert.assertEquals(OptionalLong.empty(), toLongStream(list).min());
Assert.assertEquals(OptionalLong.empty(), toLongStream(list).max());
}
}
private void addLongs(final String field, final String operation, final long... value) {
final String key = operation == null ? field : field + "__" + operation;
conditions.compute(key, (k, v) -> {
final String val = LongStream.of(value)
.mapToObj(String::valueOf)
.collect(Collectors.joining("\",\"", "[\"", "\"]"));
final List<Object> result = (v == null) ? new ArrayList<>(1) : v;
result.add(val);
return result;
});
}
@Test
public void testProcessListSuccess() throws Exception {
List<Long> longList = Lists.newArrayList(LongStream.range(0L, 10L).iterator());
List<Long> expectedList = Lists.transform(
longList,
aLong -> 2 * aLong);
Function<Long, CompletableFuture<Long>> sumFunc = value -> FutureUtils.value(2 * value);
CompletableFuture<List<Long>> totalFuture = FutureUtils.processList(
longList,
sumFunc,
null);
assertEquals(expectedList, FutureUtils.result(totalFuture));
}
@Test(dataProvider = "LongStream.limit")
public void testLongUnorderedIteration(String description, UnaryOperator<LongStream> fs) {
// Source is a right-balanced tree of infinite size
TestData.OfLong iterator = TestData.Factory.ofLongSupplier(
"[1L, 2L, 3L, ...]", () -> LongStream.iterate(1, i -> i + 1));
// Ref
withData(iterator).
stream(s -> fs.apply(s.unordered())).
resultAsserter(unorderedAsserter()).
exercise();
}
@Test(dataProvider = "LongStream.limit")
public void testLongSubsizedWithRange(String description, UnaryOperator<LongStream> fs) {
// Range is [0, Long.MAX_VALUE), splits are SUBSIZED
// Such a size will induce out of memory errors for incorrect
// slice implementations
withData(longs()).
stream(s -> fs.apply(s)).
without(LongStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
exercise();
}
default DataResult<LongStream> getLongStream(final T input) {
return getStream(input).flatMap(stream -> {
final List<T> list = stream.collect(Collectors.toList());
if (list.stream().allMatch(element -> getNumberValue(element).result().isPresent())) {
return DataResult.success(list.stream().mapToLong(element -> getNumberValue(element).result().get().longValue()));
}
return DataResult.error("Some elements are not longs: " + input);
});
}
@Test
public void testLongSingleton() {
TestData.OfLong data = TestData.Factory.ofLongSupplier("[0, 1)",
() -> LongStream.of(1));
withData(data).
stream(s -> s).
expectedResult(Collections.singletonList(1L)).
exercise();
withData(data).
stream(s -> s.map(i -> i)).
expectedResult(Collections.singletonList(1L)).
exercise();
}
/**
* Gets a stream of values from a collection of range resources.
*/
public static LongStream rangeValues(Collection<Protos.Resource> resources) {
checkNotNull(resources);
return resources.stream()
.filter(Protos.Resource::hasRanges)
.flatMap(r -> r.getRanges().getRangeList().stream())
.flatMapToLong(Utils::rangeValues);
}
@Test
public void testDouble() {
HeapDoubleVector vector = new HeapDoubleVector(SIZE);
for (int i = 0; i < SIZE; i++) {
vector.setDouble(i, i);
}
for (int i = 0; i < SIZE; i++) {
assertEquals(i, vector.getDouble(i), 0);
}
vector.fill(22);
for (int i = 0; i < SIZE; i++) {
assertEquals(22, vector.getDouble(i), 0);
}
vector.setDictionary(new TestDictionary(LongStream.range(0, SIZE).boxed()
.map(Number::doubleValue).toArray()));
setRangeDictIds(vector);
for (int i = 0; i < SIZE; i++) {
assertEquals(i, vector.getDouble(i), 0);
}
double[] doubles = LongStream.range(0, SIZE).boxed().mapToDouble(Number::doubleValue).toArray();
byte[] binary = new byte[SIZE * 8];
UNSAFE.copyMemory(doubles, DOUBLE_ARRAY_OFFSET, binary, BYTE_ARRAY_OFFSET, binary.length);
vector = new HeapDoubleVector(SIZE);
vector.setDoublesFromBinary(0, SIZE, binary, 0);
for (int i = 0; i < SIZE; i++) {
assertEquals(i, vector.getDouble(i), 0);
}
}
@Test
public void testSequenceSampler() {
try (Model model = Model.newInstance("model")) {
model.setBlock(Blocks.identityBlock());
NDManager manager = model.getNDManager();
ArrayDataset dataset =
new ArrayDataset.Builder()
.setData(
manager.arange(
0, 100, 1, DataType.INT64, Device.defaultDevice()))
.setSampling(new BatchSampler(new SequenceSampler(), 1, false))
.build();
List<Long> original = new ArrayList<>();
try (Trainer trainer = model.newTrainer(config)) {
trainer.iterateDataset(dataset)
.iterator()
.forEachRemaining(
record ->
original.add(
record.getData().singletonOrThrow().getLong()));
List<Long> expected = LongStream.range(0, 100).boxed().collect(Collectors.toList());
Assert.assertEquals(original, expected, "SequentialSampler test failed");
}
}
}
@Test(dataProvider = "LongStream.limit")
public void testLongSubsizedWithRange(String description, UnaryOperator<LongStream> fs) {
// Range is [0, Long.MAX_VALUE), splits are SUBSIZED
// Such a size will induce out of memory errors for incorrect
// slice implementations
withData(longs()).
stream(s -> fs.apply(s)).
without(LongStreamTestScenario.PAR_STREAM_TO_ARRAY_CLEAR_SIZED).
exercise();
}
/**
* Splits an {@code NDArray} into the given number of slices along the given batch axis.
*
* <p>Usually used for data parallelism where each slice is sent to one device (i.e. GPU).
*
* @param array a batch of {@code NDArray}
* @param numOfSlices the number of desired slices
* @param evenSplit whether to force all slices to have the same number of elements
* @return an NDList even if `numOfSlice` is 1
*/
private NDList split(NDArray array, int numOfSlices, boolean evenSplit) {
int batchSize = Math.toIntExact(array.size(0));
if (batchSize < numOfSlices) {
throw new IllegalArgumentException(
"Batch size("
+ batchSize
+ ") is less then slice number("
+ numOfSlices
+ ").");
}
if (evenSplit && batchSize % numOfSlices != 0) {
throw new IllegalArgumentException(
"data with shape "
+ batchSize
+ " cannot be evenly split into "
+ numOfSlices
+ ". Use a batch size that's multiple of "
+ numOfSlices
+ " or set even_split=true to allow"
+ " uneven partitioning of data.");
}
if (evenSplit) {
return array.split(numOfSlices);
}
int step = (int) Math.ceil((double) batchSize / numOfSlices);
long[] indices = LongStream.range(1, numOfSlices).map(i -> i * step).toArray();
return array.split(indices);
}
@Test
public void testLongSingleton() {
TestData.OfLong data = TestData.Factory.ofLongSupplier("[0, 1)",
() -> LongStream.of(1));
withData(data).
stream(s -> s).
expectedResult(Collections.singletonList(1L)).
exercise();
withData(data).
stream(s -> s.map(i -> i)).
expectedResult(Collections.singletonList(1L)).
exercise();
}
@Override
public LongStream longs(
long streamSize,
long randomNumberOrigin,
long randomNumberBound
) {
throw new UnsupportedOperationException();
}