Added tests for Sieve of Eratosthenes

This commit is contained in:
2021-06-30 17:14:56 -04:00
parent 83c20463de
commit 279528fe28
2 changed files with 75 additions and 4 deletions

View File

@@ -57,6 +57,7 @@ bool testFactorial();
bool testIsPalindrome();
bool testToBin();
bool testPrintVector();
bool testSieveOfEratosthenes();
int main(){
@@ -64,10 +65,10 @@ int main(){
bool passedTest = false;
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin,
testPrintVector};
testPrintVector, testSieveOfEratosthenes};
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin",
"testPrintVector"};
"testPrintVector", "testSieveOfEratosthenes"};
//Start doing tests and print out the results of each
for(int cnt = 0;cnt < functions.size();++cnt){
@@ -633,6 +634,35 @@ bool testPrintVector(){
return true;
}
bool testSieveOfEratosthenes(){
//Test 1
mee::SieveOfEratosthenes<int> sieve;
std::vector<int> correctAnswer{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
std::vector<int> answer;
for(int cnt = 0;cnt < 25;++cnt){
int prime = sieve.next();
answer.push_back(prime);
}
if(correctAnswer != answer){
return false;
}
//Test 2
mee::SieveOfEratosthenesAlt<mpz_class> mpzSieve;
std::vector<mpz_class> bigCorrectAnswer{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
std::vector<mpz_class> bigAnswer;
for(int cnt = 0;cnt < 25;++cnt){
mpz_class prime = mpzSieve.next();
bigAnswer.push_back(prime);
}
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
/* Results:
Function getPrimes() passed the test