]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
converted sfptostr from GTest to Unity
authorTomek Mrugalski <tomasz@isc.org>
Fri, 19 Jun 2015 21:07:51 +0000 (23:07 +0200)
committerTomek Mrugalski <tomasz@isc.org>
Fri, 19 Jun 2015 21:07:51 +0000 (23:07 +0200)
bk: 558484a7B4rJ-lFz--W6LysF0Jdwqw

sntp/tests/Makefile.am
sntp/tests/g_keyFile.cpp [moved from sntp/tests/keyFile.cpp with 100% similarity]
tests/libntp/Makefile.am
tests/libntp/g_sfptostr.cpp [moved from tests/libntp/sfptostr.cpp with 100% similarity]
tests/libntp/sfptostr.c [new file with mode: 0644]

index cc8b08fac668d89e817c2d04d81f8c7b528cd863..fa7ee24756af8275c198edc519912d3319ae2829 100644 (file)
@@ -29,7 +29,7 @@ base_SOURCES =                        \
 tests_SOURCES =                        \
        $(base_SOURCES)         \
        crypto.cpp              \
-       keyFile.cpp             \
+       g_keyFile.cpp           \
        g_kodDatabase.cpp       \
        kodFile.cpp             \
        g_networking.cpp        \
index 4fcbfab124e7f5b1a95d94905461dd545538fe4e..61e08e932442cdff5028056050284fa1ae3feeb6 100644 (file)
@@ -24,6 +24,7 @@ check_PROGRAMS =              \
        test-numtohost          \
        test-octtoint           \
        test-refnumtoa          \
+       test-sfptostr           \
        test-ssl_init           \
        test-socktoa            \
        test-statestr           \
@@ -98,7 +99,7 @@ tests_SOURCES =                                       \
        prettydate.cpp          \
        recvbuff.cpp            \
        g_refnumtoa.cpp         \
-       sfptostr.cpp            \
+       g_sfptostr.cpp          \
        g_socktoa.cpp           \
        g_ssl_init.cpp          \
        g_statestr.cpp          \
@@ -131,6 +132,7 @@ BUILT_SOURCES +=                    \
        $(srcdir)/run-numtohost.c       \
        $(srcdir)/run-octtoint.c        \
        $(srcdir)/run-refnumtoa.c       \
+       $(srcdir)/run-sfptostr.c        \
        $(srcdir)/run-ssl_init.c        \
        $(srcdir)/run-socktoa.c         \
        $(srcdir)/run-statestr.c        \
@@ -398,6 +400,15 @@ test_tvtots_LDADD =                                \
        $(top_builddir)/sntp/unity/libunity.a   \
        $(NULL)
 
+test_sfptostr_CFLAGS =                         \
+       -I$(top_srcdir)/sntp/unity              \
+       -DUNITY_INCLUDE_DOUBLE                  \
+       $(NULL)
+
+test_sfptostr_LDADD =                          \
+       $(LDADD)                                \
+       $(top_builddir)/sntp/unity/libunity.a   \
+       $(NULL)
 
 
 test_modetoa_SOURCES =                 \
@@ -539,6 +550,11 @@ test_tvtots_SOURCES =                      \
        run-tvtots.c                    \
        $(NULL)
 
+test_sfptostr_SOURCES =                        \
+       sfptostr.c                      \
+       run-sfptostr.c                  \
+       $(NULL)
+
 $(srcdir)/run-modetoa.c: $(srcdir)/modetoa.c $(std_unity_list)
        $(run_unity) modetoa.c run-modetoa.c
 
@@ -620,6 +636,10 @@ $(srcdir)/run-ssl_init.c: $(srcdir)/ssl_init.c $(std_unity_list)
 $(srcdir)/run-tvtots.c: $(srcdir)/tvtots.c $(std_unity_list)
        $(run_unity) tvtots.c run-tvtots.c
 
+$(srcdir)/run-sfptostr.c: $(srcdir)/sfptostr.c $(std_unity_list)
+       $(run_unity) sfptostr.c run-sfptostr.c
+
+
 TESTS =
 
 if !NTP_CROSSCOMPILE
diff --git a/tests/libntp/sfptostr.c b/tests/libntp/sfptostr.c
new file mode 100644 (file)
index 0000000..577df48
--- /dev/null
@@ -0,0 +1,73 @@
+/* 
+ * This file contains test for both fptoa and fptoms (which uses dofptoa),
+ * since all these functions are very similar.
+ */
+#include "config.h"
+#include "ntp_fp.h"
+#include "unity.h"
+#define SFP_MAX_PRECISION 6
+
+void test_PositiveInteger(void)
+{
+       s_fp test = 300 << 16; // exact 300.000000
+
+       TEST_ASSERT_EQUAL_STRING("300.000000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("300000.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_NegativeInteger(void)
+{
+       s_fp test = -200 << 16; // exact -200.000000
+
+       TEST_ASSERT_EQUAL_STRING("-200.000000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("-200000.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_PositiveIntegerPositiveFraction(void)
+{
+       s_fp test = (300 << 16) + (1 << 15); // 300 + 0.5
+
+       TEST_ASSERT_EQUAL_STRING("300.500000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("300500.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_NegativeIntegerNegativeFraction(void)
+{
+       s_fp test = (-200 << 16) - (1 << 15); // -200 - 0.5
+
+       TEST_ASSERT_EQUAL_STRING("-200.500000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("-200500.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_PositiveIntegerNegativeFraction(void)
+{
+       s_fp test = (300 << 16) - (1 << 14); // 300 - 0.25
+
+       TEST_ASSERT_EQUAL_STRING("299.750000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("299750.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_NegativeIntegerPositiveFraction(void)
+{
+       s_fp test = (-200 << 16) + (1 << 14)*3; // -200 + 0.75
+
+       TEST_ASSERT_EQUAL_STRING("-199.250000", fptoa(test, SFP_MAX_PRECISION));
+       TEST_ASSERT_EQUAL_STRING("-199250.000", fptoms(test, SFP_MAX_PRECISION));
+}
+
+void test_SingleDecimalInteger(void)
+{
+       s_fp test = 300 << 16; // 300
+
+       TEST_ASSERT_EQUAL_STRING("300.0", fptoa(test, 1));
+       TEST_ASSERT_EQUAL_STRING("300000.0", fptoms(test, 1));
+}
+
+void test_SingleDecimalRounding(void)
+{
+       s_fp test = (2 << 16) + (1 << 14)*3; // 2 + 0.25*3 = 2.75
+
+       TEST_ASSERT_EQUAL_STRING("2.8", fptoa(test, 1));
+       TEST_ASSERT_EQUAL_STRING("2750.0", fptoms(test, 1));
+}
\ No newline at end of file