Add tests for factorial example. (#112)

This commit is contained in:
Michael Mkwelele
2019-11-12 07:18:52 -08:00
committed by Aditya Bhargava
parent 4c3bc702f4
commit 184f80127c
2 changed files with 41 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
public class Factorial2 {
public static void main(String[] args) {
Factorial2 factorial2 = new Factorial2();
System.out.println("The factorial of 5 is " + factorial2.getFactorial(5));
}
public int getFactorial(int number) {
if (isZeroOrOne(number)) {
return 1;
}
return number * getFactorial(number - 1);
}
public boolean isZeroOrOne(int number) {
if (number > 1) {
return false;
}
return true;
}
}

View File

@@ -0,0 +1,20 @@
import org.junit.Assert;
import org.junit.Test;
public class Factorial2Test {
@Test
public void testIsZeroOrOne() {
Factorial2 factorial2 = new Factorial2();
Assert.assertEquals(true, factorial2.isZeroOrOne(0));
Assert.assertEquals(true, factorial2.isZeroOrOne(1));
Assert.assertEquals(false, factorial2.isZeroOrOne(5));
}
@Test
public void testFactorial() {
Factorial2 factorial2 = new Factorial2();
Assert.assertEquals(120, factorial2.getFactorial(5));
}
}