ucx/test.h

Wed, 27 Feb 2013 09:41:17 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Feb 2013 09:41:17 +0100
changeset 88
18823857ce79
parent 83
3b552d7a9610
child 103
08018864fb91
permissions
-rw-r--r--

variadic test subroutines

     1 /* 
     2  * File:   test.h
     3  * Author: Mike
     4  *
     5  * Created on 18. Februar 2012, 14:15
     6  *
     7  *
     8  *
     9  * Usage of this test framework:
    10  *
    11  * **** IN HEADER FILE: ****
    12  *
    13  * UCX_TEST_DECLARE(function_name)
    14  *
    15  * **** IN SOURCE FILE: ****
    16  *
    17  * UCX_TEST_IMPLEMENT(function_name) {
    18  *  <memory allocation and other stuff here>
    19  *  UCX_TEST_BEGIN
    20  *  <tests with UCX_TEST_ASSERT here>
    21  *  UCX_TEST_END
    22  *  <cleanup of memory here>
    23  * }
    24  *
    25  * PLEASE NOTE: if a test fails, a longjump is performed
    26  * back to the UCX_TEST_BEGIN macro!
    27  *
    28  * You may use multiple BEGIN-END blocks if you are aware of the
    29  * longjmp behaviour.
    30  *
    31  */
    33 #ifndef TEST_H
    34 #define	TEST_H
    36 #include "ucx.h"
    37 #include <stdio.h>
    38 #include <string.h>
    39 #include <setjmp.h>
    41 #ifdef	__cplusplus
    42 extern "C" {
    43 #endif
    45 #ifndef __FUNCTION__
    46 #define __FUNCTION__ __func__
    47 #endif
    49 typedef struct UcxTestList UcxTestList;
    50 typedef struct UcxTestSuite UcxTestSuite;
    51 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
    53 struct UcxTestList{
    54     UcxTest test;
    55     UcxTestList *next;
    56 };
    58 struct UcxTestSuite {
    59     unsigned int success;
    60     unsigned int failure;
    61     UcxTestList *tests;
    62 };
    64 UcxTestSuite* ucx_test_suite_new();
    65 void ucx_test_suite_free(UcxTestSuite*);
    67 int ucx_test_register(UcxTestSuite*, UcxTest);
    68 void ucx_test_run(UcxTestSuite*, FILE*);
    70 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
    71 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
    73 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
    74         fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
    75         fwrite("... ", 1, 4, _output_);\
    76         jmp_buf _env_; \
    77         if (!setjmp(_env_)) {
    79 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
    80         fwrite(message".\n", 1, 2+strlen(message), _output_); \
    81         _suite_->failure++; \
    82         longjmp(_env_, 1);\
    83     }
    85 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
    86         FILE *_output_, jmp_buf _env_, __VA_ARGS__)
    87 #define UCX_TEST_CALL_SUBROUTINE(name,...) \
    88         name(_suite_,_output_,_env_,__VA_ARGS__);
    90 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
    92 #ifdef	__cplusplus
    93 }
    94 #endif
    96 #endif	/* TEST_H */

mercurial