Added a script that tests the results of all

functions in the Algorithms script
This commit is contained in:
2019-03-28 12:14:25 -04:00
parent 68efd8b092
commit 161b051b04

304
testAlgorithms.lua Normal file
View File

@@ -0,0 +1,304 @@
--lua/luaClasses/testAlgorithms.lua
--Mathew Ellison
-- Created: 03-28-19
--Modified: 03-28-19
--This script is used to test the Algorithms script
require "Algorithms";
require "Stopwatch";
local bigint = require "bigint";
function testGetPrimes()
local failed = false; --Holds whether a test was failed
--Test 1
local 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};
local topNum = 100;
local answer = getPrimes(topNum); --Get the answer from the function
--Print an error message if the function returned the wrong answer
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getPrimes failed the first test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getPrimes passed all tests\n");
end
end
function testGetNumPrimes()
local failed = false; --Holds whether a test was failed
--Test 1
local 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};
local numPrimes = 100;
local answer = getNumPrimes(numPrimes); --Get the answer from the function
--Print an error message if the function returned the wrong answer
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getNumPrimes failed the first test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getNumPrimes passed all tests\n");
end
end
function testGetFactors()
local failed = false; --Holds whether a test was failed
--Test 1
local correctAnswer = {2, 2, 5, 5};
local number = 100;
local answer = getFactors(number);
--Print an error message if the function returned the wrong answer
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getFactors failed the first test\n");
failed = true;
end
--Test 2
correctAnswer = {2, 7, 7};
number = 98;
answer = getFactors(number);
--Print an error message if the function returned the wrong answer
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getFactors failed the second test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getFactors passed all tests\n");
end
end
function testGetDivisors()
local failed = false; --Holds whether a test was failed
--Test 1
local correctAnswer = {1, 2, 4, 5, 10, 20, 25, 50, 100};
local topNum = 100;
local answer = getDivisors(topNum);
--Print an error message if the function returned the wrong answer
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getDivisors failed the first test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getDivisors passed all tests\n");
end
end
function testGetFib()
local failed = false; --Holds whether a test was failed
--Test 1
local correctAnswer = 144;
local number = 12;
local answer = getFib(number);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getFib failed the first test\n");
failed = true;
end
--Test 2
correctAnswer = 6765
number = 20
answer = getFib(number);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getFib failed the second test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getFib passed all tests\n");
end
end
function testGetLargeFib()
local failed = false; --Holds whther a test was failed
--Test 1
local correctAnswer = bigint.new(144);
local number = 12;
local answer = getLargeFib(number);
--Print an error message if the function returned the wrong answer
if(bigint.compare(correctAnswer, answer, "!=")) then
io.write("getLargeFib failed the first test\n");
failed = true;
end
--Test 2
correctAnswer = bigint.new(6765);
number = 20;
answer = getLargeFib(number);
--Print an error message if the function returned the wrong answer
if(bigint.compare(correctAnswer, answer, "!=")) then
io.write("getLargeFib failed the second test\n");
failed = true;
end
--Test 3
correctAnswer = bigint.new("1070066266382758936764980584457396885083683896632151665013235203375314520604694040621889147582489792657804694888177591957484336466672569959512996030461262748092482186144069433051234774442750273781753087579391666192149259186759553966422837148943113074699503439547001985432609723067290192870526447243726117715821825548491120525013201478612965931381792235559657452039506137551467837543229119602129934048260706175397706847068202895486902666185435124521900369480641357447470911707619766945691070098024393439617474103736912503231365532164773697023167755051595173518460579954919410967778373229665796581646513903488154256310184224190259846088000110186255550245493937113651657039447629584714548523425950428582425306083544435428212611008992863795048006894330309773217834864543113205765659868456288616808718693835297350643986297640660000723562917905207051164077614812491885830945940566688339109350944456576357666151619317753792891661581327159616877487983821820492520348473874384736771934512787029218636250627816");
number = 4782;
answer = getLargeFib(number);
--Print an error message if the function returned the wrong answer
if(bigint.compare(correctAnswer, answer, "!=")) then
io.write("getLargeFib failed the third test\n");
failed = true;
end
--Print a message if all of the test passed
if(not failed) then
io.write("getLargeFib passed all tests\n");
end
end
function testGetSum()
local failed = false; --Holds whether a test was failed
--Test 1
local correctAnswer = 0;
local numbers = {};
local answer = getSum(numbers);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getSum failed the first test\n");
failed = true;
end
--Test 2
correctAnswer = 118;
numbers = {2, 2, 3, 3, 4, 4, 100};
answer = getSum(numbers);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getSum failed the second test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getSum passed all tests\n");
end
end
function testGetProd()
local failed = false; --Holds whether a test was failed
--Test 1
local correctAnswer = 0;
local numbers = {};
local answer = getProd(numbers);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getProd failed the first test\n");
failed = true;
end
--Test 2
correctAnswer = 57600;
numbers = {2, 2, 3, 3, 4, 4, 100};
answer = getProd(numbers);
--Print an error message if the function returned the wrong answer
if(correctAnswer ~= answer) then
io.write("getProd failed the second test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getProd passed all tests\n");
end
end
function testGetPermutations()
local failed = false; --Holds whether a test was failed
--Test 1
local permString = "012";
local correctAnswer = {"012", "021", "102", "120", "201", "210"};
local answer = getPermutations(permString);
if(table.unpack(correctAnswer) ~= table.unpack(answer)) then
io.write("getPermutatiosn failed the first test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("getPermutations passed all tests\n");
end
end
--Create the timer to time each test
local timer = Stopwatch:create();
--Test getPrimes
timer:start();
testGetPrimes();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getNumPrimes
timer:start();
testGetNumPrimes();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getFactors
timer:start();
testGetFactors();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getDivisors
timer:start();
testGetDivisors();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getFib
timer:start();
testGetFib();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getLargeFib
timer:start();
testGetLargeFib();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getSum
timer:start();
testGetSum();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getProd
timer:start();
testGetProd();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test getPermutations
timer:start();
testGetPermutations();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--[[ Results:
]]