Search in sources :

Example 1 with LongToDoubleFunction

use of java.util.function.LongToDoubleFunction 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

LongBuffer (java.nio.LongBuffer)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 OptionalLong (java.util.OptionalLong)1 OfLong (java.util.PrimitiveIterator.OfLong)1 Random (java.util.Random)1 Scanner (java.util.Scanner)1 Spliterator (java.util.Spliterator)1 Spliterators (java.util.Spliterators)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Function (java.util.function.Function)1 LongBinaryOperator (java.util.function.LongBinaryOperator)1 LongConsumer (java.util.function.LongConsumer)1 LongFunction (java.util.function.LongFunction)1 LongToDoubleFunction (java.util.function.LongToDoubleFunction)1