Added functions to check palindromes and convert to bin

This commit is contained in:
2021-06-29 10:17:41 -04:00
parent 07d32e6b42
commit 721128a276
2 changed files with 127 additions and 18 deletions

View File

@@ -54,15 +54,17 @@ bool testFindMin();
bool testFindMax();
bool testFindNumOccurrence();
bool testFactorial();
bool testIsPalindrome();
bool testToBin();
int main(){
mee::Stopwatch timer;
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};
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin};
std::vector<std::string> names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial"};
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin"};
//Start doing tests and print out the results of each
for(int cnt = 0;cnt < functions.size();++cnt){
@@ -512,67 +514,141 @@ bool testFactorial(){
return true;
}
bool testIsPalindrome(){
//Test 1
std::string str = "101";
bool correctAnswer = true;
bool answer = mee::isPalindrome(str);
if(correctAnswer != answer){
return false;
}
//Test 2
str = "100";
correctAnswer = false;
answer = mee::isPalindrome(str);
if(correctAnswer != answer){
return false;
}
//Test 3
str = "";
correctAnswer = true;
answer = mee::isPalindrome(str);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testToBin(){
//Test 1
int num = 7;
std::string correctAnswer = "111";
std::string answer = mee::toBin(num);
if(correctAnswer != answer){
return false;
}
//Test 2
uint64_t num2 = 8;
correctAnswer = "1000";
answer = mee::toBin(num2);
if(correctAnswer != answer){
return false;
}
//Test 3
num = 0;
correctAnswer = "0";
answer = mee::toBin(num);
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
The test took 0.000 nanoseconds
The test took 14.100 microseconds
Function getNumPrimes() passed the test
The test took 0.000 nanoseconds
The test took 15.100 microseconds
Function isPrime() passed the test
The test took 300.000 nanoseconds
Function getFactors() passed the test
The test took 0.000 nanoseconds
The test took 10.200 microseconds
Function getDivisors() passed the test
The test took 0.000 nanoseconds
The test took 5.800 microseconds
Function getSum() passed the test
The test took 0.000 nanoseconds
The test took 9.300 microseconds
Function getProduct() passed the test
The test took 0.000 nanoseconds
The test took 2.000 microseconds
Function isFound() passed the test
The test took 0.000 nanoseconds
The test took 23.500 microseconds
Function getPermutations() passed the test
The test took 0.000 nanoseconds
The test took 30.100 microseconds
Function getFib() passed the test
The test took 0.000 nanoseconds
The test took 228.800 microseconds
Function getAllFib() passed the test
The test took 0.000 nanoseconds
The test took 9.000 microseconds
Function bubbleSort() passed the test
The test took 1.031 seconds
The test took 995.215 milliseconds
Function quickSort() passed the test
The test took 4.000 milliseconds
The test took 3.559 milliseconds
Function search() passed the test
The test took 0.000 nanoseconds
The test took 2.800 microseconds
Function findMin() passed the test
The test took 0.000 nanoseconds
The test took 2.500 microseconds
Function findMax() passed the test
The test took 0.000 nanoseconds
The test took 2.500 microseconds
Function findNumOccurrence() passed the test
The test took 0.000 nanoseconds
The test took 1.100 microseconds
Function factorial() passed the test
The test took 200.000 nanoseconds
Function isPalindrome() passed the test
The test took 1.500 microseconds
Function toBin() passed the test
The test took 2.100 microseconds
*/