ucx/test.h

Tue, 23 Jul 2013 14:43:45 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 23 Jul 2013 14:43:45 +0200
changeset 128
b79b1ce438dd
parent 120
8170f658f017
child 134
4d320dc3a7af
permissions
-rw-r--r--

finished documentation of UcxList

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 *
universe@26 51 */
universe@26 52
olaf@120 53 #ifndef UCX_TEST_H
olaf@120 54 #define UCX_TEST_H
universe@26 55
universe@69 56 #include "ucx.h"
universe@26 57 #include <stdio.h>
universe@26 58 #include <string.h>
universe@33 59 #include <setjmp.h>
universe@26 60
universe@26 61 #ifdef __cplusplus
universe@26 62 extern "C" {
universe@26 63 #endif
universe@26 64
universe@69 65 #ifndef __FUNCTION__
universe@69 66 #define __FUNCTION__ __func__
universe@69 67 #endif
universe@69 68
universe@83 69 typedef struct UcxTestList UcxTestList;
universe@83 70 typedef struct UcxTestSuite UcxTestSuite;
universe@83 71 typedef void(*UcxTest)(UcxTestSuite*,FILE*);
universe@83 72
universe@83 73 struct UcxTestList{
universe@83 74 UcxTest test;
universe@83 75 UcxTestList *next;
universe@83 76 };
universe@83 77
universe@83 78 struct UcxTestSuite {
universe@26 79 unsigned int success;
universe@26 80 unsigned int failure;
universe@83 81 UcxTestList *tests;
universe@83 82 };
universe@26 83
universe@26 84 UcxTestSuite* ucx_test_suite_new();
universe@26 85 void ucx_test_suite_free(UcxTestSuite*);
universe@26 86
universe@83 87 int ucx_test_register(UcxTestSuite*, UcxTest);
universe@26 88 void ucx_test_run(UcxTestSuite*, FILE*);
universe@26 89
olaf@70 90 #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *)
universe@33 91 #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_)
universe@33 92
universe@33 93 #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
universe@69 94 fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
universe@33 95 fwrite("... ", 1, 4, _output_);\
universe@33 96 jmp_buf _env_; \
universe@33 97 if (!setjmp(_env_)) {
universe@26 98
universe@26 99 #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
universe@26 100 fwrite(message".\n", 1, 2+strlen(message), _output_); \
universe@26 101 _suite_->failure++; \
universe@33 102 longjmp(_env_, 1);\
universe@26 103 }
universe@26 104
universe@88 105 #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
universe@88 106 FILE *_output_, jmp_buf _env_, __VA_ARGS__)
universe@88 107 #define UCX_TEST_CALL_SUBROUTINE(name,...) \
universe@88 108 name(_suite_,_output_,_env_,__VA_ARGS__);
universe@33 109
universe@33 110 #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
universe@26 111
universe@26 112 #ifdef __cplusplus
universe@26 113 }
universe@26 114 #endif
universe@26 115
olaf@120 116 #endif /* UCX_TEST_H */
universe@26 117

mercurial