Search in sources :

Example 1 with DoubleStreamEx

use of one.util.streamex.DoubleStreamEx in project streamex by amaembo.

the class DoubleStreamExTest method testBasics.

@Test
public void testBasics() {
    assertFalse(DoubleStreamEx.of(1).isParallel());
    assertTrue(DoubleStreamEx.of(1).parallel().isParallel());
    assertFalse(DoubleStreamEx.of(1).parallel().sequential().isParallel());
    AtomicInteger i = new AtomicInteger();
    try (DoubleStreamEx s = DoubleStreamEx.of(1).onClose(i::incrementAndGet)) {
        assertEquals(1, s.count());
    }
    assertEquals(1, i.get());
    assertEquals(6, IntStreamEx.range(0, 4).asDoubleStream().sum(), 0);
    assertEquals(3, IntStreamEx.range(0, 4).asDoubleStream().max().getAsDouble(), 0);
    assertEquals(0, IntStreamEx.range(0, 4).asDoubleStream().min().getAsDouble(), 0);
    assertEquals(1.5, IntStreamEx.range(0, 4).asDoubleStream().average().getAsDouble(), 0.000001);
    assertEquals(4, IntStreamEx.range(0, 4).asDoubleStream().summaryStatistics().getCount());
    assertArrayEquals(new double[] { 1, 2, 3 }, IntStreamEx.range(0, 5).asDoubleStream().skip(1).limit(3).toArray(), 0.0);
    assertArrayEquals(new double[] { 1, 2, 3 }, DoubleStreamEx.of(3, 1, 2).sorted().toArray(), 0.0);
    assertArrayEquals(new double[] { 1, 2, 3 }, DoubleStreamEx.of(1, 2, 1, 3, 2).distinct().toArray(), 0.0);
    assertArrayEquals(new int[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().mapToInt(x -> (int) x * 2).toArray());
    assertArrayEquals(new long[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().mapToLong(x -> (long) x * 2).toArray());
    assertArrayEquals(new double[] { 2, 4, 6 }, IntStreamEx.range(1, 4).asDoubleStream().map(x -> x * 2).toArray(), 0.0);
    assertArrayEquals(new double[] { 1, 3 }, IntStreamEx.range(0, 5).asDoubleStream().filter(x -> x % 2 == 1).toArray(), 0.0);
    assertEquals(6.0, DoubleStreamEx.of(1.0, 2.0, 3.0).reduce(Double::sum).getAsDouble(), 0.0);
    assertEquals(Long.MAX_VALUE, LongStreamEx.rangeClosed(1, Long.MAX_VALUE).asDoubleStream().spliterator().getExactSizeIfKnown());
    assertArrayEquals(new double[] { 4, 2, 0, -2, -4 }, DoubleStreamEx.zip(new double[] { 5, 4, 3, 2, 1 }, new double[] { 1, 2, 3, 4, 5 }, (a, b) -> a - b).toArray(), 0.0);
    assertEquals("1.0; 0.5; 0.25; 0.125", DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).mapToObj(String::valueOf).joining("; "));
    List<Double> list = new ArrayList<>();
    DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).forEach(list::add);
    assertEquals(Arrays.asList(1.0, 0.5, 0.25, 0.125), list);
    list = new ArrayList<>();
    DoubleStreamEx.of(1.0, 0.5, 0.25, 0.125).parallel().forEachOrdered(list::add);
    assertEquals(Arrays.asList(1.0, 0.5, 0.25, 0.125), list);
    assertFalse(DoubleStreamEx.of(1.0, 2.0, 2.5).anyMatch(x -> x < 0.0));
    assertTrue(DoubleStreamEx.of(1.0, 2.0, 2.5).anyMatch(x -> x >= 2.5));
    assertTrue(DoubleStreamEx.of(1.0, 2.0, 2.5).noneMatch(x -> x < 0.0));
    assertFalse(DoubleStreamEx.of(1.0, 2.0, 2.5).noneMatch(x -> x >= 2.5));
    assertEquals(5.0, DoubleStreamEx.of(1.0, 2.0, 2.5).reduce(1, (a, b) -> a * b), 0.0);
    assertTrue(DoubleStreamEx.of(1, 2, 3).spliterator().hasCharacteristics(Spliterator.ORDERED));
    assertFalse(DoubleStreamEx.of(1, 2, 3).unordered().spliterator().hasCharacteristics(Spliterator.ORDERED));
    OfDouble iterator = DoubleStreamEx.of(1.0, 2.0, 3.0).iterator();
    assertEquals(1.0, iterator.next(), 0.0);
    assertEquals(2.0, iterator.next(), 0.0);
    assertEquals(3.0, iterator.next(), 0.0);
    assertFalse(iterator.hasNext());
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) DoubleStreamEx(one.util.streamex.DoubleStreamEx) OptionalDouble(java.util.OptionalDouble) DoubleFunction(java.util.function.DoubleFunction) Scanner(java.util.Scanner) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) OfDouble(java.util.PrimitiveIterator.OfDouble) Assert.assertSame(org.junit.Assert.assertSame) DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Builder(java.util.stream.DoubleStream.Builder) LongStream(java.util.stream.LongStream) LongStreamEx(one.util.streamex.LongStreamEx) DoubleBuffer(java.nio.DoubleBuffer) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DoubleToIntFunction(java.util.function.DoubleToIntFunction) DoubleStream(java.util.stream.DoubleStream) TestHelpers.streamEx(one.util.streamex.TestHelpers.streamEx) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) DoubleToLongFunction(java.util.function.DoubleToLongFunction) StreamEx(one.util.streamex.StreamEx) Assert.assertFalse(org.junit.Assert.assertFalse) IntStreamEx(one.util.streamex.IntStreamEx) Comparator(java.util.Comparator) FixMethodOrder(org.junit.FixMethodOrder) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) OfDouble(java.util.PrimitiveIterator.OfDouble) OptionalDouble(java.util.OptionalDouble) OfDouble(java.util.PrimitiveIterator.OfDouble) DoubleStreamEx(one.util.streamex.DoubleStreamEx) Test(org.junit.Test)

Example 2 with DoubleStreamEx

use of one.util.streamex.DoubleStreamEx in project streamex by amaembo.

the class DoubleStreamExTest method testMinMax.

@Test
public void testMinMax() {
    checkEmpty(s -> s.maxBy(Double::valueOf), s -> s.maxByInt(x -> (int) x), s -> s.maxByLong(x -> (long) x), s -> s.maxByDouble(x -> x), s -> s.minBy(Double::valueOf), s -> s.minByInt(x -> (int) x), s -> s.minByLong(x -> (long) x), s -> s.minByDouble(x -> x));
    assertEquals(9, IntStreamEx.range(5, 12).asDoubleStream().max(Comparator.comparing(String::valueOf)).getAsDouble(), 0.0);
    assertEquals(10, IntStreamEx.range(5, 12).asDoubleStream().min(Comparator.comparing(String::valueOf)).getAsDouble(), 0.0);
    assertEquals(9, IntStreamEx.range(5, 12).asDoubleStream().maxBy(String::valueOf).getAsDouble(), 0.0);
    assertEquals(10, IntStreamEx.range(5, 12).asDoubleStream().minBy(String::valueOf).getAsDouble(), 0.0);
    assertEquals(5, IntStreamEx.range(5, 12).asDoubleStream().maxByDouble(x -> 1.0 / x).getAsDouble(), 0.0);
    assertEquals(11, IntStreamEx.range(5, 12).asDoubleStream().minByDouble(x -> 1.0 / x).getAsDouble(), 0.0);
    assertEquals(29.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).maxByInt(x -> (int) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(31.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).minByInt(x -> (int) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(29.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).maxByLong(x -> (long) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    assertEquals(31.0, DoubleStreamEx.of(15, 8, 31, 47, 19, 29).minByLong(x -> (long) (x % 10 * 10 + x / 10)).getAsDouble(), 0.0);
    Supplier<DoubleStreamEx> s = () -> DoubleStreamEx.of(1, 50, 120, 35, 130, 12, 0);
    DoubleToIntFunction intKey = x -> String.valueOf(x).length();
    DoubleToLongFunction longKey = x -> String.valueOf(x).length();
    DoubleUnaryOperator doubleKey = x -> String.valueOf(x).length();
    DoubleFunction<Integer> objKey = x -> String.valueOf(x).length();
    List<Function<DoubleStreamEx, OptionalDouble>> minFns = Arrays.asList(is -> is.minByInt(intKey), is -> is.minByLong(longKey), is -> is.minByDouble(doubleKey), is -> is.minBy(objKey));
    List<Function<DoubleStreamEx, OptionalDouble>> 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()).getAsDouble(), 0.0));
    minFns.forEach(fn -> assertEquals(1, fn.apply(s.get().parallel()).getAsDouble(), 0.0));
    maxFns.forEach(fn -> assertEquals(120, fn.apply(s.get()).getAsDouble(), 0.0));
    maxFns.forEach(fn -> assertEquals(120, fn.apply(s.get().parallel()).getAsDouble(), 0.0));
}
Also used : MethodSorters(org.junit.runners.MethodSorters) Arrays(java.util.Arrays) Spliterators(java.util.Spliterators) DoubleStreamEx(one.util.streamex.DoubleStreamEx) OptionalDouble(java.util.OptionalDouble) DoubleFunction(java.util.function.DoubleFunction) Scanner(java.util.Scanner) Random(java.util.Random) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) ArrayList(java.util.ArrayList) OfDouble(java.util.PrimitiveIterator.OfDouble) Assert.assertSame(org.junit.Assert.assertSame) DoubleBinaryOperator(java.util.function.DoubleBinaryOperator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Builder(java.util.stream.DoubleStream.Builder) LongStream(java.util.stream.LongStream) LongStreamEx(one.util.streamex.LongStreamEx) DoubleBuffer(java.nio.DoubleBuffer) TestHelpers.checkSpliterator(one.util.streamex.TestHelpers.checkSpliterator) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) DoubleToIntFunction(java.util.function.DoubleToIntFunction) DoubleStream(java.util.stream.DoubleStream) TestHelpers.streamEx(one.util.streamex.TestHelpers.streamEx) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) DoubleToLongFunction(java.util.function.DoubleToLongFunction) StreamEx(one.util.streamex.StreamEx) Assert.assertFalse(org.junit.Assert.assertFalse) IntStreamEx(one.util.streamex.IntStreamEx) Comparator(java.util.Comparator) FixMethodOrder(org.junit.FixMethodOrder) Spliterator(java.util.Spliterator) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DoubleFunction(java.util.function.DoubleFunction) Function(java.util.function.Function) DoubleToIntFunction(java.util.function.DoubleToIntFunction) DoubleToLongFunction(java.util.function.DoubleToLongFunction) DoubleToLongFunction(java.util.function.DoubleToLongFunction) DoubleStreamEx(one.util.streamex.DoubleStreamEx) DoubleToIntFunction(java.util.function.DoubleToIntFunction) DoubleUnaryOperator(java.util.function.DoubleUnaryOperator) Test(org.junit.Test)

Example 3 with DoubleStreamEx

use of one.util.streamex.DoubleStreamEx in project streamex by amaembo.

the class DoubleStreamExTest method testAppend.

@Test
public void testAppend() {
    assertArrayEquals(new double[] { 1, 2, 3, 4, 5 }, DoubleStreamEx.of(1, 2, 3).append(4, 5).toArray(), 0.0);
    assertArrayEquals(new double[] { 1, 2, 3, 4, 5 }, DoubleStreamEx.of(1, 2, 3).append(DoubleStream.of(4, 5)).toArray(), 0.0);
    DoubleStreamEx s = DoubleStreamEx.of(1, 2, 3);
    assertSame(s, s.append());
}
Also used : DoubleStreamEx(one.util.streamex.DoubleStreamEx) Test(org.junit.Test)

Example 4 with DoubleStreamEx

use of one.util.streamex.DoubleStreamEx in project streamex by amaembo.

the class DoubleStreamExTest method testPrepend.

@Test
public void testPrepend() {
    assertArrayEquals(new double[] { -1, 0, 1, 2, 3 }, DoubleStreamEx.of(1, 2, 3).prepend(-1, 0).toArray(), 0.0);
    assertArrayEquals(new double[] { -1, 0, 1, 2, 3 }, DoubleStreamEx.of(1, 2, 3).prepend(DoubleStream.of(-1, 0)).toArray(), 0.0);
    DoubleStreamEx s = DoubleStreamEx.of(1, 2, 3);
    assertSame(s, s.prepend());
}
Also used : DoubleStreamEx(one.util.streamex.DoubleStreamEx) Test(org.junit.Test)

Aggregations

DoubleStreamEx (one.util.streamex.DoubleStreamEx)4 Test (org.junit.Test)4 DoubleBuffer (java.nio.DoubleBuffer)2 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Comparator (java.util.Comparator)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 OptionalDouble (java.util.OptionalDouble)2 OfDouble (java.util.PrimitiveIterator.OfDouble)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 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 DoubleBinaryOperator (java.util.function.DoubleBinaryOperator)2 DoubleFunction (java.util.function.DoubleFunction)2 DoubleToIntFunction (java.util.function.DoubleToIntFunction)2