diff --git a/pom.xml b/pom.xml index da7d171..47124df 100644 --- a/pom.xml +++ b/pom.xml @@ -29,6 +29,19 @@ myClasses 1.2.0 + + + + org.junit.jupiter + junit-jupiter-api + 5.9.1 + + + + org.mockito + mockito-junit-jupiter + 4.9.0 + diff --git a/src/main/java/com/mattrixwv/project_euler/Unsolved.java b/src/main/java/com/mattrixwv/project_euler/exceptions/Unsolved.java similarity index 95% rename from src/main/java/com/mattrixwv/project_euler/Unsolved.java rename to src/main/java/com/mattrixwv/project_euler/exceptions/Unsolved.java index 1b5da23..f1de7ac 100644 --- a/src/main/java/com/mattrixwv/project_euler/Unsolved.java +++ b/src/main/java/com/mattrixwv/project_euler/exceptions/Unsolved.java @@ -19,7 +19,7 @@ You should have received a copy of the GNU Lesser General Public License along with this program. If not, see . */ -package com.mattrixwv.project_euler; +package com.mattrixwv.project_euler.exceptions; public class Unsolved extends RuntimeException{ diff --git a/src/main/java/com/mattrixwv/project_euler/problems/Problem.java b/src/main/java/com/mattrixwv/project_euler/problems/Problem.java index 793f866..bce685d 100644 --- a/src/main/java/com/mattrixwv/project_euler/problems/Problem.java +++ b/src/main/java/com/mattrixwv/project_euler/problems/Problem.java @@ -24,14 +24,13 @@ package com.mattrixwv.project_euler.problems; import com.mattrixwv.Stopwatch; import com.mattrixwv.exceptions.InvalidResult; - -import com.mattrixwv.project_euler.Unsolved; +import com.mattrixwv.project_euler.exceptions.Unsolved; public abstract class Problem{ //Variables //Instance variables - protected final Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm + protected Stopwatch timer = new Stopwatch(); //To time how long it takes to run the algorithm private final String description; //Holds the description of the problem protected boolean solved; //Shows whether the problem has already been solved @@ -62,11 +61,14 @@ public abstract class Problem{ } return timer; } - void solvedCheck(String str){ + protected void solvedCheck(String str){ if(!solved){ throw new Unsolved("You must solve the problem before you can see the " + str); } } + public boolean getSolved(){ + return solved; + } //Solve the problem public abstract void solve() throws InvalidResult; //Reset the problem so it can be run again diff --git a/src/main/java/com/mattrixwv/project_euler/problems/Problem17.java b/src/main/java/com/mattrixwv/project_euler/problems/Problem17.java index 157fb10..675c857 100644 --- a/src/main/java/com/mattrixwv/project_euler/problems/Problem17.java +++ b/src/main/java/com/mattrixwv/project_euler/problems/Problem17.java @@ -24,7 +24,7 @@ package com.mattrixwv.project_euler.problems; import com.mattrixwv.exceptions.InvalidResult; -import com.mattrixwv.project_euler.Unsolved; +import com.mattrixwv.project_euler.exceptions.Unsolved; public class Problem17 extends Problem{ diff --git a/src/main/java/com/mattrixwv/project_euler/problems/Problem9.java b/src/main/java/com/mattrixwv/project_euler/problems/Problem9.java index 8886b19..604d660 100644 --- a/src/main/java/com/mattrixwv/project_euler/problems/Problem9.java +++ b/src/main/java/com/mattrixwv/project_euler/problems/Problem9.java @@ -23,7 +23,7 @@ package com.mattrixwv.project_euler.problems; -import com.mattrixwv.project_euler.Unsolved; +import com.mattrixwv.project_euler.exceptions.Unsolved; public class Problem9 extends Problem{ diff --git a/src/test/java/com/mattrixwv/project_euler/TestBenchmark.java b/src/test/java/com/mattrixwv/project_euler/TestBenchmark.java new file mode 100644 index 0000000..ea780d8 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/TestBenchmark.java @@ -0,0 +1,14 @@ +package com.mattrixwv.project_euler; + + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + + +public class TestBenchmark{ + @Test + public void testBenchmark(){ + assertTrue(true); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/TestDriver.java b/src/test/java/com/mattrixwv/project_euler/TestDriver.java new file mode 100644 index 0000000..5330264 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/TestDriver.java @@ -0,0 +1,14 @@ +package com.mattrixwv.project_euler; + + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + + +public class TestDriver{ + @Test + public void testDriver(){ + assertTrue(true); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/TestProblemSelection.java b/src/test/java/com/mattrixwv/project_euler/TestProblemSelection.java new file mode 100644 index 0000000..180b287 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/TestProblemSelection.java @@ -0,0 +1,12 @@ +package com.mattrixwv.project_euler; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +public class TestProblemSelection{ + @Test + public void testProblemSelection(){ + assertTrue(true); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/exceptions/TestUnsolved.java b/src/test/java/com/mattrixwv/project_euler/exceptions/TestUnsolved.java new file mode 100644 index 0000000..03cf1bf --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/exceptions/TestUnsolved.java @@ -0,0 +1,20 @@ +package com.mattrixwv.project_euler.exceptions; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.junit.jupiter.api.Test; + + +public class TestUnsolved{ + @Test + public void testUnsolved(){ + Unsolved unsolved = new Unsolved(); + assertNull(unsolved.getMessage()); + + String testMessage = "Test message"; + unsolved = new Unsolved(testMessage); + assertEquals(testMessage, unsolved.getMessage()); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/BaseTest.java b/src/test/java/com/mattrixwv/project_euler/problems/BaseTest.java new file mode 100644 index 0000000..47401e0 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/BaseTest.java @@ -0,0 +1,42 @@ +package com.mattrixwv.project_euler.problems; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.atMostOnce; +import static org.mockito.Mockito.verify; + +import org.mockito.Spy; + +import com.mattrixwv.Stopwatch; + +public abstract class BaseTest{ + @Spy + protected Stopwatch timer; + + public abstract void testDescription(); + + public abstract void testSolve(); + + protected void verifyProblem(Problem problem){ + //Verify solved was set + assertTrue(problem.getSolved()); + //Verify timer function were called + verify(timer, atLeastOnce()).start(); + verify(timer, atLeastOnce()).stop(); + } + + protected void verifyProblemSecondRun(Problem problem){ + problem.solve(); + + //Verify solved is still set + assertTrue(problem.getSolved()); + //Verify timer functions were not called again + verify(timer, atMostOnce()).start(); + verify(timer, atMostOnce()).stop(); + } + + public void testReset(){ + //Verify timer was reset + verify(timer, atLeastOnce()).reset(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem1.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem1.java new file mode 100644 index 0000000..3a606f4 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem1.java @@ -0,0 +1,57 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem1 extends BaseTest{ + @InjectMocks + private Problem1 problem = new Problem1(); + + @Test + public void testDescription(){ + assertEquals("What is the sum of all the multiples of 3 or 5 that are less than 1000?", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getResult(); }); + assertThrows(Unsolved.class, () -> { problem.getSum(); }); + + problem.solve(); + + //Verify result + assertEquals(233168, problem.getSum()); + assertEquals("The sum of all numbers < 1000 is 233168", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem10.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem10.java new file mode 100644 index 0000000..ea10a3d --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem10.java @@ -0,0 +1,58 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem10 extends BaseTest{ + @InjectMocks + private Problem10 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("Find the sum of all the primes below 2000000.", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getSum(); }); + + problem.solve(); + + //Verify result + assertEquals(142913828922L, problem.getSum()); + assertEquals("The sum of all the primes < 2000000 is 142913828922", problem.getResult()); + //Verify the porblem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem2.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem2.java new file mode 100644 index 0000000..beb9047 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem2.java @@ -0,0 +1,59 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem2 extends BaseTest{ + @InjectMocks + private Problem2 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("What is the sum of the even Fibonacci numbers less than 4000000?", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getResult(); }); + assertThrows(Unsolved.class, () -> { problem.getSum(); }); + + problem.solve(); + + //Verify result + assertEquals(4613732, problem.getSum()); + assertEquals("The sum of all even fibonacci numbers <= 3999999 is 4613732", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem3.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem3.java new file mode 100644 index 0000000..3b42745 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem3.java @@ -0,0 +1,63 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.NumberAlgorithms; +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem3 extends BaseTest{ + @InjectMocks + private Problem3 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("What is the largest prime factor of 600851475143?", problem.getDescription()); + assertEquals(600851475143L, Problem3.getGoalNumber()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getResult(); }); + assertThrows(Unsolved.class, () -> { problem.getFactors(); }); + assertThrows(Unsolved.class, () -> { problem.getLargestFactor(); }); + + problem.solve(); + + //Verify result + assertEquals(NumberAlgorithms.getFactors(600851475143L), problem.getFactors()); + assertEquals(6857, problem.getLargestFactor()); + assertEquals("The largest factor of the number 600851475143 is 6857", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem4.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem4.java new file mode 100644 index 0000000..c018139 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem4.java @@ -0,0 +1,60 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem4 extends BaseTest{ + @InjectMocks + private Problem4 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("Find the largest palindrome made from the product of two 3-digit numbers", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getPalindromes(); }); + assertThrows(Unsolved.class, () -> { problem.getLargestPalindrome(); }); + + problem.solve(); + + //verify result + assertEquals(1239, problem.getPalindromes().size()); + assertEquals(906609, problem.getLargestPalindrome()); + assertEquals("The largest palindrome is 906609", problem.getResult()); + //verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem5.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem5.java new file mode 100644 index 0000000..d262aea --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem5.java @@ -0,0 +1,58 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem5 extends BaseTest{ + @InjectMocks + private Problem5 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getNumber(); }); + + problem.solve(); + + //Verify result + assertEquals(232792560, problem.getNumber()); + assertEquals("The smallest positive number evenly divisible by all numbers 1-20 is 232792560", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem6.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem6.java new file mode 100644 index 0000000..234c5d6 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem6.java @@ -0,0 +1,62 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem6 extends BaseTest{ + @InjectMocks + private Problem6 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("Find the difference between the sum of the squares and the square of the sum of the numbers 1-100.", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getSumOfSquares(); }); + assertThrows(Unsolved.class, () -> { problem.getSquareOfSum(); }); + assertThrows(Unsolved.class, () -> { problem.getDifference(); }); + + problem.solve(); + + //Verify result + assertEquals(338350, problem.getSumOfSquares()); + assertEquals(25502500, problem.getSquareOfSum()); + assertEquals(25164150, problem.getDifference()); + assertEquals("The difference between the sum of the squares and the square of the sum of all numbers from 1-100 is 25164150", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem7.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem7.java new file mode 100644 index 0000000..d78dd84 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem7.java @@ -0,0 +1,58 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem7 extends BaseTest{ + @InjectMocks + private Problem7 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("What is the 10001th prime number?", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getPrime(); }); + + problem.solve(); + + //Verify result + assertEquals(104743, problem.getPrime()); + assertEquals("The 10001th prime number is 104743", problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem8.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem8.java new file mode 100644 index 0000000..1f03aaa --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem8.java @@ -0,0 +1,60 @@ +package com.mattrixwv.project_euler.problems; + + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + + +@ExtendWith(MockitoExtension.class) +public class TestProblem8 extends BaseTest{ + @InjectMocks + private Problem8 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getLargestNums(); }); + assertThrows(Unsolved.class, () -> { problem.getLargestProduct(); }); + + problem.solve(); + + //Verify result + assertEquals("5576689664895", problem.getLargestNums()); + assertEquals(23514624000L, problem.getLargestProduct()); + assertEquals(String.format("The greatest product is 23514624000%nThe numbers are 5576689664895"), problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the porblem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +} diff --git a/src/test/java/com/mattrixwv/project_euler/problems/TestProblem9.java b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem9.java new file mode 100644 index 0000000..d33f052 --- /dev/null +++ b/src/test/java/com/mattrixwv/project_euler/problems/TestProblem9.java @@ -0,0 +1,62 @@ +package com.mattrixwv.project_euler.problems; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.junit.jupiter.MockitoExtension; + +import com.mattrixwv.project_euler.exceptions.Unsolved; + +@ExtendWith(MockitoExtension.class) +public class TestProblem9 extends BaseTest{ + @InjectMocks + private Problem9 problem; + + + @Test + @Override + public void testDescription(){ + assertEquals("There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc.", problem.getDescription()); + assertFalse(problem.getSolved()); + } + + @Test + @Override + public void testSolve(){ + assertThrows(Unsolved.class, () -> { problem.getSideA(); }); + assertThrows(Unsolved.class, () -> { problem.getSideB(); }); + assertThrows(Unsolved.class, () -> { problem.getSideC(); }); + assertThrows(Unsolved.class, () -> { problem.getProduct(); }); + + problem.solve(); + + //Verify result + assertEquals(200, problem.getSideA()); + assertEquals(375, problem.getSideB()); + assertEquals(425, problem.getSideC()); + assertEquals(31875000, problem.getProduct()); + assertEquals(String.format("The Pythagorean triplet is 200 + 375 + 425%nThe numbers' product is 31875000"), problem.getResult()); + //Verify the problem variables and functions were called + verifyProblem(problem); + + //Verify the problem won't be run again + verifyProblemSecondRun(problem); + } + + @Test + @Override + public void testReset(){ + problem.reset(); + + //Verify solved was reset + assertFalse(problem.getSolved()); + super.testReset(); + + //Verify the problem can be solved again + testSolve(); + } +}