diff options
Diffstat (limited to 'contrib/ntp/tests/libntp/lfpfunc.c')
-rw-r--r-- | contrib/ntp/tests/libntp/lfpfunc.c | 397 |
1 files changed, 170 insertions, 227 deletions
diff --git a/contrib/ntp/tests/libntp/lfpfunc.c b/contrib/ntp/tests/libntp/lfpfunc.c index 188478d..1a13f37 100644 --- a/contrib/ntp/tests/libntp/lfpfunc.c +++ b/contrib/ntp/tests/libntp/lfpfunc.c @@ -1,7 +1,6 @@ #include "config.h" #include "ntp_stdlib.h" -#include "ntp_calendar.h" #include "ntp_fp.h" #include "unity.h" @@ -10,82 +9,72 @@ #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"); \ } -typedef struct { - uint32_t h, l; -} lfp_hl; - - -static int cmp_work(u_int32 a[3], u_int32 b[3]); - -/* -//---------------------------------------------------------------------- -// OO-wrapper for 'l_fp' -//---------------------------------------------------------------------- - - - ~LFP(); - LFP(); - LFP(const LFP& rhs); - LFP(int32 i, u_int32 f); - LFP operator+ (const LFP &rhs) const; - LFP& operator+=(const LFP &rhs); - LFP operator- (const LFP &rhs) const; - LFP& operator-=(const LFP &rhs); +typedef int bool; // typedef enum { FALSE, TRUE } boolean; -> can't use this because TRUE and FALSE are already defined - LFP& operator=(const LFP &rhs); - LFP operator-() const; - bool operator==(const LFP &rhs) const; +typedef struct { + uint32_t h, l; +} lfp_hl; - LFP neg() const; - LFP abs() const; - int signum() const; - +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); +void test_AdditionRL(void); +void test_SubtractionLR(void); +void test_SubtractionRL(void); +void test_Negation(void); +void test_Absolute(void); +void test_FDF_RoundTrip(void); +void test_SignedRelOps(void); +void test_UnsignedRelOps(void); - int ucmp(const LFP & rhs) const; - int scmp(const LFP & rhs) const; - - std::string toString() const; - std::ostream& toStream(std::ostream &oo) const; - - operator double() const; - explicit LFP(double); - - LFP(const l_fp &rhs); - static int cmp_work(u_int32 a[3], u_int32 b[3]); - - l_fp _v; +static int cmp_work(u_int32 a[3], u_int32 b[3]); - -static std::ostream& operator<<(std::ostream &oo, const LFP& rhs) -{ - return rhs.toStream(oo); -} -*/ //---------------------------------------------------------------------- // reference comparision // This is implementad as a full signed MP-subtract in 3 limbs, where // the operands are zero or sign extended before the subtraction is // executed. //---------------------------------------------------------------------- -int l_fp_scmp(const l_fp first, const l_fp second) + +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; - //const l_fp &op1(_v), &op2(rhs._v); 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; @@ -96,7 +85,8 @@ int l_fp_scmp(const l_fp first, const l_fp second) return cmp_work(a,b); } -int l_fp_ucmp(const l_fp first, l_fp second ) +int +l_fp_ucmp(const l_fp first, l_fp second ) { u_int32 a[3], b[3]; const l_fp op1 = first; @@ -108,9 +98,9 @@ int l_fp_ucmp(const l_fp first, l_fp second ) return cmp_work(a,b); } - -//maybe rename it to lf_cmp_work ??? -int cmp_work(u_int32 a[3], u_int32 b[3]) +// maybe rename it to lf_cmp_work +int +cmp_work(u_int32 a[3], u_int32 b[3]) { u_int32 cy, idx, tmp; for (cy = idx = 0; idx < 3; ++idx) { @@ -128,9 +118,8 @@ int cmp_work(u_int32 a[3], u_int32 b[3]) // This should be easy enough... //---------------------------------------------------------------------- - - -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; @@ -139,35 +128,35 @@ l_fp l_fp_init(int32 i, u_int32 f) return temp; } - - -l_fp l_fp_add(const l_fp first, const l_fp second) +l_fp +l_fp_add(const l_fp first, const l_fp second) { - l_fp temp; - temp = first; + l_fp temp = first; L_ADD(&temp, &second); + return temp; } -l_fp l_fp_subtract(const l_fp first, const l_fp second) +l_fp +l_fp_subtract(const l_fp first, const l_fp second) { - l_fp temp; - temp = first; + l_fp temp = first; L_SUB(&temp, &second); return temp; } -l_fp l_fp_negate(const l_fp first) +l_fp +l_fp_negate(const l_fp first) { - l_fp temp; - temp = first; //is this line really necessary? + l_fp temp = first; L_NEG(&temp); return temp; } -l_fp l_fp_abs(const l_fp first) +l_fp +l_fp_abs(const l_fp first) { l_fp temp = first; if (L_ISNEG(&temp)) @@ -175,74 +164,38 @@ l_fp l_fp_abs(const l_fp first) return temp; } -int l_fp_signum(const l_fp first) +int +l_fp_signum(const l_fp first) { if (first.l_ui & 0x80000000u) return -1; return (first.l_ui || first.l_uf); } -double l_fp_convert_to_double(const l_fp first) +double +l_fp_convert_to_double(const l_fp first) { double res; LFPTOD(&first, res); return res; } -l_fp l_fp_init_from_double( double rhs) +l_fp +l_fp_init_from_double( double rhs) { l_fp temp; DTOLFP(rhs, &temp); return temp; } - - -void l_fp_swap(l_fp * first, l_fp *second){ +void +l_fp_swap(l_fp * first, l_fp *second){ l_fp temp = *second; *second = *first; *first = temp; - -} - - -/* -LFP::LFP() -{ - _v.l_ui = 0; - _v.l_uf = 0; -} - - - -std::string -LFP::toString() const -{ - std::ostringstream oss; - toStream(oss); - return oss.str(); -} - -std::ostream& -LFP::toStream(std::ostream &os) const -{ - return os - << mfptoa(_v.l_ui, _v.l_uf, 9) - << " [$" << std::setw(8) << std::setfill('0') << std::hex << _v.l_ui - << ':' << std::setw(8) << std::setfill('0') << std::hex << _v.l_uf - << ']'; } -bool LFP::operator==(const LFP &rhs) const -{ - return L_ISEQU(&_v, &rhs._v); -} - - - -*/ - //---------------------------------------------------------------------- // testing the relational macros works better with proper predicate // formatting functions; it slows down the tests a bit, but makes for @@ -250,19 +203,30 @@ bool LFP::operator==(const LFP &rhs) const //---------------------------------------------------------------------- -typedef int bool; //typedef enum { FALSE, TRUE } boolean; -> can't use this because TRUE and FALSE are already defined +bool +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) { + return L_ISGTU(&first, &second); +} + +bool +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) { + return L_ISGEQ(&first, &second); +} -bool 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) - { return L_ISGTU(&first, &second); } -bool 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) - { return L_ISGEQ(&first, &second); } -bool l_isequ(const l_fp first, const l_fp second) - { return L_ISEQU(&first, &second); } +bool +l_isequ(const l_fp first, const l_fp second) { + return L_ISEQU(&first, &second); +} //---------------------------------------------------------------------- @@ -308,46 +272,39 @@ static const size_t addsub_tot = (sizeof(addsub_tab)/sizeof(addsub_tab[0][0])); // '1'-bit of the l_fp value, the roundtrip *will* create truncation // errors. This is an inherent property caused by the 54-bit mantissa of // the 'double' type. -double eps(double d) +double +eps(double d) { - return fmax(ldexp(1.0, -31), ldexp(fabs(d), -53)); //max<double> + return fmax(ldexp(1.0, -31), ldexp(fabs(d), -53)); } - - //---------------------------------------------------------------------- // test addition //---------------------------------------------------------------------- -void test_AdditionLR() { +void +test_AdditionLR(void) { - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { - - + 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); - //LFP op1(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); - //LFP exp(addsub_tab[idx][2].h, addsub_tab[idx][2].l); l_fp exp = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); - //LFP res(op1 + op2); - l_fp res = l_fp_add(op1,op2); + l_fp res = l_fp_add(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp,res); - //TEST_ASSERT_EQUAL_MEMORY(&exp, &res,sizeof(exp)); + TEST_ASSERT_EQUAL_l_fp(exp, res); } } -void test_AdditionRL() { - - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { +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 res = l_fp_add(op1,op2); + l_fp res = l_fp_add(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp,res); - //TEST_ASSERT_EQUAL_MEMORY(&exp, &res,sizeof(exp)); + TEST_ASSERT_EQUAL_l_fp(exp, res); } } @@ -356,32 +313,29 @@ void test_AdditionRL() { //---------------------------------------------------------------------- // test subtraction //---------------------------------------------------------------------- -void test_SubtractionLR() { - - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { +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 op1 = l_fp_init(addsub_tab[idx][2].h, addsub_tab[idx][2].l); - l_fp res = l_fp_subtract(op1,op2); - //LFP res(op1 - op2); + l_fp res = l_fp_subtract(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp,res); - //TEST_ASSERT_EQUAL_MEMORY(&exp, &res,sizeof(exp)); + TEST_ASSERT_EQUAL_l_fp(exp, res); } } -void test_SubtractionRL() { - - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { +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 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); + l_fp res = l_fp_subtract(op1, op2); - TEST_ASSERT_EQUAL_l_fp(exp,res); - //TEST_ASSERT_EQUAL_MEMORY(&exp, &res,sizeof(exp)); + TEST_ASSERT_EQUAL_l_fp(exp, res); } } @@ -389,19 +343,18 @@ void test_SubtractionRL() { // test negation //---------------------------------------------------------------------- -void test_Negation() { +void +test_Negation(void) { - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { + 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); + l_fp zero = l_fp_init(0, 0); - TEST_ASSERT_EQUAL_l_fp(zero,sum); - //TEST_ASSERT_EQUAL_MEMORY(&zero, &sum,sizeof(sum)); - + TEST_ASSERT_EQUAL_l_fp(zero, sum); } } @@ -410,24 +363,23 @@ void test_Negation() { //---------------------------------------------------------------------- // test absolute value //---------------------------------------------------------------------- -void test_Absolute() { - size_t idx=0; - for (idx=0; idx < addsub_cnt; ++idx) { +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); if (l_fp_signum(op1) >= 0) - op1 = l_fp_subtract(op1,op2); //op1 -= op2; - + op1 = l_fp_subtract(op1, op2); else - op1 = l_fp_add(op1,op2); + op1 = l_fp_add(op1, op2); - l_fp zero = l_fp_init(0,0); + l_fp zero = l_fp_init(0, 0); - TEST_ASSERT_EQUAL_l_fp(zero,op1); - //TEST_ASSERT_EQUAL_MEMORY(&zero, &op1,sizeof(op1)); + TEST_ASSERT_EQUAL_l_fp(zero, op1); } // There is one special case we have to check: the minimum @@ -437,36 +389,30 @@ void test_Absolute() { l_fp minAbs = l_fp_abs(minVal); TEST_ASSERT_EQUAL(-1, l_fp_signum(minVal)); - TEST_ASSERT_EQUAL_l_fp(minVal,minAbs); - //TEST_ASSERT_EQUAL_MEMORY(&minVal, &minAbs,sizeof(minAbs)); + TEST_ASSERT_EQUAL_l_fp(minVal, minAbs); } //---------------------------------------------------------------------- // fp -> double -> fp rountrip test //---------------------------------------------------------------------- -void test_FDF_RoundTrip() { +void +test_FDF_RoundTrip(void) { // 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) { + 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); l_fp op3 = l_fp_init_from_double(op2); - // for manual checks only: - // std::cout << std::setprecision(16) << op2 << std::endl; - - l_fp temp = l_fp_subtract(op1,op3); + 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)); //delta,epected,actual - - //ASSERT_LE(fabs(op1-op3), eps(op2)); //unity has no equivalent of LE!!! - //you could use TEST_ASSERT_TRUE(IsLE(fabs(op1-op3), eps(op2))); + TEST_ASSERT_DOUBLE_WITHIN(eps(op2), 0.0, fabs(d)); } } @@ -477,56 +423,54 @@ void test_FDF_RoundTrip() { // This uses the local compare and checks if the operations using the // macros in 'ntp_fp.h' produce mathing results. // ---------------------------------------------------------------------- -void test_SignedRelOps() { - //const lfp_hl * tv(&addsub_tab[0][0]); +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(op1.scmp(op2)); - int cmp = l_fp_scmp(op1,op2); + 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); switch (cmp) { case -1: //printf("op1:%d %d, op2:%d %d\n",op1.l_uf,op1.l_ui,op2.l_uf,op2.l_ui); - //std::swap(op1, op2); - l_fp_swap(&op1,&op2); + l_fp_swap(&op1, &op2); //printf("op1:%d %d, op2:%d %d\n",op1.l_uf,op1.l_ui,op2.l_uf,op2.l_ui); case 1: - TEST_ASSERT_TRUE (l_isgt(op1,op2)); - TEST_ASSERT_FALSE(l_isgt(op2,op1)); + TEST_ASSERT_TRUE (l_isgt(op1, op2)); + TEST_ASSERT_FALSE(l_isgt(op2, op1)); - TEST_ASSERT_TRUE (l_isgeq(op1,op2)); - TEST_ASSERT_FALSE(l_isgeq(op2,op1)); + TEST_ASSERT_TRUE (l_isgeq(op1, op2)); + TEST_ASSERT_FALSE(l_isgeq(op2, op1)); - TEST_ASSERT_FALSE(l_isequ(op1,op2)); - TEST_ASSERT_FALSE(l_isequ(op2,op1)); + TEST_ASSERT_FALSE(l_isequ(op1, op2)); + TEST_ASSERT_FALSE(l_isequ(op2, op1)); break; case 0: - TEST_ASSERT_FALSE(l_isgt(op1,op2)); - TEST_ASSERT_FALSE(l_isgt(op2,op1)); + TEST_ASSERT_FALSE(l_isgt(op1, op2)); + TEST_ASSERT_FALSE(l_isgt(op2, op1)); - TEST_ASSERT_TRUE (l_isgeq(op1,op2)); - TEST_ASSERT_TRUE (l_isgeq(op2,op1)); + TEST_ASSERT_TRUE (l_isgeq(op1, op2)); + TEST_ASSERT_TRUE (l_isgeq(op2, op1)); - TEST_ASSERT_TRUE (l_isequ(op1,op2)); - TEST_ASSERT_TRUE (l_isequ(op2,op1)); + TEST_ASSERT_TRUE (l_isequ(op1, op2)); + TEST_ASSERT_TRUE (l_isequ(op2, op1)); break; default: TEST_FAIL_MESSAGE("unexpected UCMP result: " ); - //TEST_ASSERT_FAIL() << "unexpected SCMP result: " << cmp; } } } -void test_UnsignedRelOps() { +void +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); - int cmp = l_fp_ucmp(op1,op2); + 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_ucmp(op1, op2); switch (cmp) { case -1: @@ -534,22 +478,21 @@ void test_UnsignedRelOps() { l_fp_swap(&op1, &op2); //printf("op1:%d %d, op2:%d %d\n",op1.l_uf,op1.l_ui,op2.l_uf,op2.l_ui); case 1: - TEST_ASSERT_TRUE (l_isgtu(op1,op2)); - TEST_ASSERT_FALSE(l_isgtu(op2,op1)); + TEST_ASSERT_TRUE (l_isgtu(op1, op2)); + TEST_ASSERT_FALSE(l_isgtu(op2, op1)); - TEST_ASSERT_TRUE (l_ishis(op1,op2)); - TEST_ASSERT_FALSE(l_ishis(op2,op1)); + TEST_ASSERT_TRUE (l_ishis(op1, op2)); + TEST_ASSERT_FALSE(l_ishis(op2, op1)); break; case 0: - TEST_ASSERT_FALSE(l_isgtu(op1,op2)); - TEST_ASSERT_FALSE(l_isgtu(op2,op1)); + TEST_ASSERT_FALSE(l_isgtu(op1, op2)); + TEST_ASSERT_FALSE(l_isgtu(op2, op1)); - TEST_ASSERT_TRUE (l_ishis(op1,op2)); - TEST_ASSERT_TRUE (l_ishis(op2,op1)); + TEST_ASSERT_TRUE (l_ishis(op1, op2)); + TEST_ASSERT_TRUE (l_ishis(op2, op1)); break; default: TEST_FAIL_MESSAGE("unexpected UCMP result: " ); - //FAIL() << "unexpected UCMP result: " << cmp; } } } |