]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fix: Fix bug in Duration arithmetic operators
authorJoel Rosdahl <joel@rosdahl.net>
Thu, 10 Nov 2022 15:29:25 +0000 (16:29 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 13 Nov 2022 15:34:58 +0000 (16:34 +0100)
The bug only affected LongLivedLockFile, which is not used yet.

src/util/Duration.hpp
unittest/CMakeLists.txt
unittest/test_util_Duration.cpp [new file with mode: 0644]

index 12c8729b6f917575324c0828a10e4d410f8a5225..5ff4bef6f7941035461e92cf7a45f520cb9cce9f 100644 (file)
@@ -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
index d6e57da32db8c25ba0ac365931b6494c027ba04b..b129d59b5ec6f1de0cb2f1d6f7c00efb62fd5c2a 100644 (file)
@@ -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 (file)
index 0000000..b920281
--- /dev/null
@@ -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();