[Git][NTPsec/ntpsec][master] Tests for dolfptoa.c
Eric S. Raymond
gitlab at mg.gitlab.com
Sun Aug 5 16:34:11 UTC 2018
Eric S. Raymond pushed to branch master at NTPsec / ntpsec
Commits:
64ff2832 by Ian Bruene at 2018-08-05T16:34:05Z
Tests for dolfptoa.c
- - - - -
5 changed files:
- libntp/dolfptoa.c
- tests/common/tests_main.c
- + tests/libntp/dolfptoa.c
- tests/libntp/ntp_endian.c
- tests/wscript
Changes:
=====================================
libntp/dolfptoa.c
=====================================
@@ -52,6 +52,10 @@ dolfptoa(
digit = fpi;
fpi /= 10U;
+ /*
+ * This should be able to be replaced by [digit -= fpi * 10].
+ * It is being left as is at the moment for subtle bug avoidance.
+ */
digit -= (fpi << 3) + (fpi << 1); /* i*10 */
*--cp = (uint8_t)digit;
}
=====================================
tests/common/tests_main.c
=====================================
@@ -42,7 +42,9 @@ static void RunAllTests(void)
RUN_TEST_GROUP(authkeys);
RUN_TEST_GROUP(calendar);
RUN_TEST_GROUP(clocktime);
+ RUN_TEST_GROUP(endian);
RUN_TEST_GROUP(decodenetnum);
+ RUN_TEST_GROUP(dolfptoa);
RUN_TEST_GROUP(hextolfp);
RUN_TEST_GROUP(lfpfunc);
RUN_TEST_GROUP(lfptostr);
=====================================
tests/libntp/dolfptoa.c
=====================================
@@ -0,0 +1,80 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_fp.h"
+
+#include "unity.h"
+#include "unity_fixture.h"
+
+TEST_GROUP(dolfptoa);
+
+TEST_SETUP(dolfptoa) {}
+
+TEST_TEAR_DOWN(dolfptoa) {}
+
+
+TEST(dolfptoa, DoLfpToA) {
+ l_fp in;
+
+ // Nothingness
+ in = lfpinit(0, 0);
+ TEST_ASSERT_EQUAL_STRING("0", dolfptoa(in, false, 0, false));
+ TEST_ASSERT_EQUAL_STRING("-0", dolfptoa(in, true, 0, false));
+ TEST_ASSERT_EQUAL_STRING("0", dolfptoa(in, false, 0, true));
+ TEST_ASSERT_EQUAL_STRING("-0", dolfptoa(in, true, 0, true));
+ // Somthingness
+ in = lfpinit(42, 0x80000000);
+ TEST_ASSERT_EQUAL_STRING("42.500", dolfptoa(in, false, 3, false));
+ TEST_ASSERT_EQUAL_STRING("-42.500", dolfptoa(in, true, 3, false));
+ TEST_ASSERT_EQUAL_STRING("42500.000", dolfptoa(in, false, 3, true));
+ TEST_ASSERT_EQUAL_STRING("-42500.000", dolfptoa(in, true, 3, true));
+ // Rounded up decimals
+ in = lfpinit(0xFFFFFFFF, 0xFFFFFF);
+ TEST_ASSERT_EQUAL_STRING("4294967295.004", dolfptoa(in, false, 3, false));
+ TEST_ASSERT_EQUAL_STRING("-4294967295.004", dolfptoa(in, true, 3, false));
+ TEST_ASSERT_EQUAL_STRING("4294967295003.906", dolfptoa(in, false, 3, true));
+ TEST_ASSERT_EQUAL_STRING("-4294967295003.906", dolfptoa(in, true, 3, true));
+ // Rounded down decimals
+ in = lfpinit(0xFFFFFFFF, 0xFF000000);
+ TEST_ASSERT_EQUAL_STRING("4294967295.99609",
+ dolfptoa(in, false, 5, false));
+ TEST_ASSERT_EQUAL_STRING("-4294967295.99609",
+ dolfptoa(in, true, 5, false));
+ TEST_ASSERT_EQUAL_STRING("4294967295996.09375",
+ dolfptoa(in, false, 5, true));
+ TEST_ASSERT_EQUAL_STRING("-4294967295996.09375",
+ dolfptoa(in, true, 5, true));
+ // Extended decimals
+ in = lfpinit(0xFFFFFFFF, 0xFF000000);
+ TEST_ASSERT_EQUAL_STRING("4294967295.99609375000000",
+ dolfptoa(in, false, 15, false));
+ TEST_ASSERT_EQUAL_STRING("-4294967295.99609375000000",
+ dolfptoa(in, true, 15, false));
+ TEST_ASSERT_EQUAL_STRING("4294967295996.09375000000",
+ dolfptoa(in, false, 15, true));
+ TEST_ASSERT_EQUAL_STRING("-4294967295996.09375000000",
+ dolfptoa(in, true, 15, true));
+}
+
+TEST(dolfptoa, MfpToA) {
+ l_fp in;
+
+ in = lfpinit(42, 0);
+ TEST_ASSERT_EQUAL_STRING("42.000", mfptoa(in, 3));
+ in = lfpinit(-42, 0);
+ TEST_ASSERT_EQUAL_STRING("-42.000", mfptoa(in, 3));
+}
+
+TEST(dolfptoa, MfpToMs) {
+ l_fp in;
+
+ in = lfpinit(42, 0);
+ TEST_ASSERT_EQUAL_STRING("42000.000", mfptoms(in, 3));
+ in = lfpinit(-42, 0);
+ TEST_ASSERT_EQUAL_STRING("-42000.000", mfptoms(in, 3));
+}
+
+TEST_GROUP_RUNNER(dolfptoa) {
+ RUN_TEST_CASE(dolfptoa, DoLfpToA);
+ RUN_TEST_CASE(dolfptoa, MfpToA);
+ RUN_TEST_CASE(dolfptoa, MfpToMs);
+}
=====================================
tests/libntp/ntp_endian.c
=====================================
@@ -14,19 +14,19 @@ TEST_TEAR_DOWN(endian) {}
TEST(endian, Bit16) {
uint8_t buffer[2] = {0x22, 0x11};
- TEST_ASSERT_EQUAL(0x1122, ntp_be16dec((void *)buffer));
+ TEST_ASSERT_EQUAL(0x2211, ntp_be16dec((void *)buffer));
}
TEST(endian, Bit32) {
uint8_t buffer[4] = {0x44, 0x33, 0x22, 0x11};
- TEST_ASSERT_EQUAL(0x11223344, ntp_be32dec((void *)buffer));
+ TEST_ASSERT_EQUAL(0x44332211, ntp_be32dec((void *)buffer));
}
TEST(endian, Bit64) {
uint8_t buffer[8] = {0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11};
- TEST_ASSERT_EQUAL(0x1122334455667788, ntp_be64dec((void *)buffer));
+ TEST_ASSERT_EQUAL(0x8877665544332211, ntp_be64dec((void *)buffer));
}
TEST_GROUP_RUNNER(endian) {
=====================================
tests/wscript
=====================================
@@ -39,6 +39,7 @@ def build(ctx):
"libntp/ntp_random.c",
"libntp/clocktime.c",
"libntp/decodenetnum.c",
+ "libntp/dolfptoa.c",
"libntp/hextolfp.c",
"libntp/lfpfunc.c",
"libntp/lfptostr.c",
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/64ff2832ef4b652b8f0039693b1f1e2d2a5b8764
--
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/64ff2832ef4b652b8f0039693b1f1e2d2a5b8764
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20180805/f6c3dcb2/attachment-0001.html>
More information about the vc
mailing list