UCX Test Framework.
More...
#include "common.h"
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
Go to the source code of this file.
|
#define | __FUNCTION__ __func__ |
| Alias for the func preprocessor macro.
|
|
#define | cx_test_run_f(suite, file) cx_test_run(suite, (void*)file, (cx_write_func)fwrite) |
| Runs a test suite and writes the test log to the specified FILE stream.
|
|
#define | cx_test_run_stdout(suite) cx_test_run_f(suite, stdout) |
| Runs a test suite and writes the test log to stdout.
|
|
#define | CX_TEST(name) void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_) |
| Macro for a CxTest function header.
|
|
#define | CX_TEST_DO |
| Defines the scope of a test.
|
|
#define | CX_TEST_ASSERTM(condition, message) |
| Checks a test assertion.
|
|
#define | CX_TEST_ASSERT(condition) CX_TEST_ASSERTM(condition, #condition " failed") |
| Checks a test assertion.
|
|
#define | CX_TEST_SUBROUTINE(name, ...) |
| Macro for a test subroutine function header.
|
|
#define | CX_TEST_CALL_SUBROUTINE(name, ...) name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__) |
| Macro for calling a test subroutine.
|
|
|
typedef struct CxTestSuite | CxTestSuite |
| Type for the CxTestSuite.
|
|
typedef void(* | CxTest) (CxTestSuite *, void *, cx_write_func) |
| Pointer to a test function.
|
|
typedef struct CxTestSet | CxTestSet |
| Type for the internal list of test cases.
|
|
UCX Test Framework.
Usage of this test framework:
**** IN HEADER FILE: ****
CX_TEST(function_name); CX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional
**** IN SOURCE FILE: **** CX_TEST_SUBROUTINE(subroutine_name, paramlist) { // tests with CX_TEST_ASSERT() }
CX_TEST(function_name) { // memory allocation and other stuff here CX_TEST_DO { // tests with CX_TEST_ASSERT() and/or // calls with CX_TEST_CALL_SUBROUTINE() here } // cleanup of memory here }
- Attention
- Do not call own functions within a test, that use CX_TEST_ASSERT() macros and are not defined by using CX_TEST_SUBROUTINE().
- Author
- Mike Becker
-
Olaf Wintermann
◆ __FUNCTION__
#define __FUNCTION__ __func__ |
Alias for the func
preprocessor macro.
Some compilers use func
and others use FUNCTION. We use FUNCTION so we define it for those compilers which use func
.
◆ CX_TEST
Macro for a CxTest function header.
Use this macro to declare and/or define a CxTest function.
- Parameters
-
name | the name of the test function |
◆ CX_TEST_ASSERT
#define CX_TEST_ASSERT |
( |
| condition | ) |
CX_TEST_ASSERTM(condition, #condition " failed") |
Checks a test assertion.
If the assertion is correct, the test carries on. If the assertion is not correct, the specified message (terminated by a dot and a line break) is written to the test suites output stream.
- Parameters
-
condition | (bool ) the condition to check |
◆ CX_TEST_ASSERTM
#define CX_TEST_ASSERTM |
( |
| condition, |
|
|
| message ) |
Value: if (!(condition)) { \
const char *_assert_msg_ = message; \
_writefnc_(_assert_msg_, 1, strlen(_assert_msg_), _output_); \
_writefnc_(".\n", 1, 2, _output_); \
_suite_->failure++; \
longjmp(_env_, 1);\
} (void) 0
Checks a test assertion.
If the assertion is correct, the test carries on. If the assertion is not correct, the specified message (terminated by a dot and a line break) is written to the test suites output stream.
- Parameters
-
condition | (bool ) the condition to check |
message | (char* ) the message that shall be printed out on failure |
◆ CX_TEST_CALL_SUBROUTINE
#define CX_TEST_CALL_SUBROUTINE |
( |
| name, |
|
|
| ... ) name(_suite_,_output_,_writefnc_,_env_,__VA_ARGS__) |
Macro for calling a test subroutine.
Subroutines declared with CX_TEST_SUBROUTINE() can be called by using this macro.
- Parameters
-
name | the name of the subroutine |
... | the argument list |
- See also
- CX_TEST_SUBROUTINE()
◆ CX_TEST_DO
Value: _writefnc_("Running ", 1, 8, _output_);\
_writefnc_("... ", 1, 4, _output_);\
jmp_buf _env_;\
for (unsigned int _cx_test_loop_ = 0 ;\
_cx_test_loop_ == 0 && !setjmp(_env_);\
_writefnc_("success.\n", 1, 9, _output_),\
_suite_->success++, _cx_test_loop_++)
#define __FUNCTION__
Alias for the func preprocessor macro.
Definition test.h:87
Defines the scope of a test.
}
}
#define CX_TEST(name)
Macro for a CxTest function header.
Definition test.h:251
#define CX_TEST_DO
Defines the scope of a test.
Definition test.h:269
- Attention
- Any CX_TEST_ASSERT() calls must be performed in scope of CX_TEST_DO.
◆ cx_test_run_f
Runs a test suite and writes the test log to the specified FILE stream.
- Parameters
-
suite | (CxTestSuite* ) the test suite to run |
file | (FILE* ) the target file to write the output to |
◆ cx_test_run_stdout
Runs a test suite and writes the test log to stdout.
- Parameters
-
suite | (CxTestSuite* ) the test suite to run |
◆ CX_TEST_SUBROUTINE
#define CX_TEST_SUBROUTINE |
( |
| name, |
|
|
| ... ) |
Value:
void *_output_,
cx_write_func _writefnc_, jmp_buf _env_, __VA_ARGS__)
size_t(* cx_write_func)(const void *, size_t, size_t, void *)
Function pointer compatible with fwrite-like functions.
Definition common.h:295
A test suite containing multiple test cases.
Definition test.h:116
Macro for a test subroutine function header.
Use this to declare and/or define a subroutine that can be called by using CX_TEST_CALL_SUBROUTINE().
- Parameters
-
name | the name of the subroutine |
... | the parameter list |
- See also
- CX_TEST_CALL_SUBROUTINE()
◆ cx_test_register()
Registers a test function with the specified test suite.
- Parameters
-
suite | the suite, the test function shall be added to |
test | the test function to register |
- Return values
-
zero | success |
non-zero | failure |
◆ cx_test_run()
Runs a test suite and writes the test log to the specified stream.
- Parameters
-
suite | the test suite to run |
out_target | the target buffer or file to write the output to |
out_writer | the write function writing to out_target |
◆ cx_test_suite_free()
Deallocates a test suite.
- Parameters
-
suite | the test suite to free |
◆ cx_test_suite_new()
static CxTestSuite * cx_test_suite_new |
( |
const char * | name | ) |
|
|
inlinestatic |
Creates a new test suite.
- Parameters
-
name | optional name of the suite |
- Returns
- a new test suite