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()));
}
Aggregations