From: Damir Tomic Date: Tue, 16 Jun 2015 16:26:31 +0000 (+0200) Subject: timespecops.c : 95% done, lacks printf error messages. Contains 2 unity_helper functi... X-Git-Tag: NTP_4_3_42~2^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f5ecb43cd1a8fea2a8f0ecb9e7695fcb07d313b;p=thirdparty%2Fntp.git timespecops.c : 95% done, lacks printf error messages. Contains 2 unity_helper functions which should be moved ot unity_helper.h bk: 55804e37Kthw1L96nVjcBu5XWHMfVw --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index 33a5a9ec8..04830dd33 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -30,6 +30,7 @@ check_PROGRAMS = \ test-socktoa \ test-decodenetnum \ test-timevalops \ + test-timespecops \ $(NULL) if GTEST_AVAILABLE @@ -100,7 +101,7 @@ tests_SOURCES = \ ssl_init.cpp \ g_statestr.cpp \ strtolfp.cpp \ - timespecops.cpp \ + g_timespecops.cpp \ timestructs.cpp \ g_timevalops.cpp \ tstotv.cpp \ @@ -337,6 +338,15 @@ test_timevalops_LDADD = \ $(top_builddir)/sntp/unity/libunity.a \ $(NULL) +test_timespecops_CFLAGS = \ + -I$(top_srcdir)/sntp/unity \ + $(NULL) + +test_timespecops_LDADD = \ + $(LDADD) \ + $(top_builddir)/sntp/unity/libunity.a \ + $(NULL) + test_modetoa_SOURCES = \ modetoa.c \ @@ -462,6 +472,10 @@ test_timevalops_SOURCES = \ run-test-timevalops.c \ $(NULL) +test_timespecops_SOURCES = \ + timespecops.c \ + run-test-timespecops.c \ + $(NULL) BUILT_SOURCES += \ $(srcdir)/run-test-a_md5encrypt.c \ @@ -488,6 +502,7 @@ BUILT_SOURCES += \ $(srcdir)/run-test-ymd2yd.c \ $(srcdir)/run-test-calendar.c \ $(srcdir)/run-test-timevalops.c \ + $(srcdir)/run-test-timespecops.c \ $(NULL) $(srcdir)/run-test-modetoa.c: $(srcdir)/modetoa.c $(std_unity_list) @@ -562,6 +577,9 @@ $(srcdir)/run-test-calendar.c: $(srcdir)/calendar.c $(std_unity_list) $(srcdir)/run-test-timevalops.c: $(srcdir)/timevalops.c $(std_unity_list) $(run_unity) timevalops.c run-test-timevalops.c +$(srcdir)/run-test-timespecops.c: $(srcdir)/timespecops.c $(std_unity_list) + $(run_unity) timespecops.c run-test-timespecops.c + TESTS = diff --git a/tests/libntp/run-test-timespecops.c b/tests/libntp/run-test-timespecops.c new file mode 100644 index 000000000..ee291ab52 --- /dev/null +++ b/tests/libntp/run-test-timespecops.c @@ -0,0 +1,105 @@ +/* AUTOGENERATED FILE. DO NOT EDIT. */ + +//=======Test Runner Used To Run Each Test Below===== +#define RUN_TEST(TestFunc, TestLineNum) \ +{ \ + Unity.CurrentTestName = #TestFunc; \ + Unity.CurrentTestLineNumber = TestLineNum; \ + Unity.NumberOfTests++; \ + if (TEST_PROTECT()) \ + { \ + setUp(); \ + TestFunc(); \ + } \ + if (TEST_PROTECT() && !TEST_IS_IGNORED) \ + { \ + tearDown(); \ + } \ + UnityConcludeTest(); \ +} + +//=======Automagically Detected Files To Include===== +#include "unity.h" +#include +#include + +//=======External Functions This Runner Calls===== +extern void setUp(void); +extern void tearDown(void); +extern void test_Helpers1(); +extern void test_Normalise(); +extern void test_SignNoFrac(); +extern void test_SignWithFrac(); +extern void test_CmpFracEQ(); +extern void test_CmpFracGT(); +extern void test_CmpFracLT(); +extern void test_AddFullNorm(); +extern void test_AddFullOflow1(); +extern void test_AddNsecNorm(); +extern void test_AddNsecOflow1(); +extern void test_SubFullNorm(); +extern void test_SubFullOflow(); +extern void test_SubNsecNorm(); +extern void test_SubNsecOflow(); +extern void test_Neg(); +extern void test_AbsNoFrac(); +extern void test_AbsWithFrac(); +extern void test_Helpers2(); +extern void test_ToLFPbittest(); +extern void test_ToLFPrelPos(); +extern void test_ToLFPrelNeg(); +extern void test_ToLFPabs(); +extern void test_FromLFPbittest(); +extern void test_FromLFPrelPos(); +extern void test_FromLFPrelNeg(); +extern void test_LFProundtrip(); +extern void test_ToString(); + + +//=======Test Reset Option===== +void resetTest() +{ + tearDown(); + setUp(); +} + +char *progname; + + +//=======MAIN===== +int main(int argc, char *argv[]) +{ + progname = argv[0]; + Unity.TestFile = "timespecops.c"; + UnityBegin("timespecops.c"); + RUN_TEST(test_Helpers1, 156); + RUN_TEST(test_Normalise, 176); + RUN_TEST(test_SignNoFrac, 190); + RUN_TEST(test_SignWithFrac, 202); + RUN_TEST(test_CmpFracEQ, 216); + RUN_TEST(test_CmpFracGT, 229); + RUN_TEST(test_CmpFracLT, 242); + RUN_TEST(test_AddFullNorm, 259); + RUN_TEST(test_AddFullOflow1, 273); + RUN_TEST(test_AddNsecNorm, 287); + RUN_TEST(test_AddNsecOflow1, 299); + RUN_TEST(test_SubFullNorm, 315); + RUN_TEST(test_SubFullOflow, 329); + RUN_TEST(test_SubNsecNorm, 343); + RUN_TEST(test_SubNsecOflow, 355); + RUN_TEST(test_Neg, 371); + RUN_TEST(test_AbsNoFrac, 388); + RUN_TEST(test_AbsWithFrac, 399); + RUN_TEST(test_Helpers2, 414); + RUN_TEST(test_ToLFPbittest, 446); + RUN_TEST(test_ToLFPrelPos, 459); + RUN_TEST(test_ToLFPrelNeg, 471); + RUN_TEST(test_ToLFPabs, 483); + RUN_TEST(test_FromLFPbittest, 498); + RUN_TEST(test_FromLFPrelPos, 517); + RUN_TEST(test_FromLFPrelNeg, 530); + RUN_TEST(test_LFProundtrip, 545); + RUN_TEST(test_ToString, 564); + + return (UnityEnd()); +} diff --git a/tests/libntp/run-test-timevalops.c b/tests/libntp/run-test-timevalops.c index 6b162d486..03ab64e87 100644 --- a/tests/libntp/run-test-timevalops.c +++ b/tests/libntp/run-test-timevalops.c @@ -50,6 +50,8 @@ extern void test_ToLFPrelPos(); extern void test_ToLFPrelNeg(); extern void test_ToLFPabs(); extern void test_FromLFPbittest(); +extern void test_FromLFPrelPos(); +extern void test_FromLFPrelNeg(); extern void test_LFProundtrip(); extern void test_ToString(); @@ -70,7 +72,7 @@ int main(int argc, char *argv[]) progname = argv[0]; Unity.TestFile = "timevalops.c"; UnityBegin("timevalops.c"); - RUN_TEST(test_Helpers1, 148); + RUN_TEST(test_Helpers1, 155); RUN_TEST(test_Normalise, 175); RUN_TEST(test_SignNoFrac, 189); RUN_TEST(test_SignWithFrac, 201); @@ -94,6 +96,8 @@ int main(int argc, char *argv[]) RUN_TEST(test_ToLFPrelNeg, 480); RUN_TEST(test_ToLFPabs, 493); RUN_TEST(test_FromLFPbittest, 511); + RUN_TEST(test_FromLFPrelPos, 529); + RUN_TEST(test_FromLFPrelNeg, 542); RUN_TEST(test_LFProundtrip, 556); RUN_TEST(test_ToString, 575); diff --git a/tests/libntp/timespecops.c b/tests/libntp/timespecops.c new file mode 100644 index 000000000..3590733d0 --- /dev/null +++ b/tests/libntp/timespecops.c @@ -0,0 +1,588 @@ +#include "config.h" +//#include "c_timestructs.h" + +#include "ntp_types.h" +#include "ntp_fp.h" + +#include +#include "timespecops.h" + +#include "unity.h" + + +#include + +//in unity_helper.h : +#define TEST_ASSERT_EQUAL_timespec(a, b) { \ + TEST_ASSERT_EQUAL_MESSAGE(a.tv_sec, b.tv_sec, "Field tv_sec"); \ + TEST_ASSERT_EQUAL_MESSAGE(a.tv_nsec, b.tv_nsec, "Field tv_nsec"); \ +} + +//what about l_fp.l_ui ??? it's a union so it's either l_fp.l_ui or l_fp.l_i? +#define TEST_ASSERT_EQUAL_l_fp(a, b) { \ + TEST_ASSERT_EQUAL_MESSAGE(a.l_i, b.l_i, "Field l_i"); \ + TEST_ASSERT_EQUAL_UINT_MESSAGE(a.l_uf, b.l_uf, "Field l_uf"); \ +} + +//timespec has time_t, long, and time_t is basically long uint, 4 or 8 bytes size, depending on 32/64bit + +static u_int32 my_tick_to_tsf(u_int32 ticks); +static u_int32 my_tsf_to_tick(u_int32 tsf); + +// that's it... +struct lfpfracdata { + long nsec; + u_int32 frac; +}; + +//******************************************MY CUSTOM FUNCTIONS******************************* + +typedef int bool; //TRUE and FALSE are already defined somewhere, so I can't do typedef enum { FALSE, TRUE } boolean; + +const bool timespec_isValid(struct timespec V) + { return V.tv_nsec >= 0 && V.tv_nsec < 1000000000; } + +struct timespec timespec_init(time_t hi, long lo){ + struct timespec V; + V.tv_sec = hi; + V.tv_nsec = lo; + return V; +} + +//taken from lfpfunc.c -> maybe remove this from timevalops.c and lfpfunc. and put in c_timstructs.h ????!!!!! +l_fp l_fp_init(int32 i, u_int32 f) +{ + l_fp temp; + temp.l_i = i; + temp.l_uf = f; + + return temp; +} + +//also in timelalops.c!!!!!! +bool AssertFpClose(const l_fp m,const l_fp n, const l_fp limit) +{ + l_fp diff; + + if (L_ISGEQ(&m, &n)) { + diff = m; + L_SUB(&diff, &n); + } else { + diff = n; + L_SUB(&diff, &m); + } + if (L_ISGEQ(&limit, &diff)){ + return TRUE; + } + else { + //<< m_expr << " which is " << l_fp_wrap(m) + //<< "\nand\n" + //<< n_expr << " which is " << l_fp_wrap(n) + //<< "\nare not close; diff=" << l_fp_wrap(diff); + return FALSE; + } +} + + +bool AssertTimespecClose(const struct timespec m,const struct timespec n, const struct timespec limit) +{ + struct timespec diff; + + diff = abs_tspec(sub_tspec(m, n)); + if (cmp_tspec(limit, diff) >= 0) + return TRUE; + else + { + //<< m_expr << " which is " << timespec_wrap(m) + //<< "\nand\n" + //<< n_expr << " which is " << timespec_wrap(n) + //<< "\nare not close; diff=" << timespec_wrap(diff); + return FALSE; + } +} + +//----------------------------------------------- + +static const struct lfpfracdata fdata[] = { + { 0, 0x00000000 }, { 2218896, 0x00916ae6 }, + { 16408100, 0x0433523d }, { 125000000, 0x20000000 }, + { 250000000, 0x40000000 }, { 287455871, 0x4996b53d }, + { 375000000, 0x60000000 }, { 500000000, 0x80000000 }, + { 518978897, 0x84dbcd0e }, { 563730222, 0x90509fb3 }, + { 563788007, 0x9054692c }, { 583289882, 0x95527c57 }, + { 607074509, 0x9b693c2a }, { 625000000, 0xa0000000 }, + { 645184059, 0xa52ac851 }, { 676497788, 0xad2ef583 }, + { 678910895, 0xadcd1abb }, { 679569625, 0xadf84663 }, + { 690926741, 0xb0e0932d }, { 705656483, 0xb4a5e73d }, + { 723553854, 0xb93ad34c }, { 750000000, 0xc0000000 }, + { 763550253, 0xc3780785 }, { 775284917, 0xc6791284 }, + { 826190764, 0xd3813ce8 }, { 875000000, 0xe0000000 }, + { 956805507, 0xf4f134a9 }, { 982570733, 0xfb89c16c } + }; + + +u_int32 my_tick_to_tsf(u_int32 ticks) +{ + // convert nanoseconds to l_fp fractional units, using double + // precision float calculations or, if available, 64bit integer + // arithmetic. This should give the precise fraction, rounded to + // the nearest representation. +#ifdef HAVE_U_INT64 + return (u_int32)((( ((u_int64)(ticks)) << 32) + 500000000) / 1000000000); +#else + return (u_int32)((double(ticks)) * 4.294967296 + 0.5); +#endif + // And before you ask: if ticks >= 1000000000, the result is + // truncated nonsense, so don't use it out-of-bounds. +} + +u_int32 my_tsf_to_tick(u_int32 tsf) +{ + // Inverse operation: converts fraction to microseconds. +#ifdef HAVE_U_INT64 + return (u_int32)(( ((u_int64)(tsf)) * 1000000000 + 0x80000000) >> 32); +#else + return (u_int32)(double(tsf) / 4.294967296 + 0.5); +#endif + // Beware: The result might be 10^9 due to rounding! +} + + + +// --------------------------------------------------------------------- +// test support stuff -- part 1 +// --------------------------------------------------------------------- + +void test_Helpers1() { + struct timespec x; + + for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) { + x.tv_nsec = -1; + TEST_ASSERT_FALSE(timespec_isValid(x)); + x.tv_nsec = 0; + TEST_ASSERT_TRUE(timespec_isValid(x)); + x.tv_nsec = 999999999; + TEST_ASSERT_TRUE(timespec_isValid(x)); + x.tv_nsec = 1000000000; + TEST_ASSERT_FALSE(timespec_isValid(x)); + } +} + + +//---------------------------------------------------------------------- +// test normalisation +//---------------------------------------------------------------------- + +void test_Normalise() { + long ns; + for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) { + struct timespec x = timespec_init(0, ns); + + x = normalize_tspec(x); + TEST_ASSERT_TRUE(timespec_isValid(x)); + } +} + +//---------------------------------------------------------------------- +// test classification +//---------------------------------------------------------------------- + +void test_SignNoFrac() { + // sign test, no fraction + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 0); + int E = (i > 0) - (i < 0); + int r = test_tspec(a); + + TEST_ASSERT_EQUAL(E, r); + } +} + +void test_SignWithFrac() { + // sign test, with fraction + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 10); + int E = (i >= 0) - (i < 0); + int r = test_tspec(a); + TEST_ASSERT_EQUAL(E, r); + } +} + +//---------------------------------------------------------------------- +// test compare +//---------------------------------------------------------------------- +void test_CmpFracEQ() { + // fractions are equal + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init( i , 200); + struct timespec b = timespec_init( j , 200); + int E = (i > j) - (i < j); + int r = cmp_tspec_denorm(a, b); + TEST_ASSERT_EQUAL(E, r); + } +} + +void test_CmpFracGT() { + // fraction a bigger fraction b + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init(i, 999999800); + struct timespec b = timespec_init(j, 200); + int E = (i >= j) - (i < j); + int r = cmp_tspec_denorm(a, b); + TEST_ASSERT_EQUAL(E, r); + } +} + +void test_CmpFracLT() { + // fraction a less fraction b + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init(i, 200); + struct timespec b = timespec_init(j, 999999800); + int E = (i > j) - (i <= j); + int r = cmp_tspec_denorm(a, b); + TEST_ASSERT_EQUAL(E, r); + } +} + +//---------------------------------------------------------------------- +// Test addition (sum) +//---------------------------------------------------------------------- + +void test_AddFullNorm() { + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init(i, 200); + struct timespec b = timespec_init(j, 400); + struct timespec E = timespec_init(i + j, 200 + 400); + struct timespec c; + + c = add_tspec(a, b); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_AddFullOflow1() { + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init(i, 200); + struct timespec b = timespec_init(j, 999999900); + struct timespec E = timespec_init(i + j + 1, 100); + struct timespec c; + + c = add_tspec(a, b); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_AddNsecNorm() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 200); + struct timespec E = timespec_init(i, 600); + struct timespec c; + + c = add_tspec_ns(a, 600 - 200); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_AddNsecOflow1() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 200); + struct timespec E = timespec_init(i + 1, 100); + struct timespec c; + + c = add_tspec_ns(a, NANOSECONDS - 100); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +//---------------------------------------------------------------------- +// test subtraction (difference) +//---------------------------------------------------------------------- + +void test_SubFullNorm() { + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init( i , 600); + struct timespec b = timespec_init( j , 400); + struct timespec E = timespec_init(i-j, 200); + struct timespec c; + + c = sub_tspec(a, b); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_SubFullOflow() { + int i,j; + for (i = -4; i <= 4; ++i) + for (j = -4; j <= 4; ++j) { + struct timespec a = timespec_init( i , 100); + struct timespec b = timespec_init( j , 999999900); + struct timespec E = timespec_init(i-j-1, 200); + struct timespec c; + + c = sub_tspec(a, b); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_SubNsecNorm() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 600); + struct timespec E = timespec_init(i, 200); + struct timespec c; + + c = sub_tspec_ns(a, 600 - 200); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +void test_SubNsecOflow() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init( i , 100); + struct timespec E = timespec_init(i-1, 200); + struct timespec c; + + c = sub_tspec_ns(a, NANOSECONDS - 100); + TEST_ASSERT_EQUAL_timespec(E, c); + } +} + +//---------------------------------------------------------------------- +// test negation +//---------------------------------------------------------------------- + +void test_Neg() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 100); + struct timespec b; + struct timespec c; + + b = neg_tspec(a); + c = add_tspec(a, b); + TEST_ASSERT_EQUAL(0, test_tspec(c)); + } +} + +//---------------------------------------------------------------------- +// test abs value +//---------------------------------------------------------------------- + +void test_AbsNoFrac() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i , 0); + struct timespec b; + + b = abs_tspec(a); + TEST_ASSERT_EQUAL((i != 0), test_tspec(b)); + } +} + +void test_AbsWithFrac() { + int i; + for (i = -4; i <= 4; ++i) { + struct timespec a = timespec_init(i, 100); + struct timespec b; + + b = abs_tspec(a); + TEST_ASSERT_EQUAL(1, test_tspec(b)); + } +} + +// --------------------------------------------------------------------- +// test support stuff -- part 2 +// --------------------------------------------------------------------- + +void test_Helpers2() { + struct timespec limit = timespec_init(0,2); + + struct timespec x, y; + long i; + + for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) + for (x.tv_nsec = 1; + x.tv_nsec < 1000000000; + x.tv_nsec += 499999999) { + for (i = -4; i < 5; i++) { + y = x; + y.tv_nsec += i; + if (i >= -2 && i <= 2){ + TEST_ASSERT_TRUE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(isClose, x, y); + } + else + { + TEST_ASSERT_FALSE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(!isClose, x, y); + } + } + } +} + +// global predicate instances we're using here +//static l_fp lfpClose = l_fp_init(0,1); //static AssertFpClose FpClose(0, 1); +//static struct timespec limit = timespec_init(0,2); //static AssertTimespecClose TimespecClose(0, 2); + +//---------------------------------------------------------------------- +// conversion to l_fp +//---------------------------------------------------------------------- + +void test_ToLFPbittest() { + l_fp lfpClose = l_fp_init(0,1); + u_int32 i; + for (i = 0; i < 1000000000; i+=1000) { + struct timespec a = timespec_init(1, i); + l_fp E= l_fp_init(1, my_tick_to_tsf(i)); + l_fp r; + + r = tspec_intv_to_lfp(a); + TEST_ASSERT_TRUE(AssertFpClose(E,r,lfpClose)); //ASSERT_PRED_FORMAT2(FpClose, E, r); + } +} + +void test_ToLFPrelPos() { + int i; + for (i = 0; i < COUNTOF(fdata); i++) { + struct timespec a = timespec_init(1, fdata[i].nsec); + l_fp E = l_fp_init(1, fdata[i].frac); + l_fp r; + + r = tspec_intv_to_lfp(a); + TEST_ASSERT_EQUAL_l_fp(E, r); + } +} + +void test_ToLFPrelNeg() { + int i; + for (i = 0; i < COUNTOF(fdata); i++) { + struct timespec a = timespec_init(-1, fdata[i].nsec); + l_fp E = l_fp_init(~0, fdata[i].frac); + l_fp r; + + r = tspec_intv_to_lfp(a); + TEST_ASSERT_EQUAL_l_fp(E, r); + } +} + +void test_ToLFPabs() { + int i; + for (i = 0; i < COUNTOF(fdata); i++) { + struct timespec a = timespec_init(1, fdata[i].nsec); + l_fp E = l_fp_init(1 + JAN_1970, fdata[i].frac); + l_fp r; + + r = tspec_stamp_to_lfp(a); + TEST_ASSERT_EQUAL_l_fp(E, r); + } +} + +//---------------------------------------------------------------------- +// conversion from l_fp +//---------------------------------------------------------------------- +void test_FromLFPbittest() { + struct timespec limit = timespec_init(0,2); + + // Not *exactly* a bittest, because 2**32 tests would take a + // really long time even on very fast machines! So we do test + // every 1000 fractional units. + u_int32 tsf; + for (tsf = 0; tsf < ~((u_int32)(1000)); tsf += 1000) { + struct timespec E = timespec_init(1, my_tsf_to_tick(tsf)); + l_fp a = l_fp_init(1, tsf); + struct timespec r; + + r = lfp_intv_to_tspec(a); + // The conversion might be off by one nanosecond when + // comparing to calculated value. + TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +void test_FromLFPrelPos() { + struct timespec limit = timespec_init(0,2); + int i; + for (i = 0; i < COUNTOF(fdata); i++) { + l_fp a = l_fp_init(1, fdata[i].frac); + struct timespec E = timespec_init(1, fdata[i].nsec); + struct timespec r; + + r = lfp_intv_to_tspec(a); + TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + +void test_FromLFPrelNeg() { + struct timespec limit = timespec_init(0,2); + int i; + for (i = 0; i < COUNTOF(fdata); i++) { + l_fp a = l_fp_init(~0, fdata[i].frac); + struct timespec E = timespec_init(-1, fdata[i].nsec); + struct timespec r; + + r = lfp_intv_to_tspec(a); + TEST_ASSERT_TRUE(AssertTimespecClose(E,r,limit)); //ASSERT_PRED_FORMAT2(TimespecClose, E, r); + } +} + + +// nsec -> frac -> nsec roundtrip, using a prime start and increment +void test_LFProundtrip() { + int32_t t; + u_int32 i; + for (t = -1; t < 2; ++t) + for (i = 4999; i < 1000000000; i+=10007) { + struct timespec E = timespec_init(t, i); + l_fp a; + struct timespec r; + + a = tspec_intv_to_lfp(E); + r = lfp_intv_to_tspec(a); + TEST_ASSERT_EQUAL_timespec(E, r); + } +} + +//---------------------------------------------------------------------- +// string formatting +//---------------------------------------------------------------------- + +void test_ToString() { + static const struct { + time_t sec; + long nsec; + const char * repr; + } data [] = { + { 0, 0, "0.000000000" }, + { 2, 0, "2.000000000" }, + {-2, 0, "-2.000000000" }, + { 0, 1, "0.000000001" }, + { 0,-1, "-0.000000001" }, + { 1,-1, "0.999999999" }, + {-1, 1, "-0.999999999" }, + {-1,-1, "-1.000000001" }, + }; + int i; + for (i = 0; i < COUNTOF(data); i++) { + struct timespec a = timespec_init(data[i].sec, data[i].nsec); + const char * E = data[i].repr; + const char * r = tspectoa(a); + TEST_ASSERT_EQUAL_STRING(E, r); + } +} + +// -*- EOF -*- diff --git a/tests/libntp/timevalops.c b/tests/libntp/timevalops.c index 1bb82819d..e8e167ea2 100644 --- a/tests/libntp/timevalops.c +++ b/tests/libntp/timevalops.c @@ -35,6 +35,13 @@ typedef struct { typedef int bool; //TRUE and FALSE are already defined somewhere, so I can't do typedef enum { FALSE, TRUE } boolean; +struct timeval timeval_init( time_t hi, long lo){ + struct timeval V; + V.tv_sec = hi; + V.tv_usec = lo; + return V; +} + const bool timeval_isValid(struct timeval V) { return V.tv_usec >= 0 && V.tv_usec < 1000000; } @@ -161,13 +168,6 @@ void test_Helpers1() { } -struct timeval timeval_init( time_t hi, long lo){ - struct timeval V; - V.tv_sec = hi; - V.tv_usec = lo; - return V; -} - //---------------------------------------------------------------------- // test normalisation //---------------------------------------------------------------------- @@ -525,7 +525,7 @@ void test_FromLFPbittest() { TEST_ASSERT_TRUE(AssertTimevalClose(E,r,timevalClose)); //ASSERT_PRED_FORMAT2(TimevalClose, E, r); } } -/* + void test_FromLFPrelPos() { struct timeval timevalClose = timeval_init(0,1); int i = 0; @@ -551,7 +551,7 @@ void test_FromLFPrelNeg() { TEST_ASSERT_TRUE(AssertTimevalClose(E,r,timevalClose)); //ASSERT_PRED_FORMAT2(TimevalClose, E, r); } } -*/ + // usec -> frac -> usec roundtrip, using a prime start and increment void test_LFProundtrip() { int32_t t = -1;