From 064ca8f59124eb3c792d2202ff12260e87e91552 Mon Sep 17 00:00:00 2001 From: Lukas <142339568+lukasb1b@users.noreply.github.com> Date: Thu, 5 Oct 2023 22:01:27 +0200 Subject: [PATCH] cleanup `FindMinRecursion` (#4568) * Create FindMinRecusionTest.java * Update FindMinRecursion.java * Update FindMinRecursion.java * Update FindMinRecursion.java * Rename FindMinRecusionTest.java to FindMinRecursionTest.java * Update FindMinRecursionTest.java * style: remove unused imports --------- Co-authored-by: vil02 <65706193+vil02@users.noreply.github.com> --- .../thealgorithms/maths/FindMinRecursion.java | 41 ++++--------------- .../maths/FindMinRecursionTest.java | 28 +++++++++++++ 2 files changed, 36 insertions(+), 33 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java diff --git a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java index 71d7795b..6ffe034e 100644 --- a/src/main/java/com/thealgorithms/maths/FindMinRecursion.java +++ b/src/main/java/com/thealgorithms/maths/FindMinRecursion.java @@ -1,38 +1,13 @@ package com.thealgorithms.maths; -import java.util.Arrays; -import java.util.Random; +public final class FindMinRecursion { -public class FindMinRecursion { - - /** - * Driver Code - */ - 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 min(array, 0, array.length - 1) == Arrays.stream(array).min().getAsInt(); - assert min(array) == Arrays.stream(array).min().getAsInt(); + private FindMinRecursion() { } - - /** - * Get min of array using divide and conquer algorithm - * - * @param array contains elements - * @param low the index of the first element - * @param high the index of the last element - * @return min of {@code array} - */ - public static int min(int[] array, int low, int high) { + public static int min(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] } @@ -52,7 +27,7 @@ public class FindMinRecursion { * @param len length of given array * @return min value of {@code array} */ - public static int min(int[] array) { - return array.length == 1 ? array[0] : min(array, 0, array.length); + public static int min(final int[] array) { + return min(array, 0, array.length - 1); } } diff --git a/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java b/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.java new file mode 100644 index 00000000..3c36702b --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/FindMinRecursionTest.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 FindMinRecursionTest { + + @ParameterizedTest + @MethodSource("inputStream") + void numberTests(int expected, int[] input) { + Assertions.assertEquals(expected, FindMinRecursion.min(input)); + } + + private static Stream inputStream() { + return Stream.of(Arguments.of(5, new int[] {5, 5, 5, 5, 5}), Arguments.of(-1, new int[] {-1, 0}), Arguments.of(-10, new int[] {-10, -9, -8, -7, -6, -5, -4, -3, -2, -1}), Arguments.of(-4, new int[] {3, -2, 3, 9, -4, -4, 8}), Arguments.of(3, new int[] {3})); + } + + @Test + public void testFindMaxThrowsExceptionForEmptyInput() { + assertThrows(IllegalArgumentException.class, () -> FindMinRecursion.min(new int[] {})); + } +}