Add test case for clock_gettime

This commit is contained in:
Sebastian Messmer 2018-05-16 21:46:05 -07:00
parent 2888ea6efd
commit bd1dc9f747
2 changed files with 41 additions and 0 deletions

View File

@ -45,6 +45,7 @@ set(SOURCES
assert/backtrace_include_test.cpp assert/backtrace_include_test.cpp
assert/assert_include_test.cpp assert/assert_include_test.cpp
assert/assert_debug_test.cpp assert/assert_debug_test.cpp
system/ClockGetTimeTest.cpp
system/MemoryTest.cpp system/MemoryTest.cpp
system/HomedirTest.cpp system/HomedirTest.cpp
) )

View File

@ -0,0 +1,40 @@
#include <gtest/gtest.h>
#include <cpp-utils/system/clock_gettime.h>
#include <chrono>
#include <thread>
namespace {
struct timespec _gettime() {
struct timespec current_time;
int res = clock_gettime(CLOCK_REALTIME, &current_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);
}