From 27087ad1633001b373406084f22fd9d409da8409 Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 28 Feb 2019 11:23:47 -0500 Subject: [PATCH] Added tests so all functions in Algorithms.hpp are tested --- testAlgorithms.cpp | 153 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 131 insertions(+), 22 deletions(-) diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index 9ea3fee..b7a108b 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -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 functions {testGetPrimes, tetsGetDivisors, testGetSum, testIsFound, testGetPermutations, testGetFib, - testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax}; - std::vector names {"testGetPrimes", "testGetDivisors", "testGetSum", "testIsFound", "testGetPermutations", "testGetFib", - "testBubbleSort", "testQuickSort", "testSearch", "testFindMin", "testFindMax"}; - //Start doing tests and print out the results of each + std::vector functions {testGetPrimes, testGetNumPrimes, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations, + testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax}; + std::vector 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 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 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 correctAnswer {2, 2, 5, 5}; + uint64_t number = 100; + std::vector 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 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 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 testVector = mee::getPrimes((uint64_t)100); @@ -192,6 +246,25 @@ bool testGetFib(){ return true; } +bool testGetAllFib(){ + std::vector correctAnswer {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}; + uint64_t highestNumber = 100; + std::vector 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 */