From: Harlan Stenn Date: Fri, 26 Jun 2015 10:58:41 +0000 (+0000) Subject: smeartest.c - Harlan Stenn X-Git-Tag: NTP_4_2_8P3_RC3~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=349821cc02e3205d950a10051e4359684bd62275;p=thirdparty%2Fntp.git smeartest.c - Harlan Stenn bk: 558d30618t4VTBexShgf1TJ_DWg67Q --- diff --git a/tests/sandbox/Makefile.am b/tests/sandbox/Makefile.am index c79e5be48..489ceab57 100644 --- a/tests/sandbox/Makefile.am +++ b/tests/sandbox/Makefile.am @@ -5,7 +5,7 @@ CLEANFILES = run_unity = cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb -check_PROGRAMS = bug-2803 first-test second-test +check_PROGRAMS = bug-2803 first-test second-test smeartest # HMS: we may not need some of these: LDADD = \ diff --git a/tests/sandbox/smeartest.c b/tests/sandbox/smeartest.c new file mode 100644 index 000000000..9b07581d6 --- /dev/null +++ b/tests/sandbox/smeartest.c @@ -0,0 +1,104 @@ +#include + +#include +#include + +/* + * we want to test a refid format of: + * 254.x.y.x + * + * where x.y.z are 24 bits containing 2 (signed) integer bits + * and 22 fractional bits. + * + * we want functions to convert to/from this format, with unit tests. + * + * Interesting test cases include: + * 254.0.0.0 + * 254.0.0.1 + * 254.127.255.255 + * 254.128.0.0 + * 254.255.255.255 + */ + +char *progname = ""; + +l_fp convertRefIDToLFP(uint32_t r); +uint32_t convertLFPToRefID(l_fp num); + + +/* + * The smear data in the refid is the bottom 3 bytes of the refid, + * 2 bits of integer + * 22 bits of fraction + */ +l_fp +convertRefIDToLFP(uint32_t r) +{ + l_fp temp; + + printf("%02x %02x %02x %x: ", + (r >> 22) & 0x3, + (r >> 14) & 0xff, + (r >> 6) & 0xff, + (r & 0x0f) + ); + + temp.l_ui = (r >> 22) & 0x3; + temp.l_uf = ((uint32_t) ((r >> 14) & 0xFF) << 26) + | ((uint32_t) ((r >> 6) & 0xFF) << 18) + | ((uint32_t) ((r >> 0) & 0x0F) << 10); + + if (temp.l_ui & 2) + temp.l_ui |= 0xFFFFFFFEu; + + return temp; +} + + +uint32_t +convertLFPToRefID(l_fp num) +{ + uint32_t temp; + + temp = (uint8_t) (num.l_ui << 6) | (num.l_uf >> 26); + temp |= (uint8_t) (num.l_uf >> 18); + temp |= (uint8_t) (num.l_uf >> 10); + + return temp; +} + + +void rtol(uint32_t r); + +void +rtol(uint32_t r) +{ + l_fp l; + + // l = convertRefIDToLFP(htonl(r)); + l = convertRefIDToLFP(r); + printf("refid %#x, smear %s\n", r, lfptoa(&l, 8)); + + return; +} + + +main() +{ + + rtol(0xfe800000); + rtol(0xfe800001); + rtol(0xfe8ffffe); + rtol(0xfe8fffff); + rtol(0xfefffffe); + rtol(0xfeffffff); + + rtol(0xfe000000); + rtol(0xfe000001); + rtol(0xfe3ffffe); + rtol(0xfe3fffff); + rtol(0xfe7ffffe); + rtol(0xfe7fffff); + + return 0; +}