diff options
Diffstat (limited to 'contrib/ntp/tests/libntp/timespecops.c')
-rw-r--r-- | contrib/ntp/tests/libntp/timespecops.c | 259 |
1 files changed, 168 insertions, 91 deletions
diff --git a/contrib/ntp/tests/libntp/timespecops.c b/contrib/ntp/tests/libntp/timespecops.c index bb2619a..86df7a1 100644 --- a/contrib/ntp/tests/libntp/timespecops.c +++ b/contrib/ntp/tests/libntp/timespecops.c @@ -2,55 +2,96 @@ #include "ntp_types.h" #include "ntp_fp.h" - -#include <math.h> #include "timespecops.h" #include "unity.h" - +#include <math.h> #include <string.h> -//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; }; + +void test_Helpers1(void); +void test_Normalise(void); +void test_SignNoFrac(void); +void test_SignWithFrac(void); +void test_CmpFracEQ(void); +void test_CmpFracGT(void); +void test_CmpFracLT(void); +void test_AddFullNorm(void); +void test_AddFullOflow1(void); +void test_AddNsecNorm(void); +void test_AddNsecOflow1(void); +void test_SubFullNorm(void); +void test_SubFullOflow(void); +void test_SubNsecNorm(void); +void test_SubNsecOflow(void); +void test_Neg(void); +void test_AbsNoFrac(void); +void test_AbsWithFrac(void); +void test_Helpers2(void); +void test_ToLFPbittest(void); +void test_ToLFPrelPos(void); +void test_ToLFPrelNeg(void); +void test_ToLFPabs(void); +void test_FromLFPbittest(void); +void test_FromLFPrelPos(void); +void test_FromLFPrelNeg(void); +void test_LFProundtrip(void); +void test_ToString(void); + +typedef int bool; + +const bool timespec_isValid(struct timespec V); +struct timespec timespec_init(time_t hi, long lo); +l_fp l_fp_init(int32 i, u_int32 f); +bool AssertFpClose(const l_fp m, const l_fp n, const l_fp limit); +bool AssertTimespecClose(const struct timespec m, const struct timespec n, const struct timespec limit); + + //******************************************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){ +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 +l_fp_init(int32 i, u_int32 f) { l_fp temp; temp.l_i = i; temp.l_uf = f; @@ -58,9 +99,9 @@ l_fp l_fp_init(int32 i, u_int32 f) return temp; } -//also in timelalops.c!!!!!! -bool AssertFpClose(const l_fp m,const l_fp n, const l_fp limit) -{ + +bool +AssertFpClose(const l_fp m, const l_fp n, const l_fp limit) { l_fp diff; if (L_ISGEQ(&m, &n)) { @@ -74,17 +115,14 @@ bool AssertFpClose(const l_fp m,const l_fp n, const l_fp limit) 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); + printf("m_expr which is %s \nand\nn_expr which is %s\nare not close; diff=%susec\n", lfptoa(&m, 10), lfptoa(&n, 10), lfptoa(&diff, 10)); return FALSE; } } -bool AssertTimespecClose(const struct timespec m,const struct timespec n, const struct timespec limit) -{ +bool +AssertTimespecClose(const struct timespec m, const struct timespec n, const struct timespec limit) { struct timespec diff; diff = abs_tspec(sub_tspec(m, n)); @@ -92,10 +130,7 @@ bool AssertTimespecClose(const struct timespec m,const struct timespec n, const 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); + printf("m_expr which is %ld.%lu \nand\nn_expr which is %ld.%lu\nare not close; diff=%ld.%lunsec\n", m.tv_sec, m.tv_nsec, n.tv_sec, n.tv_nsec, diff.tv_sec, diff.tv_nsec); return FALSE; } } @@ -120,8 +155,8 @@ static const struct lfpfracdata fdata[] = { }; -u_int32 my_tick_to_tsf(u_int32 ticks) -{ +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 @@ -135,8 +170,9 @@ u_int32 my_tick_to_tsf(u_int32 ticks) // truncated nonsense, so don't use it out-of-bounds. } -u_int32 my_tsf_to_tick(u_int32 tsf) -{ + +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); @@ -152,7 +188,8 @@ u_int32 my_tsf_to_tick(u_int32 tsf) // test support stuff -- part 1 // --------------------------------------------------------------------- -void test_Helpers1() { +void +test_Helpers1(void) { struct timespec x; for (x.tv_sec = -2; x.tv_sec < 3; x.tv_sec++) { @@ -172,7 +209,8 @@ void test_Helpers1() { // test normalisation //---------------------------------------------------------------------- -void test_Normalise() { +void +test_Normalise(void) { long ns; for ( ns = -2000000000; ns <= 2000000000; ns += 10000000) { struct timespec x = timespec_init(0, ns); @@ -186,7 +224,8 @@ void test_Normalise() { // test classification //---------------------------------------------------------------------- -void test_SignNoFrac() { +void +test_SignNoFrac(void) { // sign test, no fraction int i; for (i = -4; i <= 4; ++i) { @@ -198,7 +237,9 @@ void test_SignNoFrac() { } } -void test_SignWithFrac() { + +void +test_SignWithFrac(void) { // sign test, with fraction int i; for (i = -4; i <= 4; ++i) { @@ -212,9 +253,10 @@ void test_SignWithFrac() { //---------------------------------------------------------------------- // test compare //---------------------------------------------------------------------- -void test_CmpFracEQ() { +void +test_CmpFracEQ(void) { // fractions are equal - int i,j; + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init( i , 200); @@ -225,9 +267,11 @@ void test_CmpFracEQ() { } } -void test_CmpFracGT() { + +void +test_CmpFracGT(void) { // fraction a bigger fraction b - int i,j; + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init(i, 999999800); @@ -238,9 +282,11 @@ void test_CmpFracGT() { } } -void test_CmpFracLT() { + +void +test_CmpFracLT(void) { // fraction a less fraction b - int i,j; + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init(i, 200); @@ -255,8 +301,9 @@ void test_CmpFracLT() { // Test addition (sum) //---------------------------------------------------------------------- -void test_AddFullNorm() { - int i,j; +void +test_AddFullNorm(void) { + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init(i, 200); @@ -269,8 +316,10 @@ void test_AddFullNorm() { } } -void test_AddFullOflow1() { - int i,j; + +void +test_AddFullOflow1(void) { + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init(i, 200); @@ -283,7 +332,9 @@ void test_AddFullOflow1() { } } -void test_AddNsecNorm() { + +void +test_AddNsecNorm(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i, 200); @@ -295,7 +346,9 @@ void test_AddNsecNorm() { } } -void test_AddNsecOflow1() { + +void +test_AddNsecOflow1(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i, 200); @@ -311,8 +364,9 @@ void test_AddNsecOflow1() { // test subtraction (difference) //---------------------------------------------------------------------- -void test_SubFullNorm() { - int i,j; +void +test_SubFullNorm(void) { + int i, j; for (i = -4; i <= 4; ++i) for (j = -4; j <= 4; ++j) { struct timespec a = timespec_init( i , 600); @@ -325,13 +379,15 @@ void test_SubFullNorm() { } } -void test_SubFullOflow() { - int i,j; + +void +test_SubFullOflow(void) { + 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 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); @@ -339,7 +395,9 @@ void test_SubFullOflow() { } } -void test_SubNsecNorm() { + +void +test_SubNsecNorm(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i, 600); @@ -351,7 +409,9 @@ void test_SubNsecNorm() { } } -void test_SubNsecOflow() { + +void +test_SubNsecOflow(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init( i , 100); @@ -367,7 +427,9 @@ void test_SubNsecOflow() { // test negation //---------------------------------------------------------------------- -void test_Neg() { + +void +test_Neg(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i, 100); @@ -384,7 +446,8 @@ void test_Neg() { // test abs value //---------------------------------------------------------------------- -void test_AbsNoFrac() { +void +test_AbsNoFrac(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i , 0); @@ -395,7 +458,9 @@ void test_AbsNoFrac() { } } -void test_AbsWithFrac() { + +void +test_AbsWithFrac(void) { int i; for (i = -4; i <= 4; ++i) { struct timespec a = timespec_init(i, 100); @@ -410,8 +475,9 @@ void test_AbsWithFrac() { // test support stuff -- part 2 // --------------------------------------------------------------------- -void test_Helpers2() { - struct timespec limit = timespec_init(0,2); +void +test_Helpers2(void) { + struct timespec limit = timespec_init(0, 2); struct timespec x, y; long i; @@ -420,30 +486,27 @@ void test_Helpers2() { for (x.tv_nsec = 1; x.tv_nsec < 1000000000; x.tv_nsec += 499999999) { - for (i = -4; i < 5; i++) { + 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); + TEST_ASSERT_TRUE(AssertTimespecClose(x, y, limit)); } else { - TEST_ASSERT_FALSE(AssertTimespecClose(x,y,limit));//ASSERT_PRED_FORMAT2(!isClose, x, y); + TEST_ASSERT_FALSE(AssertTimespecClose(x, y, limit)); } } } } -// 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); +void +test_ToLFPbittest(void) { + 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); @@ -451,13 +514,15 @@ void test_ToLFPbittest() { l_fp r; r = tspec_intv_to_lfp(a); - TEST_ASSERT_TRUE(AssertFpClose(E,r,lfpClose)); //ASSERT_PRED_FORMAT2(FpClose, E, r); + TEST_ASSERT_TRUE(AssertFpClose(E, r, lfpClose)); } } -void test_ToLFPrelPos() { + +void +test_ToLFPrelPos(void) { int i; - for (i = 0; i < COUNTOF(fdata); 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; @@ -467,9 +532,11 @@ void test_ToLFPrelPos() { } } -void test_ToLFPrelNeg() { + +void +test_ToLFPrelNeg(void) { int i; - for (i = 0; i < COUNTOF(fdata); 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; @@ -479,9 +546,11 @@ void test_ToLFPrelNeg() { } } -void test_ToLFPabs() { + +void +test_ToLFPabs(void) { int i; - for (i = 0; i < COUNTOF(fdata); 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; @@ -494,8 +563,10 @@ void test_ToLFPabs() { //---------------------------------------------------------------------- // conversion from l_fp //---------------------------------------------------------------------- -void test_FromLFPbittest() { - struct timespec limit = timespec_init(0,2); + +void +test_FromLFPbittest(void) { + 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 @@ -509,43 +580,48 @@ void test_FromLFPbittest() { 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); + TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit)); } } -void test_FromLFPrelPos() { - struct timespec limit = timespec_init(0,2); + +void +test_FromLFPrelPos(void) { + struct timespec limit = timespec_init(0, 2); int i; - for (i = 0; i < COUNTOF(fdata); 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); + TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit)); } } -void test_FromLFPrelNeg() { - struct timespec limit = timespec_init(0,2); + +void +test_FromLFPrelNeg(void) { + struct timespec limit = timespec_init(0, 2); int i; - for (i = 0; i < COUNTOF(fdata); 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); + TEST_ASSERT_TRUE(AssertTimespecClose(E, r, limit)); } } // nsec -> frac -> nsec roundtrip, using a prime start and increment -void test_LFProundtrip() { +void +test_LFProundtrip(void) { int32_t t; u_int32 i; for (t = -1; t < 2; ++t) - for (i = 4999; i < 1000000000; i+=10007) { + for (i = 4999; i < 1000000000; i += 10007) { struct timespec E = timespec_init(t, i); l_fp a; struct timespec r; @@ -560,7 +636,8 @@ void test_LFProundtrip() { // string formatting //---------------------------------------------------------------------- -void test_ToString() { +void +test_ToString(void) { static const struct { time_t sec; long nsec; @@ -576,7 +653,7 @@ void test_ToString() { {-1,-1, "-1.000000001" }, }; int i; - for (i = 0; i < COUNTOF(data); 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); |