Skip to content
Snippets Groups Projects
Commit 50cf7a0a authored by Lukáš Panchartek's avatar Lukáš Panchartek
Browse files

add-factorial-test

parent 44af6a29
Branches master
No related tags found
No related merge requests found
......@@ -13,5 +13,13 @@
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package cz.cvut.fel.ts1.homework1;
public class Factorial {
public static long factorial(int n) {
if (n < 0) {
throw new IllegalArgumentException("Factorial is not defined for negative numbers.");
}
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
}
package cz.cvut.fel.ts1.homework1;
import org.junit.jupiter.api.Test;
public class TestFactorial {
@Test
public void testFactorial() {
assert Factorial.factorial(0) == 1;
assert Factorial.factorial(1) == 1;
assert Factorial.factorial(2) == 2;
assert Factorial.factorial(3) == 6;
assert Factorial.factorial(4) == 24;
}
@Test
public void testFactorialNegative() {
try {
assert Factorial.factorial(-1) == 1;
} catch (IllegalArgumentException e) {
assert true;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment