Split up algorithms to several sub files

This commit is contained in:
2021-07-02 20:50:28 -04:00
parent eba3d9e1f8
commit 5cf20b539a
14 changed files with 1527 additions and 1442 deletions

95
test/mee/testDice.cpp Normal file
View File

@@ -0,0 +1,95 @@
//myClasses/testDice.cpp
//Matthew Ellison
// Created: 1-26-19
//Modified: 1-26-19
//This file is a simple program to test the Dice class
/*
Copyright (C) 2018 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include <iostream>
#include <vector>
#include <cinttypes>
#include "Dice.hpp"
const uint64_t LENGTH_OF_TEST = 100; //How many times the dice will get rolled * the number of sides
int main(){
//Check the default constructor
std::cout << "Checking the default constructor of the Dice class:\n";
mee::Dice<int> die1;
if(die1.getSides() == 6){
std::cout << "Default constructor passes the test\n";
}
else{
std::cout << "Default constructor has an incorrect number of sides\n";
}
//Check the constructor with a high side number
std::cout << "\nChecking a constructor with a high number of sides\n";
mee::Dice<uint64_t> die2(50);
if(die2.getSides() == 50){
std::cout << "Parameterized constructor passed the test\n";
}
else{
std::cout << "Parameterized constructor has an incorrect number of sides\n";
}
//Run a long test to see how the rolls balance out for the default constructor
std::cout << "\nStarting test for the default constructor die:\n";
//Setup an array to track how many times a number has been rolled
std::vector<unsigned int> die1Rolls;
die1Rolls.reserve(die1.getSides());
for(int cnt = 0;cnt < die1.getSides();++cnt){
die1Rolls.push_back(0);
}
//A loop to roll the die and record the results
for(uint64_t cnt = 0;cnt < (LENGTH_OF_TEST * die1.getSides());++cnt){
uint64_t num = die1.roll();
++die1Rolls[num - 1]; //num - 1 to account for dice starting at 1 and array starting at 0
}
//Print out the results
for(size_t cnt = 0;cnt < die1Rolls.size();++cnt){
std::cout << cnt + 1 << ". " << die1Rolls[cnt] << '\n';
}
//Run a long test to see how the rolls balance out for the parameterized constructor
std::cout << "\nStarting test for the parameterized constructor die:\n";
//Setup an array to track how many times a number has been rolled
std::vector<unsigned int> die2Rolls;
die2Rolls.reserve(die2.getSides());
for(uint64_t cnt = 0;cnt < die2.getSides();++cnt){
die2Rolls.push_back(0);
}
//A loop to roll the die and record the results
for(uint64_t cnt = 0;cnt < (LENGTH_OF_TEST * die2.getSides());++cnt){
int num = die2.roll();
++die2Rolls[num - 1]; //num - 1 to account for dice starting at 1 and array starting at 0
}
//Print out the results
for(uint64_t cnt = 0;cnt < die2Rolls.size();++cnt){
std::cout << cnt + 1 << ". " << die2Rolls[cnt] << '\n';
}
std::cout << "Test of die class completed!" << std::endl;
return 0;
}
/*Results:
*/

View File

@@ -0,0 +1,328 @@
//myClasses/test/mee/testNumberAlgorithms.cpp
//Matthew Ellison
// Created: 07-02-21
//Modified: 07-02-21
//This file contains tests for the functions in my numberAlgorithms library
/*
Copyright (C) 2021 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include <cinttypes>
#include <iostream>
#include <gmpxx.h>
#include "mee/numberAlgorithms.hpp"
#include "mee/Stopwatch.hpp"
//Defines a type for functions so there can be an array of functions
typedef bool (*boolFn)();
//Prototypes for test functions
bool testIsPrime();
bool testGetPrimes();
bool testGetNumPrimes();
bool testGetFactors();
bool testGetDivisors();
bool testGetFib();
bool testGetAllFib();
bool testToBin();
bool testFactorial();
bool testSieveOfEratosthenes();
int main(){
mee::Stopwatch timer;
bool passedTest = false;
bool allPassed = true;
std::vector<boolFn> functions { testIsPrime, testGetPrimes, testGetNumPrimes, testGetFactors, testGetDivisors, testGetFib,
testGetAllFib, testToBin, testFactorial, testSieveOfEratosthenes };
std::vector<std::string> names { "isPrime", "getPrimes", "getNumPrimes", "getFactors", "getDivisors", "getFib",
"getAllFib", "toBin", "factorial", "sieveOfEratosthenes" };
//Start doing tests and print out the results of each
for(size_t cnt = 0;cnt < functions.size();++cnt){
timer.start();
passedTest = functions[cnt]();
timer.stop();
if(passedTest){
std::cout << "Function " << names[cnt] << "() passed the test\n";
}
else{
std::cout << "Function " << names[cnt] << "() failed the test\n";
allPassed = false;
}
std::cout << "The test took " << timer.getStr() << "\n\n" << std::endl;
}
if(allPassed){
return 0;
}
else{
return 1;
}
}
bool testIsPrime(){
//Test 1
int64_t num = 2;
bool correctAnswer = true;
bool answer = mee::isPrime(num);
if(correctAnswer != answer){
return false;
}
//Test 2
num = 97;
correctAnswer = true;
answer = mee::isPrime(num);
if(correctAnswer != answer){
return false;
}
//Test 3
num = 1000;
correctAnswer = false;
answer = mee::isPrime(num);
if(correctAnswer != answer){
return false;
}
//Test 4
num = 1;
correctAnswer = false;
answer = mee::isPrime(num);
if(correctAnswer != answer){
return false;
}
//If the false was not triggered it must have passed all tests
return true;
}
bool testGetPrimes(){
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 topNum = 100;
std::vector<uint64_t> answer = mee::getPrimes(topNum);
if(correctAnswer != answer){
return false;
}
//If the false was not triggered it must have passed all tests
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 testGetDivisors(){
std::vector<uint64_t> correctAnswer {1, 2, 4, 5, 10, 20, 25, 50, 100};
uint64_t topNum = 100;
std::vector<uint64_t> answer = mee::getDivisors(topNum);
if(correctAnswer != answer){
return false;
}
//If the false was not triggered it must have passed all tests
return true;
}
bool testGetFib(){
//Test the imbeded type getFib function
uint64_t correctAnswer = 144;
uint64_t number = 12;
uint64_t answer = mee::getFib(number);
if(correctAnswer != answer){
std::cout << "getFit() failed at test 1" << std::endl;
return false;
}
number = 20;
correctAnswer = 6765;
answer = mee::getFib(number);
if(correctAnswer != answer){
std::cout << "getFit() failed at test 2" << std::endl;
return false;
}
//Test the gmp integer function
mpz_class mpzNumber = 12;
mpz_class longCorrectAnswer = 144;
mpz_class longAnswer = mee::getFib(mpzNumber);
if(longCorrectAnswer != longAnswer){
std::cout << "getFib() for mpz failed at test 3" << std::endl;
return false;
}
mpzNumber = 4782;
longCorrectAnswer = "1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816";
longAnswer = mee::getFib(mpzNumber);
if(longCorrectAnswer != longAnswer){
std::cout << "getFib() for mpzfailed at test 4" << std::endl;
return false;
}
//If the false was not triggered it must have passed all tests
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 testToBin(){
//Test 1
int num = 7;
std::string correctAnswer = "111";
std::string answer = mee::toBin(num);
if(correctAnswer != answer){
return false;
}
//Test 2
num = 0;
correctAnswer = "0";
answer = mee::toBin(num);
if(correctAnswer != answer){
return false;
}
//Test 3
num = 1000000;
correctAnswer = "11110100001001000000";
answer = mee::toBin(num);
if(correctAnswer != answer){
return false;
}
//Test 4
uint64_t num2 = 8;
correctAnswer = "1000";
answer = mee::toBin(num2);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testFactorial(){
//Test 1
int num = 1;
int correctAnswer = 1;
int answer = mee::factorial(num);
if(correctAnswer != answer){
return false;
}
//Test 2
num = 10;
correctAnswer = 3628800;
answer = mee::factorial(num);
if(correctAnswer != answer){
return false;
}
//Test 3
num = -5;
correctAnswer = 1;
answer = mee::factorial(num);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testSieveOfEratosthenes(){
//Test 1
mee::Generator<int> gen = mee::sieveOfEratosthenes<int>();
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 = gen.next();
answer.push_back(prime);
}
if(correctAnswer != answer){
return false;
}
//Test 2
mee::Generator<mpz_class> mpzGen = mee::sieveOfEratosthenesAlt<mpz_class>();
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 = mpzGen.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:
*/

126
test/mee/testStopwatch.cpp Normal file
View File

@@ -0,0 +1,126 @@
//myClasses/testStopwatch.cpp
//Matthew Ellison
// Created: 02-12-19
//Modified: 07-09-20
//This file is a simple test for the Stopwatch class
#include <iostream>
#include "mee/Stopwatch.hpp"
int main(){
mee::Stopwatch timer;
//Try to stop the timer without starting it
std::cout << "Testing the stopBeforeStart error" << std::endl;
try{
timer.stop();
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution\n";
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started\n";
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started\n";
std::cout << "Test successful" << std::endl;
}
//Try to get the time on it without starting it
std::cout << "Testing the timeBeforeStart error" << std::endl;
try{
std::cout << timer.getStr() << '\n';
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution\n";
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started\n";
std::cout << "Test successful" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started\n";
std::cout << "Test failed" << std::endl;
}
//Use it correctly
std::cout << "Using the class correctly" << std::endl;
try{
timer.start();
for(int cnt = 0;cnt < 1000000;++cnt){
int num = cnt;
cnt = num;
}
timer.stop();
std::cout << "It took " << timer.getStr() << " to complete this loop\n";
std::cout << "It took " << timer << " to complete this loop\n";
std::cout << "Test successful" << std::endl;
}
catch(mee::Stopwatch::invalidTimeResolution){
std::cout << "There was an invalid time resolution\n";
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::timeBeforeStart){
std::cout << "Tried to get time before the stopwatch was started\n";
std::cout << "Test failed" << std::endl;
}
catch(mee::Stopwatch::stopBeforeStart){
std::cout << "Tried to stop the stopwatch before it was started\n";
std::cout << "Test failed" << std::endl;
}
//Test string function
bool failedTimeResolutionTests = false;
std::string results = mee::Stopwatch::getStr(1.0);
if(results != "1.000 nanoseconds"){
failedTimeResolutionTests = true;
std::cout << "Failed the nanosecond test" << std::endl;
}
results = mee::Stopwatch::getStr(1.0e3);
if(results != "1.000 microseconds"){
failedTimeResolutionTests = true;
std::cout << "Failed the microsecond test" << std::endl;
}
results = mee::Stopwatch::getStr(1.0e6);
if(results != "1.000 milliseconds"){
failedTimeResolutionTests = true;
std::cout << "Failed the millisecond test" << std::endl;
}
results = mee::Stopwatch::getStr(1.0e9);
if(results != "1.000 seconds"){
failedTimeResolutionTests = true;
std::cout << "Failed the second test" << std::endl;
}
results = mee::Stopwatch::getStr(1.0e12);
if(results != "16.667 minutes"){
failedTimeResolutionTests = true;
std::cout << "Failed the minute test" << std::endl;
}
results = mee::Stopwatch::getStr(1.0e13);
if(results != "2.778 hours"){
failedTimeResolutionTests = true;
std::cout << "Failed the hour test" << std::endl;
}
if(!failedTimeResolutionTests){
std::cout << "Passed all string resolution tests" << std::endl;
}
return 0;
}
/* Results:
Testing the stopBeforeStart error
Tried to stop the stopwatch before it was started
Test successful
Testing the timeBeforeStart error
Tried to get time before the stopwatch was started
Test successful
Using the class correctly
It took 1.855 milliseconds to complete this loop
Test successful
*/

View File

@@ -0,0 +1,169 @@
//myClasses/test/mee/testStringAlgorithms.cpp
//Matthew Ellison
// Created: 07-02-21
//Modified: 07-02-21
//This file contains tests for the functions in my stringAlgorithms library
/*
Copyright (C) 2021 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "mee/Stopwatch.hpp"
#include "mee/stringAlgorithms.hpp"
#include "mee/vectorAlgorithms.hpp"
//Defines a type for functions so there can be an array of functions
typedef bool (*boolFn)();
//Prototypes for test functions
bool testGetPermutations();
bool testFindNumOccurrence();
bool testSplit();
bool testIsPalindrome();
int main(){
mee::Stopwatch timer;
bool passedTest = false;
bool allPassed = true;
std::vector<boolFn> functions { testGetPermutations, testFindNumOccurrence, testSplit, testIsPalindrome};
std::vector<std::string> names { "getPermutations", "findNumOccurrence", "split", "isPalindrome"};
//Start doing tests and print out the results of each
for(size_t cnt = 0;cnt < functions.size();++cnt){
timer.start();
passedTest = functions[cnt]();
timer.stop();
if(passedTest){
std::cout << "Function " << names[cnt] << "() passed the test\n";
}
else{
std::cout << "Function " << names[cnt] << "() failed the test\n";
allPassed = false;
}
std::cout << "The test took " << timer.getStr() << "\n\n" << std::endl;
}
if(allPassed){
return 0;
}
else{
return 1;
}
}
bool testGetPermutations(){
std::string permString = "012";
std::vector<std::string> correctAnswer {"012", "021", "102", "120", "201", "210"};
std::vector<std::string> answer = mee::getPermutations(permString);
if(answer != correctAnswer){
return false;
}
//If the false was not triggered it must have passed all tests
return true;
}
bool testFindNumOccurrence(){
//Test 1
std::string testString = "abcdefgdd";
char testChar = 'a';
int correctAnswer = 1;
int answer = mee::findNumOccurrence(testString, testChar);
if(correctAnswer != answer){
return false;
}
//Test 2
testChar = 'd';
correctAnswer = 3;
answer = mee::findNumOccurrence(testString, testChar);
if(correctAnswer != answer){
return false;
}
//Test 3
testChar = 'h';
correctAnswer = 0;
answer = mee::findNumOccurrence(testString, testChar);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testSplit(){
//Test 1
std::string str = "abc0def";
std::vector<std::string> correctAnswer = {"abc", "def"};
std::vector<std::string> answer = mee::split(str, '0');
if(correctAnswer != answer){
return false;
}
//Test 2
str = "abc0def";
correctAnswer = {"abc0def"};
answer = mee::split(str, '1');
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
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;
}
/* Results:
*/

View File

@@ -0,0 +1,346 @@
//myClasses/test/mee/testVectorAlgorithms.cpp
//Matthew Ellison
// Created: 07-02-21
//Modified: 07-02-21
//This file contains tests for the functions in my vetorAlgorithms library
/*
Copyright (C) 2021 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
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
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/>.
*/
#include <cinttypes>
#include <iostream>
#include <random>
#include <vector>
#include "mee/numberAlgorithms.hpp"
#include "mee/Stopwatch.hpp"
#include "mee/vectorAlgorithms.hpp"
//Defines a type for functions so there can be an array of functions
typedef bool (*boolFn)();
//Prototypes for test functions
bool testGetSum();
bool testGetProduct();
bool testIsFound();
bool testBubbleSort();
bool testQuickSort();
bool testSearch();
bool testFindMin();
bool testFindMax();
bool testPrintVector();
int main(){
mee::Stopwatch timer;
bool passedTest = false;
bool allPassed = true;
std::vector<boolFn> functions {testGetSum, testGetProduct, testIsFound, testBubbleSort, testQuickSort, testSearch,
testFindMin, testFindMax, testPrintVector};
std::vector<std::string> names { "getSum", "getProduct", "isFound", "bubbleSort", "quickSort", "search",
"findMin", "findMax", "printVector"};
//Start doing tests and print out the results of each
for(size_t cnt = 0;cnt < functions.size();++cnt){
timer.start();
passedTest = functions[cnt]();
timer.stop();
if(passedTest){
std::cout << "Function " << names[cnt] << "() passed the test\n";
}
else{
std::cout << "Function " << names[cnt] << "() failed the test\n";
allPassed = false;
}
std::cout << "The test took " << timer.getStr() << "\n\n" << std::endl;
}
if(allPassed){
return 0;
}
else{
return 1;
}
}
bool testGetSum(){
uint64_t correctAnswer = 1060;
uint64_t topNum = 100;
uint64_t answer = mee::getSum(mee::getPrimes(topNum));
if(correctAnswer != answer){
return false;
}
//If the false was not triggered it must have passed all tests
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);
uint64_t searchFor = 79;
bool answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){
std::cout << "isFound() is failed at test 1" << std::endl;
return false;
}
searchFor = 97;
answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){
std::cout << "isFound() is failed at test 2" << std::endl;
return false;
}
searchFor = 88;
correctAnswer = false;
answer = mee::isFound(testVector, searchFor);
if(answer != correctAnswer){
std::cout << "isFound() is failed at test 3" << std::endl;
return false;
}
//If the 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{}());
std::uniform_int_distribution<int> dist(1, INT_MAX);
std::vector<int> nums;
//Run through the appropriate number of numbers to generate and add them to the vector
for(unsigned int cnt = 0;cnt < NUM_TO_GENERATE;++cnt){
nums.push_back(dist(generator));
}
//Sort the numbers with my algorithm
mee::bubbleSort(nums);
//Make sure each number is not < the one in behind it
for(size_t cnt = 1;cnt < nums.size();++cnt){
if(nums.at(cnt) < nums.at(cnt - 1)){
return false;
std::cout << "nums.size() " << nums.size() << "\ncnt " << cnt << std::endl;
}
}
//If the false was not triggered then everything must have been sorted correctly
return true;
}
bool testQuickSort(){
unsigned int NUM_TO_GENERATE = 10000;
std::default_random_engine generator(std::random_device{}());
std::uniform_int_distribution<int> dist(1, INT_MAX);
std::vector<int> nums;
//Run through the appropriate number of numbers to generate and add them to the vector
for(unsigned int cnt = 0;cnt < NUM_TO_GENERATE;++cnt){
nums.push_back(dist(generator));
}
//Sort the numbers with my algorithm
mee::quickSort(nums);
//Make sure each number is not < the one in behind it
for(size_t cnt = 1;cnt < nums.size();++cnt){
if(nums.at(cnt) < nums.at(cnt - 1)){
return false;
}
}
//If the false was not triggered then everything must have been sorted correctly
return true;
}
bool testSearch(){
int64_t found = -1;
//Create a vector of numbers
std::vector<int> nums {1, 20, 3, 40, 5, 60, 7, 80, 9};
//Search for one that is in the vector
found = mee::search(nums, 1);
if(found != 0){ //This number is in the vector, if it was not found in the vector there is a problem
return false;
}
//Search for another that is in the vector
found = mee::search(nums, 9);
if(found != 8){ //This number is in the vector, if it was not found in the vector there is a problem
return false;
}
//Search for another that is in the vector
found = mee::search(nums, 60);
if(found != 5){ //This number is in the vector, if it was not found in the vector there is a problem
return false;
}
//Search for a number that is not in the vector
found = mee::search(nums, 6);
if(found != -1){
return false;
}
//If it didn't trigger a false then everything went through correctly
return true;
}
bool testFindMin(){
std::vector<int> arr {1, 2, 3, 4, 5, 6, 7, 8, 9};
int answer = 0;
//Test first
answer = mee::findMin(arr);
if(answer != 1){
return false;
}
//Test last
answer = 0;
arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
answer = mee::findMin(arr);
if(answer != 1){
return false;
}
//Test middle
answer = 0;
arr = {9, 5, 3, 6, 8, 1, 7, 2, 4};
answer = mee::findMin(arr);
if(answer != 1){
return false;
}
//Test negative
answer = 0;
arr = {-1, -2, -3, -4, -5, -6, -7, -8, -9};
answer = mee::findMin(arr);
if(answer != -9){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testFindMax(){
std::vector<int> arr {1, 2, 3, 4, 5, 6, 7, 8, 9};
int answer = 0;
//Test last
answer = mee::findMax(arr);
if(answer != 9){
return false;
}
//Test first
answer = 0;
arr = {9, 8, 7, 6, 5, 4, 3, 2, 1};
answer = mee::findMax(arr);
if(answer != 9){
return false;
}
//Test middle
answer = 0;
arr = {9, 5, 3, 6, 8, 1, 7, 2, 4};
answer = mee::findMax(arr);
if(answer != 9){
return false;
}
//Test negative
answer = 0;
arr = {-1, -2, -3, -4, -5, -6, -7, -8, -9};
answer = mee::findMax(arr);
if(answer != -1){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
bool testPrintVector(){
//Test 1
std::vector<int> nums;
std::string correctAnswer = "[]";
std::string answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 2
nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
correctAnswer = "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]";
answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 3
nums = {-3, -2, -1, 0, 1, 2, 3};
correctAnswer = "[-3, -2, -1, 0, 1, 2, 3]";
answer = mee::printVector(nums);
if(correctAnswer != answer){
return false;
}
//Test 4
std::vector<std::string> strings = {"A", "B", "C"};
correctAnswer = "[A, B, C]";
answer = mee::printVector(strings);
if(correctAnswer != answer){
return false;
}
//Test 5
strings = {"abc", "def", "ghi"};
correctAnswer = "[abc, def, ghi]";
answer = mee::printVector(strings);
if(correctAnswer != answer){
return false;
}
//If it hasn't failed a test then return true for passing all the tests
return true;
}
/* Results:
*/