Added tests for FactorialRecursion (#5109)

* Added tests for `FactorialRecursion`

* Apply suggestions from code review

Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>

---------

Co-authored-by: Maria Paszkiewicz SCC <maria.paszkiewicz@kit.edu>
Co-authored-by: Piotr Idzik <65706193+vil02@users.noreply.github.com>
This commit is contained in:
marysiuniq 2024-04-20 20:31:13 +02:00 committed by GitHub
parent 05626f7a79
commit 8129686e2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 29 additions and 10 deletions

View File

@ -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
*

View File

@ -0,0 +1,27 @@
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.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<Arguments> 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 testThrowsForNegativeInput() {
assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
}
}