|
8 | 8 | #include <chrono> |
9 | 9 | #include <iostream> |
10 | 10 | #include <sstream> |
| 11 | +#include "ITestFixture.hpp" |
11 | 12 |
|
12 | 13 | using namespace std::chrono_literals; |
13 | 14 |
|
| 15 | +namespace testing { |
| 16 | +namespace internal { |
14 | 17 | template <typename Rep, typename Period> |
15 | | -void PrintTo(const std::chrono::duration<Rep, Period>& d, std::ostream* os) |
| 18 | +class UniversalPrinter<std::chrono::duration<Rep, Period>> |
16 | 19 | { |
17 | | - *os << std::chrono::duration_cast<std::chrono::nanoseconds>(d).count() << "ns"; |
18 | | -} |
| 20 | +public: |
| 21 | + static void Print(const std::chrono::duration<Rep, Period>& value, ::std::ostream* os) |
| 22 | + { |
| 23 | + *os << std::chrono::duration_cast<std::chrono::nanoseconds>(value).count() << "ns"; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
19 | 27 | template <typename R1, typename P1, typename R2, typename P2> |
20 | | -void PrintTo(const std::pair<std::chrono::duration<R1, P1>, std::chrono::duration<R2, P2>>& p, std::ostream* os) |
| 28 | +class UniversalPrinter<std::pair<std::chrono::duration<R1, P1>, std::chrono::duration<R2, P2>>> |
21 | 29 | { |
22 | | - *os << "("; |
23 | | - PrintTo(p.first, os); |
24 | | - *os << ", "; |
25 | | - PrintTo(p.second, os); |
26 | | - *os << ")"; |
27 | | -} |
| 30 | +public: |
| 31 | + static void Print(const std::pair<std::chrono::duration<R1, P1>, std::chrono::duration<R2, P2>>& p, |
| 32 | + ::std::ostream* os) |
| 33 | + { |
| 34 | + *os << "("; |
| 35 | + UniversalPrinter<std::chrono::nanoseconds>::Print(p.first, os); |
| 36 | + *os << ", "; |
| 37 | + UniversalPrinter<std::chrono::nanoseconds>::Print(p.second, os); |
| 38 | + *os << ")"; |
| 39 | + } |
| 40 | +}; |
| 41 | + |
| 42 | +} // namespace internal |
| 43 | +} // namespace testing |
28 | 44 |
|
29 | 45 | inline std::string ToString(const std::chrono::nanoseconds& ns) |
30 | 46 | { |
31 | 47 | std::ostringstream os; |
32 | | - PrintTo(ns, &os); |
| 48 | + testing::internal::UniversalPrinter<std::chrono::nanoseconds>::Print(ns, &os); |
33 | 49 | return os.str(); |
34 | 50 | } |
35 | 51 | inline std::string ToString(const std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds>& p) |
36 | 52 | { |
37 | 53 | std::ostringstream os; |
38 | | - os << "("; |
39 | | - PrintTo(p.first, &os); |
40 | | - os << ", "; |
41 | | - PrintTo(p.second, &os); |
42 | | - os << ")"; |
| 54 | + testing::internal::UniversalPrinter<std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds>>::Print(p, &os); |
43 | 55 | return os.str(); |
44 | 56 | } |
45 | 57 |
|
46 | | -#include "ITestFixture.hpp" |
47 | | - |
48 | 58 | namespace { |
49 | 59 |
|
50 | 60 | using namespace SilKit::Tests; |
51 | 61 | using namespace SilKit::Config; |
52 | 62 | using namespace SilKit::Services; |
53 | 63 | using namespace SilKit::Services::Orchestration; |
54 | 64 |
|
| 65 | +template <typename Rep, typename Period> |
| 66 | +struct Dummy |
| 67 | +{ |
| 68 | + std::chrono::duration<Rep, Period> value; |
| 69 | + |
| 70 | + explicit Dummy(const std::chrono::duration<Rep, Period>& v) |
| 71 | + : value{v} |
| 72 | + { |
| 73 | + } |
| 74 | + |
| 75 | + bool operator==(const Dummy<Rep, Period>& other) const |
| 76 | + { |
| 77 | + return value == other.value; |
| 78 | + } |
| 79 | + |
| 80 | + friend void PrintTo(const Dummy<Rep, Period>& d, std::ostream* os) |
| 81 | + { |
| 82 | + *os << std::chrono::duration_cast<std::chrono::nanoseconds>(d.value).count() << "ns"; |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +#define SILKIT_ASSERT_CHRONO_EQ(expected, actual) ASSERT_EQ(Dummy{(expected)}, Dummy{(actual)}) |
| 87 | +#define SILKIT_EXPECT_CHRONO_EQ(expected, actual) EXPECT_EQ(Dummy{(expected)}, Dummy{(actual)}) |
| 88 | + |
| 89 | + |
55 | 90 | struct ParticipantParams |
56 | 91 | { |
57 | 92 | std::string name{}; |
@@ -171,7 +206,7 @@ void ITest_DynStepSizes::AssertStepsEqual(const std::vector<std::chrono::nanosec |
171 | 206 |
|
172 | 207 | for (size_t j = 0; j < s1.size(); ++j) |
173 | 208 | { |
174 | | - EXPECT_EQ(s1[j], s2[j]) << "Differenz at index " << j << ": " |
| 209 | + SILKIT_EXPECT_CHRONO_EQ(s1[j], s2[j]) << "Differenz at index " << j << ": " |
175 | 210 | << "s1 now=" << ToString(s1[j]) << " vs s2 now=" << ToString(s2[j]); |
176 | 211 | } |
177 | 212 | } |
|
0 commit comments