ucx
UAP Common Extensions
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Data Structures | Macros | Typedefs | Functions
test.h File Reference

UCX Test Framework. More...

#include "common.h"
#include <stdio.h>
#include <string.h>
#include <setjmp.h>

Go to the source code of this file.

Data Structures

struct  CxTestSet
 Structure for the internal list of test cases. More...
 
struct  CxTestSuite
 A test suite containing multiple test cases. More...
 

Macros

#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.
 

Typedefs

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.
 

Functions

static CxTestSuitecx_test_suite_new (const char *name)
 Creates a new test suite.
 
static void cx_test_suite_free (CxTestSuite *suite)
 Deallocates a test suite.
 
static int cx_test_register (CxTestSuite *suite, CxTest test)
 Registers a test function with the specified test suite.
 
static void cx_test_run (CxTestSuite *suite, void *out_target, cx_write_func out_writer)
 Runs a test suite and writes the test log to the specified stream.
 

Detailed Description

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

Macro Definition Documentation

◆ __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

#define CX_TEST ( name)    void name(CxTestSuite* _suite_,void *_output_, cx_write_func _writefnc_)

Macro for a CxTest function header.

Use this macro to declare and/or define a CxTest function.

Parameters
namethe 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.

Remarks
You may only call subroutines within a CX_TEST_DO block.
Parameters
namethe name of the subroutine
...the argument list
See also
CX_TEST_SUBROUTINE()

◆ CX_TEST_DO

#define CX_TEST_DO
Value:
_writefnc_("Running ", 1, 8, _output_);\
_writefnc_(__FUNCTION__, 1, strlen(__FUNCTION__), _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.

CX_TEST(my_test_name) {
// setup code
// your tests go here
}
// tear down code
}
#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

#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.

Parameters
suite(CxTestSuite*) the test suite to run
file(FILE*) the target file to write the output to

◆ cx_test_run_stdout

#define cx_test_run_stdout ( suite)    cx_test_run_f(suite, 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 name(CxTestSuite* _suite_,\
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
namethe name of the subroutine
...the parameter list
See also
CX_TEST_CALL_SUBROUTINE()

Function Documentation

◆ cx_test_register()

static int cx_test_register ( CxTestSuite * suite,
CxTest test )
inlinestatic

Registers a test function with the specified test suite.

Parameters
suitethe suite, the test function shall be added to
testthe test function to register
Return values
zerosuccess
non-zerofailure

◆ cx_test_run()

static void cx_test_run ( CxTestSuite * suite,
void * out_target,
cx_write_func out_writer )
inlinestatic

Runs a test suite and writes the test log to the specified stream.

Parameters
suitethe test suite to run
out_targetthe target buffer or file to write the output to
out_writerthe write function writing to out_target

◆ cx_test_suite_free()

static void cx_test_suite_free ( CxTestSuite * suite)
inlinestatic

Deallocates a test suite.

Parameters
suitethe test suite to free

◆ cx_test_suite_new()

static CxTestSuite * cx_test_suite_new ( const char * name)
inlinestatic

Creates a new test suite.

Parameters
nameoptional name of the suite
Returns
a new test suite