From ca9bebce138e4673b1b28ec2200a4a9e61c124a0 Mon Sep 17 00:00:00 2001 From: Pavel Kirienko Date: Tue, 11 Mar 2014 15:00:14 +0400 Subject: [PATCH] Time overflow tests --- libuavcan/test/time.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/libuavcan/test/time.cpp b/libuavcan/test/time.cpp index fc4b39a0f3..ce99755c42 100644 --- a/libuavcan/test/time.cpp +++ b/libuavcan/test/time.cpp @@ -85,3 +85,23 @@ TEST(Time, Utc) ASSERT_EQ("0.018000", u1.toString()); ASSERT_EQ("12345.678900", UtcTime(ts).toString()); } + + +TEST(Time, Overflow) +{ + using uavcan::MonotonicDuration; + using uavcan::MonotonicTime; + + MonotonicTime max_4 = MonotonicTime::fromUSec(MonotonicTime::getMax().toUSec() / 4); + MonotonicDuration max_4_duration = max_4 - MonotonicTime(); + + std::cout << max_4 << std::endl; + ASSERT_EQ(max_4_duration.toUSec(), max_4.toUSec()); + + MonotonicTime max = (((max_4 + max_4_duration) + max_4_duration) + max_4_duration) + max_4_duration; + ASSERT_EQ(max, MonotonicTime::getMax()); // Must not overflow + + MonotonicTime min; + min -= max_4_duration; + ASSERT_EQ(min, MonotonicTime()); // Must not underflow +}