mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2026-02-03 19:52:33 -05:00
Added tests
This commit is contained in:
@@ -256,6 +256,90 @@ function testGetPermutations()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function testIsFound()
|
||||||
|
local failed = false; --Signals whether a test was failed
|
||||||
|
|
||||||
|
--Test 1
|
||||||
|
local numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
local correctAnswer = true;
|
||||||
|
local answer = isFound(numbers, 0);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("isFound failed the first test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test 2
|
||||||
|
numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = isFound(numbers, 9);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("isFound failed the second test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test 3
|
||||||
|
numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
correctAnswer = true;
|
||||||
|
answer = isFound(numbers, 5);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("isFound failed the third test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test 4
|
||||||
|
numbers = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
correctAnswer = false;
|
||||||
|
answer = isFound(numbers, 10);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("isFound failed the fourth test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Print a message if all of the tests passed
|
||||||
|
if(not failed) then
|
||||||
|
io.write("isFound passed all tests\n");
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function testGCD()
|
||||||
|
local failed = false; --Signals whether a test was failed
|
||||||
|
|
||||||
|
--Test 1
|
||||||
|
local num1 = 2;
|
||||||
|
local num2 = 3;
|
||||||
|
local correctAnswer = 1;
|
||||||
|
local answer = gcd(num1, num2);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("gcd failed the first test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test 2
|
||||||
|
num1 = 1000;
|
||||||
|
num2 = 575;
|
||||||
|
correctAnswer = 25;
|
||||||
|
answer = gcd(num1, num2);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("gcd failed the second test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Test 3
|
||||||
|
num1 = 1000;
|
||||||
|
num2 = 1000;
|
||||||
|
correctAnswer = 1000;
|
||||||
|
answer = gcd(num1, num2);
|
||||||
|
if(answer ~= correctAnswer) then
|
||||||
|
io.write("gcd failed the third test\n");
|
||||||
|
failed = true;
|
||||||
|
end
|
||||||
|
|
||||||
|
--Print a message if all of the tests passed
|
||||||
|
if(not failed) then
|
||||||
|
io.write("gcd passed all tests\n");
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
--Create the timer to time each test
|
--Create the timer to time each test
|
||||||
local timer = Stopwatch:create();
|
local timer = Stopwatch:create();
|
||||||
@@ -314,7 +398,14 @@ testGetPermutations();
|
|||||||
timer:stop();
|
timer:stop();
|
||||||
io.write("It took " .. timer:getString() .. " to run this test\n");
|
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||||
|
|
||||||
|
--Test isFound
|
||||||
|
timer:start();
|
||||||
|
testIsFound();
|
||||||
|
timer:stop();
|
||||||
|
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||||
|
|
||||||
--[[ Results:
|
--Test gcd
|
||||||
|
timer:start();
|
||||||
]]
|
testGCD();
|
||||||
|
timer:stop();
|
||||||
|
io.write("It took " .. timer:getString() .. " to run this test\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user