forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmt_test.cc
82 lines (63 loc) · 2.12 KB
/
fmt_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "drake/common/fmt.h"
#include <ostream>
#include <fmt/ranges.h>
#include <gtest/gtest.h>
#include "drake/common/drake_assert.h"
// This namespace contains example code (i.e., what a user would write) for
// formatting a class (or struct) using the "format as" helper.
namespace sample {
// A simple, non-templated struct.
struct Int {
int i{};
};
// A template with 1 type argument.
template <typename T>
struct Wrap {
T t{};
};
// A template with 2 type arguments.
template <typename T, typename U>
struct Pair {
T t{};
U u{};
};
} // namespace sample
// Tell fmt how to format the sample types.
DRAKE_FORMATTER_AS(, sample, Int, x, x.i)
DRAKE_FORMATTER_AS(typename T, sample, Wrap<T>, x, x.t)
DRAKE_FORMATTER_AS(typename... Ts, sample, Pair<Ts...>, x, std::pair(x.t, x.u))
namespace drake {
namespace {
// Spot check for the "format as" formatter.
GTEST_TEST(FmtTest, FormatAsFormatter) {
const sample::Int plain{1};
EXPECT_EQ(fmt::format("{}", plain), "1");
EXPECT_EQ(fmt::format("{:3}", plain), " 1");
EXPECT_EQ(fmt::format("{:<3}", plain), "1 ");
const sample::Wrap<double> real{1.1234567e6};
EXPECT_EQ(fmt::format("{}", real), "1123456.7");
EXPECT_EQ(fmt::format("{:.3G}", real), "1.12E+06");
// N.B. The fmt:formatter for std::pair comes from <fmt/ranges.h>.
const sample::Pair<int, int> pear{1, 2};
EXPECT_EQ(fmt::format("{}", pear), "(1, 2)");
}
// The googletest infrastructure uses fmt's formatters.
GTEST_TEST(FmtTest, TestPrinter) {
const sample::Int plain{1};
EXPECT_EQ(testing::PrintToString(plain), "1");
const sample::Wrap<double> real{1.1};
EXPECT_EQ(testing::PrintToString(real), "1.1");
const sample::Pair<int, int> pear{1, 2};
EXPECT_EQ(testing::PrintToString(pear), "(1, 2)");
}
// Spot check for the floating point formatter.
GTEST_TEST(FmtTest, FloatingPoint) {
EXPECT_EQ(fmt_floating_point(1.11), "1.11");
EXPECT_EQ(fmt_floating_point(1.1), "1.1");
EXPECT_EQ(fmt_floating_point(1.0), "1.0");
EXPECT_EQ(fmt_floating_point(1.11f), "1.11");
EXPECT_EQ(fmt_floating_point(1.1f), "1.1");
EXPECT_EQ(fmt_floating_point(1.0f), "1.0");
}
} // namespace
} // namespace drake