Executing unit tests for functions that contain endless loops poses a problem when checking assertions. This is because test cases require the tested function to exit in order to perform assertion validation. In some situations, however, users can implement test cases for a function containing an endless loop. This is accomplished by using special macros and functions that allow conditional breaks of endless loop code execution:
Sets an internal jump buffer using setjmp or sigsetjmp and evaluates the passed expression. The buffer can be jumped to using the CPPTEST_JMP API call.
This macro is typically used inside the test case where it wraps the call to the tested function (see example below).
Executes a longjmp call (longjmp or siglongjmp is used), which restores the exectuion state to the most recent invocation of the
This macro is typically used inside the stub body. It accepts an integer argument that will be returned inside the test case via the cpptestGetJmpReturn function. The integer argrument can be used to verify the correctness of the performed jump (see example below).
|Returns the return value of the most recent CPPTEST_JMP call, i.e. the argument of longjmp/siglongjmp, which is a return value from setmp/sigsetjmp.|
These macros are intended to be used in conjunction and are intended to provide users with the ability to break the execution of the tested code (e.g. inside the stub body) and return to the test case to perform assertion checks. They function similarly to the setjmp and longjmp calls declared in the standard setjmp.h header.
This feature is useful for testing typical events-processing embedded functions that contain endless loops. Breaking test code execution is accomplished by a call to CPPTEST_JMP. The recommended technique for injecting a call to CPPTEST_JMP is through the stub body.
The following example illustrates the most common use for these macros and functions:
When C++test executes the TestSuite_main_c_883563c3_test_signalData test case, it saves the jump buffer prior to calling the tested function. When the tested code executes, it performs the call to CPPTEST_JMP from the CppTest_Stub_scanFrequencySensor stub body, which transfers code execution back to the test case to perform assertions checks.