From 87480d42384e2f01dc36e77de8ed6f26ee10c29e Mon Sep 17 00:00:00 2001
From: Lukas Weber <lweber@physik.rwth-aachen.de>
Date: Fri, 7 Jun 2019 10:20:09 +0200
Subject: [PATCH] using a unit test to find a bug in the duration parser B)

---
 meson.build              |  1 +
 src/runner.cpp           |  4 ++--
 test/duration_parser.cpp | 17 +++++++++++++++++
 test/meson.build         |  8 ++++++++
 4 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 test/duration_parser.cpp
 create mode 100644 test/meson.build

diff --git a/meson.build b/meson.build
index d177d31..255d5b4 100644
--- a/meson.build
+++ b/meson.build
@@ -19,3 +19,4 @@ should_install = not meson.is_subproject()
 loadleveller_deps = [ fmt_dep, yamlcpp_dep, mpi_dep, hdf5_lib ]
 
 subdir('src')
+subdir('test')
diff --git a/src/runner.cpp b/src/runner.cpp
index 1e53a99..2b9ecc0 100644
--- a/src/runner.cpp
+++ b/src/runner.cpp
@@ -26,7 +26,7 @@ enum {
 
 // parses the duration '[[hours:]minutes:]seconds' into seconds
 // replace as soon as there is an alternative
-static double parse_duration(std::string str) {
+static int parse_duration(const std::string &str) {
 	size_t idx;
 
 	try {
@@ -46,7 +46,7 @@ static double parse_duration(std::string str) {
 					throw std::runtime_error{"minutes"};
 				}
 
-				return 24 * 60 * i1 + 60 * i2 + i3;
+				return 60 * 60 * i1 + 60 * i2 + i3;
 			} else {
 				throw std::runtime_error{"hours"};
 			}
diff --git a/test/duration_parser.cpp b/test/duration_parser.cpp
new file mode 100644
index 0000000..243ff83
--- /dev/null
+++ b/test/duration_parser.cpp
@@ -0,0 +1,17 @@
+#include "runner.cpp"
+
+int main() {
+	using namespace loadl;
+
+	if(parse_duration("20") != 20) {
+		return 1;
+	}
+	if(parse_duration("10:03") != 10*60+3) {
+		return 2;
+	}
+	if(parse_duration("24:06:10") != 60*60*24+60*6+10) {
+		return 3;
+	}
+
+	return 0;
+}
diff --git a/test/meson.build b/test/meson.build
new file mode 100644
index 0000000..0b4807b
--- /dev/null
+++ b/test/meson.build
@@ -0,0 +1,8 @@
+
+t1 = executable('test_duration_parser', 'duration_parser.cpp',
+  dependencies : loadleveller_dep,
+  include_directories : include_directories('../src')
+)
+
+test('test duration parser', t1)
+        
-- 
GitLab