13
13
#include <zephyr/sys/util.h>
14
14
15
15
BUILD_ASSERT (sizeof (time_t ) == sizeof (int64_t ), "time_t must be 64-bit" );
16
- BUILD_ASSERT (sizeof (((struct timespec * )0 )-> tv_sec ) == sizeof (int64_t ), "tv_sec must be 64-bit" );
17
-
18
- /* need NSEC_PER_SEC to be signed for the purposes of this testsuite */
19
- #undef NSEC_PER_SEC
20
- #define NSEC_PER_SEC 1000000000L
21
16
22
17
#undef CORRECTABLE
23
18
#define CORRECTABLE true
24
19
25
20
#undef UNCORRECTABLE
26
21
#define UNCORRECTABLE false
27
22
23
+ /* Initialize a struct timespec object from a tick count with additional nanoseconds */
24
+ #define K_TICKS_TO_TIMESPEC_PLUS_NSECS (ticks , ns ) \
25
+ K_TIMESPEC(K_TICKS_TO_SECS(ticks) + \
26
+ (K_TICKS_TO_NSECS(ticks) + (uint64_t)(ns)) / NSEC_PER_SEC, \
27
+ (K_TICKS_TO_NSECS(ticks) + (uint64_t)(ns)) % NSEC_PER_SEC)
28
+
28
29
/*
29
30
* test spec for simple timespec validation
30
31
*
@@ -104,12 +105,12 @@ static const struct ts_test_spec ts_tests[] = {
104
105
CORRECTABLE ),
105
106
106
107
/* Uncorrectable, invalid cases */
107
- DECL_INVALID_TS_TEST (INT64_MIN + 2 , -2 * NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
108
- DECL_INVALID_TS_TEST (INT64_MIN + 1 , - NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
109
- DECL_INVALID_TS_TEST (INT64_MIN + 1 , - NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
108
+ DECL_INVALID_TS_TEST (INT64_MIN + 2 , -2 * ( int64_t ) NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
109
+ DECL_INVALID_TS_TEST (INT64_MIN + 1 , - ( int64_t ) NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
110
+ DECL_INVALID_TS_TEST (INT64_MIN + 1 , - ( int64_t ) NSEC_PER_SEC - 1 , 0 , 0 , UNCORRECTABLE ),
110
111
DECL_INVALID_TS_TEST (INT64_MIN , -1 , 0 , 0 , UNCORRECTABLE ),
111
- DECL_INVALID_TS_TEST (INT64_MAX , NSEC_PER_SEC , 0 , 0 , UNCORRECTABLE ),
112
- DECL_INVALID_TS_TEST (INT64_MAX - 1 , 2 * NSEC_PER_SEC , 0 , 0 , UNCORRECTABLE ),
112
+ DECL_INVALID_TS_TEST (INT64_MAX , ( int64_t ) NSEC_PER_SEC , 0 , 0 , UNCORRECTABLE ),
113
+ DECL_INVALID_TS_TEST (INT64_MAX - 1 , 2 * ( int64_t ) NSEC_PER_SEC , 0 , 0 , UNCORRECTABLE ),
113
114
};
114
115
115
116
ZTEST (timeutil_api , test_timespec_is_valid )
@@ -133,17 +134,23 @@ ZTEST(timeutil_api, test_timespec_normalize)
133
134
const struct ts_test_spec * const tspec = & ts_tests [i ];
134
135
struct timespec norm = tspec -> invalid_ts ;
135
136
137
+ TC_PRINT ("%zu: timespec_normalize({%lld, %lld})\n" , i ,
138
+ (long long )tspec -> invalid_ts .tv_sec , (long long )tspec -> invalid_ts .tv_nsec );
139
+
136
140
overflow = !timespec_normalize (& norm );
137
141
zexpect_not_equal (tspec -> expect_valid || tspec -> correctable , overflow ,
138
- "%d: timespec_normalize({%ld, %ld}) %s, unexpectedly" , i ,
139
- (long )tspec -> invalid_ts .tv_sec , (long )tspec -> invalid_ts .tv_nsec ,
142
+ "%d: timespec_normalize({%lld, %lld}) %s, unexpectedly" , i ,
143
+ (long long )tspec -> invalid_ts .tv_sec ,
144
+ (long long )tspec -> invalid_ts .tv_nsec ,
140
145
tspec -> correctable ? "failed" : "succeeded" );
141
146
142
147
if (!tspec -> expect_valid && tspec -> correctable ) {
143
148
different = !timespec_equal (& tspec -> invalid_ts , & norm );
144
- zexpect_true (different , "%d: {%ld, %ld} and {%ld, %ld} are unexpectedly %s" ,
145
- i , tspec -> invalid_ts .tv_sec , tspec -> invalid_ts .tv_nsec ,
146
- norm .tv_sec , tspec -> valid_ts .tv_sec ,
149
+ zexpect_true (different ,
150
+ "%d: {%lld, %lld} and {%lld, %lld} are unexpectedly %s" , i ,
151
+ (long long )tspec -> invalid_ts .tv_sec ,
152
+ (long long )tspec -> invalid_ts .tv_nsec , (long long )norm .tv_sec ,
153
+ (long long )tspec -> valid_ts .tv_sec ,
147
154
(tspec -> expect_valid || tspec -> correctable ) ? "different"
148
155
: "equal" );
149
156
}
@@ -268,43 +275,147 @@ ZTEST(timeutil_api, test_timespec_equal)
268
275
zexpect_false (timespec_equal (& a , & b ));
269
276
}
270
277
271
- #define NS_PER_TICK (NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
278
+ ZTEST (timeutil_api , test_K_TICKS_TO_SECS )
279
+ {
280
+ zexpect_equal (K_TICKS_TO_SECS (0 ), 0 );
281
+ zexpect_equal (K_TICKS_TO_SECS (CONFIG_SYS_CLOCK_TICKS_PER_SEC ), 1 );
282
+ zexpect_equal (K_TICKS_TO_SECS (2 * CONFIG_SYS_CLOCK_TICKS_PER_SEC ), 2 );
283
+ zexpect_equal (K_TICKS_TO_SECS (K_TICK_MAX ), K_TIMESPEC_MAX .tv_sec );
284
+ zexpect_equal (K_TICKS_TO_SECS (K_TICKS_FOREVER ), INT64_MAX );
285
+
286
+ #if defined(CONFIG_TIMEOUT_64BIT ) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100 )
287
+ zexpect_equal (K_TIMESPEC_MAX .tv_sec , 92233720368547758LL );
288
+ #endif
289
+
290
+ #if (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 32768 )
291
+ #if defined(CONFIG_TIMEOUT_64BIT )
292
+ zexpect_equal (K_TIMESPEC_MAX .tv_sec , 281474976710655LL );
293
+ #else
294
+ zexpect_equal (K_TIMESPEC_MAX .tv_sec , 131071 );
295
+ #endif
296
+ #endif
297
+ }
272
298
273
- /* 0 := lower limit, 2 := upper limit */
274
- static const struct timespec k_timeout_limits [] = {
275
- /* K_NO_WAIT + 1 tick */
276
- {
277
- .tv_sec = 0 ,
278
- .tv_nsec = NS_PER_TICK ,
279
- },
280
- /* K_FOREVER - 1 tick */
281
- {
282
- .tv_sec = (time_t )CLAMP ((NS_PER_TICK * (uint64_t )K_TICK_MAX ) / NSEC_PER_SEC , 0 ,
283
- INT64_MAX ),
284
- .tv_nsec = (long )CLAMP ((NS_PER_TICK * (uint64_t )K_TICK_MAX ) % NSEC_PER_SEC , 0 ,
285
- NSEC_PER_SEC - 1 ),
286
- },
287
- };
299
+ ZTEST (timeutil_api , test_K_TICKS_TO_NSECS )
300
+ {
301
+ zexpect_equal (K_TICKS_TO_NSECS (0 ), 0 );
302
+ zexpect_equal (K_TICKS_TO_NSECS (1 ) % NSEC_PER_SEC ,
303
+ (NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC ) % NSEC_PER_SEC );
304
+ zexpect_equal (K_TICKS_TO_NSECS (2 ) % NSEC_PER_SEC ,
305
+ (2 * NSEC_PER_SEC / CONFIG_SYS_CLOCK_TICKS_PER_SEC ) % NSEC_PER_SEC );
306
+ zexpect_equal (K_TICKS_TO_NSECS (K_TICK_MAX ), K_TIMESPEC_MAX .tv_nsec );
307
+ zexpect_equal (K_TICKS_TO_NSECS (K_TICKS_FOREVER ), NSEC_PER_SEC - 1 );
308
+
309
+ #if defined(CONFIG_TIMEOUT_64BIT ) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100 )
310
+ zexpect_equal (K_TIMESPEC_MAX .tv_nsec , 70000000L );
311
+ #endif
312
+
313
+ #if (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 32768 )
314
+ #if defined(CONFIG_TIMEOUT_64BIT )
315
+ zexpect_equal (K_TIMESPEC_MAX .tv_nsec , 999969482L );
316
+ #else
317
+ zexpect_equal (K_TIMESPEC_MAX .tv_nsec , 999938964L );
318
+ #endif
319
+ #endif
320
+ }
321
+
322
+ /* non-saturating */
323
+ #define DECL_TOSPEC_TEST (to , ts , sat , neg , round ) \
324
+ { \
325
+ .timeout = (to), \
326
+ .tspec = (ts), \
327
+ .saturation = (sat), \
328
+ .negative = (neg), \
329
+ .roundup = (round), \
330
+ }
331
+ /* negative timespecs rounded up to K_NO_WAIT */
332
+ #define DECL_TOSPEC_NEGATIVE_TEST (ts ) DECL_TOSPEC_TEST(K_NO_WAIT, (ts), 0, true, false)
333
+ /* zero-valued timeout */
334
+ #define DECL_TOSPEC_ZERO_TEST (to ) DECL_TOSPEC_TEST((to), K_TIMESPEC(0, 0), 0, false, false)
335
+ /* round up toward K_TICK_MIN */
336
+ #define DECL_NSAT_TOSPEC_TEST (ts ) DECL_TOSPEC_TEST(K_TICKS(K_TICK_MIN), (ts), -1, false, false)
337
+ /* round up toward next tick boundary */
338
+ #define DECL_ROUND_TOSPEC_TEST (to , ts ) DECL_TOSPEC_TEST((to), (ts), 0, false, true)
339
+ /* round down toward K_TICK_MAX */
340
+ #define DECL_PSAT_TOSPEC_TEST (ts ) DECL_TOSPEC_TEST(K_TICKS(K_TICK_MAX), (ts), 1, false, false)
288
341
289
342
static const struct tospec {
290
343
k_timeout_t timeout ;
291
344
struct timespec tspec ;
292
345
int saturation ;
346
+ bool negative ;
347
+ bool roundup ;
293
348
} tospecs [] = {
294
- {K_NO_WAIT , {INT64_MIN , 0 }, -1 },
295
- {K_NO_WAIT , {-1 , 0 }, -1 },
296
- {K_NO_WAIT , {-1 , NSEC_PER_SEC - 1 }, -1 },
297
- {K_NO_WAIT , {0 , 0 }, 0 },
298
- {K_NSEC (0 ), {0 , 0 }, 0 },
299
- {K_NSEC (2000000000 ), {2 , 0 }, 0 },
300
- {K_USEC (0 ), {0 , 0 }, 0 },
301
- {K_USEC (2000000 ), {2 , 0 }, 0 },
302
- {K_MSEC (100 ), {0 , 100000000 }, 0 },
303
- {K_MSEC (2000 ), {2 , 0 }, 0 },
304
- {K_SECONDS (0 ), {0 , 0 }, 0 },
305
- {K_SECONDS (1 ), {1 , 0 }, 0 },
306
- {K_SECONDS (100 ), {100 , 0 }, 0 },
307
- {K_FOREVER , {INT64_MAX , NSEC_PER_SEC - 1 }, 0 },
349
+ /* negative timespecs should round-up to K_NO_WAIT */
350
+ DECL_TOSPEC_NEGATIVE_TEST (K_TIMESPEC (INT64_MIN , 0 )),
351
+ DECL_TOSPEC_NEGATIVE_TEST (K_TIMESPEC (-1 , 0 )),
352
+ DECL_TOSPEC_NEGATIVE_TEST (K_TIMESPEC (-1 , NSEC_PER_SEC - 1 )),
353
+
354
+ /* zero-valued timeouts are equivalent to K_NO_WAIT */
355
+ DECL_TOSPEC_ZERO_TEST (K_NSEC (0 )),
356
+ DECL_TOSPEC_ZERO_TEST (K_USEC (0 )),
357
+ DECL_TOSPEC_ZERO_TEST (K_MSEC (0 )),
358
+ DECL_TOSPEC_ZERO_TEST (K_SECONDS (0 )),
359
+
360
+ /* round up to K_TICK_MIN */
361
+ DECL_NSAT_TOSPEC_TEST (K_TIMESPEC (0 , 1 )),
362
+ DECL_NSAT_TOSPEC_TEST (K_TIMESPEC (0 , 2 )),
363
+ #if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
364
+ DECL_NSAT_TOSPEC_TEST (K_TIMESPEC (0 , K_TICKS_TO_NSECS (K_TICK_MIN ))),
365
+ #endif
366
+
367
+ #if CONFIG_SYS_CLOCK_TICKS_PER_SEC < MHZ (1 )
368
+ DECL_NSAT_TOSPEC_TEST (K_TIMESPEC (0 , NSEC_PER_USEC )),
369
+ #endif
370
+ #if CONFIG_SYS_CLOCK_TICKS_PER_SEC < KHZ (1 )
371
+ DECL_NSAT_TOSPEC_TEST (K_TIMESPEC (0 , NSEC_PER_MSEC )),
372
+ #endif
373
+
374
+ /* round to next tick boundary (low-end) */
375
+ #if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
376
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (2 ), K_TICKS_TO_TIMESPEC_PLUS_NSECS (1 , 1 )),
377
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (2 ),
378
+ K_TICKS_TO_TIMESPEC_PLUS_NSECS (1 , K_TICKS_TO_NSECS (1 ) / 2 )),
379
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (2 ),
380
+ K_TICKS_TO_TIMESPEC_PLUS_NSECS (1 , K_TICKS_TO_NSECS (1 ) - 1 )),
381
+ #endif
382
+
383
+ /* exact conversions for large timeouts */
384
+ #ifdef CONFIG_TIMEOUT_64BIT
385
+ DECL_TOSPEC_TEST (K_NSEC (2000000000 ), K_TIMESPEC (2 , 0 ), 0 , false, false),
386
+ #endif
387
+ DECL_TOSPEC_TEST (K_USEC (2000000 ), K_TIMESPEC (2 , 0 ), 0 , false, false),
388
+ DECL_TOSPEC_TEST (K_MSEC (2000 ), K_TIMESPEC (2 , 0 ), 0 , false, false),
389
+
390
+ DECL_TOSPEC_TEST (K_SECONDS (1 ),
391
+ K_TIMESPEC (1 , K_TICKS_TO_NSECS (CONFIG_SYS_CLOCK_TICKS_PER_SEC )), 0 , false,
392
+ false),
393
+ DECL_TOSPEC_TEST (K_SECONDS (2 ),
394
+ K_TIMESPEC (2 , K_TICKS_TO_NSECS (2 * CONFIG_SYS_CLOCK_TICKS_PER_SEC )), 0 ,
395
+ false, false),
396
+ DECL_TOSPEC_TEST (K_SECONDS (100 ),
397
+ K_TIMESPEC (100 , K_TICKS_TO_NSECS (100 * CONFIG_SYS_CLOCK_TICKS_PER_SEC )), 0 ,
398
+ false, false),
399
+
400
+ DECL_TOSPEC_TEST (K_TICKS (1000 ), K_TICKS_TO_TIMESPEC (1000 ), 0 , false, false),
401
+
402
+ /* round to next tick boundary (high-end) */
403
+ #if CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1
404
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (1000 ), K_TICKS_TO_TIMESPEC_PLUS_NSECS (999 , 1 )),
405
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (1000 ),
406
+ K_TICKS_TO_TIMESPEC_PLUS_NSECS (999 , K_TICKS_TO_NSECS (1 ) / 2 )),
407
+ DECL_ROUND_TOSPEC_TEST (K_TICKS (1000 ),
408
+ K_TICKS_TO_TIMESPEC_PLUS_NSECS (999 , K_TICKS_TO_NSECS (1 ) - 1 )),
409
+ #endif
410
+
411
+ /* round down toward K_TICK_MAX */
412
+ DECL_PSAT_TOSPEC_TEST (K_TICKS_TO_TIMESPEC (K_TICK_MAX )),
413
+ #if defined(CONFIG_TIMEOUT_64BIT ) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC > 1 )
414
+ DECL_PSAT_TOSPEC_TEST (K_TICKS_TO_TIMESPEC ((uint64_t )K_TICK_MAX + 1 )),
415
+ #endif
416
+
417
+ /* K_FOREVER <=> K_TIMESPEC_FOREVER */
418
+ DECL_TOSPEC_TEST (K_FOREVER , K_TIMESPEC (INT64_MAX , NSEC_PER_SEC - 1 ), 0 , false, false),
308
419
};
309
420
310
421
ZTEST (timeutil_api , test_timespec_from_timeout )
@@ -315,11 +426,20 @@ ZTEST(timeutil_api, test_timespec_from_timeout)
315
426
const struct tospec * const tspec = & tospecs [i ];
316
427
struct timespec actual ;
317
428
318
- if (tspec -> saturation != 0 ) {
319
- /* saturation cases are only checked in test_timespec_to_timeout */
429
+ /*
430
+ * In this test we only check exact conversions, so skip negative timespecs that
431
+ * saturate up to K_NO_WAIT and skip values under K_TIMESPEC_MIN and over
432
+ * K_TIMESPEC_MAX. Also, skip "normal" conversions that just round up to the next
433
+ * tick boundary.
434
+ */
435
+ if (tspec -> negative || (tspec -> saturation != 0 ) || tspec -> roundup ) {
320
436
continue ;
321
437
}
322
438
439
+ TC_PRINT ("%zu: ticks: {%lld}, timespec: {%lld, %lld}\n" , i ,
440
+ (long long )tspec -> timeout .ticks , (long long )tspec -> tspec .tv_sec ,
441
+ (long long )tspec -> tspec .tv_nsec );
442
+
323
443
timespec_from_timeout (tspec -> timeout , & actual );
324
444
zexpect_true (timespec_equal (& actual , & tspec -> tspec ),
325
445
"%d: {%ld, %ld} and {%ld, %ld} are unexpectedly different" , i ,
@@ -336,48 +456,92 @@ ZTEST(timeutil_api, test_timespec_to_timeout)
336
456
const struct tospec * const tspec = & tospecs [i ];
337
457
k_timeout_t actual ;
338
458
459
+ TC_PRINT ("%zu: ticks: {%lld}, timespec: {%lld, %lld}\n" , i ,
460
+ (long long )tspec -> timeout .ticks , (long long )tspec -> tspec .tv_sec ,
461
+ (long long )tspec -> tspec .tv_nsec );
462
+
463
+ actual = timespec_to_timeout (& tspec -> tspec );
339
464
if (tspec -> saturation == 0 ) {
340
- /* no saturation / exact match */
341
- actual = timespec_to_timeout (& tspec -> tspec );
465
+ /* exact match or rounding up */
466
+ if (!tspec -> negative &&
467
+ (timespec_compare (& tspec -> tspec , & K_TIMESPEC_NO_WAIT ) != 0 ) &&
468
+ (timespec_compare (& tspec -> tspec , & K_TIMESPEC_FOREVER ) != 0 )) {
469
+ __ASSERT (timespec_compare (& tspec -> tspec , & K_TIMESPEC_MIN ) >= 0 ,
470
+ "%zu: timespec: {%lld, %lld} is not greater than "
471
+ "K_TIMESPEC_MIN" ,
472
+ i , (long long )tspec -> tspec .tv_sec ,
473
+ (long long )tspec -> tspec .tv_nsec );
474
+ __ASSERT (timespec_compare (& tspec -> tspec , & K_TIMESPEC_MAX ) <= 0 ,
475
+ "%zu: timespec: {%lld, %lld} is not less than "
476
+ "K_TIMESPEC_MAX" ,
477
+ i , (long long )tspec -> tspec .tv_sec ,
478
+ (long long )tspec -> tspec .tv_nsec );
479
+ }
342
480
zexpect_equal (actual .ticks , tspec -> timeout .ticks ,
343
481
"%d: {%" PRId64 "} and {%" PRId64
344
482
"} are unexpectedly different" ,
345
483
i , (int64_t )actual .ticks , (int64_t )tspec -> timeout .ticks );
346
- continue ;
347
- }
348
-
349
- if ((tspec -> saturation < 0 ) ||
350
- (timespec_compare (& tspec -> tspec , & k_timeout_limits [0 ]) < 0 )) {
351
- /* K_NO_WAIT saturation */
352
- actual = timespec_to_timeout (& tspec -> tspec );
353
- zexpect_equal (actual .ticks , K_NO_WAIT .ticks ,
484
+ } else if (tspec -> saturation < 0 ) {
485
+ /* K_TICK_MIN saturation */
486
+ __ASSERT (timespec_compare (& tspec -> tspec , & K_TIMESPEC_MIN ) <= 0 ,
487
+ "timespec: {%lld, %lld} is not less than or equal to "
488
+ "K_TIMESPEC_MIN "
489
+ "{%lld, %lld}" ,
490
+ (long long )tspec -> tspec .tv_sec , (long long )tspec -> tspec .tv_nsec ,
491
+ (long long )K_TIMESPEC_MIN .tv_sec ,
492
+ (long long )K_TIMESPEC_MIN .tv_nsec );
493
+ zexpect_equal (actual .ticks , K_TICK_MIN ,
354
494
"%d: {%" PRId64 "} and {%" PRId64
355
495
"} are unexpectedly different" ,
356
- i , (int64_t )actual .ticks , (int64_t )K_NO_WAIT .ticks );
357
- continue ;
358
- }
359
-
360
- if ((tspec -> saturation > 0 ) ||
361
- (timespec_compare (& tspec -> tspec , & k_timeout_limits [1 ]) > 0 )) {
362
- /* K_FOREVER saturation */
363
- actual = timespec_to_timeout (& tspec -> tspec );
364
- zexpect_equal (actual .ticks , K_TICKS_FOREVER ,
496
+ i , (int64_t )actual .ticks , (int64_t )K_TICK_MIN );
497
+ } else if (tspec -> saturation > 0 ) {
498
+ /* K_TICK_MAX saturation */
499
+ __ASSERT (timespec_compare (& tspec -> tspec , & K_TIMESPEC_MAX ) >= 0 ,
500
+ "timespec: {%lld, %lld} is not greater than or equal to "
501
+ "K_TIMESPEC_MAX "
502
+ "{%lld, %lld}" ,
503
+ (long long )tspec -> tspec .tv_sec , (long long )tspec -> tspec .tv_nsec ,
504
+ (long long )K_TIMESPEC_MAX .tv_sec ,
505
+ (long long )K_TIMESPEC_MAX .tv_nsec );
506
+ zexpect_equal (actual .ticks , K_TICK_MAX ,
365
507
"%d: {%" PRId64 "} and {%" PRId64
366
508
"} are unexpectedly different" ,
367
- i , (int64_t )actual .ticks , (int64_t )K_TICKS_FOREVER );
368
- continue ;
509
+ i , (int64_t )actual .ticks , (int64_t )K_TICK_MAX );
369
510
}
370
511
}
512
+
513
+ #if defined(CONFIG_TIMEOUT_64BIT ) && (CONFIG_SYS_CLOCK_TICKS_PER_SEC == 100 )
514
+ {
515
+ k_timeout_t to = K_TICKS (K_TICK_MAX );
516
+ /* K_TIMESPEC_MAX corresponding K_TICK_MAX with a tick rate of 100 Hz */
517
+ struct timespec ts = K_TIMESPEC (92233720368547758LL , 70000000L );
518
+
519
+ zexpect_true (K_TIMEOUT_EQ (timespec_to_timeout (& ts ), to ),
520
+ "timespec_to_timeout(%lld, %lld) != %lld" , (long long )ts .tv_sec ,
521
+ (long long )ts .tv_nsec , (long long )to .ticks );
522
+
523
+ TC_PRINT ("timespec_to_timeout():\nts: {%lld, %lld} => to: {%lld}\n" ,
524
+ (long long )ts .tv_sec , (long long )ts .tv_nsec , (long long )to .ticks );
525
+ }
526
+ #endif
371
527
}
372
528
373
529
static void * setup (void )
374
530
{
375
- printk ("CONFIG_TIMEOUT_64BIT=%c\n" , CONFIG_TIMEOUT_64BIT ? 'y' : 'n' );
376
- printk ("K_TICK_MAX: %lld\n" , (long long )K_TICK_MAX );
377
- printk ("minimum timeout: {%lld, %lld}\n" , (long long )k_timeout_limits [0 ].tv_sec ,
378
- (long long )k_timeout_limits [0 ].tv_nsec );
379
- printk ("maximum timeout: {%lld, %lld}\n" , (long long )k_timeout_limits [1 ].tv_sec ,
380
- (long long )k_timeout_limits [1 ].tv_nsec );
531
+ TC_PRINT ("CONFIG_SYS_CLOCK_TICKS_PER_SEC=%d\n" , CONFIG_SYS_CLOCK_TICKS_PER_SEC );
532
+ TC_PRINT ("CONFIG_TIMEOUT_64BIT=%c\n" , IS_ENABLED (CONFIG_TIMEOUT_64BIT ) ? 'y' : 'n' );
533
+ TC_PRINT ("K_TICK_MIN: %lld\n" , (long long )K_TICK_MIN );
534
+ TC_PRINT ("K_TICK_MAX: %lld\n" , (long long )K_TICK_MAX );
535
+ TC_PRINT ("K_TIMESPEC_MIN: {%lld, %lld}\n" , (long long )K_TIMESPEC_MIN .tv_sec ,
536
+ (long long )K_TIMESPEC_MIN .tv_nsec );
537
+ TC_PRINT ("K_TIMESPEC_MAX: {%lld, %lld}\n" , (long long )K_TIMESPEC_MAX .tv_sec ,
538
+ (long long )K_TIMESPEC_MAX .tv_nsec );
539
+ TC_PRINT ("INT64_MIN: %lld\n" , (long long )INT64_MIN );
540
+ TC_PRINT ("INT64_MAX: %lld\n" , (long long )INT64_MAX );
541
+ PRINT_LINE ;
542
+
543
+ /* check numerical values corresponding to K_TICK_MAX */
544
+ zassert_equal (K_TICK_MAX , IS_ENABLED (CONFIG_TIMEOUT_64BIT ) ? INT64_MAX : UINT32_MAX - 1 );
381
545
382
546
return NULL ;
383
547
}
0 commit comments