summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Rosdahl <joel@rosdahl.net>2022-11-10 16:29:25 +0100
committerJoel Rosdahl <joel@rosdahl.net>2022-11-13 16:34:58 +0100
commitbcfdd3e0e927fc8b021f98ff3e22d96cbd5de94d (patch)
treeadcd08d556f8dddd1803baf0e4553e45c1a69b4c
parent4905b1afacb01bff907b89dd4b05dff66b31790b (diff)
downloadccache-bcfdd3e0e927fc8b021f98ff3e22d96cbd5de94d.tar.gz
fix: Fix bug in Duration arithmetic operators
The bug only affected LongLivedLockFile, which is not used yet.
-rw-r--r--src/util/Duration.hpp8
-rw-r--r--unittest/CMakeLists.txt1
-rw-r--r--unittest/test_util_Duration.cpp131
3 files changed, 136 insertions, 4 deletions
diff --git a/src/util/Duration.hpp b/src/util/Duration.hpp
index 12c8729b..5ff4bef6 100644
--- a/src/util/Duration.hpp
+++ b/src/util/Duration.hpp
@@ -93,25 +93,25 @@ Duration::operator>=(const Duration& other) const
inline Duration
Duration::operator+(const Duration& other) const
{
- return Duration(m_ns + other.m_ns);
+ return Duration(0, m_ns + other.m_ns);
}
inline Duration
Duration::operator-(const Duration& other) const
{
- return Duration(m_ns - other.m_ns);
+ return Duration(0, m_ns - other.m_ns);
}
inline Duration
Duration::operator*(double factor) const
{
- return Duration(factor * m_ns);
+ return Duration(0, factor * m_ns);
}
inline Duration
Duration::operator/(double factor) const
{
- return Duration(m_ns / factor);
+ return Duration(0, m_ns / factor);
}
inline int64_t
diff --git a/unittest/CMakeLists.txt b/unittest/CMakeLists.txt
index d6e57da3..b129d59b 100644
--- a/unittest/CMakeLists.txt
+++ b/unittest/CMakeLists.txt
@@ -21,6 +21,7 @@ set(
test_storage_local_StatsFile.cpp
test_storage_local_util.cpp
test_util_Bytes.cpp
+ test_util_Duration.cpp
test_util_LockFile.cpp
test_util_TextTable.cpp
test_util_TimePoint.cpp
diff --git a/unittest/test_util_Duration.cpp b/unittest/test_util_Duration.cpp
new file mode 100644
index 00000000..b920281d
--- /dev/null
+++ b/unittest/test_util_Duration.cpp
@@ -0,0 +1,131 @@
+// Copyright (C) 2022 Joel Rosdahl and other contributors
+//
+// See doc/AUTHORS.adoc for a complete list of contributors.
+//
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General Public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+//
+// This program is distributed in the hope that it will be useful, but WITHOUT
+// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+// more details.
+//
+// You should have received a copy of the GNU General Public License along with
+// this program; if not, write to the Free Software Foundation, Inc., 51
+// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+
+#include <util/Duration.hpp>
+
+#include <third_party/doctest.h>
+
+TEST_SUITE_BEGIN("util::Duration");
+
+using util::Duration;
+
+TEST_CASE("Basics")
+{
+ Duration d0(4711, 2042);
+
+ CHECK(d0.sec() == 4711);
+ CHECK(d0.nsec() == 4711000002042);
+ CHECK(d0.nsec_decimal_part() == 2042);
+}
+
+TEST_CASE("Comparison operators")
+{
+ Duration d0(1000, 0);
+ Duration d1(1000, 42);
+ Duration d2(1001, 0);
+
+ SUBCASE("operator==")
+ {
+ CHECK(d0 == d0);
+ CHECK(!(d0 == d1));
+ CHECK(!(d1 == d0));
+ CHECK(!(d0 == d2));
+ CHECK(!(d2 == d0));
+ }
+
+ SUBCASE("operator!=")
+ {
+ CHECK(!(d0 != d0));
+ CHECK(d0 != d1);
+ CHECK(d1 != d0);
+ }
+
+ SUBCASE("operator<")
+ {
+ CHECK(d0 < d1);
+ CHECK(d0 < d2);
+ CHECK(d1 < d2);
+ CHECK(!(d1 < d0));
+ CHECK(!(d2 < d0));
+ CHECK(!(d2 < d1));
+ }
+
+ SUBCASE("operator>")
+ {
+ CHECK(d2 > d1);
+ CHECK(d2 > d0);
+ CHECK(d1 > d0);
+ CHECK(!(d1 > d2));
+ CHECK(!(d0 > d2));
+ CHECK(!(d0 > d1));
+ }
+
+ SUBCASE("operator<=")
+ {
+ CHECK(d0 <= d0);
+ CHECK(d0 <= d1);
+ CHECK(d0 <= d2);
+ CHECK(!(d1 <= d0));
+ CHECK(!(d2 <= d0));
+ }
+
+ SUBCASE("operator>=")
+ {
+ CHECK(d2 >= d1);
+ CHECK(d2 >= d0);
+ CHECK(d1 >= d0);
+ CHECK(!(d1 >= d2));
+ CHECK(!(d0 >= d2));
+ }
+}
+
+TEST_CASE("Arithmetic operators")
+{
+ Duration d0(1, 2);
+ Duration d1(3, 9);
+
+ SUBCASE("operator+")
+ {
+ Duration d = d0 + d1;
+ CHECK(d.sec() == 4);
+ CHECK(d.nsec_decimal_part() == 11);
+ }
+
+ SUBCASE("operator-")
+ {
+ Duration d = d0 - d1;
+ CHECK(d.sec() == -2);
+ CHECK(d.nsec_decimal_part() == -7);
+ }
+
+ SUBCASE("operator*")
+ {
+ Duration d = d1 * 4;
+ CHECK(d.sec() == 12);
+ CHECK(d.nsec_decimal_part() == 36);
+ }
+
+ SUBCASE("operator/")
+ {
+ Duration d = d1 / 0.8;
+ CHECK(d.sec() == 3);
+ CHECK(d.nsec_decimal_part() == 750'000'011);
+ }
+}
+
+TEST_SUITE_END();