From 002de5674534940035eb1efdbe6f4657586b23fc Mon Sep 17 00:00:00 2001 From: Maria Paszkiewicz SCC Date: Sat, 20 Apr 2024 19:58:32 +0200 Subject: [PATCH 1/2] Added tests for `FactorialRecursion` --- .../maths/FactorialRecursion.java | 12 ++------ .../maths/FactorialRecursionTest.java | 28 +++++++++++++++++++ 2 files changed, 30 insertions(+), 10 deletions(-) create mode 100644 src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java diff --git a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java b/src/main/java/com/thealgorithms/maths/FactorialRecursion.java index 85e03c4dd1a4..d9bafd1e39e9 100644 --- a/src/main/java/com/thealgorithms/maths/FactorialRecursion.java +++ b/src/main/java/com/thealgorithms/maths/FactorialRecursion.java @@ -1,16 +1,8 @@ package com.thealgorithms.maths; -public class FactorialRecursion { - - /* Driver Code */ - public static void main(String[] args) { - assert factorial(0) == 1; - assert factorial(1) == 1; - assert factorial(2) == 2; - assert factorial(3) == 6; - assert factorial(5) == 120; +public final class FactorialRecursion { + private FactorialRecursion() { } - /** * Recursive FactorialRecursion Method * diff --git a/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java b/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java new file mode 100644 index 000000000000..204fc6048d1a --- /dev/null +++ b/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java @@ -0,0 +1,28 @@ +package com.thealgorithms.maths; + +import static org.junit.jupiter.api.Assertions.assertEquals; +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 FactorialRecursionTest { + @ParameterizedTest + @MethodSource("inputStream") + void testFactorialRecursion(long expected, int number) { + assertEquals(expected, FactorialRecursion.factorial(number)); + } + + private static Stream inputStream() { + return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5)); + } + + @Test + void testNegativeNumber() { + Exception exception = assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1)); + } +} From 86bd7202e7eadf6fb15941a3e7697b44457742a2 Mon Sep 17 00:00:00 2001 From: marysiuniq <67139391+marysiuniq@users.noreply.github.com> Date: Sat, 20 Apr 2024 20:20:01 +0200 Subject: [PATCH 2/2] Apply suggestions from code review Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com> --- .../java/com/thealgorithms/maths/FactorialRecursionTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java b/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java index 204fc6048d1a..db18b46356b4 100644 --- a/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java +++ b/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java @@ -4,7 +4,6 @@ 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; @@ -22,7 +21,7 @@ private static Stream inputStream() { } @Test - void testNegativeNumber() { - Exception exception = assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1)); + void testThrowsForNegativeInput() { + assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1)); } }