summaryrefslogtreecommitdiff
path: root/tests/libntp/tstotv.cpp
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2014-12-02 09:01:21 +0000
committer <>2014-12-04 16:11:25 +0000
commitbdab5265fcbf3f472545073a23f8999749a9f2b9 (patch)
treec6018dd03dea906f8f1fb5f105f05b71a7dc250a /tests/libntp/tstotv.cpp
downloadntp-bdab5265fcbf3f472545073a23f8999749a9f2b9.tar.gz
Imported from /home/lorry/working-area/delta_ntp/ntp-dev-4.2.7p482.tar.gz.ntp-dev-4.2.7p482
Diffstat (limited to 'tests/libntp/tstotv.cpp')
-rw-r--r--tests/libntp/tstotv.cpp57
1 files changed, 57 insertions, 0 deletions
diff --git a/tests/libntp/tstotv.cpp b/tests/libntp/tstotv.cpp
new file mode 100644
index 0000000..41f44d7
--- /dev/null
+++ b/tests/libntp/tstotv.cpp
@@ -0,0 +1,57 @@
+#include "libntptest.h"
+
+extern "C" {
+#include "ntp_fp.h"
+#include "timevalops.h"
+};
+
+class tstotvTest : public libntptest {
+protected:
+ ::testing::AssertionResult IsEqual(const timeval& expected,
+ const timeval& actual) {
+ if (expected.tv_sec == actual.tv_sec &&
+ expected.tv_usec == actual.tv_usec) {
+ // Success
+ return ::testing::AssertionSuccess();
+ } else {
+ return ::testing::AssertionFailure()
+ << "expected: " << expected.tv_sec << "."
+ << expected.tv_usec
+ << " but was: " << actual.tv_sec << "."
+ << actual.tv_usec;
+ }
+ }
+
+ static const u_long HALF = 2147483648UL;
+};
+
+TEST_F(tstotvTest, Seconds) {
+ const l_fp input = {50, 0}; // 50.0 s
+ const timeval expected = {50, 0};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+}
+
+TEST_F(tstotvTest, MicrosecondsExact) {
+ const l_fp input = {50, HALF}; // 10.5 s
+ const timeval expected = {50, 500000};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+
+}
+
+TEST_F(tstotvTest, MicrosecondsRounding) {
+ const l_fp input = {50, 3865471UL}; // Should round to 50.0009
+ const timeval expected = {50, 900};
+ timeval actual;
+
+ TSTOTV(&input, &actual);
+
+ EXPECT_TRUE(IsEqual(expected, actual));
+}