Skip to content

Commit 8129686

Browse files
marysiuniqMaria Paszkiewicz SCCvil02
authored
Added tests for FactorialRecursion (#5109)
* Added tests for `FactorialRecursion` * Apply suggestions from code review Co-authored-by: Piotr Idzik <[email protected]> --------- Co-authored-by: Maria Paszkiewicz SCC <[email protected]> Co-authored-by: Piotr Idzik <[email protected]>
1 parent 05626f7 commit 8129686

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

src/main/java/com/thealgorithms/maths/FactorialRecursion.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package com.thealgorithms.maths;
22

3-
public class FactorialRecursion {
4-
5-
/* Driver Code */
6-
public static void main(String[] args) {
7-
assert factorial(0) == 1;
8-
assert factorial(1) == 1;
9-
assert factorial(2) == 2;
10-
assert factorial(3) == 6;
11-
assert factorial(5) == 120;
3+
public final class FactorialRecursion {
4+
private FactorialRecursion() {
125
}
13-
146
/**
157
* Recursive FactorialRecursion Method
168
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.Test;
8+
import org.junit.jupiter.params.ParameterizedTest;
9+
import org.junit.jupiter.params.provider.Arguments;
10+
import org.junit.jupiter.params.provider.MethodSource;
11+
12+
public class FactorialRecursionTest {
13+
@ParameterizedTest
14+
@MethodSource("inputStream")
15+
void testFactorialRecursion(long expected, int number) {
16+
assertEquals(expected, FactorialRecursion.factorial(number));
17+
}
18+
19+
private static Stream<Arguments> inputStream() {
20+
return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
21+
}
22+
23+
@Test
24+
void testThrowsForNegativeInput() {
25+
assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
26+
}
27+
}

0 commit comments

Comments
 (0)