diff --git a/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java b/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java index f6acafd2..574fe5f2 100644 --- a/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java +++ b/src/main/java/com/thealgorithms/maths/FindMaxRecursion.java @@ -1,26 +1,9 @@ package com.thealgorithms.maths; -import java.util.Arrays; -import java.util.Random; +public final class FindMaxRecursion { -public class FindMaxRecursion { - - public static void main(String[] args) { - Random rand = new Random(); - - /* rand size */ - int size = rand.nextInt(100) + 1; - int[] array = new int[size]; - - /* init array with rand numbers */ - for (int i = 0; i < size; i++) { - array[i] = rand.nextInt() % 100; - } - - assert max(array) == Arrays.stream(array).max().getAsInt(); - assert max(array, 0, array.length - 1) == Arrays.stream(array).max().getAsInt(); + private FindMaxRecursion() { } - /** * Get max of array using divide and conquer algorithm * @@ -29,7 +12,10 @@ public class FindMaxRecursion { * @param high the index of the last element * @return max of {@code array} */ - public static int max(int[] array, int low, int high) { + public static int max(final int[] array, final int low, final int high) { + if (array.length == 0) { + throw new IllegalArgumentException("array must be non-empty."); + } if (low == high) { return array[low]; // or array[high] } @@ -48,7 +34,7 @@ public class FindMaxRecursion { * @param array contains elements * @return max value of {@code array} */ - public static int max(int[] array) { - return array.length == 1 ? array[0] : max(array, 0, array.length); + public static int max(final int[] array) { + return max(array, 0, array.length - 1); } } diff --git a/src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java b/src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java new file mode 100644 index 00000000..d54cae67 --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/FindMaxRecursionTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.util.stream.Stream; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class FindMaxRecursionTest { + + @ParameterizedTest + @MethodSource("inputStream") + void numberTests(int expected, int[] input) { + Assertions.assertEquals(expected, FindMaxRecursion.max(input)); + } + + private static Stream inputStream() { + return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(0, new int[] {-1, 0}), Arguments.of(-1, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(9, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3})); + } + + @Test + public void testFindMaxThrowsExceptionForEmptyInput() { + assertThrows(IllegalArgumentException.class, () -> FindMaxRecursion.max(new int[] {})); + } +}