mirror of
https://bitbucket.org/Mattrixwv/my-classes.git
synced 2025-12-06 18:23:57 -05:00
Added tests so all functions in Algorithms.hpp are tested
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//myClasses/Algorithms.cpp
|
||||
//Matthew Ellison
|
||||
// Created: 11-14-18
|
||||
//Modified: 12-10-18
|
||||
//Modified: 02-28-19
|
||||
//This is the file that tests all my algorithms, both for speed and accuracy
|
||||
/*
|
||||
Copyright (C) 2018 Matthew Ellison
|
||||
Copyright (C) 2019 Matthew Ellison
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
@@ -37,11 +37,15 @@ typedef bool (*boolFn)();
|
||||
//A function to test every set of funtions in Algorithms.hpp
|
||||
//Some of the functions are overloaded, so I test all overloaded functions in each function
|
||||
bool testGetPrimes();
|
||||
bool testGetNumPrimes();
|
||||
bool testGetFactors();
|
||||
bool tetsGetDivisors();
|
||||
bool testGetSum();
|
||||
bool testGetProduct();
|
||||
bool testIsFound();
|
||||
bool testGetPermutations();
|
||||
bool testGetFib();
|
||||
bool testGetAllFib();
|
||||
bool testBubbleSort();
|
||||
bool testQuickSort();
|
||||
bool testSearch();
|
||||
@@ -52,23 +56,23 @@ bool testFindMax();
|
||||
int main(){
|
||||
mee::Stopwatch timer;
|
||||
bool passedTest = false;
|
||||
std::vector<boolFn> functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib,
|
||||
testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax};
|
||||
std::vector<std::string> names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib",
|
||||
"testBubbleSort", "testQuickSort", "testSearch", "testFindMin", "testFindMax"};
|
||||
//Start doing tests and print out the results of each
|
||||
std::vector<boolFn> functions {testGetPrimes, testGetNumPrimes, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations,
|
||||
testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax};
|
||||
std::vector<std::string> names {"getPrimes", "getNumPrimes", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations",
|
||||
"getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax"};
|
||||
|
||||
//Start doing tests and print out the results of each
|
||||
for(int cnt = 0;cnt < functions.size();++cnt){
|
||||
timer.start();
|
||||
passedTest = functions[cnt]();
|
||||
timer.stop();
|
||||
if(passedTest){
|
||||
std::cout << "Function " << names.at(cnt) << "() passed the test" << std::endl;
|
||||
std::cout << "Function " << names.at(cnt) << "() passed the test\n";
|
||||
}
|
||||
else{
|
||||
std::cout << "Function " << names.at(cnt) << "() failed the test" << std::endl;
|
||||
std::cout << "Function " << names.at(cnt) << "() failed the test\n";
|
||||
}
|
||||
std::cout << "The test took " << timer.getMilli() << " milliseconds\n\n" << std::endl;
|
||||
std::cout << "The test took " << timer.getStr() << "\n\n" << std::endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -86,6 +90,37 @@ bool testGetPrimes(){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testGetNumPrimes(){
|
||||
std::vector<uint64_t> 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};
|
||||
uint64_t numPrimes = 25;
|
||||
std::vector<uint64_t> answer = mee::getNumPrimes(numPrimes);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If the false was not triggered it must have passed all tests
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testGetFactors(){
|
||||
std::vector<uint64_t> correctAnswer {2, 2, 5, 5};
|
||||
uint64_t number = 100;
|
||||
std::vector<uint64_t> answer = mee::getFactors(number);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
correctAnswer = {2, 7, 7};
|
||||
number = 98;
|
||||
answer = mee::getFactors(number);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If a false was not triggered it must have passed all tests
|
||||
return true;
|
||||
}
|
||||
|
||||
bool tetsGetDivisors(){
|
||||
std::vector<uint64_t> correctAnswer {1, 2, 4, 5, 10, 20, 25, 50, 100};
|
||||
uint64_t topNum = 100;
|
||||
@@ -110,6 +145,25 @@ bool testGetSum(){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testGetProduct(){
|
||||
uint64_t correctAnswer = 0;
|
||||
std::vector<uint64_t> numbers = {};
|
||||
uint64_t answer = mee::getProduct(numbers);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
correctAnswer = 57600;
|
||||
numbers = {2, 2, 3, 3, 4, 4, 100};
|
||||
answer = mee::getProduct(numbers);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If a false was not triggered it must have passed all tests
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testIsFound(){
|
||||
bool correctAnswer = true;
|
||||
std::vector<uint64_t> testVector = mee::getPrimes((uint64_t)100);
|
||||
@@ -192,6 +246,25 @@ bool testGetFib(){
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testGetAllFib(){
|
||||
std::vector<uint64_t> correctAnswer {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89};
|
||||
uint64_t highestNumber = 100;
|
||||
std::vector<uint64_t> answer = mee::getAllFib(highestNumber);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
correctAnswer = {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987};
|
||||
highestNumber = 1000;
|
||||
answer = mee::getAllFib(highestNumber);
|
||||
if(correctAnswer != answer){
|
||||
return false;
|
||||
}
|
||||
|
||||
//If a false was not triggered it must have passed all tests
|
||||
return true;
|
||||
}
|
||||
|
||||
bool testBubbleSort(){
|
||||
unsigned int NUM_TO_GENERATE = 10000;
|
||||
std::default_random_engine generator(std::random_device{}());
|
||||
@@ -352,26 +425,62 @@ bool testFindMax(){
|
||||
}
|
||||
|
||||
/* Results:
|
||||
Function testGetPrimes() passed the test
|
||||
The test took 0.013203 milliseconds
|
||||
Function getPrimes() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function testGetDivisors() passed the test
|
||||
The test took 0.005816 milliseconds
|
||||
Function getNumPrimes() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function testGetSum() passed the test
|
||||
The test took 0.00935 milliseconds
|
||||
Function getFactors() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function testIsFound() passed the test
|
||||
The test took 0.010102 milliseconds
|
||||
Function getDivisors() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function testGetPermutations() passed the test
|
||||
The test took 0.0113 milliseconds
|
||||
Function getSum() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function testGetFib() passed the test
|
||||
The test took 0.245473 milliseconds
|
||||
Function getProduct() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function isFound() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function getPermutations() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function getFib() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function getAllFib() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function bubbleSort() passed the test
|
||||
The test took 142.002 milliseconds
|
||||
|
||||
|
||||
Function quickSort() passed the test
|
||||
The test took 1.003 milliseconds
|
||||
|
||||
|
||||
Function search() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function findMin() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
|
||||
|
||||
Function findMax() passed the test
|
||||
The test took 0.000 nanoseconds
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user