Thu, 31 May 2012 12:51:22 +0200
major refactoring of test framework
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@26 | 36 | #include <stdio.h> |
universe@26 | 37 | #include <string.h> |
universe@33 | 38 | #include <setjmp.h> |
universe@26 | 39 | #include "list.h" |
universe@26 | 40 | |
universe@26 | 41 | #ifdef __cplusplus |
universe@26 | 42 | extern "C" { |
universe@26 | 43 | #endif |
universe@26 | 44 | |
universe@26 | 45 | typedef struct { |
universe@26 | 46 | unsigned int success; |
universe@26 | 47 | unsigned int failure; |
universe@26 | 48 | UcxList *tests; |
universe@26 | 49 | } UcxTestSuite; |
universe@26 | 50 | |
universe@26 | 51 | typedef void(*UcxTest)(UcxTestSuite*,FILE*); |
universe@26 | 52 | |
universe@26 | 53 | UcxTestSuite* ucx_test_suite_new(); |
universe@26 | 54 | void ucx_test_suite_free(UcxTestSuite*); |
universe@26 | 55 | |
universe@26 | 56 | void ucx_test_register(UcxTestSuite*, UcxTest); |
universe@26 | 57 | void ucx_test_run(UcxTestSuite*, FILE*); |
universe@26 | 58 | |
universe@27 | 59 | #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *); |
universe@33 | 60 | #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) |
universe@33 | 61 | |
universe@33 | 62 | #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ |
universe@33 | 63 | fwrite(__func__, 1, strlen(__func__), _output_);\ |
universe@33 | 64 | fwrite("... ", 1, 4, _output_);\ |
universe@33 | 65 | jmp_buf _env_; \ |
universe@33 | 66 | if (!setjmp(_env_)) { |
universe@26 | 67 | |
universe@26 | 68 | #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ |
universe@26 | 69 | fwrite(message".\n", 1, 2+strlen(message), _output_); \ |
universe@26 | 70 | _suite_->failure++; \ |
universe@33 | 71 | longjmp(_env_, 1);\ |
universe@26 | 72 | } |
universe@26 | 73 | |
universe@33 | 74 | #define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\ |
universe@33 | 75 | FILE *_output_, jmp_buf _env_, void* data) |
universe@33 | 76 | #define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data); |
universe@33 | 77 | |
universe@33 | 78 | #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} |
universe@26 | 79 | |
universe@26 | 80 | #ifdef __cplusplus |
universe@26 | 81 | } |
universe@26 | 82 | #endif |
universe@26 | 83 | |
universe@26 | 84 | #endif /* TEST_H */ |
universe@26 | 85 |