--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2019 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control 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 General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]

-- Semicolons are not required in Lua code
title = "Timers Test"

require("defines");
require("functions");

LastRunTime = 0.0;
LastMSTime = 0.0;
ErrorCount = 0;
Accuracy = 0.101;		-- looking for all timers equal within 0.1 seconds +/- rounding error

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- this function is called once when the [Run Script] button is pushed
	print("setup()");
	-- Engine number selected on the Main window is used
	if (Debug() > 0) then print(string.format("Debug level %d", Debug())); end
	print("Accuracy set to " .. Accuracy);

	for iSecs = 1,25 do
		print(string.format("Test Value %d Seconds", iSecs));
		-- run for one test period
		LastRunTime = RunTime();
		LastMSTime = MicroSeconds()/1000000;
		Sleep(iSecs);			
		-- read up RunTime and MicroSeconds
		CurrentRunTime = RunTime();
		CurrentMSTime = MicroSeconds()/1000000;
		RunTimeDelta = CurrentRunTime - LastRunTime;
		MSTimeDelta = CurrentMSTime - LastMSTime;
		print(string.format("Run Time = %.2f (%.2f-%.2f)", RunTimeDelta, CurrentRunTime, LastRunTime)); 
		print(string.format("MS Time = %.2f (%.2f-%.2f)", MSTimeDelta, CurrentMSTime, LastMSTime)); 
		if ((RunTimeDelta - MSTimeDelta) > Accuracy) then
			print("Error : MicroSeconds() vs RunTime() Delta > Accuracy ----------------------------");
			ErrorCount = ErrorCount + 1;
			end;
		if ((RunTimeDelta - iSecs) > Accuracy) then
			print("Error : RunTime() vs Sleep() Delta > Accuracy ----------------------------");
			ErrorCount = ErrorCount + 1;
			end;			
		if ((MSTimeDelta - iSecs) > Accuracy) then
			print("Error : MicroSeconds() vs Sleep() Delta > Accuracy ----------------------------");
			ErrorCount = ErrorCount + 1;
			end;			
		end
	return Stop("Done");	-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- this function is called once when the user presses the [STOP] button
	print("cleanup()");
	if (ErrorCount == 0) then
		print("No Errors");
		else
		print(ErrorCount .. " Errors");		
		end;
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
