|
| 1 | +package com.thealgorithms.maths; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 5 | + |
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.api.Assertions; |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.Arguments; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
| 12 | + |
| 13 | +public class SecondMinMaxTest { |
| 14 | + |
| 15 | + private static final String EXP_MSG_ARR_LEN_LESS_2 = "Input array must have length of at least two"; |
| 16 | + private static final String EXP_MSG_ARR_SAME_ELE = "Input array should have at least 2 distinct elements"; |
| 17 | + |
| 18 | + public static class TestCase { |
| 19 | + public TestCase(final int[] inInputArray, final int inSecondMin, final int inSecondMax) { |
| 20 | + inputArray = inInputArray; |
| 21 | + secondMin = inSecondMin; |
| 22 | + secondMax = inSecondMax; |
| 23 | + } |
| 24 | + final int[] inputArray; |
| 25 | + final int secondMin; |
| 26 | + final int secondMax; |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void testForEmptyInputArray() { |
| 31 | + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {})); |
| 32 | + assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2); |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testForArrayWithSingleElement() { |
| 37 | + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMax(new int[] {1})); |
| 38 | + assertEquals(exception.getMessage(), EXP_MSG_ARR_LEN_LESS_2); |
| 39 | + } |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testForArrayWithSameElements() { |
| 43 | + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SecondMinMax.findSecondMin(new int[] {1, 1, 1, 1})); |
| 44 | + assertEquals(exception.getMessage(), EXP_MSG_ARR_SAME_ELE); |
| 45 | + } |
| 46 | + |
| 47 | + @ParameterizedTest |
| 48 | + @MethodSource("inputStream") |
| 49 | + void numberTests(final TestCase tc) { |
| 50 | + Assertions.assertEquals(tc.secondMax, SecondMinMax.findSecondMax(tc.inputArray)); |
| 51 | + Assertions.assertEquals(tc.secondMin, SecondMinMax.findSecondMin(tc.inputArray)); |
| 52 | + } |
| 53 | + |
| 54 | + private static Stream<Arguments> inputStream() { |
| 55 | + return Stream.of(Arguments.of(new TestCase(new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 2, 9)), Arguments.of(new TestCase(new int[] {5, 4, 5, 5, 5}, 5, 4)), Arguments.of(new TestCase(new int[] {-1, 0}, 0, -1)), |
| 56 | + Arguments.of(new TestCase(new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}, -9, -2)), Arguments.of(new TestCase(new int[] {3, -2, 3, 9, -4, -4, 8}, -2, 8))); |
| 57 | + } |
| 58 | +} |
0 commit comments