Fri, 08 Feb 2013 17:09:12 +0100
modified code to compile with -pedantic
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 | |
universe@26 | 41 | #ifdef __cplusplus |
universe@26 | 42 | extern "C" { |
universe@26 | 43 | #endif |
universe@26 | 44 | |
universe@69 | 45 | #ifndef __FUNCTION__ |
universe@69 | 46 | #define __FUNCTION__ __func__ |
universe@69 | 47 | #endif |
universe@69 | 48 | |
universe@83 | 49 | typedef struct UcxTestList UcxTestList; |
universe@83 | 50 | typedef struct UcxTestSuite UcxTestSuite; |
universe@83 | 51 | typedef void(*UcxTest)(UcxTestSuite*,FILE*); |
universe@83 | 52 | |
universe@83 | 53 | struct UcxTestList{ |
universe@83 | 54 | UcxTest test; |
universe@83 | 55 | UcxTestList *next; |
universe@83 | 56 | }; |
universe@83 | 57 | |
universe@83 | 58 | struct UcxTestSuite { |
universe@26 | 59 | unsigned int success; |
universe@26 | 60 | unsigned int failure; |
universe@83 | 61 | UcxTestList *tests; |
universe@83 | 62 | }; |
universe@26 | 63 | |
universe@26 | 64 | UcxTestSuite* ucx_test_suite_new(); |
universe@26 | 65 | void ucx_test_suite_free(UcxTestSuite*); |
universe@26 | 66 | |
universe@83 | 67 | int ucx_test_register(UcxTestSuite*, UcxTest); |
universe@26 | 68 | void ucx_test_run(UcxTestSuite*, FILE*); |
universe@26 | 69 | |
olaf@70 | 70 | #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) |
universe@33 | 71 | #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) |
universe@33 | 72 | |
universe@33 | 73 | #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ |
universe@69 | 74 | fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ |
universe@33 | 75 | fwrite("... ", 1, 4, _output_);\ |
universe@33 | 76 | jmp_buf _env_; \ |
universe@33 | 77 | if (!setjmp(_env_)) { |
universe@26 | 78 | |
universe@26 | 79 | #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ |
universe@26 | 80 | fwrite(message".\n", 1, 2+strlen(message), _output_); \ |
universe@26 | 81 | _suite_->failure++; \ |
universe@33 | 82 | longjmp(_env_, 1);\ |
universe@26 | 83 | } |
universe@26 | 84 | |
universe@33 | 85 | #define UCX_TEST_SUBROUTINE(name,data) void name(UcxTestSuite* _suite_,\ |
universe@33 | 86 | FILE *_output_, jmp_buf _env_, void* data) |
universe@33 | 87 | #define UCX_TEST_CALL_SUBROUTINE(name,data) name(_suite_,_output_,_env_,data); |
universe@33 | 88 | |
universe@33 | 89 | #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} |
universe@26 | 90 | |
universe@26 | 91 | #ifdef __cplusplus |
universe@26 | 92 | } |
universe@26 | 93 | #endif |
universe@26 | 94 | |
universe@26 | 95 | #endif /* TEST_H */ |
universe@26 | 96 |