]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
Changes for <lfptostr> & <strtolfp> tests
authorLokesh Walase <lokeshw24@ntp.org>
Wed, 17 Jun 2015 09:42:46 +0000 (15:12 +0530)
committerLokesh Walase <lokeshw24@ntp.org>
Wed, 17 Jun 2015 09:42:46 +0000 (15:12 +0530)
bk: 55814116u6jVzABDiHpXEuL5iymjSQ

tests/libntp/Makefile.am
tests/libntp/g_lfptostr.cpp [new file with mode: 0644]
tests/libntp/g_strtolfp.cpp [new file with mode: 0644]
tests/libntp/lfptostr.c [new file with mode: 0644]
tests/libntp/strtolfp.c [new file with mode: 0644]

index 04830dd338a2208884e0d71d5ba0db1a1dad2f94..20a1ae1220f2ecbf9af5503ed75b96a4762d7b44 100644 (file)
@@ -31,6 +31,8 @@ check_PROGRAMS =              \
        test-decodenetnum       \
        test-timevalops         \
        test-timespecops        \
+       test-strtolfp       \
+       test-lfptostr       \
        $(NULL)
 
 if GTEST_AVAILABLE
@@ -86,7 +88,7 @@ tests_SOURCES =                                       \
                g_hextolfp.cpp          \
                humandate.cpp           \
                g_lfpfunc.cpp           \
-               lfptostr.cpp            \
+               g_lfptostr.cpp          \
                g_modetoa.cpp           \
                msyslog.cpp             \
                g_netof.cpp             \
@@ -97,10 +99,10 @@ tests_SOURCES =                                     \
                recvbuff.cpp            \
                g_refnumtoa.cpp         \
                sfptostr.cpp            \
-               socktoa.cpp             \
+               g_socktoa.cpp           \
                ssl_init.cpp            \
                g_statestr.cpp          \
-               strtolfp.cpp            \
+               g_strtolfp.cpp          \
                g_timespecops.cpp               \
                timestructs.cpp         \
                g_timevalops.cpp        \
@@ -242,6 +244,23 @@ test_socktoa_LDADD =                       \
        $(unity_tests_LDADD)            \
        $(NULL)
 
+test_strtolfp_CFLAGS =                 \
+       -I$(top_srcdir)/sntp/unity      \
+       $(NULL)
+
+test_strtolfp_LDADD =                  \
+       $(unity_tests_LDADD)            \
+       $(NULL)
+
+test_lfptostr_CFLAGS =                 \
+       -I$(top_srcdir)/sntp/unity      \
+       $(NULL)
+
+test_lfptostr_LDADD =                  \
+       $(unity_tests_LDADD)            \
+       $(NULL)
+
+
 test_a_md5encrypt_CFLAGS =                     \
        -I$(top_srcdir)/sntp/unity              \
        $(NULL)
@@ -418,6 +437,17 @@ test_socktoa_SOURCES =                     \
        run-test-socktoa.c              \
        $(NULL)
 
+test_strtolfp_SOURCES =                        \
+       strtolfp.c                      \
+       run-test-strtolfp.c             \
+       $(NULL)
+
+test_lfptostr_SOURCES =                        \
+       lfptostr.c                      \
+       run-test-lfptostr.c             \
+       $(NULL)
+
+
 test_atouint_SOURCES =                 \
        atouint.c                       \
        run-test-atouint.c              \
@@ -489,6 +519,7 @@ BUILT_SOURCES +=                            \
        $(srcdir)/run-test-hextoint.c           \
        $(srcdir)/run-test-hextolfp.c           \
        $(srcdir)/run-test-lfpfunc.c            \
+       $(srcdir)/run-test-lfptostr.c           \
        $(srcdir)/run-test-modetoa.c            \
        $(srcdir)/run-test-netof.c              \
        $(srcdir)/run-test-numtoa.c             \
@@ -496,6 +527,7 @@ BUILT_SOURCES +=                            \
        $(srcdir)/run-test-octtoint.c           \
        $(srcdir)/run-test-refnumtoa.c          \
        $(srcdir)/run-test-socktoa.c            \
+       $(srcdir)/run-test-strtolfp.c           \
        $(srcdir)/run-test-statestr.c           \
        $(srcdir)/run-test-uglydate.c           \
        $(srcdir)/run-test-vi64ops.c            \
@@ -544,6 +576,12 @@ $(srcdir)/run-test-decodenetnum.c: $(srcdir)/decodenetnum.c $(std_unity_list)
 $(srcdir)/run-test-socktoa.c: $(srcdir)/socktoa.c $(std_unity_list)
        $(run_unity) socktoa.c run-test-socktoa.c
 
+$(srcdir)/run-test-strtolfp.c: $(srcdir)/strtolfp.c $(std_unity_list)
+       $(run_unity) strtolfp.c run-test-strtolfp.c
+
+$(srcdir)/run-test-lfptostr.c: $(srcdir)/lfptostr.c $(std_unity_list)
+       $(run_unity) lfptostr.c run-test-lfptostr.c
+
 $(srcdir)/run-test-a_md5encrypt.c: $(srcdir)/a_md5encrypt.c $(std_unity_list)
        $(run_unity) a_md5encrypt.c run-test-a_md5encrypt.c
 
diff --git a/tests/libntp/g_lfptostr.cpp b/tests/libntp/g_lfptostr.cpp
new file mode 100644 (file)
index 0000000..aa5f511
--- /dev/null
@@ -0,0 +1,103 @@
+/* 
+ * This file contains test for both mfptoa and mfptoms (which uses dolfptoa),
+ * since all these functions are very similar. It also tests ulfptoa, which is
+ * a macro.
+ */
+
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp_fp.h"
+};
+
+class lfptostrTest : public libntptest {
+protected:
+       static const int LFP_MAX_PRECISION = 10;
+       static const int LFP_MAX_PRECISION_MS = 7;
+
+       static const int ONE_FOURTH = 1073741824; // (1 << 30)
+       static const int HALF = (1 << 31);
+       static const int THREE_FOURTH = -ONE_FOURTH;
+       static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
+       static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
+};
+
+TEST_F(lfptostrTest, PositiveInteger) {
+       l_fp test = {200, 0}; // exact 200.0000000000
+
+       EXPECT_STREQ("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
+       EXPECT_STREQ("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
+}
+
+TEST_F(lfptostrTest, NegativeInteger) {
+       l_fp test = {-100, 0}; // -100
+
+       EXPECT_STREQ("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
+       EXPECT_STREQ("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+TEST_F(lfptostrTest, PositiveIntegerWithFraction) {
+       l_fp test = {200, ONE_FOURTH}; // 200.25
+
+       EXPECT_STREQ("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
+       EXPECT_STREQ("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+TEST_F(lfptostrTest, NegativeIntegerWithFraction) {
+       l_fp test = {-100, ONE_FOURTH}; // -99.75
+
+       EXPECT_STREQ("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
+       EXPECT_STREQ("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+TEST_F(lfptostrTest, RoundingDownToInteger) {
+       l_fp test = {10, ONE_FOURTH}; // 10.25
+
+       EXPECT_STREQ("10", lfptoa(&test, 0));
+       EXPECT_STREQ("10250", lfptoms(&test, 0));
+}
+
+TEST_F(lfptostrTest, RoundingMiddleToInteger) {
+       l_fp test = {10, HALF}; // 10.5
+
+       EXPECT_STREQ("11", lfptoa(&test, 0));
+       EXPECT_STREQ("10500", lfptoms(&test, 0));
+}
+
+TEST_F(lfptostrTest, RoundingUpToInteger) {
+       l_fp test = {5, THREE_FOURTH}; // 5.75
+
+       EXPECT_STREQ("6", lfptoa(&test, 0));
+       EXPECT_STREQ("5750", lfptoms(&test, 0));
+}
+
+TEST_F(lfptostrTest, SingleDecimal) {
+       l_fp test = {8, ONE_FOURTH}; // 8.25
+
+       EXPECT_STREQ("8.3", lfptoa(&test, 1));
+       EXPECT_STREQ("8250.0", lfptoms(&test, 1));
+}
+
+TEST_F(lfptostrTest, MillisecondsRoundingUp) {
+       l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005
+
+       EXPECT_STREQ("1.0", lfptoa(&test, 1));
+
+       EXPECT_STREQ("1000.5", lfptoms(&test, 1));
+       EXPECT_STREQ("1001", lfptoms(&test, 0));
+}
+
+TEST_F(lfptostrTest, MillisecondsRoundingDown) {
+       l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
+
+       EXPECT_STREQ("1.0", lfptoa(&test, 1));
+
+       EXPECT_STREQ("1000.5", lfptoms(&test, 1));
+       EXPECT_STREQ("1000", lfptoms(&test, 0));
+}
+
+TEST_F(lfptostrTest, UnsignedInteger) {
+       l_fp test = {3000000000UL, 0};
+
+       EXPECT_STREQ("3000000000.0", ulfptoa(&test, 1));
+}
diff --git a/tests/libntp/g_strtolfp.cpp b/tests/libntp/g_strtolfp.cpp
new file mode 100644 (file)
index 0000000..729996e
--- /dev/null
@@ -0,0 +1,107 @@
+#include "lfptest.h"
+
+/* This class tests both atolfp and mstolfp */
+
+class strtolfpTest : public lfptest {
+};
+
+TEST_F(strtolfpTest, PositiveInteger) {
+       const char *str = "500";
+       const char *str_ms = "500000";
+
+       l_fp expected = {500,0};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeInteger) {
+       const char *str = "-300";
+       const char *str_ms = "-300000";
+
+       l_fp expected;
+       expected.l_i = -300;
+       expected.l_uf = 0;
+
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, PositiveFraction) {
+       const char *str = "+500.5";
+       const char *str_ms = "500500.0";
+
+       l_fp expected = {500, HALF};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeFraction) {
+       const char *str = "-300.75";
+       const char *str_ms = "-300750";
+
+       l_fp expected;
+       expected.l_i = -301;
+       expected.l_uf = QUARTER;
+
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, PositiveMsFraction) {
+       const char *str = "300.00025";
+       const char *str_ms = "300000.25";
+
+       l_fp expected = {300, QUARTER_PROMILLE_APPRX};
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, NegativeMsFraction) {
+       const char *str = "-199.99975";
+       const char *str_ms = "-199999.75";
+
+       l_fp expected;
+       expected.l_i = -200;
+       expected.l_uf = QUARTER_PROMILLE_APPRX;
+
+       l_fp actual, actual_ms;
+
+       ASSERT_TRUE(atolfp(str, &actual));
+       ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       EXPECT_TRUE(IsEqual(expected, actual));
+       EXPECT_TRUE(IsEqual(expected, actual_ms));
+}
+
+TEST_F(strtolfpTest, InvalidChars) {
+       const char *str = "500.4a2";
+       l_fp actual, actual_ms;
+
+       ASSERT_FALSE(atolfp(str, &actual));
+       ASSERT_FALSE(mstolfp(str, &actual_ms));
+}
diff --git a/tests/libntp/lfptostr.c b/tests/libntp/lfptostr.c
new file mode 100644 (file)
index 0000000..a5f67fb
--- /dev/null
@@ -0,0 +1,102 @@
+/* 
+ * This file contains test for both mfptoa and mfptoms (which uses dolfptoa),
+ * since all these functions are very similar. It also tests ulfptoa, which is
+ * a macro.
+ */
+
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_calendar.h"
+#include "unity.h"
+#include "ntp_fp.h"
+
+static const int LFP_MAX_PRECISION = 10;
+static const int LFP_MAX_PRECISION_MS = 7;
+
+static const int ONE_FOURTH = 1073741824; // (1 << 30)
+static const int HALF = (1 << 31);
+static const int THREE_FOURTH = -1073741824;
+static const int HALF_PROMILLE_UP = 2147484; // slightly more than 0.0005
+static const int HALF_PROMILLE_DOWN = 2147483; // slightly less than 0.0005
+
+void test_PositiveInteger(void) {
+       l_fp test = {200, 0}; // exact 200.0000000000
+
+       TEST_ASSERT_EQUAL_STRING("200.0000000000", mfptoa(test.l_ui, test.l_uf, LFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("200000.0000000", mfptoms(test.l_ui, test.l_uf, LFP_MAX_PRECISION_MS));
+}
+
+void test_NegativeInteger(void) {
+       l_fp test = {-100, 0}; // -100
+
+       TEST_ASSERT_EQUAL_STRING("-100.0000000000", lfptoa(&test, LFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("-100000.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+void test_PositiveIntegerWithFraction(void) {
+       l_fp test = {200, ONE_FOURTH}; // 200.25
+
+       TEST_ASSERT_EQUAL_STRING("200.2500000000", lfptoa(&test, LFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("200250.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+void test_NegativeIntegerWithFraction(void) {
+       l_fp test = {-100, ONE_FOURTH}; // -99.75
+
+       TEST_ASSERT_EQUAL_STRING("-99.7500000000", lfptoa(&test, LFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("-99750.0000000", lfptoms(&test, LFP_MAX_PRECISION_MS));
+}
+
+void test_RoundingDownToInteger(void) {
+       l_fp test = {10, ONE_FOURTH}; // 10.25
+
+       TEST_ASSERT_EQUAL_STRING("10", lfptoa(&test, 0));
+       TEST_ASSERT_EQUAL_STRING("10250", lfptoms(&test, 0));
+}
+
+void test_RoundingMiddleToInteger(void) {
+       l_fp test = {10, HALF}; // 10.5
+
+       TEST_ASSERT_EQUAL_STRING("11", lfptoa(&test, 0));
+       TEST_ASSERT_EQUAL_STRING("10500", lfptoms(&test, 0));
+}
+
+void test_RoundingUpToInteger(void) {
+       l_fp test = {5, THREE_FOURTH}; // 5.75
+
+       TEST_ASSERT_EQUAL_STRING("6", lfptoa(&test, 0));
+       TEST_ASSERT_EQUAL_STRING("5750", lfptoms(&test, 0));
+}
+
+void test_SingleDecimal(void) {
+       l_fp test = {8, ONE_FOURTH}; // 8.25
+
+       TEST_ASSERT_EQUAL_STRING("8.3", lfptoa(&test, 1));
+       TEST_ASSERT_EQUAL_STRING("8250.0", lfptoms(&test, 1));
+}
+
+void test_MillisecondsRoundingUp(void) {
+       l_fp test = {1, HALF_PROMILLE_UP}; //slightly more than 1.0005
+
+       TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
+
+       TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
+       TEST_ASSERT_EQUAL_STRING("1001", lfptoms(&test, 0));
+}
+
+void test_MillisecondsRoundingDown(void) {
+       l_fp test = {1, HALF_PROMILLE_DOWN}; // slightly less than 1.0005
+
+       TEST_ASSERT_EQUAL_STRING("1.0", lfptoa(&test, 1));
+
+       TEST_ASSERT_EQUAL_STRING("1000.5", lfptoms(&test, 1));
+       TEST_ASSERT_EQUAL_STRING("1000", lfptoms(&test, 0));
+}
+
+void test_UnsignedInteger(void) {
+       l_fp test = {3000000000UL, 0};
+
+       TEST_ASSERT_EQUAL_STRING("3000000000.0", ulfptoa(&test, 1));
+}
+
+
diff --git a/tests/libntp/strtolfp.c b/tests/libntp/strtolfp.c
new file mode 100644 (file)
index 0000000..d328244
--- /dev/null
@@ -0,0 +1,112 @@
+#include "config.h"
+#include "ntp_stdlib.h"
+#include "ntp_calendar.h"
+#include "unity.h"
+#include "c_lfptest.h"
+
+/* This file tests both atolfp and mstolfp */
+
+void test_PositiveInteger(void) {
+       const char *str = "500";
+       const char *str_ms = "500000";
+
+       l_fp expected = {500,0};
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+}
+
+void test_NegativeInteger(void) {
+       const char *str = "-300";
+       const char *str_ms = "-300000";
+
+       l_fp expected;
+       expected.l_i = -300;
+       expected.l_uf = 0;
+
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+}
+
+void test_PositiveFraction(void) {
+       const char *str = "+500.5";
+       const char *str_ms = "500500.0";
+
+       l_fp expected = {500, HALF};
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+}
+
+void test_NegativeFraction(void) {
+       const char *str = "-300.75";
+       const char *str_ms = "-300750";
+
+       l_fp expected;
+       expected.l_i = -301;
+       expected.l_uf = QUARTER;
+
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+}
+
+void test_PositiveMsFraction(void) {
+       const char *str = "300.00025";
+       const char *str_ms = "300000.25";
+
+       l_fp expected = {300, QUARTER_PROMILLE_APPRX};
+       l_fp actual, actual_ms;
+
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+
+}
+
+void test_NegativeMsFraction(void) {
+       const char *str = "-199.99975";
+       const char *str_ms = "-199999.75";
+
+       l_fp expected;
+       expected.l_i = -200;
+       expected.l_uf = QUARTER_PROMILLE_APPRX;
+
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_TRUE(atolfp(str, &actual));
+       TEST_ASSERT_TRUE(mstolfp(str_ms, &actual_ms));
+
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual_ms));
+
+}
+
+void test_InvalidChars(void) {
+       const char *str = "500.4a2";
+       l_fp actual, actual_ms;
+
+       TEST_ASSERT_FALSE(atolfp(str, &actual));
+       TEST_ASSERT_FALSE(mstolfp(str, &actual_ms));
+}
+