From bd1dc9f747d144c5fed833a9c51a982ee07c062d Mon Sep 17 00:00:00 2001 From: Sebastian Messmer Date: Wed, 16 May 2018 21:46:05 -0700 Subject: [PATCH] Add test case for clock_gettime --- test/cpp-utils/CMakeLists.txt | 1 + test/cpp-utils/system/ClockGetTimeTest.cpp | 40 ++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 test/cpp-utils/system/ClockGetTimeTest.cpp diff --git a/test/cpp-utils/CMakeLists.txt b/test/cpp-utils/CMakeLists.txt index 67786f96..4faa8585 100644 --- a/test/cpp-utils/CMakeLists.txt +++ b/test/cpp-utils/CMakeLists.txt @@ -45,6 +45,7 @@ set(SOURCES assert/backtrace_include_test.cpp assert/assert_include_test.cpp assert/assert_debug_test.cpp + system/ClockGetTimeTest.cpp system/MemoryTest.cpp system/HomedirTest.cpp ) diff --git a/test/cpp-utils/system/ClockGetTimeTest.cpp b/test/cpp-utils/system/ClockGetTimeTest.cpp new file mode 100644 index 00000000..41fecde1 --- /dev/null +++ b/test/cpp-utils/system/ClockGetTimeTest.cpp @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +namespace { +struct timespec _gettime() { + struct timespec current_time; + int res = clock_gettime(CLOCK_REALTIME, ¤t_time); + EXPECT_EQ(0, res); + return current_time; +} + +uint64_t _to_nanos(struct timespec time) { + constexpr uint64_t nanos = UINT64_C(1000000000); + return time.tv_sec * nanos + time.tv_nsec; +} +} + +TEST(ClockGetTimeTest, DoesntCrash) { + _gettime(); +} + +TEST(ClockGetTimeTest, IsLaterThanYear2010) { + struct timespec current_time = _gettime(); + EXPECT_LT(1262304000, current_time.tv_sec); +} + +TEST(ClockGetTimeTest, IsNondecreasing) { + uint64_t time1 = _to_nanos(_gettime()); + uint64_t time2 = _to_nanos(_gettime()); + EXPECT_LE(time1, time2); +} + +TEST(ClockGetTimeTest, IsIncreasedAfterPause) { + uint64_t time1 = _to_nanos(_gettime()); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + uint64_t time2 = _to_nanos(_gettime()); + EXPECT_LT(time1, time2); +}