Skip to content

Commit

Permalink
Added DoublePredicate test
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 12, 2024
1 parent 1b5d68b commit 1361184
Showing 1 changed file with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
import java.util.function.Predicate;
import java.util.function.*;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -226,4 +224,35 @@ void testLongPredicate() {
Assertions.assertArrayEquals(
new long[] {70L, 100L}, LongStream.of(70L, 100L, 200L).filter(highwayCheck).toArray());
}

@Test
void testDoublePredicate() {
// weight categories (weight in lbs)
DoublePredicate underweight = weight -> weight <= 125;
DoublePredicate healthy = weight -> weight >= 126 && weight <= 168;
DoublePredicate overweight = weight -> weight >= 169 && weight <= 202;
DoublePredicate obese = weight -> weight >= 203;
DoublePredicate needToLose = weight -> weight >= 169;
DoublePredicate notHealthy = healthy.negate();
DoublePredicate alsoNotHealthy = underweight.or(overweight).or(obese);
DoublePredicate skipSugar = needToLose.and(overweight.or(obese));

// check need to lose weight
Assertions.assertArrayEquals(
new double[] {200D}, DoubleStream.of(100D, 140D, 160D, 200D).filter(needToLose).toArray());

// check need to lose weight
Assertions.assertArrayEquals(
new double[] {100D, 200D},
DoubleStream.of(100D, 140D, 160D, 200D).filter(notHealthy).toArray());

// check negate()
Assertions.assertArrayEquals(
DoubleStream.of(100D, 140D, 160D, 200D).filter(notHealthy).toArray(),
DoubleStream.of(100D, 140D, 160D, 200D).filter(alsoNotHealthy).toArray());

// check and()
Assertions.assertArrayEquals(
new double[] {200D}, DoubleStream.of(100D, 140D, 160D, 200D).filter(skipSugar).toArray());
}
}

0 comments on commit 1361184

Please sign in to comment.