A test case needs to be defined with the CX_TEST macro. The general structure of a test case is 1) setup code, 2) the actual test, 3) tear down code. In UCX this is realized by the CX_TEST_DO macro with which you can define a scope for the actual test. Whenever a test assertion fails, the scope is exited and the tear down code is executed.
CX_TEST(test_foo) {
struct mystruct *x = malloc(sizeof(*x));
x->d = 42;
CX_TEST_DO {
int y = foo(x);
// auto-generated failure message
CX_TEST_ASSERT(y == 42);
// alternatively: custom failure message
CX_TEST_ASSERTM(y == 42,
"foo does not return correct value");
}
free(x);
}
struct mystruct {
int d;
};
int foo(struct mystruct *s) {
return s->d;
}
Once you have registered all test cases, you can run the test suite with cx_test_run() or one of the convenience macros.
// for example: UCX buffer
CxBuffer buf;
cxBufferInit(&buf, NULL, 1024, NULL, 0);
cx_test_run(suite, &buf, cxBufferWriteFunc);
// do something with the buffer
cxBufferDestroy(&buf);
Finally, clean up all resources consumed by the test suite.
cx_test_suite_free(suite);
Test Subroutines
For parameterized testing you can define and call test subroutines. It is not possible to call arbitrary functions and use the CX_TEST_ASSERT macro. Instead, you define a callable test subroutine with CX_TEST_SUBROUTINE and call it with CX_TEST_CALL_SUBROUTINE. The following example illustrates this with an adaption of the above test case.