From 34495e1f8c4bfa175ab1db5e0a60bbcfd779e79d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Dennis=20Gl=C3=A4ser?= <dennis.glaeser@iws.uni-stuttgart.de>
Date: Wed, 25 Oct 2023 11:44:21 +0200
Subject: [PATCH] [test] add unit tests for chrono helpers

---
 test/io/CMakeLists.txt        |  1 +
 test/io/chrono/CMakeLists.txt |  5 +++++
 test/io/chrono/test_chrono.cc | 39 +++++++++++++++++++++++++++++++++++
 3 files changed, 45 insertions(+)
 create mode 100644 test/io/chrono/CMakeLists.txt
 create mode 100644 test/io/chrono/test_chrono.cc

diff --git a/test/io/CMakeLists.txt b/test/io/CMakeLists.txt
index 859b7c724d..394a45a794 100644
--- a/test/io/CMakeLists.txt
+++ b/test/io/CMakeLists.txt
@@ -1,6 +1,7 @@
 # SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
 # SPDX-License-Identifier: GPL-3.0-or-later
 
+add_subdirectory(chrono)
 add_subdirectory(container)
 add_subdirectory(format)
 add_subdirectory(gnuplotinterface)
diff --git a/test/io/chrono/CMakeLists.txt b/test/io/chrono/CMakeLists.txt
new file mode 100644
index 0000000000..cac1ca0197
--- /dev/null
+++ b/test/io/chrono/CMakeLists.txt
@@ -0,0 +1,5 @@
+# SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
+# SPDX-License-Identifier: GPL-3.0-or-later
+
+dumux_add_test(SOURCES test_chrono.cc
+               LABELS io unit)
diff --git a/test/io/chrono/test_chrono.cc b/test/io/chrono/test_chrono.cc
new file mode 100644
index 0000000000..b1cb2593aa
--- /dev/null
+++ b/test/io/chrono/test_chrono.cc
@@ -0,0 +1,39 @@
+//
+// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+#include <iostream>
+
+#include <dune/common/exceptions.hh>
+#include <dumux/io/chrono.hh>
+
+int main(int argc, char* argv[])
+{
+    using namespace Dumux;
+    using namespace std::chrono;
+    using dseconds = std::chrono::duration<double>;
+    using dmilliseconds = std::chrono::duration<double, std::milli>;
+
+    // seconds (test scientific notation and real values)
+    if (Chrono::toSeconds("1") != seconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1s") != seconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1.0s") != seconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("42.42s") != dseconds(42.42)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1e3s") != dseconds(1.0e3)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("42.42e3s") != dseconds(42.42e3)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1e-3s") != milliseconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1.5e-3s") != dmilliseconds(1.5)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+
+    if (Chrono::toSeconds("1ms") != milliseconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1us") != microseconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1ns") != nanoseconds(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+
+    if (Chrono::toSeconds("1min") != minutes(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1h") != hours(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+    if (Chrono::toSeconds("1d") != hours(24)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+#if __cplusplus >= 202002L
+    if (Chrono::toSeconds("1y") != years(1)) DUNE_THROW(Dune::InvalidStateException, "Wrong parse result");
+#endif
+
+    return 0;
+}
-- 
GitLab