| the Compartmented Robust Posix C++ Unit Test system |
|
|---|
Assert that the block of code immediately following the macro does
not consume more than ms milliseconds CPU time.
Used in: A test function body, the constructor or destructor of a
fixture, or a function called from them.
See TEST(name, ...).
Requirement: ms is an unsigned integer value.
Requirement: A code block follows immediately after.
The assertion succeeds if, and only if, the code block following
completes its execution in at most ms milliseconds
of CPU-time.
On success the test function continues without side effects.
On failure, the test is terminated with an error report. The report
includes the parameter value ms and the actual
CPU-time consumed.
![]() | Note |
|---|---|
It is possible to temporarily disable the assertion with the
-t / --disable-timeouts
command line flag, in which case the assertion will always succeed,
regardless of the amount of CPU-time consumed by the following
code block. This can be useful when running with time consuming tools
like valgrind.
|
See also:
VERIFY_SCOPE_MAX_CPUTIME_MS(ms).
Example: The test program
#include <crpcut.hpp>
#include <sys/times.h>
TEST(long_real_time_short_cpu_time)
{
ASSERT_SCOPE_MAX_CPUTIME_MS(3)
{
for (int i = 0; i < 50; ++i)
{
usleep(1000); // would fail if implemented as busy wait
}
}
}
TEST(short_real_time_long_cpu_time)
{
const clock_t clocks_per_tick = sysconf(_SC_CLK_TCK);
tms t;
times(&t);
clock_t deadline = t.tms_utime + t.tms_stime + clocks_per_tick/10;
ASSERT_SCOPE_MAX_CPUTIME_MS(100)
{
for (;;)
{
for (volatile int n = 0; n < 100000; ++n)
;
times(&t);
if (t.tms_utime + t.tms_stime > deadline) break;
}
}
}
int main(int argc, char *argv[])
{
return crpcut::run(argc, argv);
}
reports one failed test:
FAILED!: short_real_time_long_cpu_time
phase="running" --------------------------------------------------------------
samples/assert_scope_max_cpu.cpp:48
ASSERT_SCOPE_MAX_CPUTIME_MS(100)
Actual time used was 112ms
-------------------------------------------------------------------------------
===============================================================================
2 test cases selected
Total : Sum Critical Non-critical
PASSED : 1 1 0
FAILED : 1 1 0