Skip to content

Commit b96dc2e

Browse files
author
Maria Paszkiewicz SCC
committed
Added tests for FactorialRecursion
1 parent 7201dc7 commit b96dc2e

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-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,28 @@
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.Assertions;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.params.ParameterizedTest;
10+
import org.junit.jupiter.params.provider.Arguments;
11+
import org.junit.jupiter.params.provider.MethodSource;
12+
13+
public class FactorialRecursionTest {
14+
@ParameterizedTest
15+
@MethodSource("inputStream")
16+
void testFactorialRecursion(long expected, int number) {
17+
assertEquals(expected, FactorialRecursion.factorial(number));
18+
}
19+
20+
private static Stream<Arguments> inputStream() {
21+
return Stream.of(Arguments.of(1, 0), Arguments.of(1, 1), Arguments.of(2, 2), Arguments.of(6, 3), Arguments.of(120, 5));
22+
}
23+
24+
@Test
25+
void testNegativeNumber() {
26+
Exception exception = assertThrows(IllegalArgumentException.class, () -> FactorialRecursion.factorial(-1));
27+
}
28+
}

0 commit comments

Comments
 (0)