From a2a5cb0f24144f71cdf46cd335303f22fdf8355c Mon Sep 17 00:00:00 2001 From: Brian Gianforcaro Date: Sat, 14 Aug 2021 16:49:53 -0700 Subject: [PATCH] AK: Add Time::is_negative() to detect negative time values --- AK/Time.h | 1 + Tests/AK/TestTime.cpp | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/AK/Time.h b/AK/Time.h index b869034933a..93d7dc1eeba 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -164,6 +164,7 @@ public: [[nodiscard]] timeval to_timeval() const; [[nodiscard]] bool is_zero() const { return !m_seconds && !m_nanoseconds; } + [[nodiscard]] bool is_negative() const { return m_seconds < 0; } bool operator==(const Time& other) const { return this->m_seconds == other.m_seconds && this->m_nanoseconds == other.m_nanoseconds; } bool operator!=(const Time& other) const { return !(*this == other); } diff --git a/Tests/AK/TestTime.cpp b/Tests/AK/TestTime.cpp index 61319cd6053..76bd9ce36da 100644 --- a/Tests/AK/TestTime.cpp +++ b/Tests/AK/TestTime.cpp @@ -261,3 +261,16 @@ TEST_CASE(truncation) EXPECT_EQ(TIME(9223372036854, 775'807'000).to_truncated_microseconds(), 0x7fff'ffff'ffff'ffff); EXPECT_EQ(TIME(9223372036854, 775'808'000).to_truncated_microseconds(), 0x7fff'ffff'ffff'ffff); } + +TEST_CASE(is_negative) +{ + auto small = Time::from_nanoseconds(10); + auto large = Time::from_nanoseconds(15); + auto result = small - large; + EXPECT_EQ(result.to_nanoseconds(), -5); + EXPECT(result.is_negative()); + + result = large - small; + EXPECT_EQ(result.to_nanoseconds(), 5); + EXPECT(!result.is_negative()); +}