Added functions to check for palindromes and convert nums to bin strings

This commit is contained in:
2021-06-29 14:14:35 -04:00
parent a9de121a88
commit 75f4441f18
2 changed files with 115 additions and 2 deletions

View File

@@ -1,7 +1,7 @@
--lua/luaClasses/testAlgorithms.lua
--Mathew Ellison
-- Created: 03-28-19
--Modified: 06-01-21
--Modified: 06-29-21
--This script is used to test the Algorithms script
--[[
Copyright (C) 2021 Matthew Ellison
@@ -349,6 +349,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the first test\n");
failed = true;
end
--Test 2
local num = 10;
@@ -356,6 +357,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the second test\n");
failed = true;
end
--Test 3
local num = -5;
@@ -363,6 +365,7 @@ local function testFactorial()
local answer = factorial(num);
if(answer ~= correctAnswer) then
io.write("factorial failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
@@ -371,6 +374,74 @@ local function testFactorial()
end
end
local function testIsPalindrome()
local failed = false; --Signals whether a test was failed
--Test 1
local str = "101";
local correctAnswer = true;
local answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the first test\n");
failed = true;
end
--Test 2
str = "100";
correctAnswer = false;
answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the second test\n");
failed = true;
end
--Test 3
str = "";
correctAnswer = true;
answer = isPalindrome(str);
if(answer ~= correctAnswer) then
io.write("isPalindrome failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("isPalindrome passed all tests\n");
end
end
local function testToBin()
local failed = false; --Signals whether a test was failed
--Test 1
local num = 7;
local correctAnswer = "111";
local answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the first test\n");
failed = true;
end
--Test 2
num = 0;
correctAnswer = "0";
answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the second test\n");
failed = true;
end
--Test 3
num = 1000000;
correctAnswer = "11110100001001000000";
answer = toBin(num);
if(answer ~= correctAnswer) then
io.write("toBin failed the third test\n");
failed = true;
end
--Print a message if all of the tests passed
if(not failed) then
io.write("toBin passed all tests\n");
end
end
--Create the timer to time each test
local timer = Stopwatch:create();
@@ -446,3 +517,15 @@ timer:start();
testFactorial();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test isPalindrome
timer:start();
testIsPalindrome();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");
--Test toBin
timer:start();
testToBin();
timer:stop();
io.write("It took " .. timer:getString() .. " to run this test\n");