GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: dumux/dumux/io/chrono.hh
Date: 2025-04-12 19:19:20
Exec Total Coverage
Lines: 17 18 94.4%
Functions: 3 3 100.0%
Branches: 33 84 39.3%

Line Branch Exec Source
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 //
4 // SPDX-FileCopyrightText: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7 /*!
8 * \file
9 * \ingroup InputOutput
10 * \brief Helper functions for working with std::chrono.
11 */
12 #ifndef DUMUX_IO_CHRONO_HH
13 #define DUMUX_IO_CHRONO_HH
14
15 #include <array>
16 #include <cctype>
17 #include <chrono>
18 #include <string>
19 #include <string_view>
20 #include <algorithm>
21
22 #include <dune/common/exceptions.hh>
23
24 namespace Dumux::Chrono {
25
26 //! Try to construct a std::chrono::duration from a string
27 template<typename Rep, typename Period>
28 27 void toDuration(std::chrono::duration<Rep, Period>& duration, const std::string& s)
29 {
30 using std::chrono::duration_cast;
31 using namespace std::chrono_literals;
32 using S = std::chrono::duration<Rep>;
33 27 constexpr std::array<std::pair<std::string_view, S>, 9> unitMap{{
34 {"", S(1)}, // assume seconds if no unit is given
35 {"s", S(1)},
36 {"ns", duration_cast<S>(std::chrono::nanoseconds(1))},
37 {"us", duration_cast<S>(std::chrono::microseconds(1))},
38 {"ms", duration_cast<S>(std::chrono::milliseconds(1))},
39 {"min", duration_cast<S>(std::chrono::minutes(1))},
40 {"h", duration_cast<S>(std::chrono::hours(1))},
41 // After requiring cpp20, we can use the aliases in std::chrono
42 {"d", duration_cast<S>(std::chrono::hours(24))},
43 {"y", duration_cast<S>(std::chrono::seconds(31556952))} // to match cpp20, see https://en.cppreference.com/w/cpp/chrono/duration
44 }};
45
46
9/14
✗ Branch 0 not taken.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 6 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 7 times.
✓ Branch 12 taken 11 times.
✗ Branch 13 not taken.
64 const auto unitIt = std::find_if(s.rbegin(), s.rend(), [] (const auto& c) { return !std::isalpha(c); });
47 27 const auto unitPos = s.size() - std::distance(s.rbegin(), unitIt);
48 54 const auto number = std::stod(s.substr(0, unitPos));
49 27 const auto unit = s.substr(unitPos);
50 27 const auto conversion = [&] () {
51
3/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 27 times.
123 const auto it = std::find_if(unitMap.begin(), unitMap.end(), [u=std::string_view{unit}] (const auto& p) {
52 96 return p.first == u;
53 });
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 27 times.
27 if (it == unitMap.end())
55 DUNE_THROW(Dune::InvalidStateException, "Unsupported unit " << unit << ".");
56 27 return it->second;
57
1/2
✓ Branch 1 taken 27 times.
✗ Branch 2 not taken.
27 } ();
58 27 duration = duration_cast<std::chrono::duration<Rep, Period>>(number*conversion);
59 27 }
60
61 //! Try to construct an instance of std::chrono::seconds from a string including a unit suffix
62 template<typename Rep = double>
63 27 std::chrono::duration<Rep> toSeconds(const std::string& s)
64 {
65 std::chrono::duration<Rep> result;
66
15/30
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 4 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 22 taken 1 times.
✗ Branch 23 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 28 taken 1 times.
✗ Branch 29 not taken.
✓ Branch 31 taken 1 times.
✗ Branch 32 not taken.
✓ Branch 34 taken 1 times.
✗ Branch 35 not taken.
✓ Branch 37 taken 1 times.
✗ Branch 38 not taken.
✓ Branch 40 taken 1 times.
✗ Branch 41 not taken.
✓ Branch 43 taken 1 times.
✗ Branch 44 not taken.
27 toDuration(result, s);
67
4/8
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
27 return result;
68 }
69
70 } // end namespace Dumux::Chrono
71
72 #endif
73