Sat, 20 Jul 2013 11:13:26 +0200
some fixes
universe@103 | 1 | /* |
universe@103 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@26 | 3 | * |
universe@103 | 4 | * Copyright 2013 Olaf Wintermann. All rights reserved. |
universe@33 | 5 | * |
universe@103 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@103 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@33 | 8 | * |
universe@103 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@103 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@103 | 11 | * |
universe@103 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@103 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@103 | 14 | * documentation and/or other materials provided with the distribution. |
universe@103 | 15 | * |
universe@103 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@103 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@103 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@103 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@103 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@103 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@103 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@103 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@103 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@103 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@103 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@103 | 27 | */ |
universe@103 | 28 | |
universe@103 | 29 | /* |
universe@33 | 30 | * |
universe@33 | 31 | * Usage of this test framework: |
universe@33 | 32 | * |
universe@33 | 33 | * **** IN HEADER FILE: **** |
universe@33 | 34 | * |
universe@33 | 35 | * UCX_TEST_DECLARE(function_name) |
universe@33 | 36 | * |
universe@33 | 37 | * **** IN SOURCE FILE: **** |
universe@33 | 38 | * |
universe@33 | 39 | * UCX_TEST_IMPLEMENT(function_name) { |
universe@33 | 40 | * <memory allocation and other stuff here> |
universe@33 | 41 | * UCX_TEST_BEGIN |
universe@33 | 42 | * <tests with UCX_TEST_ASSERT here> |
universe@33 | 43 | * UCX_TEST_END |
universe@33 | 44 | * <cleanup of memory here> |
universe@33 | 45 | * } |
universe@33 | 46 | * |
universe@33 | 47 | * PLEASE NOTE: if a test fails, a longjump is performed |
universe@33 | 48 | * back to the UCX_TEST_BEGIN macro! |
universe@33 | 49 | * |
universe@33 | 50 | * You may use multiple BEGIN-END blocks if you are aware of the |
universe@33 | 51 | * longjmp behaviour. |
universe@33 | 52 | * |
universe@26 | 53 | */ |
universe@26 | 54 | |
olaf@120 | 55 | #ifndef UCX_TEST_H |
olaf@120 | 56 | #define UCX_TEST_H |
universe@26 | 57 | |
universe@69 | 58 | #include "ucx.h" |
universe@26 | 59 | #include <stdio.h> |
universe@26 | 60 | #include <string.h> |
universe@33 | 61 | #include <setjmp.h> |
universe@26 | 62 | |
universe@26 | 63 | #ifdef __cplusplus |
universe@26 | 64 | extern "C" { |
universe@26 | 65 | #endif |
universe@26 | 66 | |
universe@69 | 67 | #ifndef __FUNCTION__ |
universe@69 | 68 | #define __FUNCTION__ __func__ |
universe@69 | 69 | #endif |
universe@69 | 70 | |
universe@83 | 71 | typedef struct UcxTestList UcxTestList; |
universe@83 | 72 | typedef struct UcxTestSuite UcxTestSuite; |
universe@83 | 73 | typedef void(*UcxTest)(UcxTestSuite*,FILE*); |
universe@83 | 74 | |
universe@83 | 75 | struct UcxTestList{ |
universe@83 | 76 | UcxTest test; |
universe@83 | 77 | UcxTestList *next; |
universe@83 | 78 | }; |
universe@83 | 79 | |
universe@83 | 80 | struct UcxTestSuite { |
universe@26 | 81 | unsigned int success; |
universe@26 | 82 | unsigned int failure; |
universe@83 | 83 | UcxTestList *tests; |
universe@83 | 84 | }; |
universe@26 | 85 | |
universe@26 | 86 | UcxTestSuite* ucx_test_suite_new(); |
universe@26 | 87 | void ucx_test_suite_free(UcxTestSuite*); |
universe@26 | 88 | |
universe@83 | 89 | int ucx_test_register(UcxTestSuite*, UcxTest); |
universe@26 | 90 | void ucx_test_run(UcxTestSuite*, FILE*); |
universe@26 | 91 | |
olaf@70 | 92 | #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) |
universe@33 | 93 | #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) |
universe@33 | 94 | |
universe@33 | 95 | #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ |
universe@69 | 96 | fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ |
universe@33 | 97 | fwrite("... ", 1, 4, _output_);\ |
universe@33 | 98 | jmp_buf _env_; \ |
universe@33 | 99 | if (!setjmp(_env_)) { |
universe@26 | 100 | |
universe@26 | 101 | #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ |
universe@26 | 102 | fwrite(message".\n", 1, 2+strlen(message), _output_); \ |
universe@26 | 103 | _suite_->failure++; \ |
universe@33 | 104 | longjmp(_env_, 1);\ |
universe@26 | 105 | } |
universe@26 | 106 | |
universe@88 | 107 | #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\ |
universe@88 | 108 | FILE *_output_, jmp_buf _env_, __VA_ARGS__) |
universe@88 | 109 | #define UCX_TEST_CALL_SUBROUTINE(name,...) \ |
universe@88 | 110 | name(_suite_,_output_,_env_,__VA_ARGS__); |
universe@33 | 111 | |
universe@33 | 112 | #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} |
universe@26 | 113 | |
universe@26 | 114 | #ifdef __cplusplus |
universe@26 | 115 | } |
universe@26 | 116 | #endif |
universe@26 | 117 | |
olaf@120 | 118 | #endif /* UCX_TEST_H */ |
universe@26 | 119 |