[calc-percentile] add 0 and 100 boundary test cases

This commit is contained in:
Kai.Yuan 2024-03-15 19:47:55 +01:00
parent 4c48476258
commit 2195e2c404
1 changed files with 7 additions and 4 deletions

View File

@ -9,7 +9,6 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.LongStream;
import org.junit.jupiter.api.Test;
@ -40,36 +39,40 @@ public class CalculatePercentileUnitTest {
.collect(Collectors.toList());
Collections.shuffle(list100);
assertEquals(1, getPercentile(list100, 0));
assertEquals(10, getPercentile(list100, 10));
assertEquals(25, getPercentile(list100, 25));
assertEquals(50, getPercentile(list100, 50));
assertEquals(76, getPercentile(list100, 75.3));
assertEquals(100, getPercentile(list100, 100));
List<Integer> list8 = IntStream.of(-1, 200, 30, 42, -5, 7, 8, 92)
.boxed()
.collect(Collectors.toList());
assertEquals(-5, getPercentile(list8, 0));
assertEquals(-5, getPercentile(list8, 10));
assertEquals(-1, getPercentile(list8, 25));
assertEquals(8, getPercentile(list8, 50));
assertEquals(92, getPercentile(list8, 75.3));
assertEquals(200, getPercentile(list8, 100));
}
@Test
void whenCallingGetPercentileWithAnArray_thenGetExpectedResult() {
//prepare the input array
long[] theArray = LongStream.of(-1, 200, 30, 42, -5, 7, 8, 92)
.toArray();
long[] theArray = new long[] { -1, 200, 30, 42, -5, 7, 8, 92 };
//convert the long[] array to a List<Long>
List<Long> list8 = Arrays.stream(theArray)
.boxed()
.toList();
assertEquals(-5, getPercentile(list8, 0));
assertEquals(-5, getPercentile(list8, 10));
assertEquals(-1, getPercentile(list8, 25));
assertEquals(8, getPercentile(list8, 50));
assertEquals(92, getPercentile(list8, 75.3));
assertEquals(200, getPercentile(list8, 100));
}
}