@@ -268,4 +268,82 @@ ZTEST(timeutil_api, test_timespec_equal)
268
268
zexpect_false (timespec_equal (& a , & b ));
269
269
}
270
270
271
+ static const struct tospec {
272
+ k_timeout_t timeout ;
273
+ struct timespec tspec ;
274
+ int saturation ;
275
+ } tospecs [] = {
276
+ {K_NO_WAIT , {INT64_MIN , 0 }, -1 },
277
+ {K_NO_WAIT , {-1 , 0 }, -1 },
278
+ {K_NO_WAIT , {-1 , NSEC_PER_SEC - 1 }, -1 },
279
+ {K_NO_WAIT , {0 , 0 }, 0 },
280
+ {K_NSEC (0 ), {0 , 0 }, 0 },
281
+ {K_NSEC (2000000000 ), {2 , 0 }, 0 },
282
+ {K_USEC (0 ), {0 , 0 }, 0 },
283
+ {K_USEC (2000000 ), {2 , 0 }, 0 },
284
+ {K_MSEC (100 ), {0 , 100000000 }, 0 },
285
+ {K_MSEC (2000 ), {2 , 0 }, 0 },
286
+ {K_SECONDS (0 ), {0 , 0 }, 0 },
287
+ {K_SECONDS (1 ), {1 , 0 }, 0 },
288
+ {K_SECONDS (100 ), {100 , 0 }, 0 },
289
+ {K_FOREVER , {INT64_MAX , NSEC_PER_SEC - 1 }, 0 },
290
+ };
291
+
292
+ ZTEST (timeutil_api , test_timespec_from_timeout )
293
+ {
294
+ ARRAY_FOR_EACH (tospecs , i ) {
295
+ const struct tospec * const tspec = & tospecs [i ];
296
+ struct timespec actual ;
297
+
298
+ if (tspec -> saturation != 0 ) {
299
+ /* saturation cases are only checked in test_timespec_to_timeout */
300
+ continue ;
301
+ }
302
+
303
+ timespec_from_timeout (tspec -> timeout , & actual );
304
+ zexpect_true (timespec_equal (& actual , & tspec -> tspec ),
305
+ "%d: {%ld, %ld} and {%ld, %ld} are unexpectedly different" , i ,
306
+ actual .tv_sec , actual .tv_nsec , tspec -> tspec .tv_sec ,
307
+ tspec -> tspec .tv_nsec );
308
+ }
309
+ }
310
+
311
+ ZTEST (timeutil_api , test_timespec_to_timeout )
312
+ {
313
+ ARRAY_FOR_EACH (tospecs , i ) {
314
+ const struct tospec * const tspec = & tospecs [i ];
315
+ k_timeout_t actual ;
316
+
317
+ if (tspec -> saturation == 0 ) {
318
+ /* no saturation / exact match */
319
+ actual = timespec_to_timeout (& tspec -> timeout );
320
+ zexpect_equal (actual .ticks , tspec -> timeout .ticks ,
321
+ "%d: {%" PRId64 "} and {%" PRId64
322
+ "} are unexpectedly different" ,
323
+ i , (int64_t )actual .ticks , (int64_t )tspec -> timeout .ticks );
324
+ continue ;
325
+ }
326
+
327
+ if (tspec -> saturation < 0 ) {
328
+ /* K_NO_WAIT saturation */
329
+ actual = timespec_to_timeout (& tspec -> tspec );
330
+ zexpect_equal (actual .ticks , K_NO_WAIT .ticks ,
331
+ "%d: {%" PRId64 "} and {%" PRId64
332
+ "} are unexpectedly different" ,
333
+ i , (int64_t )actual .ticks , (int64_t )K_NO_WAIT .ticks );
334
+ continue ;
335
+ }
336
+
337
+ if (tspec -> saturation > 0 ) {
338
+ /* K_FOREVER saturation */
339
+ actual = timespec_to_timeout (& tspec -> tspec );
340
+ zexpect_equal (actual .ticks , K_TICKS_FOREVER ,
341
+ "%d: {%" PRId64 "} and {%" PRId64
342
+ "} are unexpectedly different" ,
343
+ i , (int64_t )actual .ticks , (int64_t )K_TICKS_FOREVER );
344
+ continue ;
345
+ }
346
+ }
347
+ }
348
+
271
349
ZTEST_SUITE (timeutil_api , NULL , NULL , NULL , NULL , NULL );
0 commit comments