forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorialTest.java
More file actions
43 lines (35 loc) · 1.32 KB
/
FactorialTest.java
File metadata and controls
43 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.thealgorithms.maths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigInteger;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
/**
* Unit tests for Factorial.
*/
class FactorialTest {
private static final String EXCEPTION_MESSAGE = "Input number cannot be negative";
@Test
@DisplayName("Factorial should throw exception for negative input")
void testWhenInvalidInputProvidedShouldThrowException() {
IllegalArgumentException exception =
assertThrows(IllegalArgumentException.class, () -> Factorial.factorial(-1));
assertEquals(EXCEPTION_MESSAGE, exception.getMessage());
}
@Test
@DisplayName("Factorial should return correct values for small numbers")
void testCorrectFactorialCalculation() {
assertEquals(1, Factorial.factorial(0));
assertEquals(1, Factorial.factorial(1));
assertEquals(120, Factorial.factorial(5));
assertEquals(3_628_800, Factorial.factorial(10));
}
@Test
@DisplayName("Factorial should correctly calculate large values")
void testLargeFactorial() {
assertEquals(
new BigInteger("2432902008176640000"),
Factorial.factorial(20)
);
}
}