From 9be0edbfcb057c4acadb7b24a8e688443ac082ac Mon Sep 17 00:00:00 2001 From: sagaofsilence Date: Wed, 15 May 2024 12:49:17 +0530 Subject: [PATCH] Added test on BinaryOperator, IntBinaryOperator --- .../reflectoring/function/OperatorTest.java | 68 +++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function/OperatorTest.java b/core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function/OperatorTest.java index 4080246b..516b1fd2 100644 --- a/core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function/OperatorTest.java +++ b/core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function/OperatorTest.java @@ -1,9 +1,9 @@ package io.reflectoring.function; -import java.util.function.Function; -import java.util.function.IntUnaryOperator; -import java.util.function.LongUnaryOperator; -import java.util.function.UnaryOperator; +import java.util.Optional; +import java.util.OptionalInt; +import java.util.function.*; +import java.util.stream.DoubleStream; import java.util.stream.IntStream; import java.util.stream.LongStream; import org.junit.jupiter.api.Assertions; @@ -54,4 +54,64 @@ void longUnaryOperator() { final long[] result = input.map(distance).toArray(); Assertions.assertArrayEquals(new long[] {931410L, 1862820L, 2794230L}, result); } + + @Test + void doubleUnaryOperator() { + DoubleUnaryOperator circleArea = radius -> radius * radius * Math.PI; + DoubleUnaryOperator doubleIt = area -> area * 4; + DoubleUnaryOperator scaling = circleArea.andThen(doubleIt); + + Assertions.assertEquals(153.93D, circleArea.applyAsDouble(7), 0.01); + Assertions.assertEquals(615.75D, scaling.applyAsDouble(7), 0.01); + + final DoubleStream input = DoubleStream.of(7d, 14d, 21d); + final double[] result = input.map(circleArea).toArray(); + Assertions.assertArrayEquals(new double[] {153.93D, 615.75D, 1385.44D}, result, 0.01); + } + + @Test + void binaryOperator() { + LongUnaryOperator factorial = + n -> { + long result = 1L; + for (int i = 1; i <= n; i++) { + result *= i; + } + return result; + }; + // Calculate permutations + BinaryOperator npr = (n, r) -> factorial.applyAsLong(n) / factorial.applyAsLong(n - r); + // Verify permutations + // 3P2 means the number of permutations of 2 that can be achieved from a choice of 3. + final Long result3P2 = npr.apply(3L, 2L); + Assertions.assertEquals(6L, result3P2); + + // Add two prices + BinaryOperator addPrices = Double::sum; + // Apply discount + UnaryOperator applyDiscount = total -> total * 0.9; // 10% discount + // Apply tax + UnaryOperator applyTax = total -> total * 1.07; // 7% tax + // Composing the final operation + BiFunction finalCost = + addPrices.andThen(applyDiscount).andThen(applyTax); + + // Prices of two items + double item1 = 50.0; + double item2 = 100.0; + // Calculate final cost + double cost = finalCost.apply(item1, item2); + // Verify the final calculated cost + Assertions.assertEquals(144.45D, cost, 0.01); + } + + @Test + void intBinaryOperator() { + IntBinaryOperator add = Integer::sum; + Assertions.assertEquals(10, add.applyAsInt(4, 6)); + + IntStream input = IntStream.of(2, 3, 4); + OptionalInt result = input.reduce(add); + Assertions.assertEquals(OptionalInt.of(9), result); + } }