diff options
Diffstat (limited to 'base/chrono_utils_test.cpp')
| -rw-r--r-- | base/chrono_utils_test.cpp | 36 |
1 files changed, 35 insertions, 1 deletions
diff --git a/base/chrono_utils_test.cpp b/base/chrono_utils_test.cpp index 057132d9f..da442f455 100644 --- a/base/chrono_utils_test.cpp +++ b/base/chrono_utils_test.cpp @@ -19,6 +19,9 @@ #include <time.h> #include <chrono> +#include <sstream> +#include <string> +#include <thread> #include <gtest/gtest.h> @@ -42,5 +45,36 @@ TEST(ChronoUtilsTest, BootClockNowSeconds) { EXPECT_EQ(now, boot_seconds); } +template <typename T> +void ExpectAboutEqual(T expected, T actual) { + auto expected_upper_bound = expected * 1.05f; + auto expected_lower_bound = expected * .95; + EXPECT_GT(expected_upper_bound, actual); + EXPECT_LT(expected_lower_bound, actual); +} + +TEST(ChronoUtilsTest, TimerDurationIsSane) { + auto start = boot_clock::now(); + Timer t; + std::this_thread::sleep_for(50ms); + auto stop = boot_clock::now(); + auto stop_timer = t.duration(); + + auto expected = std::chrono::duration_cast<std::chrono::milliseconds>(stop - start); + ExpectAboutEqual(expected, stop_timer); +} + +TEST(ChronoUtilsTest, TimerOstream) { + Timer t; + std::this_thread::sleep_for(50ms); + auto stop_timer = t.duration().count(); + std::stringstream os; + os << t; + decltype(stop_timer) stop_timer_from_stream; + os >> stop_timer_from_stream; + EXPECT_NE(0, stop_timer); + ExpectAboutEqual(stop_timer, stop_timer_from_stream); +} + } // namespace base -} // namespace android
\ No newline at end of file +} // namespace android |
