From: Lokesh Walase Date: Sat, 13 Jun 2015 09:12:51 +0000 (+0530) Subject: hextolfp test converted to unity X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=58ded4e0946cce773dafb20610a47a11f205cdf6;p=thirdparty%2Fntp.git hextolfp test converted to unity bk: 557bf413mZ5C2qdi45su1BxQvAAU5Q --- diff --git a/tests/libntp/Makefile.am b/tests/libntp/Makefile.am index bfbda22cf..e3af5f30e 100644 --- a/tests/libntp/Makefile.am +++ b/tests/libntp/Makefile.am @@ -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 index 000000000..2e9f072cd --- /dev/null +++ b/tests/libntp/g_hextolfp.cpp @@ -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 index 000000000..04e96d164 --- /dev/null +++ b/tests/libntp/hextolfp.c @@ -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)); +} + +*/ diff --git a/tests/libntp/lfptest.h b/tests/libntp/lfptest.h index 034eb41dd..58857354e 100644 --- a/tests/libntp/lfptest.h +++ b/tests/libntp/lfptest.h @@ -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 */