From 4e9b21c60232e252b66bf204e76efdfe1339b32b Mon Sep 17 00:00:00 2001 From: Matthew Ellison Date: Fri, 29 Mar 2019 12:35:05 -0400 Subject: [PATCH] Added a test script for the Stopwatch class --- testStopwatch.lua | 84 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 testStopwatch.lua diff --git a/testStopwatch.lua b/testStopwatch.lua new file mode 100644 index 0000000..5d8db13 --- /dev/null +++ b/testStopwatch.lua @@ -0,0 +1,84 @@ +--luaClasses/testStopwatch.lua +--Matthew Ellison +-- Created: 03-29-19 +--Modified: 03-29-19 +--This script is used to test the stopwatch class + + +require "Stopwatch" + + +--Setup a stopwatch +timer = Stopwatch:create(); + +--Try to get the time before starting the timer +test = timer:getSeconds(); +if(test ~= -1) then + io.write("Failed test of getting time before starting the timer\n"); + io.write("Value should be -1 but is " .. test .. '\n'); +end + +--Start the timer +timer:start(); + +--Do a loop to run up some time +for cnt = 1, 100000 do + cnt = cnt; +end + +--Stop the timer +timer:stop(); + +--Test the different resolutions +baseTime = timer:getSeconds(); --Start with seconds because that is lua's native time resolution +--Test microseconds +if(timer:getMicroseconds() ~= (baseTime * 1000000)) then + io.write("Failed microsecond test\n"); +else + io.write("Passed the microsecond test\n"); +end + +--Test milliseconds +if(timer:getMilliseconds() ~= (baseTime * 1000)) then + io.write("Failed millisecond test\n"); +else + io.write("Passed the millisecond test\n"); +end + +--Test Minutes +if(timer:getMinutes() ~= (baseTime / 60)) then + io.write("Failed minute test\n"); +else + io.write("Passed the minute test\n"); +end + +--Test Hours +if(timer:getHours() ~= (baseTime / 3600)) then + io.write("Failed hour test\n"); +else + io.write("Passed the hour test\n"); +end + +--Print the results +io.write("\nHere is a printing of the different times, starting with the string:\n"); +io.write("It took " .. timer:getString() .. " to run this algorithm\n"); +io.write("It took " .. timer:getMicroseconds() .. " microseconds to run this algorithm\n"); +io.write("It took " .. timer:getMilliseconds() .. " milliseconds to run this algorithm\n"); +io.write("It took " .. timer:getSeconds() .. " seconds to run this algorithm\n"); +io.write("It took " .. timer:getMinutes() .. " minutes to run this algorithm\n"); +io.write("It took " .. timer:getHours() .. " hours to run this algorithm\n"); + +--[[ Results: +Passed the microsecond test +Passed the millisecond test +Passed the minute test +Passed the hour test + +Here is a printing of the different times, starting with the string: +It took 383 microseconds to run this algorithm +It took 383.0 microseconds to run this algorithm +It took 0.383 milliseconds to run this algorithm +It took 0.000383 seconds to run this algorithm +It took 6.3833333333333e-06 minutes to run this algorithm +It took 1.0638888888889e-07 hours to run this algorithm +]]