diff -r 0f94d369bb02 -r 1bcaac272cdf ucx/test.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ucx/test.h Fri Nov 30 21:18:13 2012 +0100 @@ -0,0 +1,90 @@ +/* + * File: test.h + * Author: Mike + * + * Created on 18. Februar 2012, 14:15 + * + * + * + * Usage of this test framework: + * + * **** IN HEADER FILE: **** + * + * UCX_TEST_DECLARE(function_name) + * + * **** IN SOURCE FILE: **** + * + * UCX_TEST_IMPLEMENT(function_name) { + * + * UCX_TEST_BEGIN + * + * UCX_TEST_END + * + * } + * + * PLEASE NOTE: if a test fails, a longjump is performed + * back to the UCX_TEST_BEGIN macro! + * + * You may use multiple BEGIN-END blocks if you are aware of the + * longjmp behaviour. + * + */ + +#ifndef TEST_H +#define TEST_H + +#include "ucx.h" +#include +#include +#include +#include "list.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef __FUNCTION__ +#define __FUNCTION__ __func__ +#endif + +typedef struct { + unsigned int success; + unsigned int failure; + UcxList *tests; +} UcxTestSuite; + +typedef void(*UcxTest)(UcxTestSuite*,FILE*); + +UcxTestSuite* ucx_test_suite_new(); +void ucx_test_suite_free(UcxTestSuite*); + +void ucx_test_register(UcxTestSuite*, UcxTest); +void ucx_test_run(UcxTestSuite*, FILE*); + +#define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) +#define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) + +#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ + fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ + fwrite("... ", 1, 4, _output_);\ + jmp_buf _env_; \ + if (!setjmp(_env_)) { + +#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ + fwrite(message".\n", 1, 2+strlen(message), _output_); \ + _suite_->failure++; \ + longjmp(_env_, 1);\ + } + +#define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\ + FILE *_output_, jmp_buf _env_, void* data) +#define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data); + +#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} + +#ifdef __cplusplus +} +#endif + +#endif /* TEST_H */ +