diff --git a/Algorithms.hpp b/Algorithms.hpp index e49d7b5..3bd0097 100644 --- a/Algorithms.hpp +++ b/Algorithms.hpp @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -619,6 +620,21 @@ std::string toBin(T num){ return trimmedString; } +//Print a vector +template +std::string printVector(std::vector& ary){ + std::stringstream str; + str << "["; + for(int cnt = 0;cnt < ary.size();++cnt){ + str << ary[cnt]; + if(cnt < ary.size() - 1){ + str << ", "; + } + } + str << "]"; + return str.str(); +} + } diff --git a/testAlgorithms.cpp b/testAlgorithms.cpp index cd3afa8..0817d4d 100644 --- a/testAlgorithms.cpp +++ b/testAlgorithms.cpp @@ -56,15 +56,18 @@ bool testFindNumOccurrence(); bool testFactorial(); bool testIsPalindrome(); bool testToBin(); +bool testPrintVector(); int main(){ mee::Stopwatch timer; bool passedTest = false; std::vector functions {testGetPrimes, testGetNumPrimes, testIsPrime, testGetFactors, tetsGetDivisors, testGetSum, testGetProduct, testIsFound, testGetPermutations, - testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin}; + testGetFib, testGetAllFib, testBubbleSort, testQuickSort, testSearch, testFindMin, testFindMax, testFindNumOccurrence, testFactorial, testIsPalindrome, testToBin, + testPrintVector}; std::vector names {"getPrimes", "getNumPrimes", "isPrime", "getFactors", "getDivisors", "getSum", "getProduct", "isFound", "getPermutations", - "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin"}; + "getFib", "getAllFib", "bubbleSort", "quickSort", "search", "findMin", "findMax", "findNumOccurrence", "factorial", "isPalindrome", "toBin", + "testPrintVector"}; //Start doing tests and print out the results of each for(int cnt = 0;cnt < functions.size();++cnt){ @@ -580,73 +583,116 @@ bool testToBin(){ return true; } +bool testPrintVector(){ + //Test 1 + std::vector 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 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: Function getPrimes() passed the test -The test took 14.100 microseconds +The test took 9.400 microseconds Function getNumPrimes() passed the test -The test took 15.100 microseconds +The test took 6.400 microseconds Function isPrime() passed the test -The test took 300.000 nanoseconds +The test took 200.000 nanoseconds Function getFactors() passed the test -The test took 10.200 microseconds +The test took 5.400 microseconds Function getDivisors() passed the test -The test took 5.800 microseconds - - -Function getSum() passed the test -The test took 9.300 microseconds - - -Function getProduct() passed the test The test took 2.000 microseconds +Function getSum() passed the test +The test took 8.500 microseconds + + +Function getProduct() passed the test +The test took 1.200 microseconds + + Function isFound() passed the test -The test took 23.500 microseconds +The test took 7.100 microseconds Function getPermutations() passed the test -The test took 30.100 microseconds +The test took 8.800 microseconds Function getFib() passed the test -The test took 228.800 microseconds +The test took 221.400 microseconds Function getAllFib() passed the test -The test took 9.000 microseconds +The test took 4.100 microseconds Function bubbleSort() passed the test -The test took 995.215 milliseconds +The test took 1.186 seconds Function quickSort() passed the test -The test took 3.559 milliseconds +The test took 3.580 milliseconds Function search() passed the test -The test took 2.800 microseconds +The test took 1.300 microseconds Function findMin() passed the test -The test took 2.500 microseconds +The test took 1.700 microseconds Function findMax() passed the test -The test took 2.500 microseconds +The test took 1.600 microseconds Function findNumOccurrence() passed the test -The test took 1.100 microseconds +The test took 1.400 microseconds Function factorial() passed the test @@ -654,9 +700,13 @@ The test took 200.000 nanoseconds Function isPalindrome() passed the test -The test took 1.500 microseconds +The test took 1.400 microseconds Function toBin() passed the test -The test took 2.100 microseconds +The test took 9.300 microseconds + + +Function testPrintVector() passed the test +The test took 9.900 microseconds */