Unit Test Framework
C and Traditional C++ API Reference
test_time.h
1
/*
2
* (c) Copyright, Real-Time Innovations, 2023.
3
* All rights reserved.
4
* No duplications, whole or partial, manual or electronic, may be made
5
* without express written permission. Any such copies, or
6
* revisions thereof, must display this notice unaltered.
7
* This code contains trade secrets of Real-Time Innovations, Inc.
8
*/
9
10
#ifndef rtitest_time_h
11
#define rtitest_time_h
12
13
#include "rtitest/test_types.h"
14
#include "rtitest/test_dll.h"
15
16
#ifdef __cplusplus
17
extern
"C"
{
18
#endif
19
20
typedef
struct
RTITestTime {
21
/* Seconds. */
22
unsigned
int
sec;
23
/* Fraction of a second in 1/2^32 form. */
24
unsigned
int
frac;
25
} RTITestTime;
26
27
extern
RTITestDllExport RTIBool RTITest_sleep(
const
RTITestTime *timeIn);
28
29
#ifdef RTI_RUNNING_VALGRIND
30
#define RTI_TEST_DEFAULT_WAIT_TIME 20
31
#define RTI_TEST_DEFAULT_LONG_WAIT_TIME 200
32
#else
33
#define RTI_TEST_DEFAULT_WAIT_TIME 10
34
#define RTI_TEST_DEFAULT_LONG_WAIT_TIME 120
35
#endif
36
41
#define RTITest_wait(maxSeconds__) \
42
{ \
43
const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
44
int sleeps__ = 0; \
45
while (sleeps__ < ((int) (maxSeconds__)) * 10) { \
46
RTITest_sleep(&sleepTime__); \
47
++sleeps__; \
48
} \
49
}
50
59
#define RTITest_waitForConditionNoFailureEx( \
60
cond__, \
61
maxSeconds__, \
62
additionalSleepTimeSec__) \
63
{ \
64
/* 100 ms */
\
65
struct RTITestTime sleepTime__ = { 0, 429496730 }; \
66
unsigned int sleeps__ = 0; \
67
unsigned int maxSleeps__ = ((unsigned int) (maxSeconds__) * 10) \
68
/ (((unsigned int) (additionalSleepTimeSec__) * 10) + 1); \
69
\
70
sleepTime__.sec = (additionalSleepTimeSec__); \
71
while (!(cond__) && sleeps__ < maxSleeps__) { \
72
RTITest_sleep(&sleepTime__); \
73
++sleeps__; \
74
} \
75
}
76
86
#define RTITest_waitForConditionWithActionEx( \
87
cond__, \
88
maxSeconds__, \
89
additionalSleepTimeSec__, \
90
action__) \
91
{ \
92
/* 100 ms */
\
93
struct RTITestTime sleepTime__ = { 0, 429496730 }; \
94
unsigned int sleeps__ = 0; \
95
int condEv__; \
96
unsigned int maxSleeps__ = ((unsigned int) (maxSeconds__) * 10) \
97
/ (((unsigned int) (additionalSleepTimeSec__) * 10) + 1); \
98
\
99
sleepTime__.sec = (additionalSleepTimeSec__); \
100
while (!(condEv__ = (cond__)) && sleeps__ < maxSleeps__) { \
101
RTITest_sleep(&sleepTime__); \
102
++sleeps__; \
103
} \
104
if (!condEv__) { \
105
RTITest_assertLog( \
106
&RTI_TEST_LOG_EMPTY_TEMPLATE, \
107
"[%s:%d] wait for condition failed: %s"
, \
108
__FILE__, \
109
__LINE__, \
110
#cond__); \
111
action__; \
112
} \
113
}
114
121
#define RTITest_waitForConditionNoFailure(cond__, maxSeconds__) \
122
RTITest_waitForConditionNoFailureEx( \
123
cond__, \
124
maxSeconds__, \
125
0
/* additionalSleepTimeSec__ */
)
126
135
#define RTITest_waitForConditionWithAction(cond__, maxSeconds__, action__) \
136
RTITest_waitForConditionWithActionEx( \
137
cond__, \
138
maxSeconds__, \
139
0,
/* additionalSleepTimeSec__ */
\
140
action__)
141
152
#define RTITest_waitForConditionEx( \
153
cond__, \
154
maxSeconds__, \
155
additionalSleepTimeSec__) \
156
RTITest_waitForConditionWithActionEx( \
157
cond__, \
158
maxSeconds__, \
159
additionalSleepTimeSec__, \
160
goto done)
161
162
#define RTI_TEST_TIME_ZERO {0, 0}
163
173
#define RTITest_waitForCondition(cond__, maxSeconds__) \
174
RTITest_waitForConditionEx(cond__, maxSeconds__, 0)
175
185
#define RTITest_waitForOk(retcode__, maxSeconds__) \
186
RTITest_waitForConditionEx((retcode__) == DDS_RETCODE_OK, (maxSeconds__), 0)
187
196
#define RTITest_waitWhileConditionMatchWithAction( \
197
cond__, \
198
maxSeconds__, \
199
action__) \
200
{ \
201
const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
202
int sleeps__ = 0; \
203
int condEv__; \
204
while ((condEv__ = (cond__)) && sleeps__ < ((maxSeconds__) * 10)) { \
205
RTITest_sleep(&sleepTime__); \
206
++sleeps__; \
207
} \
208
if (!condEv__) { \
209
RTITest_assertLog( \
210
&RTI_TEST_LOG_EMPTY_TEMPLATE, \
211
"[%s:%d] wait while condition match failed: %s"
, \
212
__FILE__, \
213
__LINE__, \
214
#cond__); \
215
action__; \
216
} \
217
}
218
225
#define RTITest_waitWhileConditionMatch(cond__, maxSeconds__) \
226
RTITest_waitWhileConditionMatchWithAction(cond__, maxSeconds__, goto done)
227
236
#define RTITest_waitForEquals( \
237
expected__, \
238
actual__, \
239
cond__, \
240
typeFormat__, \
241
maxSeconds__) \
242
{ \
243
const struct RTITestTime sleepTime__ = { 0, 429496730 }; \
244
int sleeps__ = 0; \
245
int condEv__; \
246
while (!(condEv__ = (cond__)) && sleeps__ < (maxSeconds__) * 10) { \
247
RTITest_sleep(&sleepTime__); \
248
++sleeps__; \
249
} \
250
if (!condEv__) { \
251
RTITest_assertLog( \
252
&RTI_TEST_LOG_EMPTY_TEMPLATE, \
253
"[%s:%d] wait for condition failed: %s("
#typeFormat__ \
254
") == %s(" #typeFormat__ ")", \
255
__FILE__, \
256
__LINE__, \
257
#expected__, \
258
(expected__), \
259
#actual__, \
260
(actual__)); \
261
goto done; \
262
} \
263
}
264
265
/*
266
* Used for more complex conditions that may require doing some work
267
* within the sleep loop while waiting for a condition.
268
*
269
* Useful when waiting for a statistic, for example, because we need to call
270
* 'get_status' over and over until the statistic we're waiting on is the
271
* expected value:
272
*
273
* RTITest_waitForGreaterThanIntExt(
274
* RTITest_assertOk(DDS_DataWriter_get_datawriter_protocol_status(
275
* (DDS_DataWriter *)writer,
276
* &writerStatus)),
277
* nackCount,
278
* writerStatus.received_nack_count,
279
* RTI_TEST_DEFAULT_WAIT_TIME
280
* )
281
*
282
* This assert allows the caller to perform an action and then use the output
283
* of that action in the break condition.
284
*
285
* This macro should not be used directly, use (or add if it's not
286
* there) one of the specialized ones below that calls this one with the
287
* correct operation and testAssert
288
*/
289
#define RTITest_waitForConditionExtImpl( \
290
action__, \
291
expected__, \
292
actual__, \
293
maxSeconds__, \
294
operation__, \
295
testAssert__) \
296
{ \
297
struct RTITestTime sleepTime__ = { 0, 42949672 }; \
298
int i__, count__ = (int) (maxSeconds__) * 100; \
299
for (i__ = 0; i__ < count__; i__++) { \
300
action__; \
301
if ((actual__) operation__ (expected__)) { \
302
break; \
303
} \
304
RTITest_sleep(&sleepTime__); \
305
} \
306
\
307
if (i__ == count__) { \
308
testAssert__(expected__, actual__); \
309
} \
310
}
311
312
/*
313
* Like RTITest_waitForConditionExtImpl, this is used for more complex
314
* conditions that may require doing some work within the sleep loop while
315
* waiting for a condition. However this lets you pass a generic condition into
316
* the macro instead of requiring an operator. This is intended to be used for
317
* when you need to wait for multiple conditions to be true before continuing
318
* or conditions that do not require an operator, such as returning true
319
* from a function.
320
*
321
* Useful when waiting for a case where multiple variables are updated.
322
*
323
* RTITest_waitForConditionExt(
324
* RTITest_assertOk(DDS_DataWriter_assert_liveliness(manualWriter),
325
* manualPacketsSent >= expectedManualPackets
326
* && autoPackets >= expectedAutoPackets,
327
* RTI_TEST_DEFAULT_WAIT_TIME
328
* )
329
*
330
* Calls `goto done` if the condition is never met. Therefore, this macro
331
* requires a `done` label to be defined in the method that this macro is called
332
* in.
333
*/
334
#define RTITest_waitForConditionExt(action__, condition__, maxSeconds__) \
335
{ \
336
struct RTITestTime sleepTime__ = { 0, 42949672 }; \
337
int i__, count__ = (maxSeconds__) * 100; \
338
for (i__ = 0; i__ < count__; i__++) { \
339
action__; \
340
if ((condition__)) { \
341
break; \
342
} \
343
RTITest_sleep(&sleepTime__); \
344
} \
345
\
346
if (i__ == count__) { \
347
RTITest_assert(condition__); \
348
} \
349
}
350
351
/*
352
* Same as RTITest_waitForConditionExt but, upon failure, does not make the test
353
* fail. This is useful when you want to wait for a condition to be true but
354
* you don't want to fail the test if it's not true after the timeout.
355
*/
356
#define RTITest_waitForConditionNoFailureExt( \
357
action__, \
358
condition__, \
359
maxSeconds__) \
360
{ \
361
struct RTITestTime sleepTime__ = { 0, 42949672 }; \
362
int i__, count__ = (maxSeconds__) *100; \
363
for (i__ = 0; i__ < count__; i__++) { \
364
action__; \
365
if ((condition__)) { \
366
break; \
367
} \
368
RTITest_sleep(&sleepTime__); \
369
} \
370
}
371
384
#define RTITest_waitForEqualsIntExt( \
385
action__, \
386
expected__, \
387
actual__, \
388
maxSeconds__) \
389
RTITest_waitForConditionExtImpl( \
390
action__, \
391
expected__, \
392
actual__, \
393
maxSeconds__, \
394
==, \
395
RTITest_assertEqualsInt)
396
409
#define RTITest_waitForGreaterThanIntExt( \
410
action__, \
411
expected__, \
412
actual__, \
413
maxSeconds__) \
414
RTITest_waitForConditionExtImpl( \
415
action__, \
416
expected__, \
417
actual__, \
418
maxSeconds__, \
419
>, \
420
RTITest_assertGreaterThanInt)
421
434
#define RTITest_waitForGreaterOrEqualsIntExt( \
435
action__, \
436
expected__, \
437
actual__, \
438
maxSeconds__) \
439
RTITest_waitForConditionExtImpl( \
440
action__, \
441
expected__, \
442
actual__, \
443
maxSeconds__, \
444
>=, \
445
RTITest_assertGreaterOrEqualsInt)
446
459
#define RTITest_waitForLessThanOrEqualsIntExt( \
460
action__, \
461
expected__, \
462
actual__, \
463
maxSeconds__) \
464
RTITest_waitForConditionExtImpl( \
465
action__, \
466
expected__, \
467
actual__, \
468
maxSeconds__, \
469
<=, \
470
RTITest_assertGreaterOrEqualsInt)
471
482
#define RTITest_waitForGreaterThanInt(expected__, actual__, maxSeconds__) \
483
RTITest_waitForGreaterThanIntExt( \
484
((void) 0), \
485
expected__, \
486
actual__, \
487
maxSeconds__)
488
499
#define RTITest_waitForGreaterOrEqualsInt(expected__, actual__, maxSeconds__) \
500
RTITest_waitForGreaterOrEqualsIntExt( \
501
((void) 0), \
502
expected__, \
503
actual__, \
504
maxSeconds__)
505
516
#define RTITest_waitForLessThanOrEqualsInt(expected__, actual__, maxSeconds__) \
517
RTITest_waitForLessThanOrEqualsIntExt( \
518
((void) 0), \
519
expected__, \
520
actual__, \
521
maxSeconds__)
522
533
#define RTITest_waitForEqualsInt(expected__, actual__, maxSeconds__) \
534
RTITest_waitForEquals( \
535
expected__, \
536
actual__, \
537
((expected__) == (actual__)), \
538
"%d"
, \
539
maxSeconds__)
540
551
#define RTITest_waitForEqualsLong(expected__, actual__, maxSeconds__) \
552
RTITest_waitForEquals( \
553
expected__, \
554
actual__, \
555
((expected__) == (actual__)), \
556
"%ld"
, \
557
maxSeconds__)
558
569
#define RTITest_waitForEqualsLongLong(expected__, actual__, maxSeconds__) \
570
RTITest_waitForEquals( \
571
expected__, \
572
actual__, \
573
((expected__) == (actual__)), \
574
"%lld"
, \
575
maxSeconds__)
576
589
#define RTITest_waitForGreaterThanLongLongExt( \
590
action__, \
591
expected__, \
592
actual__, \
593
maxSeconds__) \
594
RTITest_waitForConditionExtImpl( \
595
action__, \
596
expected__, \
597
actual__, \
598
maxSeconds__, \
599
>, \
600
RTITest_assertGreaterThanLongLong)
601
612
#define RTITest_waitForGreaterThanLongLong(expected__, actual__, maxSeconds__) \
613
RTITest_waitForGreaterThanLongLongExt( \
614
((void) 0), \
615
expected__, \
616
actual__, \
617
maxSeconds__)
618
629
#define RTITest_waitForEqualsUnsignedInt(expected__, actual__, maxSeconds__) \
630
RTITest_waitForEquals( \
631
expected__, \
632
actual__, \
633
((expected__) == (actual__)), \
634
"%u"
, \
635
maxSeconds__)
636
647
#define RTITest_waitForEqualsUnsignedLongLong( \
648
expected__, \
649
actual__, \
650
maxSeconds__) \
651
RTITest_waitForEquals( \
652
expected__, \
653
actual__, \
654
((expected__) == (actual__)), \
655
"%llu"
, \
656
maxSeconds__)
657
670
#define RTITest_waitForEqualsUnsignedLongLongExt( \
671
action__, \
672
expected__, \
673
actual__, \
674
maxSeconds__) \
675
RTITest_waitForConditionExtImpl( \
676
action__, \
677
expected__, \
678
actual__, \
679
maxSeconds__, \
680
==, \
681
RTITest_assertEqualsUnsignedLongLong)
682
695
#define RTITest_waitForGreaterOrEqualsUnsignedLongLongExt( \
696
action__, \
697
expected__, \
698
actual__, \
699
maxSeconds__) \
700
RTITest_waitForConditionExtImpl( \
701
action__, \
702
expected__, \
703
actual__, \
704
maxSeconds__, \
705
>=, \
706
RTITest_assertGreaterOrEqualsUnsignedLongLong)
707
719
#define RTITest_waitForGreaterOrEqualsUnsignedLongLong( \
720
expected__, \
721
actual__, \
722
maxSeconds__) \
723
RTITest_waitForGreaterOrEqualsUnsignedLongLongExt( \
724
((void) 0), \
725
expected__, \
726
actual__, \
727
maxSeconds__)
728
729
#define RTI_TEST_TIME_SEC_MAX 0xFFFFFFFFU
730
#define RTI_TEST_TIME_FRAC_MAX 0xFFFFFFFFU
731
#define RTI_TEST_TIME_FRAC_TO_SEC 0x100000000U
732
733
#define RTI_TEST_TIME_SEC_TO_MILLISECONDS 1000U
734
#define RTI_TEST_TIME_SEC_TO_MICROSECONDS 1000000U
735
#define RTI_TEST_TIME_SEC_TO_NANOSECONDS 1000000000U
736
754
#define RTITestTime_toPrecision(s__, value__, time__, conversion_factor__) \
755
do { \
756
unsigned long long unitTemp__ = (unsigned long long) (time__).frac; \
757
s__ = (time__).sec; \
758
unitTemp__ = \
759
(unitTemp__ * (conversion_factor__)) / (RTI_TEST_TIME_FRAC_TO_SEC); \
760
value__ = (unsigned int) unitTemp__; \
761
if (unitTemp__ >= (conversion_factor__) && (s__) != RTI_TEST_TIME_SEC_MAX) { \
762
(s__)++; \
763
value__ = (unsigned int) (unitTemp__ - conversion_factor__); \
764
} \
765
} while(0)
766
774
#define RTITestTime_toMilliseconds(s__, msec__, time__) \
775
RTITestTime_toPrecision( \
776
s__, \
777
msec__, \
778
time__, \
779
RTI_TEST_TIME_SEC_TO_MILLISECONDS);
780
788
#define RTITestTime_toMicroseconds(s__, usec__, time__) \
789
RTITestTime_toPrecision( \
790
s__, \
791
usec__, \
792
time__, \
793
RTI_TEST_TIME_SEC_TO_MICROSECONDS);
794
802
#define RTITestTime_toNanoseconds(s__, nsec__, time__) \
803
RTITestTime_toPrecision( \
804
s__, \
805
nsec__, \
806
time__, \
807
RTI_TEST_TIME_SEC_TO_NANOSECONDS);
808
826
#define RTITestTime_fromPrecision(time__, s__, value__, conversion_factor__) \
827
do { \
828
unsigned long long unitTemp__ = (unsigned long long) (value__); \
829
(time__).sec = (unsigned int) (s__); \
830
if ((time__).sec != RTI_TEST_TIME_SEC_MAX) { \
831
(time__).sec += (unsigned int) (unitTemp__ / conversion_factor__); \
832
} \
833
unitTemp__ = unitTemp__ % conversion_factor__; \
834
(time__).frac = \
835
(unsigned int) ((unitTemp__ * RTI_TEST_TIME_FRAC_MAX) / conversion_factor__); \
836
} while(0)
837
845
#define RTITestTime_fromMilliseconds(time__, s__, msec__) \
846
RTITestTime_fromPrecision( \
847
time__, \
848
s__, \
849
msec__, \
850
RTI_TEST_TIME_SEC_TO_MILLISECONDS)
851
859
#define RTITestTime_fromMicroseconds(time__, s__, usec__) \
860
RTITestTime_fromPrecision( \
861
time__, \
862
s__, \
863
usec__, \
864
RTI_TEST_TIME_SEC_TO_MICROSECONDS)
865
873
#define RTITestTime_fromNanoseconds(time__, s__, nsec__) \
874
RTITestTime_fromPrecision( \
875
time__, \
876
s__, \
877
nsec__, \
878
RTI_TEST_TIME_SEC_TO_NANOSECONDS)
879
880
#ifdef __cplusplus
881
}
/* extern "C" */
882
#endif
883
884
#endif