@@ -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
196237// For mocking purposes
@@ -221,6 +262,10 @@ TEST_F(TestTimeFixture, test_rcutils_with_bad_system_clocks) {
221262 ret = rcutils_steady_time_now (&now);
222263 EXPECT_EQ (RCUTILS_RET_ERROR, ret);
223264 rcutils_reset_error ();
265+
266+ ret = rcutils_raw_steady_time_now (&now);
267+ EXPECT_EQ (RCUTILS_RET_ERROR, ret);
268+ rcutils_reset_error ();
224269 }
225270#endif
226271 {
@@ -240,6 +285,10 @@ TEST_F(TestTimeFixture, test_rcutils_with_bad_system_clocks) {
240285 ret = rcutils_steady_time_now (&now);
241286 EXPECT_EQ (RCUTILS_RET_ERROR, ret);
242287 rcutils_reset_error ();
288+
289+ ret = rcutils_raw_steady_time_now (&now);
290+ EXPECT_EQ (RCUTILS_RET_ERROR, ret);
291+ rcutils_reset_error ();
243292 }
244293}
245294
0 commit comments