ucx/test.h

Fri, 12 Oct 2012 10:54:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 12 Oct 2012 10:54:55 +0200
changeset 69
fb59270b1de3
parent 33
9c219a62070d
child 70
6721482eaf8e
permissions
-rw-r--r--

made the code work with VC++ compiler (use make CONF=windows)

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

mercurial