diff options
Diffstat (limited to 'contrib/ntp/tests/libntp/lfpfunc.c')
-rw-r--r-- | contrib/ntp/tests/libntp/lfpfunc.c | 198 |
1 files changed, 121 insertions, 77 deletions
diff --git a/contrib/ntp/tests/libntp/lfpfunc.c b/contrib/ntp/tests/libntp/lfpfunc.c index 1a13f37..cedbb60 100644 --- a/contrib/ntp/tests/libntp/lfpfunc.c +++ b/contrib/ntp/tests/libntp/lfpfunc.c @@ -9,17 +9,18 @@ #include <math.h> -/* replaced TEST_ASSERT_EQUAL_MEMORY(&a, &b, sizeof(a)) with TEST_ASSERT_EQUAL_l_fp(a, b). - It's safer this way, because structs can be compared even if they aren't initiated - with memset (due to padding bytes). +/* + replaced: TEST_ASSERT_EQUAL_MEMORY(&a, &b, sizeof(a)) + with: TEST_ASSERT_EQUAL_l_fp(a, b). + It's safer this way, because structs can be compared even if they + aren't initiated with memset (due to padding bytes). */ -#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"); \ +#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"); \ } - typedef int bool; // typedef enum { FALSE, TRUE } boolean; -> can't use this because TRUE and FALSE are already defined @@ -28,23 +29,23 @@ typedef struct { } lfp_hl; -int l_fp_scmp(const l_fp first, const l_fp second); -int l_fp_ucmp(const l_fp first, l_fp second ); -l_fp l_fp_init(int32 i, u_int32 f); -l_fp l_fp_add(const l_fp first, const l_fp second); -l_fp l_fp_subtract(const l_fp first, const l_fp second); -l_fp l_fp_negate(const l_fp first); -l_fp l_fp_abs(const l_fp first); -int l_fp_signum(const l_fp first); -double l_fp_convert_to_double(const l_fp first); -l_fp l_fp_init_from_double( double rhs); -void l_fp_swap(l_fp * first, l_fp *second); -bool l_isgt(const l_fp first, const l_fp second); -bool l_isgtu(const l_fp first, const l_fp second); -bool l_ishis(const l_fp first, const l_fp second); -bool l_isgeq(const l_fp first, const l_fp second); -bool l_isequ(const l_fp first, const l_fp second); -double eps(double d); +int l_fp_scmp(const l_fp first, const l_fp second); +int l_fp_ucmp(const l_fp first, l_fp second); +l_fp l_fp_init(int32 i, u_int32 f); +l_fp l_fp_add(const l_fp first, const l_fp second); +l_fp l_fp_subtract(const l_fp first, const l_fp second); +l_fp l_fp_negate(const l_fp first); +l_fp l_fp_abs(const l_fp first); +int l_fp_signum(const l_fp first); +double l_fp_convert_to_double(const l_fp first); +l_fp l_fp_init_from_double( double rhs); +void l_fp_swap(l_fp * first, l_fp *second); +bool l_isgt(const l_fp first, const l_fp second); +bool l_isgtu(const l_fp first, const l_fp second); +bool l_ishis(const l_fp first, const l_fp second); +bool l_isgeq(const l_fp first, const l_fp second); +bool l_isequ(const l_fp first, const l_fp second); +double eps(double d); void test_AdditionLR(void); @@ -58,7 +59,6 @@ void test_SignedRelOps(void); void test_UnsignedRelOps(void); - static int cmp_work(u_int32 a[3], u_int32 b[3]); //---------------------------------------------------------------------- @@ -72,10 +72,10 @@ int l_fp_scmp(const l_fp first, const l_fp second) { u_int32 a[3], b[3]; - + const l_fp op1 = first; const l_fp op2 = second; - + a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0; b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0; @@ -86,12 +86,12 @@ l_fp_scmp(const l_fp first, const l_fp second) } int -l_fp_ucmp(const l_fp first, l_fp second ) +l_fp_ucmp(const l_fp first, l_fp second) { u_int32 a[3], b[3]; const l_fp op1 = first; const l_fp op2 = second; - + a[0] = op1.l_uf; a[1] = op1.l_ui; a[2] = 0; b[0] = op2.l_uf; b[1] = op2.l_ui; b[2] = 0; @@ -142,7 +142,7 @@ l_fp_subtract(const l_fp first, const l_fp second) { l_fp temp = first; L_SUB(&temp, &second); - + return temp; } @@ -151,7 +151,7 @@ l_fp_negate(const l_fp first) { l_fp temp = first; L_NEG(&temp); - + return temp; } @@ -189,11 +189,14 @@ l_fp_init_from_double( double rhs) } void -l_fp_swap(l_fp * first, l_fp *second){ +l_fp_swap(l_fp * first, l_fp *second) +{ l_fp temp = *second; *second = *first; *first = temp; + + return; } //---------------------------------------------------------------------- @@ -204,27 +207,37 @@ l_fp_swap(l_fp * first, l_fp *second){ bool -l_isgt (const l_fp first, const l_fp second) { +l_isgt (const l_fp first, const l_fp second) +{ + return L_ISGT(&first, &second); } bool -l_isgtu(const l_fp first, const l_fp second) { +l_isgtu(const l_fp first, const l_fp second) +{ + return L_ISGTU(&first, &second); } bool -l_ishis(const l_fp first, const l_fp second) { +l_ishis(const l_fp first, const l_fp second) +{ + return L_ISHIS(&first, &second); } bool -l_isgeq(const l_fp first, const l_fp second) { +l_isgeq(const l_fp first, const l_fp second) +{ + return L_ISGEQ(&first, &second); } bool -l_isequ(const l_fp first, const l_fp second) { +l_isequ(const l_fp first, const l_fp second) +{ + return L_ISEQU(&first, &second); } @@ -275,6 +288,7 @@ static const size_t addsub_tot = (sizeof(addsub_tab)/sizeof(addsub_tab[0][0])); double eps(double d) { + return fmax(ldexp(1.0, -31), ldexp(fabs(d), -53)); } @@ -282,61 +296,71 @@ eps(double d) // test addition //---------------------------------------------------------------------- void -test_AdditionLR(void) { - +test_AdditionLR(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); l_fp op2 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l); - l_fp exp = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); - l_fp res = l_fp_add(op1, op2); + l_fp e_res = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + l_fp res = l_fp_add(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp, res); - } + TEST_ASSERT_EQUAL_l_fp(e_res, res); + } + return; } void -test_AdditionRL(void) { +test_AdditionRL(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op2 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); l_fp op1 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l); - l_fp exp = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); + l_fp e_res = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); l_fp res = l_fp_add(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp, res); - } + TEST_ASSERT_EQUAL_l_fp(e_res, res); + } + return; } - //---------------------------------------------------------------------- // test subtraction //---------------------------------------------------------------------- void -test_SubtractionLR(void) { +test_SubtractionLR(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op2 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); - l_fp exp = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l); + l_fp e_res = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l); l_fp op1 = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); l_fp res = l_fp_subtract(op1, op2); - - TEST_ASSERT_EQUAL_l_fp(exp, res); - } + + TEST_ASSERT_EQUAL_l_fp(e_res, res); + } + return; } void -test_SubtractionRL(void) { +test_SubtractionRL(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { - l_fp exp = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); + l_fp e_res = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); l_fp op2 = l_fp_init(addsub_tab[idx][1].h, addsub_tab[idx][1].l); l_fp op1 = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); l_fp res = l_fp_subtract(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp, res); - } + TEST_ASSERT_EQUAL_l_fp(e_res, res); + } + return; } //---------------------------------------------------------------------- @@ -344,18 +368,20 @@ test_SubtractionRL(void) { //---------------------------------------------------------------------- void -test_Negation(void) { - +test_Negation(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); l_fp op2 = l_fp_negate(op1); l_fp sum = l_fp_add(op1, op2); - + l_fp zero = l_fp_init(0, 0); TEST_ASSERT_EQUAL_l_fp(zero, sum); - } + } + return; } @@ -364,21 +390,23 @@ test_Negation(void) { // test absolute value //---------------------------------------------------------------------- void -test_Absolute(void) { +test_Absolute(void) +{ size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); l_fp op2 = l_fp_abs(op1); - TEST_ASSERT_TRUE(l_fp_signum(op2) >= 0); + TEST_ASSERT_TRUE(l_fp_signum(op2) >= 0); if (l_fp_signum(op1) >= 0) - op1 = l_fp_subtract(op1, op2); + op1 = l_fp_subtract(op1, op2); else op1 = l_fp_add(op1, op2); - + l_fp zero = l_fp_init(0, 0); - + TEST_ASSERT_EQUAL_l_fp(zero, op1); } @@ -390,6 +418,8 @@ test_Absolute(void) { TEST_ASSERT_EQUAL(-1, l_fp_signum(minVal)); TEST_ASSERT_EQUAL_l_fp(minVal, minAbs); + + return; } @@ -397,14 +427,17 @@ test_Absolute(void) { // fp -> double -> fp rountrip test //---------------------------------------------------------------------- void -test_FDF_RoundTrip(void) { +test_FDF_RoundTrip(void) +{ + size_t idx = 0; + // since a l_fp has 64 bits in it's mantissa and a double has // only 54 bits available (including the hidden '1') we have to // make a few concessions on the roundtrip precision. The 'eps()' // function makes an educated guess about the avilable precision // and checks the difference in the two 'l_fp' values against // that limit. - size_t idx = 0; + for (idx = 0; idx < addsub_cnt; ++idx) { l_fp op1 = l_fp_init(addsub_tab[idx][0].h, addsub_tab[idx][0].l); double op2 = l_fp_convert_to_double(op1); @@ -412,8 +445,10 @@ test_FDF_RoundTrip(void) { l_fp temp = l_fp_subtract(op1, op3); double d = l_fp_convert_to_double(temp); - TEST_ASSERT_DOUBLE_WITHIN(eps(op2), 0.0, fabs(d)); - } + TEST_ASSERT_DOUBLE_WITHIN(eps(op2), 0.0, fabs(d)); + } + + return; } @@ -424,14 +459,16 @@ test_FDF_RoundTrip(void) { // macros in 'ntp_fp.h' produce mathing results. // ---------------------------------------------------------------------- void -test_SignedRelOps(void) { +test_SignedRelOps(void) +{ const lfp_hl * tv = (&addsub_tab[0][0]); size_t lc ; + for (lc = addsub_tot - 1; lc; --lc, ++tv) { l_fp op1 = l_fp_init(tv[0].h, tv[0].l); l_fp op2 = l_fp_init(tv[1].h, tv[1].l); - int cmp = l_fp_scmp(op1, op2); - + int cmp = l_fp_scmp(op1, op2); + switch (cmp) { case -1: //printf("op1:%d %d, op2:%d %d\n",op1.l_uf,op1.l_ui,op2.l_uf,op2.l_ui); @@ -458,15 +495,19 @@ test_SignedRelOps(void) { TEST_ASSERT_TRUE (l_isequ(op2, op1)); break; default: - TEST_FAIL_MESSAGE("unexpected UCMP result: " ); + TEST_FAIL_MESSAGE("unexpected UCMP result: "); } } + + return; } void -test_UnsignedRelOps(void) { - const lfp_hl * tv =(&addsub_tab[0][0]); +test_UnsignedRelOps(void) +{ + const lfp_hl * tv =(&addsub_tab[0][0]); size_t lc; + for (lc = addsub_tot - 1; lc; --lc, ++tv) { l_fp op1 = l_fp_init(tv[0].h, tv[0].l); l_fp op2 = l_fp_init(tv[1].h, tv[1].l); @@ -492,10 +533,13 @@ test_UnsignedRelOps(void) { TEST_ASSERT_TRUE (l_ishis(op2, op1)); break; default: - TEST_FAIL_MESSAGE("unexpected UCMP result: " ); + TEST_FAIL_MESSAGE("unexpected UCMP result: "); } } + + return; } + /* */ |