-- Module to test Calendar package
--calTools = require "CalTools"; -- "require" not available in LOTRO
calTools = FrostyPlugins.Calendar.CalTools;
TestCalendar = {};
---------------------------------------------------------------------------
local function TestCalendar:testIsLeapYear()
print "\ntestIsLeapYear ...";
local rslt = true; -- assume success
-- One year has the length of 365 days, 5 hours, 48 minutes and 47 seconds.
-- Because this is rather unfunctional, a normal year has been given 365 days
-- and a leap year 366 days.At leap years February 29th is added, which doesn't
-- exist in a normal year. A leap year is every 4 years, but not every 100 years,
-- then again every 400 years.
local expectedData = {
[2012] = true,
[1999] = false,
[1996] = true,
[1998] = false,
[100] = false,
[200] = false,
[400] = true,
[600] = false,
[800] = true,
};
-- print column headers
local columnHeaders = {" year:"," actual:"," expected:",""};
print("\t" .. table.concat(columnHeaders));
local fmt = "\t%" .. #columnHeaders[1] .. "s%" ..
#columnHeaders[2] .. "s%" ..
#columnHeaders[3] .. "s%s";
-- run each test case
for k,v in pairs(expectedData) do
actual = calTools.isLeapYear(k);
rslt = rslt and (v == actual);
print(string.format(fmt,k,
( v and "TRUE" or "FALSE"),
( actual and "TRUE" or "FALSE"),
(actual == v and "" or " <<< FAIL")));
end
print("testIsLeapYear: " .. (rslt and "PASS" or "FAIL"));
return rslt;
end
---------------------------------------------------------------------------
local function TestCalendar.testDaysPerMonth()
print "\ntestDaysPerMonth ...";
local rslt = true; -- assume success
-- some test data
local expectedDaysPerMonth = {
-- month, year, days
{ month=1, year=1999, days=31 },
{ month=2, year=1999, days=28 },
{ month=3, year=1999, days=31 },
{ month=4, year=1999, days=30 },
{ month=5, year=1999, days=31 },
{ month=6, year=1999, days=30 },
{ month=7, year=1999, days=31 },
{ month=8, year=1999, days=31 },
{ month=9, year=1999, days=30 },
{ month=10, year=1999, days=31 },
{ month=11, year=1999, days=30 },
{ month=12, year=1999, days=31 },
{ month=2, year=2012, days=29 }, -- A leap year
};
-- print column headers
local columnHeaders = {" MM/YYYY"," days:"," expected:",""};
print("\t" .. table.concat(columnHeaders));
local fmt = "\t%" .. #columnHeaders[1] .. "s%" ..
#columnHeaders[2] .. "s%" ..
#columnHeaders[3] .. "s%s";
for k,v in pairs(expectedDaysPerMonth) do
local expected = v.days;
local actual = calTools.daysInMonth(v.month, v.year);
rslt = rslt and (expected == actual);
print(string.format(fmt,
(v.month .. "/" .. v.year),
actual,
expected,
(actual == expected and "" or " <<< FAIL")));
end
print("\ntestDaysPerMonth: " .. (rslt and "PASS" or "FAIL"));
return rslt;
end
---------------------------------------------------------------------------
local function TestCalendar.testDayOfWeek()
print "\ntestDayOfWeek ...";
local rslt = true; -- assume success
-- some test data
local expectedDaysPerMonth = {
-- month, day, year, weekday(0=Sunday,1=Monday...)
{ month=4, day=5, year=2017, weekday=3 },
{ month=1, day=1, year=2015, weekday=4 },
{ month=2, day=1, year=2015, weekday=0 },
{ month=3, day=1, year=2015, weekday=0 },
{ month=4, day=1, year=2015, weekday=3 },
{ month=5, day=1, year=2015, weekday=5 },
{ month=6, day=1, year=2015, weekday=1 },
{ month=7, day=1, year=2015, weekday=3 },
{ month=8, day=1, year=2015, weekday=6 },
{ month=9, day=1, year=2015, weekday=2 },
{ month=10, day=1, year=2015, weekday=4 },
{ month=11, day=1, year=2015, weekday=0 },
{ month=12, day=1, year=2015, weekday=2 },
{ month=2, day=29, year=2012, weekday=3 }, -- A leap year
};
-- print column headers
local columnHeaders = {" MM/DD/YYYY"," weekday:"," expected:",""};
print("\t" .. table.concat(columnHeaders));
local fmt = "\t%" .. #columnHeaders[1] .. "s%" ..
#columnHeaders[2] .. "s%" ..
#columnHeaders[3] .. "s%s";
for k,v in pairs(expectedDaysPerMonth) do
local expected = v.weekday;
local actual = calTools.dayOfWeek(v.year, v.month, v.day);
rslt = rslt and (expected == actual);
print(string.format(fmt,
(v.month .. "/" .. v.day .. "/" .. v.year),
actual,
expected,
(actual == expected and "" or " <<< FAIL")));
end
print("\ntestDayOfWeek: " .. (rslt and "PASS" or "FAIL"));
return rslt;
end
---------------------------------------------------------------------------
function TestCalendar.tests()
return TestCalendar.testIsLeapYear() and
TestCalendar.testDaysPerMonth() and
TestCalendar.testDayOfWeek();
end
return TestCalendar;