#include "ntp_stdlib.h" /* test fail without this include, for some reason */
#include "ntp_calendar.h"
#include "ntp_calgps.h"
#include "ntp_unixtime.h"
#include "ntp_fp.h"
#include "unity.h"
#include <string.h>
static char mbuf[128];
static int leapdays(int year);
void setUp(void);
int isGT(int first, int second);
int leapdays(int year);
char * CalendarFromCalToString(const struct calendar *cal);
char * CalendarFromIsoToString(const struct isodate *iso);
int IsEqualCal(const struct calendar *expected, const struct calendar *actual);
int IsEqualIso(const struct isodate *expected, const struct isodate *actual);
char * DateFromCalToString(const struct calendar *cal);
char * DateFromIsoToString(const struct isodate *iso);
int IsEqualDateCal(const struct calendar *expected, const struct calendar *actual);
int IsEqualDateIso(const struct isodate *expected, const struct isodate *actual);
/* test the day/sec join & split ops, making sure that 32bit
* intermediate results would definitely overflow and the hi DWORD of
* the 'vint64' is definitely needed.
*/
void
test_DaySplitMerge(void)
{
int32 day,sec;
for (day = -1000000; day <= 1000000; day += 100) {
for (sec = -100000; sec <= 186400; sec += 10000) {
vint64 merge;
ntpcal_split split;
int32 eday;
int32 esec;
/* Full roundtrip from 1601-01-01 to 2400-12-31
* checks sequence of rata die numbers and validates date output
* (since the input is all nominal days of the calendar in that range
* and the result of the inverse calculation must match the input no
* invalid output can occur.)
*/
void
test_RoundTripDate(void)
{
struct calendar truDate, expDate = { 1600, 0, 12, 31 };;
int leaps;
int32 truRdn, expRdn = ntpcal_date_to_rd(&expDate);
/* ---------------------------------------------------------------------
* ISO8601 week calendar internals
*
* The ISO8601 week calendar implementation is simple in the terms of
* the math involved, but the implementation of the calculations must
* take care of a few things like overflow, floor division, and sign
* corrections.
*
* Most of the functions are straight forward, but converting from years
* to weeks and from weeks to years warrants some extra tests. These use
* an independent reference implementation of the conversion from years
* to weeks.
* ---------------------------------------------------------------------
*/
/* helper / reference implementation for the first week of year in the
* ISO8601 week calendar. This is based on the reference definition of
* the ISO week calendar start: The Monday closest to January,1st of the
* corresponding year in the Gregorian calendar.
*/
static int32_t
refimpl_WeeksInIsoYears(
int32_t years)
{
int32_t days, weeks;
days = ntpcal_weekday_close(
ntpcal_days_in_years(years) + 1,
CAL_MONDAY) - 1;
/* the weekday functions operate on RDN, while we want elapsed
* units here -- we have to add / sub 1 in the midlle / at the
* end of the operation that gets us the first day of the ISO
* week calendar day.
*/
weeks = days / 7;
days = days % 7;
TEST_ASSERT_EQUAL(0, days); /* paranoia check... */
return weeks;
}
/* The next tests loop over 5000yrs, but should still be very fast. If
* they are not, the calendar needs a better implementation...
*/
void
test_IsoCalYearsToWeeks(void)
{
int32_t years;
int32_t wref, wcal;
for (years = -1000; years < 4000; ++years) {
/* get number of weeks before years (reference) */
wref = refimpl_WeeksInIsoYears(years);
/* get number of weeks before years (object-under-test) */
wcal = isocal_weeks_in_years(years);
TEST_ASSERT_EQUAL(wref, wcal);
}
days = ntpcal_daysec_to_date(&cal, -86400);
TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==0),
"failed for -86400");
days = ntpcal_daysec_to_date(&cal, -86399);
TEST_ASSERT_MESSAGE((days==-1 && cal.hour==0 && cal.minute==0 && cal.second==1),
"failed for -86399");
days = ntpcal_daysec_to_date(&cal, -1);
TEST_ASSERT_MESSAGE((days==-1 && cal.hour==23 && cal.minute==59 && cal.second==59),
"failed for -1");
days = ntpcal_daysec_to_date(&cal, 0);
TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==0),
"failed for 0");
days = ntpcal_daysec_to_date(&cal, 1);
TEST_ASSERT_MESSAGE((days==0 && cal.hour==0 && cal.minute==0 && cal.second==1),
"failed for 1");
days = ntpcal_daysec_to_date(&cal, 86399);
TEST_ASSERT_MESSAGE((days==0 && cal.hour==23 && cal.minute==59 && cal.second==59),
"failed for 86399");
days = ntpcal_daysec_to_date(&cal, 86400);
TEST_ASSERT_MESSAGE((days==1 && cal.hour==0 && cal.minute==0 && cal.second==0),
"failed for 86400");
return;
}
/* --------------------------------------------------------------------
* unfolding of (truncated) NTP time stamps to full 64bit values.
*
* Note: These tests need a 64bit time_t to be useful.
*/
void
test_NtpToNtp(void)
{
# if SIZEOF_TIME_T <= 4
TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
pivot = 0;
for (loops = 0; loops < 16; ++loops) {
for (iloop = 0; iloop < 6; ++iloop) {
hold = ntpcal_ntp_to_ntp(
ntp_vals[iloop], &pivot);
texp = vint64_to_time(&hold);
/* constraint 1: texp must be in the
* (right-open) intervall [p-(2^31), p+(2^31)[,
* but the pivot 'p' must be taken in full NTP
* time scale!
*/
diff = texp - (pivot + JAN_1970);
snprintf(lbuf, sizeof(lbuf),
"bounds check: piv=%lld exp=%lld dif=%lld",
(long long)pivot,
(long long)texp,
(long long)diff);
TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
lbuf);
/* constraint 2: low word must be equal to
* input
*/
snprintf(lbuf, sizeof(lbuf),
"low check: ntp(in)=$%08lu ntp(out[0:31])=$%08lu",
(unsigned long)ntp_vals[iloop],
(unsigned long)hold.D_s.lo);
TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], hold.D_s.lo, lbuf);
}
pivot += 0x20000000;
}
# endif
}
void
test_NtpToTime(void)
{
# if SIZEOF_TIME_T <= 4
TEST_IGNORE_MESSAGE("test only useful for sizeof(time_t) > 4, skipped");
pivot = 0;
for (loops = 0; loops < 16; ++loops) {
for (iloop = 0; iloop < 6; ++iloop) {
hold = ntpcal_ntp_to_time(
ntp_vals[iloop], &pivot);
texp = vint64_to_time(&hold);
/* constraint 1: texp must be in the
* (right-open) intervall [p-(2^31), p+(2^31)[
*/
diff = texp - pivot;
snprintf(lbuf, sizeof(lbuf),
"bounds check: piv=%lld exp=%lld dif=%lld",
(long long)pivot,
(long long)texp,
(long long)diff);
TEST_ASSERT_MESSAGE((diff >= INT32_MIN) && (diff <= INT32_MAX),
lbuf);
/* constraint 2: conversion from full time back
* to truncated NTP time must yield same result
* as input.
*/
back = (uint32_t)texp + JAN_1970;
snprintf(lbuf, sizeof(lbuf),
"modulo check: ntp(in)=$%08lu ntp(out)=$%08lu",
(unsigned long)ntp_vals[iloop],
(unsigned long)back);
TEST_ASSERT_EQUAL_MESSAGE(ntp_vals[iloop], back, lbuf);
}
pivot += 0x20000000;
}
# endif
}
/* test on 2nd rollover, April 2019
* we set the base date properly one week *before the rollover, to
* check if the expansion merrily hops over the warp.
*/
basedate_set_day(2047 * 7 + NTP_TO_GPS_DAYS);
strToCal(&jd, "19-04-03T12:00:00");
gps = gpscal_from_calendar(&jd, fpz);
TEST_ASSERT_EQUAL_MESSAGE(week0, gps.weeks, "(week test 1))");
TEST_ASSERT_EQUAL_MESSAGE(wsec1, gps.wsecs, "(secs test 1)");
strToCal(&jd, "19-04-06T23:59:59");
gps = gpscal_from_calendar(&jd, fpz);
TEST_ASSERT_EQUAL_MESSAGE(week0, gps.weeks, "(week test 2)");
TEST_ASSERT_EQUAL_MESSAGE(wsec2, gps.wsecs, "(secs test 2)");
strToCal(&jd, "19-04-07T00:00:00");
gps = gpscal_from_calendar(&jd, fpz);
TEST_ASSERT_EQUAL_MESSAGE(week1, gps.weeks, "(week test 3)");
TEST_ASSERT_EQUAL_MESSAGE( 0 , gps.wsecs, "(secs test 3)");
strToCal(&jd, "19-04-10T12:00:00");
gps = gpscal_from_calendar(&jd, fpz);
TEST_ASSERT_EQUAL_MESSAGE(week1, gps.weeks, "(week test 4)");
TEST_ASSERT_EQUAL_MESSAGE(wsec1, gps.wsecs, "(secs test 4)");
}
/* Century expansion tests. Reverse application of Zeller's congruence,
* sort of... hence the name "Rellez", Zeller backwards. Just in case
* you didn't notice ;)
*/
void test_RellezEra(void);
void test_RellezEra(void)
{
static const unsigned int mt[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
unsigned int yi, yo, m, d, wd;
/* last day before our era -- fold forward */
yi = 1899;
m = 12;
d = 31;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = ntpcal_expand_century((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(2299, yo, mbuf);
/* 1st day after our era -- fold back */
yi = 2300;
m = 1;
d = 1;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = ntpcal_expand_century((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(1900, yo, mbuf);
/* test every month in our 400y era */
for (yi = 1900; yi < 2300; ++yi) {
for (m = 1; m < 12; ++m) {
/* test first day of month */
d = 1;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = ntpcal_expand_century((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(yi, yo, mbuf);
/* test last day of month */
d = mt[m] + (m == 2 && is_leapyear(yi));
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = ntpcal_expand_century((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(yi, yo, mbuf);
}
}
}
/* This is nearly a verbatim copy of the in-situ implementation of
* Zeller's congruence in libparse/clk_rawdcf.c, so the algorithm
* can be tested.
*/
static int
zeller_expand(
unsigned int y,
unsigned int m,
unsigned int d,
unsigned int wd
)
{
unsigned int c;
if ((m += 10u) >= 12u)
m -= 12u;
else if (--y >= 100u)
y += 100u;
d += y + (y >> 2) + 2u;
d += (m * 83u + 16u) >> 5;
c = (((252u + wd - d) * 0x6db6db6eU) >> 29) & 7u;
if (c > 3u)
return 0;
if ((m > 9u) && (++y >= 100u)) {
y -= 100u;
c = (c + 1) & 3u;
}
y += (c * 100u);
y += (y < 370u) ? 2000 : 1600;
return (int)y;
}
void test_zellerDirect(void);
void test_zellerDirect(void)
{
static const unsigned int mt[13] = { 0, 31,28,31,30,31,30,31,31,30,31,30,31 };
unsigned int yi, yo, m, d, wd;
/* last day before our era -- fold forward */
yi = 1969;
m = 12;
d = 31;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = zeller_expand((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(2369, yo, mbuf);
/* 1st day after our era -- fold back */
yi = 2370;
m = 1;
d = 1;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = zeller_expand((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(1970, yo, mbuf);
/* test every month in our 400y era */
for (yi = 1970; yi < 2370; ++yi) {
for (m = 1; m < 12; ++m) {
/* test first day of month */
d = 1;
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = zeller_expand((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(yi, yo, mbuf);
/* test last day of month */
d = mt[m] + (m == 2 && is_leapyear(yi));
wd = ntpcal_edate_to_eradays(yi-1, m-1, d-1) % 7 + 1;
yo = zeller_expand((yi%100), m, d, wd);
snprintf(mbuf, sizeof(mbuf), "failed, di=%04u-%02u-%02u, wd=%u",
yi, m, d, wd);
TEST_ASSERT_EQUAL_MESSAGE(yi, yo, mbuf);
}
}
}
void test_ZellerDirectBad(void);
void test_ZellerDirectBad(void)
{
unsigned int y, n, wd;
for (y = 2001; y < 2101; ++y) {
wd = ntpcal_edate_to_eradays(y-1, 0, 0) % 7 + 1;
/* move 4 centuries ahead */
wd = (wd + 5) % 7 + 1;
for (n = 0; n < 3; ++n) {
TEST_ASSERT_EQUAL(0, zeller_expand((y%100), 1, 1, wd));
wd = (wd + 4) % 7 + 1;
}
}
}
void test_zellerModInv(void);
void test_zellerModInv(void)
{
unsigned int i, r1, r2;