]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
hextolfp test converted to unity
authorLokesh Walase <lokeshw24@ntp.org>
Sat, 13 Jun 2015 09:12:51 +0000 (14:42 +0530)
committerLokesh Walase <lokeshw24@ntp.org>
Sat, 13 Jun 2015 09:12:51 +0000 (14:42 +0530)
bk: 557bf413mZ5C2qdi45su1BxQvAAU5Q

tests/libntp/Makefile.am
tests/libntp/g_hextolfp.cpp [new file with mode: 0644]
tests/libntp/hextolfp.c [new file with mode: 0644]
tests/libntp/lfptest.h

index bfbda22cf03aaed5b951a320c39aeee4d058fb82..e3af5f30eac6e318470aae1d3bc2fcc3ef9d0449 100644 (file)
@@ -6,7 +6,8 @@ run_unity =     cd $(srcdir) && ruby ../../sntp/unity/auto/generate_test_runner.rb
 
 #removed test-libntp
 check_PROGRAMS = test-modetoa test-uglydate test-ymd2yd test-statestr test-numtoa test-numtohost \
-test-hextoint test-atoint test-atouint test-authkeys test-a_md5encrypt test-lfpfunc test-octtoint
+test-hextoint test-atoint test-atouint test-authkeys test-a_md5encrypt test-lfpfunc test-octtoint \
+test-hextolfp
 
 if GTEST_AVAILABLE
 check_PROGRAMS += tests
@@ -57,7 +58,7 @@ tests_SOURCES = $(top_srcdir)/sntp/tests_main.cpp     \
                clocktime.cpp           \
                decodenetnum.cpp        \
                g_hextoint.cpp          \
-               hextolfp.cpp            \
+               g_hextolfp.cpp          \
                humandate.cpp           \
                g_lfpfunc.cpp           \
                lfptostr.cpp            \
@@ -179,6 +180,14 @@ test_octtoint_LDADD =                   \
        $(unity_tests_LDADD)
        $(NULL)
 
+test_hextolfp_CFLAGS =                  \
+       -I$(top_srcdir)/sntp/unity      \
+       $(NULL)
+
+test_hextolfp_LDADD =                   \
+       $(unity_tests_LDADD)
+       $(NULL)
+
 
 test_a_md5encrypt_CFLAGS =                  \
        -I$(top_srcdir)/sntp/unity      \
@@ -270,6 +279,11 @@ test_octtoint_SOURCES =                    \
        run-test-octtoint.c                     \
        $(NULL)
 
+test_hextolfp_SOURCES =                \
+       hextolfp.c                      \
+       run-test-hextolfp.c                     \
+       $(NULL)
+
 
 test_a_md5encrypt_SOURCES =                    \
        a_md5encrypt.c                          \
@@ -320,6 +334,9 @@ $(srcdir)/run-test-atoint.c: $(srcdir)/atoint.c $(std_unity_list)
 $(srcdir)/run-test-octtoint.c: $(srcdir)/octtoint.c $(std_unity_list)
        $(run_unity) octtoint.c run-test-octtoint.c
 
+$(srcdir)/run-test-hextolfp.c: $(srcdir)/hextolfp.c $(std_unity_list)
+       $(run_unity) hextolfp.c run-test-hextolfp.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_hextolfp.cpp b/tests/libntp/g_hextolfp.cpp
new file mode 100644 (file)
index 0000000..2e9f072
--- /dev/null
@@ -0,0 +1,58 @@
+#include "lfptest.h"
+
+class hextolfpTest : public lfptest {
+};
+
+TEST_F(hextolfpTest, PositiveInteger) {
+       const char *str = "00001000.00000000";
+       l_fp actual;
+
+       l_fp expected = {4096, 0}; // 16^3, no fraction part.
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, NegativeInteger) {
+       const char *str = "ffffffff.00000000"; // -1 decimal
+       l_fp actual;
+
+       l_fp expected = {-1, 0};
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, PositiveFraction) {
+       const char *str = "00002000.80000000"; // 8196.5 decimal
+       l_fp actual;
+
+       l_fp expected = {8192, HALF};
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, NegativeFraction) {
+       const char *str = "ffffffff.40000000"; // -1 + 0.25 decimal
+       l_fp actual;
+
+       l_fp expected = {-1, QUARTER}; //-1 + 0.25
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, IllegalNumberOfInteger) {
+       const char *str = "1000000.00000000"; // Missing one digit in integral part.
+       l_fp actual;
+
+       ASSERT_FALSE(hextolfp(str, &actual));
+}
+
+TEST_F(hextolfpTest, IllegalChar) {
+       const char *str = "10000000.0000h000"; // Illegal character h.
+       l_fp actual;
+
+       ASSERT_FALSE(hextolfp(str, &actual));
+}
diff --git a/tests/libntp/hextolfp.c b/tests/libntp/hextolfp.c
new file mode 100644 (file)
index 0000000..04e96d1
--- /dev/null
@@ -0,0 +1,61 @@
+#include "testcalshims.h"
+#include "unity.h"
+#include "lfptest.h"
+
+
+void test_PositiveInteger(void) {
+       const char *str = "00001000.00000000";
+       l_fp actual;
+
+       l_fp expected = {4096, 0}; // 16^3, no fraction part.
+
+       TEST_ASSERT_TRUE(hextolfp(str, &actual));
+       TEST_ASSERT_TRUE(IsEqual(expected, actual));
+}
+
+/*
+TEST_F(hextolfpTest, NegativeInteger) {
+       const char *str = "ffffffff.00000000"; // -1 decimal
+       l_fp actual;
+
+       l_fp expected = {-1, 0};
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, PositiveFraction) {
+       const char *str = "00002000.80000000"; // 8196.5 decimal
+       l_fp actual;
+
+       l_fp expected = {8192, HALF};
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, NegativeFraction) {
+       const char *str = "ffffffff.40000000"; // -1 + 0.25 decimal
+       l_fp actual;
+
+       l_fp expected = {-1, QUARTER}; //-1 + 0.25
+
+       ASSERT_TRUE(hextolfp(str, &actual));
+       EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(hextolfpTest, IllegalNumberOfInteger) {
+       const char *str = "1000000.00000000"; // Missing one digit in integral part.
+       l_fp actual;
+
+       ASSERT_FALSE(hextolfp(str, &actual));
+}
+
+TEST_F(hextolfpTest, IllegalChar) {
+       const char *str = "10000000.0000h000"; // Illegal character h.
+       l_fp actual;
+
+       ASSERT_FALSE(hextolfp(str, &actual));
+}
+
+*/
index 034eb41dd6ce0b46aa929860d295dd5acb70f8c3..58857354e2edabd49cab579c0c67cb0a0cb731b0 100644 (file)
@@ -1,6 +1,28 @@
 #ifndef NTP_TESTS_LFPTEST_H
 #define NTP_TESTS_LFPTEST_H
 
+//#include "libntptest.h"
+//Including this gives some error during compilation.
+//Hence right now commenting it out, as it is not required currently.
+
+#include "ntp_fp.h"
+
+int IsEqual(const l_fp expected, const l_fp actual) {
+       if (L_ISEQU(&expected, &actual)) {
+               return 1==1;
+       } else {
+               return 1==2;
+       }
+
+}
+
+#endif
+
+
+/*
+#ifndef NTP_TESTS_LFPTEST_H
+#define NTP_TESTS_LFPTEST_H
+
 #include "libntptest.h"
 
 extern "C" {
@@ -28,4 +50,6 @@ protected:
        static const int32 QUARTER_PROMILLE_APPRX = 1073742L;
 };
 
-#endif /* NTP_TESTS_LFPTEST_H */
+#endif
+*/
+/* NTP_TESTS_LFPTEST_H */