- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathSecondMinMaxTest.java
58 lines (48 loc) · 2.53 KB
/
SecondMinMaxTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
packagecom.thealgorithms.maths;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
importjava.util.stream.Stream;
importorg.junit.jupiter.api.Assertions;
importorg.junit.jupiter.api.Test;
importorg.junit.jupiter.params.ParameterizedTest;
importorg.junit.jupiter.params.provider.Arguments;
importorg.junit.jupiter.params.provider.MethodSource;
publicclassSecondMinMaxTest {
privatestaticfinalStringEXP_MSG_ARR_LEN_LESS_2 = "Input array must have length of at least two";
privatestaticfinalStringEXP_MSG_ARR_SAME_ELE = "Input array should have at least 2 distinct elements";
publicstaticclassTestCase {
publicTestCase(finalint[] inInputArray, finalintinSecondMin, finalintinSecondMax) {
inputArray = inInputArray;
secondMin = inSecondMin;
secondMax = inSecondMax;
}
finalint[] inputArray;
finalintsecondMin;
finalintsecondMax;
}
@Test
publicvoidtestForEmptyInputArray() {
IllegalArgumentExceptionexception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(newint[] {}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
}
@Test
publicvoidtestForArrayWithSingleElement() {
IllegalArgumentExceptionexception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMax(newint[] {1}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2);
}
@Test
publicvoidtestForArrayWithSameElements() {
IllegalArgumentExceptionexception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(newint[] {1, 1, 1, 1}));
assertEquals(exception.getMessage(), EXP_MSG_ARR_SAME_ELE);
}
@ParameterizedTest
@MethodSource("inputStream")
voidnumberTests(finalTestCasetc) {
Assertions.assertEquals(tc.secondMax, SecondMinMax.findSecondMax(tc.inputArray));
Assertions.assertEquals(tc.secondMin, SecondMinMax.findSecondMin(tc.inputArray));
}
privatestaticStream<Arguments> inputStream() {
returnStream.of(Arguments.of(newTestCase(newint[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2, 9)), Arguments.of(newTestCase(newint[] {5, 4, 5, 5, 5}, 5, 4)), Arguments.of(newTestCase(newint[] {-1, 0}, 0, -1)),
Arguments.of(newTestCase(newint[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}, -9, -2)), Arguments.of(newTestCase(newint[] {3, -2, 3, 9, -4, -4, 8}, -2, 8)));
}
}