use of one.util.streamex.LongStreamEx in project streamex by amaembo.
the class LongStreamExTest method testBasics.
@Test
public void testBasics() {
assertFalse(LongStreamEx.of(1).isParallel());
assertTrue(LongStreamEx.of(1).parallel().isParallel());
assertFalse(LongStreamEx.of(1).parallel().sequential().isParallel());
AtomicInteger i = new AtomicInteger();
try (LongStreamEx s = LongStreamEx.of(1).onClose(i::incrementAndGet)) {
assertEquals(1, s.count());
}
assertEquals(1, i.get());
assertEquals(6, LongStreamEx.range(0, 4).sum());
assertEquals(3, LongStreamEx.range(0, 4).max().getAsLong());
assertEquals(0, LongStreamEx.range(0, 4).min().getAsLong());
assertEquals(1.5, LongStreamEx.range(0, 4).average().getAsDouble(), 0.000001);
assertEquals(4, LongStreamEx.range(0, 4).summaryStatistics().getCount());
assertArrayEquals(new long[] { 1, 2, 3 }, LongStreamEx.range(0, 5).skip(1).limit(3).toArray());
assertArrayEquals(new long[] { 1, 2, 3 }, LongStreamEx.of(3, 1, 2).sorted().toArray());
assertArrayEquals(new long[] { 1, 2, 3 }, LongStreamEx.of(1, 2, 1, 3, 2).distinct().toArray());
assertArrayEquals(new int[] { 2, 4, 6 }, LongStreamEx.range(1, 4).mapToInt(x -> (int) x * 2).toArray());
assertArrayEquals(new long[] { 2, 4, 6 }, LongStreamEx.range(1, 4).map(x -> x * 2).toArray());
assertArrayEquals(new double[] { 2, 4, 6 }, LongStreamEx.range(1, 4).mapToDouble(x -> x * 2).toArray(), 0.0);
assertArrayEquals(new long[] { 1, 3 }, LongStreamEx.range(0, 5).filter(x -> x % 2 == 1).toArray());
assertEquals(6, LongStreamEx.of(1, 2, 3).reduce(Long::sum).getAsLong());
assertEquals(Long.MAX_VALUE, LongStreamEx.rangeClosed(1, Long.MAX_VALUE).spliterator().getExactSizeIfKnown());
assertTrue(LongStreamEx.of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.ORDERED));
assertFalse(LongStreamEx.of(1, 2, 3).unordered().spliterator().hasCharacteristics(Spliterator.ORDERED));
OfLong iterator = LongStreamEx.of(1, 2, 3).iterator();
assertEquals(1L, iterator.nextLong());
assertEquals(2L, iterator.nextLong());
assertEquals(3L, iterator.nextLong());
assertFalse(iterator.hasNext());
AtomicInteger idx = new AtomicInteger();
long[] result = new long[500];
LongStreamEx.range(1000).atLeast(500).parallel().forEachOrdered(val -> result[idx.getAndIncrement()] = val);
assertArrayEquals(LongStreamEx.range(500, 1000).toArray(), result);
assertTrue(LongStreamEx.empty().noneMatch(x -> true));
assertFalse(LongStreamEx.of(1).noneMatch(x -> true));
assertTrue(LongStreamEx.of(1).noneMatch(x -> false));
}
use of one.util.streamex.LongStreamEx in project streamex by amaembo.
the class LongStreamExTest method testWithout.
@Test
public void testWithout() {
assertArrayEquals(new long[] { 1, 2 }, LongStreamEx.range(1, 4).without(3).toArray());
assertArrayEquals(new long[] { 1, 2, 3 }, LongStreamEx.range(1, 4).without(5).toArray());
LongStreamEx lse = LongStreamEx.range(5);
assertSame(lse, lse.without());
assertArrayEquals(new long[] { 0, 1, 3, 4 }, LongStreamEx.range(5).without(new long[] { 2 }).toArray());
assertArrayEquals(new long[] { 0 }, LongStreamEx.range(5).without(1, 2, 3, 4, 5, 6).toArray());
}
use of one.util.streamex.LongStreamEx in project streamex by amaembo.
the class LongStreamExTest method testMinMax.
@Test
public void testMinMax() {
checkEmpty(s -> s.maxBy(Long::valueOf), s -> s.maxByInt(x -> (int) x), s -> s.maxByLong(x -> x), s -> s.maxByDouble(x -> x), s -> s.minBy(Long::valueOf), s -> s.minByInt(x -> (int) x), s -> s.minByLong(x -> x), s -> s.minByDouble(x -> x));
assertEquals(9, LongStreamEx.range(5, 12).max(Comparator.comparing(String::valueOf)).getAsLong());
assertEquals(10, LongStreamEx.range(5, 12).min(Comparator.comparing(String::valueOf)).getAsLong());
assertEquals(9, LongStreamEx.range(5, 12).maxBy(String::valueOf).getAsLong());
assertEquals(10, LongStreamEx.range(5, 12).minBy(String::valueOf).getAsLong());
assertEquals(5, LongStreamEx.range(5, 12).maxByDouble(x -> 1.0 / x).getAsLong());
assertEquals(11, LongStreamEx.range(5, 12).minByDouble(x -> 1.0 / x).getAsLong());
assertEquals(29, LongStreamEx.of(15, 8, 31, 47, 19, 29).maxByInt(x -> (int) (x % 10 * 10 + x / 10)).getAsLong());
assertEquals(31, LongStreamEx.of(15, 8, 31, 47, 19, 29).minByInt(x -> (int) (x % 10 * 10 + x / 10)).getAsLong());
assertEquals(29, LongStreamEx.of(15, 8, 31, 47, 19, 29).maxByLong(x -> x % 10 * 10 + x / 10).getAsLong());
assertEquals(31, LongStreamEx.of(15, 8, 31, 47, 19, 29).minByLong(x -> x % 10 * 10 + x / 10).getAsLong());
Supplier<LongStreamEx> s = () -> LongStreamEx.of(1, 50, 120, 35, 130, 12, 0);
LongToIntFunction intKey = x -> String.valueOf(x).length();
LongUnaryOperator longKey = x -> String.valueOf(x).length();
LongToDoubleFunction doubleKey = x -> String.valueOf(x).length();
LongFunction<Integer> objKey = x -> String.valueOf(x).length();
List<Function<LongStreamEx, OptionalLong>> minFns = Arrays.asList(is -> is.minByInt(intKey), is -> is.minByLong(longKey), is -> is.minByDouble(doubleKey), is -> is.minBy(objKey));
List<Function<LongStreamEx, OptionalLong>> maxFns = Arrays.asList(is -> is.maxByInt(intKey), is -> is.maxByLong(longKey), is -> is.maxByDouble(doubleKey), is -> is.maxBy(objKey));
minFns.forEach(fn -> assertEquals(1, fn.apply(s.get()).getAsLong()));
minFns.forEach(fn -> assertEquals(1, fn.apply(s.get().parallel()).getAsLong()));
maxFns.forEach(fn -> assertEquals(120, fn.apply(s.get()).getAsLong()));
maxFns.forEach(fn -> assertEquals(120, fn.apply(s.get().parallel()).getAsLong()));
}
Aggregations