Added more unit tests

This commit is contained in:
2022-12-05 23:50:14 -05:00
parent 7a3f2214f8
commit 703b79c002
11 changed files with 594 additions and 1 deletions

View File

@@ -36,7 +36,7 @@ public class TestProblem10 extends BaseTest{
//Verify result //Verify result
assertEquals(142913828922L, problem.getSum()); assertEquals(142913828922L, problem.getSum());
assertEquals("The sum of all the primes < 2000000 is 142913828922", problem.getResult()); assertEquals("The sum of all the primes < 2000000 is 142913828922", problem.getResult());
//Verify the porblem variables and functions were called //Verify the problem variables and functions were called
verifyProblem(problem); verifyProblem(problem);
//Verify the problem won't be run again //Verify the problem won't be run again

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 java.util.List;
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 TestProblem11 extends BaseTest{
@InjectMocks
private Problem11 problem;
@Test
@Override
public void testDescription(){
assertEquals("What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getNumbers(); });
assertThrows(Unsolved.class, () -> { problem.getProduct(); });
problem.solve();
//Verify result
assertEquals(List.of(89, 94, 97, 87), problem.getNumbers());
assertEquals(70600674, problem.getProduct());
assertEquals(String.format("The greatest product of 4 numbers in a line is 70600674%nThe numbers are [89, 94, 97, 87]"), 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,64 @@
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 TestProblem12 extends BaseTest{
@InjectMocks
private Problem12 problem;
@Test
@Override
public void testDescription(){
assertEquals("What is the value of the first triangle number to have over 500 divisors?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTriangularNumber(); });
assertThrows(Unsolved.class, () -> { problem.getLastNumberAdded(); });
assertThrows(Unsolved.class, () -> { problem.getDivisorsOfTriangularNumber(); });
assertThrows(Unsolved.class, () -> { problem.getNumberOfDivisors(); });
problem.solve();
//Verify result
assertEquals(76576500, problem.getTriangularNumber());
assertEquals(12375, problem.getLastNumberAdded());
assertEquals(576, problem.getDivisorsOfTriangularNumber().size());
assertEquals(576, problem.getNumberOfDivisors());
assertEquals("The triangular number 76576500 is the sum of all numbers >= 12375 and has 576 divisors", 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 porblem 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 java.math.BigInteger;
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 TestProblem13 extends BaseTest{
@InjectMocks
private Problem13 problem;
@Test
@Override
public void testDescription(){
assertEquals("Work out the first ten digits of the sum of the one-hundred 50-digit numbers", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getNumbers(); });
assertThrows(Unsolved.class, () -> { problem.getSum(); });
problem.solve();
//Verify result
assertEquals(100, problem.getNumbers().size());
assertEquals(new BigInteger("5537376230390876637302048746832985971773659831892672"), problem.getSum());
assertEquals(String.format("The sum of all 100 numbers is 5537376230390876637302048746832985971773659831892672%nThe first 10 digits of the sum of the numbers is 5537376230"), 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 TestProblem14 extends BaseTest{
@InjectMocks
private Problem14 problem;
@Test
@Override
public void testDescription(){
assertEquals("Which starting number, under 1000000, produces the longest chain using the iterative sequence?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getLength(); });
assertThrows(Unsolved.class, () -> { problem.getStartingNumber(); });
problem.solve();
//Verify result
assertEquals(525, problem.getLength());
assertEquals(837799, problem.getStartingNumber());
assertEquals("The number 837799 produced a chain of 525 steps", problem.getResult());
//Verify the porblem 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,61 @@
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 static org.junit.jupiter.api.Assumptions.assumeTrue;
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 TestProblem15 extends BaseTest{
@InjectMocks
private Problem15 problem;
@Test
@Override
public void testDescription(){
assertEquals("How many routes from the top left corner to the bottom right corner are there through a 20x20 grid if you can only move right and down?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
assertThrows(Unsolved.class, () -> { problem.getNumberOfRoutes(); });
problem.solve();
//Verify result
assertEquals(137846528820L, problem.getNumberOfRoutes());
assertEquals("The number of routes is 137846528820", 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(){
assumeTrue(Boolean.valueOf(System.getProperty("longTests")));
problem.reset();
//Verify solved was reset
assertFalse(problem.getSolved());
super.testReset();
//Verify the problem can be solved again
testSolve();
}
}

View File

@@ -0,0 +1,56 @@
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 TestProblem16 extends BaseTest{
@InjectMocks
private Problem16 problem;
@Test
@Override
public void testDescription(){
assertEquals("What is the sum of the digits of the number 2^1000?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getSum(); });
problem.solve();
//Verify result
assertEquals(1366, problem.getSum());
assertEquals(String.format("2^1000 = 10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376%nThe sum of the elements is 1366"), 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,56 @@
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 TestProblem17 extends BaseTest{
@InjectMocks
private Problem17 problem;
@Test
@Override
public void testDescription(){
assertEquals("If all the numbers from 1 to 1000 inclusive were written out in words, how many letters would be used?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getLetterCount(); });
problem.solve();
//Verify result
assertEquals(21124, problem.getLetterCount());
assertEquals("The sum of all the letters in all the numbers 1-1000 is 21124", 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,56 @@
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 TestProblem18 extends BaseTest{
@InjectMocks
private Problem18 problem;
@Test
@Override
public void testDescription(){
assertEquals("Find the maximum total from top to bottom", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTotal(); });
problem.solve();
//Verify result
assertEquals(1074, problem.getTotal());
assertEquals("The value of the longest path is 1074", 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 TestProblem19 extends BaseTest{
@InjectMocks
private Problem19 problem;
@Test
@Override
public void testDescription(){
assertEquals("How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getTotalSundays(); });
problem.solve();
//Verify result
assertEquals(171, problem.getTotalSundays());
assertEquals("There are 171 Sundays that landed on the first of the months from 1901 to 2000", 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 TestProblem20 extends BaseTest{
@InjectMocks
private Problem20 problem;
@Test
@Override
public void testDescription(){
assertEquals("What is the sum of the digits of 100!?", problem.getDescription());
assertFalse(problem.getSolved());
}
@Test
@Override
public void testSolve(){
assertThrows(Unsolved.class, () -> { problem.getNumber(); });
assertThrows(Unsolved.class, () -> { problem.getNumberString(); });
assertThrows(Unsolved.class, () -> { problem.getSum(); });
problem.solve();
//Verify result
assertEquals(648, problem.getSum());
assertEquals(String.format("100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000%nThe sum of the digits is: 648"), 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();
}
}