Skip to content

Commit

Permalink
Added long and double binary op test
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 15, 2024
1 parent 9be0edb commit 07e46aa
Showing 1 changed file with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.reflectoring.function;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;
import java.util.function.*;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
Expand Down Expand Up @@ -114,4 +115,43 @@ void intBinaryOperator() {
OptionalInt result = input.reduce(add);
Assertions.assertEquals(OptionalInt.of(9), result);
}

@Test
void longBinaryOperator() {
// Greatest Common Divisor
LongBinaryOperator gcd =
(a, b) -> {
while (b != 0) {
long temp = b;
b = a % b;
a = temp;
}
return a;
};
Assertions.assertEquals(6L, gcd.applyAsLong(54L, 24L));

LongBinaryOperator add = Long::sum;
// Time car traveled
LongStream input = LongStream.of(1715785375164L, 1715785385771L);
final OptionalLong result = input.reduce(add);
Assertions.assertEquals(OptionalLong.of(3431570760935L), result);
}

@Test
void doubleBinaryOperator() {
DoubleBinaryOperator subtractAreas = (a, b) -> a - b;
// Area of a rectangle
double rectangleArea = 20.0 * 30.0;
// Area of a circle
double circleArea = Math.PI * 7.0 * 7.0;

// Subtract the two areas
double difference = subtractAreas.applyAsDouble(rectangleArea, circleArea);
Assertions.assertEquals(446.06, difference, 0.01);

DoubleBinaryOperator add = Double::sum;
DoubleStream input = DoubleStream.of(10.2, 5.6, 15.8, 20.12);
OptionalDouble result = input.reduce(add);
Assertions.assertEquals(OptionalDouble.of(51.72), result);
}
}

0 comments on commit 07e46aa

Please sign in to comment.