@@ -191,6 +191,47 @@ TEST_F(TestTimeFixture, test_rcutils_steady_time_now) {
191191 llabs (steady_diff - sc_diff), RCUTILS_MS_TO_NS (k_tolerance_ms)) << " steady_clock differs" ;
192192}
193193
194+ // Tests the rcutils_raw_steady_time_now() function.
195+ TEST_F (TestTimeFixture, test_rcutils_raw_steady_time_now) {
196+ rcutils_ret_t ret;
197+ // Check for invalid argument error condition (allowed to alloc).
198+ ret = rcutils_raw_steady_time_now (nullptr );
199+ EXPECT_EQ (ret, RCUTILS_RET_INVALID_ARGUMENT) << rcutils_get_error_string ().str ;
200+ rcutils_reset_error ();
201+ // Check for normal operation (not allowed to alloc).
202+ rcutils_time_point_value_t now = 0 ;
203+ EXPECT_NO_MEMORY_OPERATIONS (
204+ {
205+ ret = rcutils_raw_steady_time_now (&now);
206+ });
207+ EXPECT_EQ (ret, RCUTILS_RET_OK) << rcutils_get_error_string ().str ;
208+ EXPECT_NE (0u , now);
209+ // Compare to std::chrono::steady_clock difference of two times (within a second).
210+ now = 0 ;
211+ EXPECT_NO_MEMORY_OPERATIONS (
212+ {
213+ ret = rcutils_raw_steady_time_now (&now);
214+ });
215+ std::chrono::steady_clock::time_point now_sc = std::chrono::steady_clock::now ();
216+ EXPECT_EQ (ret, RCUTILS_RET_OK) << rcutils_get_error_string ().str ;
217+ // Wait for a little while.
218+ std::this_thread::sleep_for (std::chrono::milliseconds (100 ));
219+ // Then take a new timestamp with each and compare.
220+ rcutils_time_point_value_t later;
221+ EXPECT_NO_MEMORY_OPERATIONS (
222+ {
223+ ret = rcutils_raw_steady_time_now (&later);
224+ });
225+ std::chrono::steady_clock::time_point later_sc = std::chrono::steady_clock::now ();
226+ EXPECT_EQ (ret, RCUTILS_RET_OK) << rcutils_get_error_string ().str ;
227+ int64_t steady_diff = later - now;
228+ int64_t sc_diff =
229+ std::chrono::duration_cast<std::chrono::nanoseconds>(later_sc - now_sc).count ();
230+ const int k_tolerance_ms = 1 ;
231+ EXPECT_LE (
232+ llabs (steady_diff - sc_diff), RCUTILS_MS_TO_NS (k_tolerance_ms)) << " steady_clock differs" ;
233+ }
234+
194235#if !defined(_WIN32)
195236
196237TEST_F (TestTimeFixture, test_rcutils_with_bad_system_clocks) {
@@ -211,6 +252,10 @@ TEST_F(TestTimeFixture, test_rcutils_with_bad_system_clocks) {
211252 ret = rcutils_steady_time_now (&now);
212253 EXPECT_EQ (RCUTILS_RET_ERROR, ret);
213254 rcutils_reset_error ();
255+
256+ ret = rcutils_raw_steady_time_now (&now);
257+ EXPECT_EQ (RCUTILS_RET_ERROR, ret);
258+ rcutils_reset_error ();
214259 }
215260 {
216261 auto mock = mocking_utils::patch (
@@ -229,6 +274,10 @@ TEST_F(TestTimeFixture, test_rcutils_with_bad_system_clocks) {
229274 ret = rcutils_steady_time_now (&now);
230275 EXPECT_EQ (RCUTILS_RET_ERROR, ret);
231276 rcutils_reset_error ();
277+
278+ ret = rcutils_raw_steady_time_now (&now);
279+ EXPECT_EQ (RCUTILS_RET_ERROR, ret);
280+ rcutils_reset_error ();
232281 }
233282}
234283
0 commit comments