Skip to content

Commit ab897e4

Browse files
committed
Add test case for time conversion macros
Signed-off-by: Johannes Meyer <[email protected]>
1 parent 45d794d commit ab897e4

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

test/test_time.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,74 @@ class TestTimeFixture : public ::testing::Test
5050
}
5151
};
5252

53+
// Tests the rcutils time unit conversion macros.
54+
TEST_F(TestTimeFixture, test_rcutils_time_conversion_macros) {
55+
int64_t ns = 0;
56+
57+
// Note: 9007199254740993 or higher cannot be represented anymore by intermediate type double
58+
// without a loss of precision.
59+
60+
// seconds to nanoseconds
61+
EXPECT_EQ(ns = RCUTILS_S_TO_NS(1), 1000000000ll); // int
62+
EXPECT_EQ(ns = RCUTILS_S_TO_NS(0.2), 200000000ll); // double
63+
EXPECT_EQ(ns = RCUTILS_S_TO_NS(1 + 1), 2000000000ll); // sum of ints
64+
EXPECT_EQ(
65+
ns = RCUTILS_S_TO_NS(9007199.254740992),
66+
9007199254740992ll); // maximum precision double (53 bits)
67+
EXPECT_NE(ns = RCUTILS_S_TO_NS(9007199.254740993), 9007199254740993ll);
68+
EXPECT_EQ(ns = RCUTILS_S_TO_NS(9007199.254740993), 9007199254740992ll); // value is truncated!
69+
70+
// milliseconds to nanoseconds
71+
EXPECT_EQ(ns = RCUTILS_MS_TO_NS(1), 1000000ll); // int
72+
EXPECT_EQ(ns = RCUTILS_MS_TO_NS(0.2), 200000ll); // double
73+
EXPECT_EQ(ns = RCUTILS_MS_TO_NS(1 + 1), 2000000ll); // sum of ints
74+
EXPECT_EQ(
75+
ns = RCUTILS_MS_TO_NS(9007199254.740992),
76+
9007199254740992ll); // maximum precision double (53 bits)
77+
EXPECT_NE(ns = RCUTILS_MS_TO_NS(9007199254.740993), 9007199254740993ll);
78+
EXPECT_EQ(ns = RCUTILS_MS_TO_NS(9007199254.740993), 9007199254740994ll); // value is truncated!
79+
80+
// microseconds to nanoseconds
81+
EXPECT_EQ(ns = RCUTILS_US_TO_NS(1), 1000ll); // int
82+
EXPECT_EQ(ns = RCUTILS_US_TO_NS(0.2), 200ll); // double
83+
EXPECT_EQ(ns = RCUTILS_US_TO_NS(1 + 1), 2000ll); // sum of ints
84+
EXPECT_EQ(
85+
ns = RCUTILS_US_TO_NS(9007199254740.992),
86+
9007199254740992ll); // maximum precision double (53 bits)
87+
EXPECT_NE(ns = RCUTILS_US_TO_NS(9007199254740.993), 9007199254740993ll);
88+
EXPECT_EQ(ns = RCUTILS_US_TO_NS(9007199254740.993), 9007199254740992ll); // value is truncated!
89+
90+
// nanoseconds to seconds
91+
EXPECT_EQ(RCUTILS_NS_TO_S(1000000000ll), 1ll); // int64_t
92+
EXPECT_EQ(RCUTILS_NS_TO_S(1000000042ll), 1ll); // int64_t (truncated)
93+
EXPECT_EQ(RCUTILS_NS_TO_S(-1999999999ll), -1ll); // int64_t (truncated)
94+
EXPECT_EQ(RCUTILS_NS_TO_S(200000000.), 0.2); // double
95+
EXPECT_EQ(RCUTILS_NS_TO_S(1.0 + 1.0), 0.000000002); // sum of doubles
96+
EXPECT_EQ(
97+
RCUTILS_NS_TO_S(9007199254740992.),
98+
9007199.254740992); // maximum precision double (53 bits)
99+
100+
// nanoseconds to milliseconds
101+
EXPECT_EQ(RCUTILS_NS_TO_MS(1000000ll), 1ll); // int64_t
102+
EXPECT_EQ(RCUTILS_NS_TO_MS(1000042ll), 1ll); // int64_t (truncated)
103+
EXPECT_EQ(RCUTILS_NS_TO_MS(-1999999ll), -1ll); // int64_t (truncated)
104+
EXPECT_EQ(RCUTILS_NS_TO_MS(200000.), 0.2); // double
105+
EXPECT_EQ(RCUTILS_NS_TO_MS(1.0 + 1.0), 0.000002); // sum of doubles
106+
EXPECT_EQ(
107+
RCUTILS_NS_TO_MS(9007199254740992.),
108+
9007199254.740992); // maximum precision double (53 bits)
109+
110+
// nanoseconds to microseconds
111+
EXPECT_EQ(RCUTILS_NS_TO_US(1000ll), 1ll); // int64_t
112+
EXPECT_EQ(RCUTILS_NS_TO_US(1042ll), 1ll); // int64_t (truncated)
113+
EXPECT_EQ(RCUTILS_NS_TO_US(-1999ll), -1ll); // int64_t (truncated)
114+
EXPECT_EQ(RCUTILS_NS_TO_US(200.), 0.2); // double
115+
EXPECT_EQ(RCUTILS_NS_TO_US(1.0 + 1.0), 0.002); // sum of doubles
116+
EXPECT_EQ(
117+
RCUTILS_NS_TO_US(9007199254740992.),
118+
9007199254740.992); // maximum precision double (53 bits)
119+
}
120+
53121
// Tests the rcutils_system_time_now() function.
54122
TEST_F(TestTimeFixture, test_rcutils_system_time_now) {
55123
rcutils_ret_t ret;

0 commit comments

Comments
 (0)