Started verification tests

This commit is contained in:
2022-12-04 23:27:25 -05:00
parent 62a8589364
commit 7a3f2214f8
20 changed files with 721 additions and 7 deletions

13
pom.xml
View File

@@ -29,6 +29,19 @@
<artifactId>myClasses</artifactId>
<version>1.2.0</version>
</dependency>
<!--Testing framework-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.1</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>4.9.0</version>
</dependency>
</dependencies>
<build>

View File

@@ -19,7 +19,7 @@
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.mattrixwv.project_euler;
package com.mattrixwv.project_euler.exceptions;
public class Unsolved extends RuntimeException{

View File

@@ -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

View File

@@ -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{

View File

@@ -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{

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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());
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}