[Git][NTPsec/ntpsec][master] 2 commits: Setup to add filegen tests
Hal Murray
gitlab at mg.gitlab.com
Sat Mar 25 08:53:59 UTC 2017
Hal Murray pushed to branch master at NTPsec / ntpsec
Commits:
20e71e08 by Hal Murray at 2017-03-24T21:59:05-07:00
Setup to add filegen tests
- - - - -
254c56a3 by Hal Murray at 2017-03-25T01:53:00-07:00
Remove unused calendar code
Lots more to go.
- - - - -
4 changed files:
- include/ntp_calendar.h
- libntp/ntp_calendar.c
- + tests/ntpd/filegen.c
- tests/wscript
Changes:
=====================================
include/ntp_calendar.h
=====================================
--- a/include/ntp_calendar.h
+++ b/include/ntp_calendar.h
@@ -195,17 +195,10 @@ extern int
ntpcal_rd_to_date(struct calendar * /* jt */, int32_t /* rd */);
/*
- * Convert a RataDie number into the date part of a 'struct
- * tm'. Return false if the year is regular year, true if the year is a leap
- * year.
- */
-extern bool
-ntpcal_rd_to_tm(struct tm * /* utm */, int32_t /* rd */);
-
-/*
* Take a value of seconds since midnight and split it into hhmmss in
* a 'struct calendar'. Return excessive days.
*/
+/* used by ntpd/refclock_nmea.c */
extern int32_t
ntpcal_daysec_to_date(struct calendar * /* jt */, int32_t /* secs */);
@@ -213,31 +206,14 @@ ntpcal_daysec_to_date(struct calendar * /* jt */, int32_t /* secs */);
* Take the time part of a 'struct calendar' and return the seconds
* since midnight.
*/
+/* used by ntpd/refclock_nmea.c */
extern int32_t
ntpcal_date_to_daysec(const struct calendar *);
-/*
- * Take a value of seconds since midnight and split it into hhmmss in
- * a 'struct tm'. Return excessive days.
- */
-extern int32_t
-ntpcal_daysec_to_tm(struct tm * /* utm */, int32_t /* secs */);
-
+/* used by ntpd/refclock_gpsd.c */
extern int32_t
ntpcal_tm_to_daysec(const struct tm * /* utm */);
-/*
- * convert a year number to rata die of year start
- */
-extern int32_t
-ntpcal_year_to_ystart(int32_t /* year */);
-
-/*
- * For a given RataDie, get the RataDie of the associated year start,
- * that is, the RataDie of the last January,1st on or before that day.
- */
-extern int32_t
-ntpcal_rd_to_ystart(int32_t /* rd */);
/*
* convert a RataDie to the RataDie of start of the calendar month.
@@ -246,17 +222,11 @@ extern int32_t
ntpcal_rd_to_mstart(int32_t /* year */);
-extern int
-ntpcal_daysplit_to_date(struct calendar * /* jt */,
- const ntpcal_split * /* ds */, int32_t /* dof */);
-
-extern int
-ntpcal_daysplit_to_tm(struct tm * /* utm */, const ntpcal_split * /* ds */,
- int32_t /* dof */);
extern int
ntpcal_time_to_date(struct calendar * /* jd */, const time64_t /* ts */);
+/* used by ntpd/refclock_nmea.c */
extern int32_t
ntpcal_periodic_extend(int32_t /* pivot */, int32_t /* value */,
int32_t /* cycle */);
@@ -264,63 +234,12 @@ ntpcal_periodic_extend(int32_t /* pivot */, int32_t /* value */,
extern int
ntpcal_ntp64_to_date(struct calendar * /* jd */, const time64_t /* ntp */);
+/* used by ntpd/refclock_nmea.c */
extern int
ntpcal_ntp_to_date(struct calendar * /* jd */, uint32_t /* ntp */,
const time_t * /* pivot */);
-extern time64_t
-ntpcal_date_to_ntp64(const struct calendar * /* jd */);
-
-extern uint32_t
-ntpcal_date_to_ntp(const struct calendar * /* jd */);
-
-extern time_t
-ntpcal_date_to_time(const struct calendar * /* jd */);
-/*
- * ISO week-calendar conversions
- */
-extern int32_t
-isocal_weeks_in_years(int32_t /* years */);
-
-extern ntpcal_split
-isocal_split_eraweeks(int32_t /* weeks */);
-
-extern int
-isocal_ntp64_to_date(struct isodate * /* id */, const time64_t /* ntp */);
-
-extern int
-isocal_ntp_to_date(struct isodate * /* id */, uint32_t /* ntp */,
- const time_t * /* pivot */);
-
-extern time64_t
-isocal_date_to_ntp64(const struct isodate * /* id */);
-
-extern uint32_t
-isocal_date_to_ntp(const struct isodate * /* id */);
-
-
-/*
- * day-of-week calculations
- *
- * Given a RataDie and a day-of-week, calculate a RDN that is reater-than,
- * greater-or equal, closest, less-or-equal or less-than the given RDN
- * and denotes the given day-of-week
- */
-extern int32_t
-ntpcal_weekday_gt(int32_t /* rdn */, int32_t /* dow */);
-
-extern int32_t
-ntpcal_weekday_ge(int32_t /* rdn */, int32_t /* dow */);
-
-extern int32_t
-ntpcal_weekday_close(int32_t /* rdn */, int32_t /* dow */);
-
-extern int32_t
-ntpcal_weekday_le(int32_t /* rdn */, int32_t /* dow */);
-
-extern int32_t
-ntpcal_weekday_lt(int32_t /* rdn */, int32_t /* dow */);
/*
* Additional support stuff for Ed Rheingold's calendrical calculations
=====================================
libntp/ntp_calendar.c
=====================================
--- a/libntp/ntp_calendar.c
+++ b/libntp/ntp_calendar.c
@@ -628,38 +628,6 @@ ntpcal_rd_to_date(
return retv ? retv : leaps;
}
-/*
- *---------------------------------------------------------------------
- * Convert a RD into the date part of a 'struct tm'.
- *---------------------------------------------------------------------
- */
-bool
-ntpcal_rd_to_tm(
- struct tm *utm,
- int32_t rd
- )
-{
- ntpcal_split split;
- int32_t leaps;
-
- leaps = 0;
- /* get day-of-week first */
- utm->tm_wday = rd % 7;
- if (utm->tm_wday < 0)
- utm->tm_wday += 7;
-
- /* get year and day-of-year */
- split = ntpcal_split_eradays(rd - 1, &leaps);
- utm->tm_year = split.hi - 1899;
- utm->tm_yday = split.lo; /* 0-based */
-
- /* convert to month and mday */
- split = ntpcal_split_yeardays(split.lo, leaps);
- utm->tm_mon = split.hi; /* 0-based */
- utm->tm_mday = split.lo + 1; /* 1-based */
-
- return leaps != 0;
-}
/*
*---------------------------------------------------------------------
@@ -684,71 +652,7 @@ ntpcal_daysec_to_date(
return days;
}
-/*
- *---------------------------------------------------------------------
- * Take a value of seconds since midnight and split it into hhmmss in a
- * 'struct tm'.
- *---------------------------------------------------------------------
- */
-int32_t
-ntpcal_daysec_to_tm(
- struct tm *utm,
- int32_t sec
- )
-{
- int32_t days;
- int32_t ts[3];
- days = priv_timesplit(ts, sec);
- utm->tm_hour = ts[0];
- utm->tm_min = ts[1];
- utm->tm_sec = ts[2];
-
- return days;
-}
-
-/*
- *---------------------------------------------------------------------
- * take a split representation for day/second-of-day and day offset
- * and convert it to a 'struct calendar'. The seconds will be normalised
- * into the range of a day, and the day will be adjusted accordingly.
- *
- * returns >0 if the result is in a leap year, 0 if in a regular
- * year and <0 if the result did not fit into the calendar struct.
- *---------------------------------------------------------------------
- */
-int
-ntpcal_daysplit_to_date(
- struct calendar *jd,
- const ntpcal_split *ds,
- int32_t dof
- )
-{
- dof += ntpcal_daysec_to_date(jd, ds->lo);
- return ntpcal_rd_to_date(jd, ds->hi + dof);
-}
-
-/*
- *---------------------------------------------------------------------
- * take a split representation for day/second-of-day and day offset
- * and convert it to a 'struct tm'. The seconds will be normalised
- * into the range of a day, and the day will be adjusted accordingly.
- *
- * returns 1 if the result is in a leap year and zero if in a regular
- * year.
- *---------------------------------------------------------------------
- */
-int
-ntpcal_daysplit_to_tm(
- struct tm *utm,
- const ntpcal_split *ds ,
- int32_t dof
- )
-{
- dof += ntpcal_daysec_to_tm(utm, ds->lo);
-
- return ntpcal_rd_to_tm(utm, ds->hi + dof);
-}
/*
*---------------------------------------------------------------------
@@ -1005,38 +909,6 @@ ntpcal_date_to_rd(
/*
*---------------------------------------------------------------------
- * convert a year number to rata die of year start
- *---------------------------------------------------------------------
- */
-int32_t
-ntpcal_year_to_ystart(
- int32_t year
- )
-{
- return ntpcal_days_in_years(year - 1) + 1;
-}
-
-/*
- *---------------------------------------------------------------------
- * For a given RD, get the RD of the associated year start,
- * that is, the RD of the last January,1st on or before that day.
- *---------------------------------------------------------------------
- */
-int32_t
-ntpcal_rd_to_ystart(
- int32_t rd
- )
-{
- /*
- * Rather simple exercise: split the day number into elapsed
- * years and elapsed days, then remove the elapsed days from the
- * input value. Nice'n sweet...
- */
- return rd - ntpcal_split_eradays(rd - 1, NULL).lo;
-}
-
-/*
- *---------------------------------------------------------------------
* For a given RD, get the RD of the associated month start.
*---------------------------------------------------------------------
*/
@@ -1082,26 +954,6 @@ ntpcal_tm_to_daysec(
utm->tm_sec);
}
-/*
- *---------------------------------------------------------------------
- * take a 'struct calendar' and convert it to a 'time_t'
- *---------------------------------------------------------------------
- */
-time_t
-ntpcal_date_to_time(
- const struct calendar *jd
- )
-{
- time64_t join;
- int32_t days, secs;
-
- days = ntpcal_date_to_rd(jd) - DAY_UNIX_STARTS;
- secs = ntpcal_date_to_daysec(jd);
- join = ntpcal_dayjoin(days, secs);
-
- return time64_to_time(join);
-}
-
int
ntpcal_ntp64_to_date(
@@ -1136,315 +988,4 @@ ntpcal_ntp_to_date(
}
-time64_t
-ntpcal_date_to_ntp64(
- const struct calendar *jd
- )
-{
- /*
- * Convert date to NTP. Ignore yearday, use d/m/y only.
- */
- return ntpcal_dayjoin(ntpcal_date_to_rd(jd) - DAY_NTP_STARTS,
- ntpcal_date_to_daysec(jd));
-}
-
-
-uint32_t
-ntpcal_date_to_ntp(
- const struct calendar *jd
- )
-{
- /*
- * Get lower half of 64-bit NTP timestamp from date/time.
- */
- return time64lo(ntpcal_date_to_ntp64(jd));
-}
-
-
-
-/*
- * ==================================================================
- *
- * day-of-week calculations
- *
- * ==================================================================
- */
-/*
- * Given a RataDie and a day-of-week, calculate a RDN that is reater-than,
- * greater-or equal, closest, less-or-equal or less-than the given RDN
- * and denotes the given day-of-week
- */
-int32_t
-ntpcal_weekday_gt(
- int32_t rdn,
- int32_t dow
- )
-{
- return ntpcal_periodic_extend(rdn+1, dow, 7);
-}
-
-int32_t
-ntpcal_weekday_ge(
- int32_t rdn,
- int32_t dow
- )
-{
- return ntpcal_periodic_extend(rdn, dow, 7);
-}
-
-int32_t
-ntpcal_weekday_close(
- int32_t rdn,
- int32_t dow
- )
-{
- return ntpcal_periodic_extend(rdn-3, dow, 7);
-}
-
-int32_t
-ntpcal_weekday_le(
- int32_t rdn,
- int32_t dow
- )
-{
- return ntpcal_periodic_extend(rdn, dow, -7);
-}
-
-int32_t
-ntpcal_weekday_lt(
- int32_t rdn,
- int32_t dow
- )
-{
- return ntpcal_periodic_extend(rdn-1, dow, -7);
-}
-
-/*
- * ==================================================================
- *
- * ISO week-calendar conversions
- *
- * The ISO 8601 calendar defines a calendar of years, weeks and weekdays.
- * It is related to the Gregorian calendar, and a ISO year starts at the
- * Monday closest to Jan,1st of the corresponding Gregorian year. A ISO
- * calendar year has always 52 or 53 weeks, and like the Grogrian
- * calendar the ISO 8601 calendar repeats itself every 400 years, or
- * 146097 days, or 20871 weeks.
- *
- * While it is possible to write ISO calendar functions based on the
- * Gregorian calendar functions, the following implementation takes a
- * different approach, based directly on years and weeks.
- *
- * Analysis of the tabulated data shows that it is not possible to
- * interpolate from years to weeks over a full 400 year range; cyclic
- * shifts over 400 years do not provide a solution here. But it *is*
- * possible to interpolate over every single century of the 400-year
- * cycle. (The centennial leap year rule seems to be the culprit here.)
- *
- * It can be shown that a conversion from years to weeks can be done
- * using a linear transformation of the form
- *
- * w = floor( y * a + b )
- *
- * where the slope a must hold to
- *
- * 52.1780821918 <= a < 52.1791044776
- *
- * and b must be chosen according to the selected slope and the number
- * of the century in a 400-year period.
- *
- * The inverse calculation can also be done in this way. Careful scaling
- * provides an unlimited set of integer coefficients a,k,b that enable
- * us to write the calulation in the form
- *
- * w = (y * a + b ) / k
- * y = (w * a' + b') / k'
- *
- * In this implementation the values of k and k' are chosen to be
- * smallest possible powers of two, so the division can be implemented
- * as shifts if the optimiser chooses to do so.
- *
- * ==================================================================
- */
-
-/*
- * Given a number of elapsed (ISO-)years since the begin of the
- * christian era, return the number of elapsed weeks corresponding to
- * the number of years.
- */
-int32_t
-isocal_weeks_in_years(
- int32_t years
- )
-{
- /*
- * use: w = (y * 53431 + b[c]) / 1024 as interpolation
- */
- static const int32_t bctab[4] = { 449, 157, 889, 597 };
- int32_t cycle; /* full gregorian cycle */
- int32_t cents; /* full centuries */
- int32_t weeks; /* accumulated weeks */
-
- /* split off full calendar cycles, using floor division */
- cycle = years / 400;
- years = years % 400;
- if (years < 0) {
- cycle -= 1;
- years += 400;
- }
-
- /* split off full centuries */
- cents = years / 100;
- years = years % 100;
-
- /*
- * calculate elapsed weeks, taking into account that the
- * first, third and fourth century have 5218 weeks but the
- * second century falls short by one week.
- */
- weeks = (years * 53431 + bctab[cents]) / 1024;
-
- return cycle * GREGORIAN_CYCLE_WEEKS
- + cents * 5218 - (cents > 1)
- + weeks;
-}
-
-/*
- * Given a number of elapsed weeks since the begin of the christian
- * era, split this number into the number of elapsed years in res.hi
- * and the excessive number of weeks in res.lo. (That is, res.lo is
- * the number of elapsed weeks in the remaining partial year.)
- */
-ntpcal_split
-isocal_split_eraweeks(
- int32_t weeks
- )
-{
- /*
- * use: y = (w * 157 + b[c]) / 8192 as interpolation
- */
- static const int32_t bctab[4] = { 85, 131, 17, 62 };
- ntpcal_split res;
- int32_t cents;
-
- /*
- * split off 400-year cycles, using the fact that a 400-year
- * cycle has 146097 days, which is exactly 20871 weeks.
- */
- res.hi = weeks / GREGORIAN_CYCLE_WEEKS;
- res.lo = weeks % GREGORIAN_CYCLE_WEEKS;
- if (res.lo < 0) {
- res.hi -= 1;
- res.lo += GREGORIAN_CYCLE_WEEKS;
- }
- res.hi *= 400;
-
- /*
- * split off centuries, taking into account that the first,
- * third and fourth century have 5218 weeks but that the
- * second century falls short by one week.
- */
- res.lo += (res.lo >= 10435);
- cents = res.lo / 5218;
- res.lo %= 5218; /* res.lo is weeks in century now */
-
- /* convert elapsed weeks in century to elapsed years and weeks */
- res.lo = res.lo * 157 + bctab[cents];
- res.hi += cents * 100 + res.lo / 8192;
- res.lo = (res.lo % 8192) / 157;
-
- return res;
-}
-
-/*
- * Given a second in the NTP time scale and a pivot, expand the NTP
- * time stamp around the pivot and convert into an ISO calendar time
- * stamp.
- */
-int
-isocal_ntp64_to_date(
- struct isodate *id,
- const time64_t ntp
- )
-{
- ntpcal_split ds;
- int32_t ts[3];
-
- /*
- * Split NTP time into days and seconds, shift days into CE
- * domain and process the parts.
- */
- ds = ntpcal_daysplit(ntp);
-
- /* split time part */
- ds.hi += priv_timesplit(ts, ds.lo);
- id->hour = (uint8_t)ts[0];
- id->minute = (uint8_t)ts[1];
- id->second = (uint8_t)ts[2];
-
- /* split date part */
- ds.lo = ds.hi + DAY_NTP_STARTS - 1; /* elapsed era days */
- ds.hi = ds.lo / 7; /* elapsed era weeks */
- ds.lo = ds.lo % 7; /* elapsed week days */
- if (ds.lo < 0) { /* floor division! */
- ds.hi -= 1;
-// ds.lo += 7;
- }
- id->weekday = (uint8_t)ds.lo + 1; /* weekday result */
-
- ds = isocal_split_eraweeks(ds.hi); /* elapsed years&week*/
- id->year = (uint16_t)ds.hi + 1; /* shift to current */
- id->week = (uint8_t )ds.lo + 1;
-
- return (ds.hi >= 0 && ds.hi < 0x0000FFFF);
-}
-
-int
-isocal_ntp_to_date(
- struct isodate *id,
- uint32_t ntp,
- const time_t *piv
- )
-{
- time64_t ntp64;
-
- /*
- * Unfold ntp time around current time into NTP domain, then
- * convert the full time stamp.
- */
- ntp64 = ntpcal_ntp_to_ntp(ntp, piv);
- return isocal_ntp64_to_date(id, ntp64);
-}
-
-/*
- * Convert a ISO date spec into a second in the NTP time scale,
- * properly truncated to 32 bit.
- */
-time64_t
-isocal_date_to_ntp64(
- const struct isodate *id
- )
-{
- int32_t weeks, days, secs;
-
- weeks = isocal_weeks_in_years((int32_t)id->year - 1)
- + (int32_t)id->week - 1;
- days = weeks * 7 + (int32_t)id->weekday;
- /* days is RDN of ISO date now */
- secs = ntpcal_etime_to_seconds(id->hour, id->minute, id->second);
-
- return ntpcal_dayjoin(days - DAY_NTP_STARTS, secs);
-}
-
-uint32_t
-isocal_date_to_ntp(
- const struct isodate *id
- )
-{
- /*
- * Get lower half of 64-bit NTP timestamp from date/time.
- */
- return time64lo(isocal_date_to_ntp64(id));
-}
-
/* -*-EOF-*- */
=====================================
tests/ntpd/filegen.c
=====================================
--- /dev/null
+++ b/tests/ntpd/filegen.c
@@ -0,0 +1,56 @@
+#include "config.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+#include "ntp_filegen.h"
+
+
+FILEGEN day, week, month;
+
+TEST_GROUP(filegen);
+
+TEST_SETUP(filegen) {
+ filegen_register("/tmp/foo/", "day", &day);
+ filegen_register("/tmp/foo/", "week", &week);
+ filegen_register("/tmp/foo/", "month", &month);
+}
+
+TEST_TEAR_DOWN(filegen) {
+#ifdef DEBUG
+ filegen_unregister("day");
+ filegen_unregister("week");
+ filegen_unregister("month");
+#endif
+}
+
+
+// =====================================================================
+// VALIDATION TESTS
+// =====================================================================
+
+TEST(filegen, Day) {
+ time_t time;
+// TEST_ASSERT_EQUAL(LSVALID_GOODHASH, rc);
+}
+
+TEST(filegen, Week) {
+// const char *cp = leap2;
+// int rc = leapsec_validate(stringreader, &cp);
+// TEST_ASSERT_EQUAL(LSVALID_NOHASH, rc);
+}
+
+TEST(filegen, Month) {
+// const char *cp = leap_bhash;
+// int rc = leapsec_validate(stringreader, &cp);
+// TEST_ASSERT_EQUAL(LSVALID_BADHASH, rc);
+}
+
+
+
+
+TEST_GROUP_RUNNER(filegen) {
+ RUN_TEST_CASE(filegen, Day);
+ RUN_TEST_CASE(filegen, Week);
+ RUN_TEST_CASE(filegen, Month);
+}
=====================================
tests/wscript
=====================================
--- a/tests/wscript
+++ b/tests/wscript
@@ -64,6 +64,7 @@ def build(ctx):
)
ntpd_source = [
+# "ntpd/filegen.c",
"ntpd/leapsec.c",
"ntpd/restrict.c",
] + common_source
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/62d1412989d2b1be536ef4b22af90294bf7b06a0...254c56a3c8770aa7bdad0a5c7ffd9e848c225c28
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170325/d0182ed8/attachment.html>
More information about the vc
mailing list