universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: ucx: /home/mike/workspace/c/ucx/src/ucx/test.h File Reference universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
ucx universe@390:
universe@390:
UAP Common Extensions
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390:
universe@390: universe@390:
universe@390: universe@390: universe@390:
universe@390:
universe@390:
universe@390: Data Structures | universe@390: Macros | universe@390: Typedefs | universe@390: Functions
universe@390:
universe@390:
test.h File Reference
universe@390:
universe@390:
universe@390: universe@390:

UCX Test Framework. universe@390: More...

universe@390:
#include "ucx.h"
universe@390: #include <stdio.h>
universe@390: #include <string.h>
universe@390: #include <setjmp.h>
universe@390:
universe@390:

Go to the source code of this file.

universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:

universe@390: Data Structures

struct  UcxTestList
 Structure for the internal list of test cases. More...
 
struct  UcxTestSuite
 A test suite containing multiple test cases. More...
 
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:

universe@390: Macros

#define __FUNCTION__   __func__
 Alias for the __func__ preprocessor macro. More...
 
#define UCX_TEST(name)   void name(UcxTestSuite* _suite_,FILE *_output_)
 Macro for a UcxTest function header. More...
 
#define UCX_TEST_BEGIN
 Marks the begin of a test. More...
 
#define UCX_TEST_ASSERT(condition, message)
 Checks a test assertion. More...
 
#define UCX_TEST_SUBROUTINE(name, ...)
 Macro for a test subroutine function header. More...
 
#define UCX_TEST_CALL_SUBROUTINE(name, ...)   name(_suite_,_output_,_env_,__VA_ARGS__);
 Macro for calling a test subroutine. More...
 
#define UCX_TEST_END   fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
 Marks the end of a test. More...
 
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:

universe@390: Typedefs

typedef struct UcxTestSuite UcxTestSuite
 Type for the UcxTestSuite. More...
 
typedef void(* UcxTest) (UcxTestSuite *, FILE *)
 Pointer to a test function. More...
 
typedef struct UcxTestList UcxTestList
 Type for the internal list of test cases. More...
 
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:

universe@390: Functions

UcxTestSuiteucx_test_suite_new ()
 Creates a new test suite. More...
 
void ucx_test_suite_free (UcxTestSuite *suite)
 Destroys a test suite. More...
 
int ucx_test_register (UcxTestSuite *suite, UcxTest test)
 Registers a test function with the specified test suite. More...
 
void ucx_test_run (UcxTestSuite *suite, FILE *outstream)
 Runs a test suite and writes the test log to the specified stream. More...
 
universe@390:

Detailed Description

universe@390:

UCX Test Framework.

universe@390:

Usage of this test framework:

universe@390:

**** IN HEADER FILE: ****

universe@390:
universe@390: UCX_TEST(function_name);
universe@390: UCX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional
universe@390: 

**** IN SOURCE FILE: ****

universe@390: UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
universe@390:   // tests with UCX_TEST_ASSERT()
universe@390: }
UCX_TEST(function_name) {
universe@390:   // memory allocation and other stuff here
universe@390:   UCX_TEST_BEGIN
universe@390:   // tests with UCX_TEST_ASSERT() and/or
universe@390:   // calls with UCX_TEST_CALL_SUBROUTINE() here
universe@390:   UCX_TEST_END
universe@390:   // cleanup of memory here
universe@390: }
universe@390: 

Note: if a test fails, a longjump is performed back to the UCX_TEST_BEGIN macro!

universe@390:

Attention: Do not call own functions within a test, that use UCX_TEST_ASSERT() macros and are not defined by using UCX_TEST_SUBROUTINE().

universe@390:
Author
Mike Becker
universe@390:
universe@390: Olaf Wintermann
universe@390:

Macro Definition Documentation

universe@390: universe@390:

◆ __FUNCTION__

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
#define __FUNCTION__   __func__
universe@390:
universe@390: universe@390:

Alias for the __func__ preprocessor macro.

universe@390:

Some compilers use __func__ and others use FUNCTION. We use FUNCTION so we define it for those compilers which use __func__.

universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST( name)   void name(UcxTestSuite* _suite_,FILE *_output_)
universe@390:
universe@390: universe@390:

Macro for a UcxTest function header.

universe@390:

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

universe@390:
Parameters
universe@390: universe@390: universe@390:
namethe name of the test function
universe@390:
universe@390:
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST_ASSERT

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST_ASSERT( condition,
 message 
)
universe@390:
universe@390: Value:
if (!(condition)) { \
fwrite(message".\n", 1, 2+strlen(message), _output_); \
_suite_->failure++; \
longjmp(_env_, 1);\
}
universe@390:

Checks a test assertion.

universe@390:

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
universe@390: universe@390: universe@390: universe@390:
conditionthe condition to check
messagethe message that shall be printed out on failure
universe@390:
universe@390:
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST_BEGIN

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST_BEGIN
universe@390:
universe@390: Value:
fwrite("Running ", 1, 8, _output_);\
fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
fwrite("... ", 1, 4, _output_);\
jmp_buf _env_; \
if (!setjmp(_env_)) {
#define __FUNCTION__
Alias for the __func__ preprocessor macro.
Definition: test.h:91
universe@390:
universe@390:

Marks the begin of a test.

universe@390:

Note: Any UCX_TEST_ASSERT() calls must be performed after UCX_TEST_BEGIN.

universe@390:
See also
UCX_TEST_END
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST_CALL_SUBROUTINE

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST_CALL_SUBROUTINE( name,
 ... 
)   name(_suite_,_output_,_env_,__VA_ARGS__);
universe@390:
universe@390: universe@390:

Macro for calling a test subroutine.

universe@390:

Subroutines declared with UCX_TEST_SUBROUTINE() can be called by using this macro.

universe@390:

Note: You may only call subroutines within a UCX_TEST_BEGIN- UCX_TEST_END-block.

universe@390:
Parameters
universe@390: universe@390: universe@390: universe@390:
namethe name of the subroutine
...the argument list
universe@390:
universe@390:
universe@390:
See also
UCX_TEST_SUBROUTINE()
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST_END

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST_END   fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
universe@390:
universe@390: universe@390:

Marks the end of a test.

universe@390:

Note: Any UCX_TEST_ASSERT() calls must be performed before UCX_TEST_END.

universe@390:
See also
UCX_TEST_BEGIN
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UCX_TEST_SUBROUTINE

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
#define UCX_TEST_SUBROUTINE( name,
 ... 
)
universe@390:
universe@390: Value:
void name(UcxTestSuite* _suite_,\
FILE *_output_, jmp_buf _env_, __VA_ARGS__)
A test suite containing multiple test cases.
Definition: test.h:116
universe@390:
universe@390:

Macro for a test subroutine function header.

universe@390:

Use this to declare and/or define a subroutine that can be called by using UCX_TEST_CALL_SUBROUTINE().

universe@390:
Parameters
universe@390: universe@390: universe@390: universe@390:
namethe name of the subroutine
...the parameter list
universe@390:
universe@390:
universe@390:
See also
UCX_TEST_CALL_SUBROUTINE()
universe@390: universe@390:
universe@390:
universe@390:

Typedef Documentation

universe@390: universe@390:

◆ UcxTest

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
typedef void(* UcxTest) (UcxTestSuite *, FILE *)
universe@390:
universe@390: universe@390:

Pointer to a test function.

universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UcxTestList

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
typedef struct UcxTestList UcxTestList
universe@390:
universe@390: universe@390:

Type for the internal list of test cases.

universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ UcxTestSuite

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390:
typedef struct UcxTestSuite UcxTestSuite
universe@390:
universe@390: universe@390:

Type for the UcxTestSuite.

universe@390: universe@390:
universe@390:
universe@390:

Function Documentation

universe@390: universe@390:

◆ ucx_test_register()

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
int ucx_test_register (UcxTestSuitesuite,
UcxTest test 
)
universe@390:
universe@390: universe@390:

Registers a test function with the specified test suite.

universe@390:
Parameters
universe@390: universe@390: universe@390: universe@390:
suitethe suite, the test function shall be added to
testthe test function to register
universe@390:
universe@390:
universe@390:
Returns
EXIT_SUCCESS on success or EXIT_FAILURE on failure
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ ucx_test_run()

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
void ucx_test_run (UcxTestSuitesuite,
FILE * outstream 
)
universe@390:
universe@390: universe@390:

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

universe@390:
Parameters
universe@390: universe@390: universe@390: universe@390:
suitethe test suite to run
outstreamthe stream the log shall be written to
universe@390:
universe@390:
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ ucx_test_suite_free()

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
void ucx_test_suite_free (UcxTestSuitesuite)
universe@390:
universe@390: universe@390:

Destroys a test suite.

universe@390:
Parameters
universe@390: universe@390: universe@390:
suitethe test suite to destroy
universe@390:
universe@390:
universe@390: universe@390:
universe@390:
universe@390: universe@390:

◆ ucx_test_suite_new()

universe@390: universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390: universe@390:
UcxTestSuite* ucx_test_suite_new ()
universe@390:
universe@390: universe@390:

Creates a new test suite.

universe@390:
Returns
a new test suite
universe@390: universe@390:
universe@390:
universe@390:
universe@390: universe@390: universe@390: universe@390: