--[[
---------------------------------------------------------------------------
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
--
-- times depends on garbage collection
--
title = "Sleep Test from loop()"
require("defines");
require("functions");

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

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
	-- the setup() function is called once at the beginning of the run
	print("setup(): Engine Number " .. Engine .. " TIU Number " .. TIU .. " Debug " .. Debug());
	return true;		-- false=setup failed, true=setup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
	print("begin loop() Sleep(5) call");
	b4 = RunTime();
	Sleep(5.0);
	now = RunTime();
	print("end loop() Sleep(5) call");
	now = RunTime();
	print(string.format("loop b4 %.2f", b4))
	print(string.format("loop now %.2f", now))
	if ((now - b4 - 5.0) > Accuracy) then
			print("Error : RunTime() vs Sleep() Delta > Accuracy ----------------------------");
			ErrorCount = ErrorCount + 1;
			end;	
	return true;		-- false=loop failed, true=loop succeeded
	end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
	-- the cleanup() function is called once when the [STOP] button is pressed
	print("Almost Done!");
	print("begin cleanup() Sleep(5) call");
	b4 = RunTime();
	retv = Sleep(5.0);
	now = RunTime();
	print(string.format("cleanup b4 %.2f", b4));
	print("end cleanup() Sleep(5) call (" .. (retv and "true" or "false") .. ")");
	
	print(string.format("cleanup now %.2f", now));
	if ((now - b4 - 5.0) > Accuracy) then
			ErrorCount = ErrorCount + 1;
			end;	
	if (ErrorCount == 0) then
		print("No Errors");
		else
		print(ErrorCount .. " Errors");
		end;
	return true;		-- false=cleanup failed, true=cleanup succeeded
	end
--[[---------------------------------------------------------------------------------------]]
