From a7c4346af59f0ed3488252254aa8e8ccaa2154bc Mon Sep 17 00:00:00 2001 From: Mattrixwv Date: Thu, 11 Mar 2021 20:42:40 -0500 Subject: [PATCH] Changed algorithm test to unit test --- TestAlgorithms.py | 155 ++++++++++++++++++++ testAlgorithms.old.py | 325 ------------------------------------------ 2 files changed, 155 insertions(+), 325 deletions(-) create mode 100644 TestAlgorithms.py delete mode 100644 testAlgorithms.old.py diff --git a/TestAlgorithms.py b/TestAlgorithms.py new file mode 100644 index 0000000..bb401d1 --- /dev/null +++ b/TestAlgorithms.py @@ -0,0 +1,155 @@ +#Python/pyClasses/TestAlgorithms.py +#Matthew Ellison +# Created: 01-27-19 +#Modified: 03-11-21 +#This is a file that contains a few algorithms that I have used several times +""" +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 . +""" + + +import unittest +import Algorithms + + +class TestAlgorithms(unittest.TestCase): + #This function tests the getPrimes function + def testGetPrimes(self): + #Test 1 + 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] + topNum = 100 + answer = Algorithms.getPrimes(topNum) #Get the answer from the function + self.assertEqual(correctAnswer, answer, "getPrimes failed the first test") + + #This function tests the getNumPrimes function + def testGetNumPrimes(self): + #Test 1 + 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] + numPrimes = 25 + answer = Algorithms.getNumPrimes(numPrimes) #Get the answer from the function + self.assertEqual(correctAnswer, answer, "getNumPrimes failed the first test") + + #This function tests the getFactors function + def testGetFactors(self): + #Test 1 + correctAnswer = [2, 2, 5, 5] + number = 100 + answer = Algorithms.getFactors(number) + self.assertEqual(correctAnswer, answer, "getFactors failed the first test") + + #Test 2 + correctAnswer = [2, 7, 7] + number = 98 + answer = Algorithms.getFactors(number) + self.assertEqual(correctAnswer, answer, "getFactors failed the second test") + + #This function tests the getDivisors function + def testGetDivisors(self): + #Test 1 + correctAnswer = [1, 2, 4, 5, 10, 20, 25, 50, 100] + topNum = 100 + answer = Algorithms.getDivisors(topNum) + self.assertEqual(correctAnswer, answer, "getDivisors failed the first test") + + #This function tests the getFib function + def testGetFib(self): + #Test 1 + correctAnswer = 144 + number = 12 + answer = Algorithms.getFib(number) + self.assertEqual(correctAnswer, answer, "getFib failed the first test") + + #Test 2 + correctAnswer = 6765 + number = 20 + answer = Algorithms.getFib(number) + self.assertEqual(correctAnswer, answer, "getFib failed the second test") + + #Test 3 + correctAnswer = 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 + number = 4782 + answer = Algorithms.getFib(number) + self.assertEqual(correctAnswer, answer, "getFib failed the third test") + + #This function tests the getAllFib function + def testGetAllFib(self): + #Test 1 + correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] + highestNumber = 100 + answer = Algorithms.getAllFib(highestNumber) + self.assertEqual(correctAnswer, answer, "getAllFib failed the first test") + + #Test 2 + correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] + highestNumber = 1000 + answer = Algorithms.getAllFib(highestNumber) + self.assertEqual(correctAnswer, answer, "getAllFib failed the second test") + + #This function tests the prod function + def testProd(self): + #Test 1 + correctAnswer = 0 + numbers = [] + answer = Algorithms.prod(numbers) + self.assertEqual(correctAnswer, answer, "prod failed the first test") + + #Test 2 + correctAnswer = 57600 + numbers = [2, 2, 3, 3, 4, 4, 100] + answer = Algorithms.prod(numbers) + self.assertEqual(correctAnswer, answer, "prod failed the second test") + + #This function tests the getPermutations function + def testGetPermutations(self): + #Test 1 + permString = "012" + correctAnswer = ["012", "021", "102", "120", "201", "210"] + answer = Algorithms.getPermutations(permString) + self.assertEqual(correctAnswer, answer, "getPermutations failed the first test") + + #This function tests the gcd function + def testGCD(self): + #Test 1 + num1 = 2 + num2 = 3 + correctAnswer = 1 + answer = Algorithms.gcd(num1, num2) + self.assertEqual(correctAnswer, answer, "getGCD failed the first test") + #Test 2 + num1 = 1000 + num2 = 575 + correctAnswer = 25 + answer = Algorithms.gcd(num1, num2) + self.assertEqual(correctAnswer, answer, "getGCD failed the second test") + #Test 3 + num1 = 1000 + num2 = 1000 + correctAnswer = 1000 + answer = Algorithms.gcd(num1, num2) + self.assertEqual(correctAnswer, answer, "getGCD failed the third test") + + +#Run the unit test if the script is called +if __name__ == "__main__": + unittest.main() + +"""Results: +......... +---------------------------------------------------------------------- +Ran 9 tests in 0.002s + +OK +""" diff --git a/testAlgorithms.old.py b/testAlgorithms.old.py deleted file mode 100644 index 5ab9252..0000000 --- a/testAlgorithms.old.py +++ /dev/null @@ -1,325 +0,0 @@ -#Python/pyClasses/testAlgorithms.py -#Matthew Ellison -# Created: 03-02-19 -#Modified: 03-02-19 -#This script runs tests on all function in the Algorithms library -""" -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 - 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 . -""" - - -import Algorithms -from Stopwatch import Stopwatch - - -#This function tests the getPrimes function -def testGetPrimes(): - failed = False #Holds whether a test was failed - - #Test 1 - 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] - topNum = 100 - answer = Algorithms.getPrimes(topNum) #Get the answer from the function - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getPrimes failed the first test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getPrimes passed all tests") - -#This function tests the getNumPrimes function -def testGetNumPrimes(): - failed = False #Holds whether a test was failed - - #Test 1 - 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] - numPrimes = 25 - answer = Algorithms.getNumPrimes(numPrimes) #Get the answer from the function - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getNumPrimes failed the first test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getNumPrimes passed all tests") - -#This function tests the getFactors function -def testGetFactors(): - failed = False #Holds whether a test was failed - - #Test 1 - correctAnswer = [2, 2, 5, 5] - number = 100 - answer = Algorithms.getFactors(number) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getFactors failed the first test") - failed = True - - #Test 2 - correctAnswer = [2, 7, 7] - number = 98 - answer = Algorithms.getFactors(number) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getFactors failed the second test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getFactors passed all tests") - -#This function tests the getDivisors function -def testGetDivisors(): - failed = False #Holds whether a test was failed - - #Test 1 - correctAnswer = [1, 2, 4, 5, 10, 20, 25, 50, 100] - topNum = 100 - answer = Algorithms.getDivisors(topNum) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getDivisors failed the first test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getDivisors passed all tests") - -#This function tests the getFib function -def testGetFib(): - failed = False #Holds whether a test was failed - - #Test 1 - correctAnswer = 144 - number = 12 - answer = Algorithms.getFib(number) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getFib failed the first test") - failed = True - - #Test 2 - correctAnswer = 6765 - number = 20 - answer = Algorithms.getFib(number) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getFib failed the second test") - failed = True - - #Test 3 - correctAnswer = 1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816 - number = 4782 - answer = Algorithms.getFib(number) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getFib failed the third test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getFib passed all tests") - -#This function tests the getAllFib function -def testGetAllFib(): - failed = False #Holds whether a test was failed - - #Test 1 - correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] - highestNumber = 100 - answer = Algorithms.getAllFib(highestNumber) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getAllFib failed the first test") - failed = True - - #Test 2 - correctAnswer = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987] - highestNumber = 1000 - answer = Algorithms.getAllFib(highestNumber) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("getAllFib failed the second test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("getAllFib passed all tests") - -#This function tests the prod function -def testProd(): - failed = False #Holds whether a test was failed - - #Test 1 - correctAnswer = 0 - numbers = [] - answer = Algorithms.prod(numbers) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("prod failed the first test") - failed = True - - #Test 2 - correctAnswer = 57600 - numbers = [2, 2, 3, 3, 4, 4, 100] - answer = Algorithms.prod(numbers) - #Print an error message if the function returned the wrong answer - if(correctAnswer != answer): - print("prod failed the second test") - failed = True - - #Print a message if all of the tests passed - if(not failed): - print("prod passed all tests") - -#This function tests the getPermutations function -def testGetPermutations(): - failed = False #Holds whether a test was failed - - #Test 1 - permString = "012" - correctAnswer = ["012", "021", "102", "120", "201", "210"] - answer = Algorithms.getPermutations(permString) - if(answer != correctAnswer): - print("getPermutations failed the first test") - failed = True - - #If the false was not triggered it must have passed all tests - if(not failed): - print("getPermutations passed all tests") - -#This function tests the gcd function -def testGCD(): - failed = False #Holds whether a test was failed - - #Test 1 - num1 = 2 - num2 = 3 - correctAnswer = 1 - answer = Algorithms.gcd(num1, num2) - if(answer != correctAnswer): - print("getGCD failed the first test") - failed = True - #Test 2 - num1 = 1000 - num2 = 575 - correctAnswer = 25 - answer = Algorithms.gcd(num1, num2) - if(answer != correctAnswer): - print("getGCD failed the second test") - failed = True - #Test 3 - num1 = 1000 - num2 = 1000 - correctAnswer = 1000 - answer = Algorithms.gcd(num1, num2) - if(answer != correctAnswer): - print("getGCD failed the third test") - failed = True - - if(not failed): - print("getGCD passed all tests") - -#This runs all of the tests and times them if this script is implicitly called -if __name__ == "__main__": - timer = Stopwatch() - - #Test getPrimes - timer.start() - testGetPrimes() - timer.stop() - print("testGetPrimes took " + timer.getString() + " to run\n") - - #Test getNumPrimes - timer.start() - testGetNumPrimes() - timer.stop() - print("testGetNumPrimes took " + timer.getString() + " to run\n") - - #Test getFactors - timer.start() - testGetFactors() - timer.stop() - print("testGetFactors took " + timer.getString() + " to run\n") - - #Test getDivisors - timer.start() - testGetDivisors() - timer.stop() - print("testGetDivisors took " + timer.getString() + " to run\n") - - #Test getFib - timer.start() - testGetFib() - timer.stop() - print("testGetFib took " + timer.getString() + " to run\n") - - #Test getAllFib - timer.start() - testGetAllFib() - timer.stop() - print("testGetAllFib took " + timer.getString() + " to run\n") - - #Test prod - timer.start() - testProd() - timer.stop() - print("testProd took " + timer.getString() + " to run\n") - - #Test getPermutations - timer.start() - testGetPermutations() - timer.stop() - print("testGetPermutations took " + timer.getString() + " to run\n") - - #Test gcd - timer.start() - testGCD() - timer.stop() - print("testGCD took " + timer.getString() + " to run") - -"""Results: -getPrimes passed all tests -testGetPrimes took 189.500 microseconds to run - -getNumPrimes passed all tests -testGetNumPrimes took 164.300 microseconds to run - -getFactors passed all tests -testGetFactors took 162.400 microseconds to run - -getDivisors passed all tests -testGetDivisors took 166.400 microseconds to run - -getFib passed all tests -testGetFib took 717.400 microseconds to run - -getAllFib passed all tests -testGetAllFib took 168.300 microseconds to run - -prod passed all tests -testProd took 161.800 microseconds to run - -getPermutations passed all tests -testGetPermutations took 162.900 microseconds to run - -getGCD passed all tests -testGCD took 186.100 microseconds to run -"""