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

© Copyright 2020 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
--[[---------------------------------------------------------------------------------------]]
-- Comments start with two dashes
--[[
Mulitline comments
start with two dashes and two left brackets.
They end with two right brackets. ]]

title = "Simple Sleep Test"

require([[defines]]);
require([[functions]]);

--[[
When using this module, all variables (including functions!) must be declared through 
a regular assignment (even assigning nil will do) in a strict scope before being used 
anywhere or assigned to inside a nested scope.
https://github.com/lua-stdlib/strict
]]
local _ENV = require 'std.strict' (_G)	-- strict.lua will detect undeclared global variables
local _;
--
local SleepTime = 10;
local IntNo = 9;
local tstval = 156;
local ErrorCount = 0;

--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
--
print("setup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());
--
ClearGlobals();		-- clear global variables
local val = GetGlobalInt(IntNo);
print("GetGlobalInt No " .. IntNo .. " returned " .. tostring(val) .. " should be 0");
		if (val ~= 0) then
			print("1 Global Int " .. IntNo .. " not set correctly (0)");
			ErrorCount = ErrorCount + 1;
			end;
local flg = SetGlobalInt(IntNo, tstval);
print("SetGlobalInt No " .. IntNo .. " returned " .. tostring(flg) .. " should be true");
		if (flg ~= true) then
			print("2 Global Int " .. IntNo .. " not set correctly (true)");
			ErrorCount = ErrorCount + 1;
			end;
val = GetGlobalInt(IntNo);
print("GetGlobalInt No " .. IntNo .. " returned " .. tostring(val) .. " should be " .. tstval);
		if (val ~= tstval) then
			print("3 Global Int " .. IntNo .. " not set correctly (" .. tstval .. ")");
			ErrorCount = ErrorCount + 1;
			end;
local success =  TrySetGlobalInt(IntNo, tstval + 1);
print("TrySetGlobalInt " .. IntNo .. " returned " .. tostring(success) .. " should be false");
		if (success ~= false) then
			print("4 Global Int " .. IntNo .. " not set correctly (false)");
			ErrorCount = ErrorCount + 1;
			end;
local success =  TrySetGlobalInt(IntNo, tstval);
print("TrySetGlobalInt " .. IntNo .. " returned " .. tostring(success) .. " should be true");
		if (success ~= true) then
			print("4 Global Int " .. IntNo .. " not set correctly (true)");
			ErrorCount = ErrorCount + 1;
			end;
print("Sleep(" .. SleepTime .. ") seconds started at " .. GetCounter(ELAPSEDTIME) .. " seconds");
local retv = Sleep(SleepTime);
print("Return from Sleep(" .. SleepTime .. ") with value " .. tostring(retv) .. " at " .. GetCounter(ELAPSEDTIME) .. " seconds");
return true;
end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
--
print("loop : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug() .. " at " .. GetCounter(ELAPSEDTIME) .. " seconds");

Sleep(1);
return Stop("Done");	-- false=do not loop, true=continue to loop
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
--
print("cleanup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug() .. " at " .. GetCounter(ELAPSEDTIME) .. " seconds");
if (ErrorCount == 0) then
	printc(clGreen, "No Errors");
	else
	printc(clRed, ErrorCount .. " Errors");
	end;
return true;
end
--[[---------------------------------------------------------------------------------------]]
