Skip to content

Commit 2d373b5

Browse files
Fix gtest printing
Signed-off-by: Konrad Breitsprecher <Konrad.Breitsprecher@vector.com>
1 parent 13af3cc commit 2d373b5

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

SilKit/IntegrationTests/ITest_DynStepSizes.cpp

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,85 @@
88
#include <chrono>
99
#include <iostream>
1010
#include <sstream>
11+
#include "ITestFixture.hpp"
1112

1213
using namespace std::chrono_literals;
1314

15+
namespace testing {
16+
namespace internal {
1417
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>>
1619
{
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+
1927
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>>>
2129
{
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
2844

2945
inline std::string ToString(const std::chrono::nanoseconds& ns)
3046
{
3147
std::ostringstream os;
32-
PrintTo(ns, &os);
48+
testing::internal::UniversalPrinter<std::chrono::nanoseconds>::Print(ns, &os);
3349
return os.str();
3450
}
3551
inline std::string ToString(const std::pair<std::chrono::nanoseconds, std::chrono::nanoseconds>& p)
3652
{
3753
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);
4355
return os.str();
4456
}
4557

46-
#include "ITestFixture.hpp"
47-
4858
namespace {
4959

5060
using namespace SilKit::Tests;
5161
using namespace SilKit::Config;
5262
using namespace SilKit::Services;
5363
using namespace SilKit::Services::Orchestration;
5464

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+
5590
struct ParticipantParams
5691
{
5792
std::string name{};
@@ -171,7 +206,7 @@ void ITest_DynStepSizes::AssertStepsEqual(const std::vector<std::chrono::nanosec
171206

172207
for (size_t j = 0; j < s1.size(); ++j)
173208
{
174-
EXPECT_EQ(s1[j], s2[j]) << "Differenz at index " << j << ": "
209+
SILKIT_EXPECT_CHRONO_EQ(s1[j], s2[j]) << "Differenz at index " << j << ": "
175210
<< "s1 now=" << ToString(s1[j]) << " vs s2 now=" << ToString(s2[j]);
176211
}
177212
}

0 commit comments

Comments
 (0)