GCC Code Coverage Report


Directory: ../../../builds/dumux-repositories/
File: /builds/dumux-repositories/dumux/dumux/io/chrono.hh
Date: 2024-09-21 20:52:54
Exec Total Coverage
Lines: 16 17 94.1%
Functions: 4 6 66.7%
Branches: 55 124 44.4%

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-FileCopyrightInfo: 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 26 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
6/12
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
6014 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 6 times.
✓ Branch 12 taken 10 times.
✗ Branch 13 not taken.
113 const auto unitIt = std::find_if(s.rbegin(), s.rend(), [] (const auto& c) { return !std::isalpha(c); });
47 78 const auto unitPos = s.size() - std::distance(s.rbegin(), unitIt);
48
2/6
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
26 const auto number = std::stod(s.substr(0, unitPos));
49 26 const auto unit = s.substr(unitPos);
50 26 const auto conversion = [&] () {
51 278 const auto it = std::find_if(unitMap.begin(), unitMap.end(), [u=std::string_view{unit}] (const auto& p) {
52
2/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
87 return p.first == u;
53 });
54
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
52 if (it == unitMap.end())
55 DUNE_THROW(Dune::InvalidStateException, "Unsupported unit " << unit << ".");
56 26 return it->second;
57
1/4
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
26 } ();
58
3/6
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
78 duration = duration_cast<std::chrono::duration<Rep, Period>>(number*conversion);
59 26 }
60
61 //! Try to construct an instance of std::chrono::seconds from a string including a unit suffix
62 template<typename Rep = double>
63 std::chrono::duration<Rep> toSeconds(const std::string& s)
64 {
65 std::chrono::duration<Rep> result;
66
14/28
✓ 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.
26 toDuration(result, s);
67
16/28
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 3 times.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 1 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 1 times.
✗ Branch 16 not taken.
✓ Branch 17 taken 1 times.
✗ Branch 18 not taken.
✓ Branch 19 taken 1 times.
✗ Branch 20 not taken.
✓ Branch 21 taken 1 times.
✗ Branch 22 not taken.
✓ Branch 23 taken 1 times.
✗ Branch 24 not taken.
✓ Branch 25 taken 1 times.
✗ Branch 26 not taken.
✓ Branch 27 taken 1 times.
26 return result;
68 }
69
70 } // end namespace Dumux::Chrono
71
72 #endif
73