Search in sources :

Example 1 with LongStreamEx

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));
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) LongBinaryOperator(java.util.function.LongBinaryOperator) Spliterators(java.util.Spliterators) Assert.assertThrows(org.junit.Assert.assertThrows) Scanner(java.util.Scanner) Random(java.util.Random) Builder(java.util.stream.LongStream.Builder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) OptionalLong(java.util.OptionalLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) OfLong(java.util.PrimitiveIterator.OfLong) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongStream(java.util.stream.LongStream) LongStreamEx(one.util.streamex.LongStreamEx) LongFunction(java.util.function.LongFunction) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LongConsumer(java.util.function.LongConsumer) TestHelpers.streamEx(one.util.streamex.TestHelpers.streamEx) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) LongUnaryOperator(java.util.function.LongUnaryOperator) StreamEx(one.util.streamex.StreamEx) Assert.assertFalse(org.junit.Assert.assertFalse) LongBuffer(java.nio.LongBuffer) IntStreamEx(one.util.streamex.IntStreamEx) TestHelpers.longStreamEx(one.util.streamex.TestHelpers.longStreamEx) Comparator(java.util.Comparator) FixMethodOrder(org.junit.FixMethodOrder) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) OfLong(java.util.PrimitiveIterator.OfLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LongStreamEx(one.util.streamex.LongStreamEx) OptionalLong(java.util.OptionalLong) OfLong(java.util.PrimitiveIterator.OfLong) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 2 with LongStreamEx

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());
}
Also used : LongStreamEx(one.util.streamex.LongStreamEx) Test(org.junit.Test)

Example 3 with LongStreamEx

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()));
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) LongBinaryOperator(java.util.function.LongBinaryOperator) Spliterators(java.util.Spliterators) Assert.assertThrows(org.junit.Assert.assertThrows) Scanner(java.util.Scanner) Random(java.util.Random) Builder(java.util.stream.LongStream.Builder) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Assert.assertSame(org.junit.Assert.assertSame) OptionalLong(java.util.OptionalLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) OfLong(java.util.PrimitiveIterator.OfLong) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongStream(java.util.stream.LongStream) LongStreamEx(one.util.streamex.LongStreamEx) LongFunction(java.util.function.LongFunction) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Collectors(java.util.stream.Collectors) LongConsumer(java.util.function.LongConsumer) TestHelpers.streamEx(one.util.streamex.TestHelpers.streamEx) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) LongUnaryOperator(java.util.function.LongUnaryOperator) StreamEx(one.util.streamex.StreamEx) Assert.assertFalse(org.junit.Assert.assertFalse) LongBuffer(java.nio.LongBuffer) IntStreamEx(one.util.streamex.IntStreamEx) TestHelpers.longStreamEx(one.util.streamex.TestHelpers.longStreamEx) Comparator(java.util.Comparator) FixMethodOrder(org.junit.FixMethodOrder) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(java.util.function.Function) LongToDoubleFunction(java.util.function.LongToDoubleFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongFunction(java.util.function.LongFunction) LongToIntFunction(java.util.function.LongToIntFunction) LongUnaryOperator(java.util.function.LongUnaryOperator) LongStreamEx(one.util.streamex.LongStreamEx) LongToDoubleFunction(java.util.function.LongToDoubleFunction) Test(org.junit.Test)

Aggregations

LongStreamEx (one.util.streamex.LongStreamEx)3 Test (org.junit.Test)3 LongBuffer (java.nio.LongBuffer)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Map (java.util.Map)2 OptionalLong (java.util.OptionalLong)2 OfLong (java.util.PrimitiveIterator.OfLong)2 Random (java.util.Random)2 Scanner (java.util.Scanner)2 Spliterator (java.util.Spliterator)2 Spliterators (java.util.Spliterators)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Function (java.util.function.Function)2 LongBinaryOperator (java.util.function.LongBinaryOperator)2 LongConsumer (java.util.function.LongConsumer)2