src/ucx/test.h

changeset 390
d345541018fa
parent 389
92e482410453
child 391
f094a53c1178
     1.1 --- a/src/ucx/test.h	Mon Dec 30 09:54:10 2019 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,241 +0,0 @@
     1.4 -/*
     1.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 - *
     1.7 - * Copyright 2017 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 - *
     1.9 - * Redistribution and use in source and binary forms, with or without
    1.10 - * modification, are permitted provided that the following conditions are met:
    1.11 - *
    1.12 - *   1. Redistributions of source code must retain the above copyright
    1.13 - *      notice, this list of conditions and the following disclaimer.
    1.14 - *
    1.15 - *   2. Redistributions in binary form must reproduce the above copyright
    1.16 - *      notice, this list of conditions and the following disclaimer in the
    1.17 - *      documentation and/or other materials provided with the distribution.
    1.18 - *
    1.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 - * POSSIBILITY OF SUCH DAMAGE.
    1.30 - */
    1.31 - 
    1.32 -/**
    1.33 - * @file: test.h
    1.34 - * 
    1.35 - * UCX Test Framework.
    1.36 - * 
    1.37 - * Usage of this test framework:
    1.38 - *
    1.39 - * **** IN HEADER FILE: ****
    1.40 - *
    1.41 - * <pre>
    1.42 - * UCX_TEST(function_name);
    1.43 - * UCX_TEST_SUBROUTINE(subroutine_name, paramlist); // optional
    1.44 - * </pre>
    1.45 - *
    1.46 - * **** IN SOURCE FILE: ****
    1.47 - * <pre>
    1.48 - * UCX_TEST_SUBROUTINE(subroutine_name, paramlist) {
    1.49 - *   // tests with UCX_TEST_ASSERT()
    1.50 - * }
    1.51 - * 
    1.52 - * UCX_TEST(function_name) {
    1.53 - *   // memory allocation and other stuff here
    1.54 - *   #UCX_TEST_BEGIN
    1.55 - *   // tests with UCX_TEST_ASSERT() and/or
    1.56 - *   // calls with UCX_TEST_CALL_SUBROUTINE() here
    1.57 - *   #UCX_TEST_END
    1.58 - *   // cleanup of memory here
    1.59 - * }
    1.60 - * </pre>
    1.61 - *
    1.62 - * <b>Note:</b> if a test fails, a longjump is performed
    1.63 - * back to the #UCX_TEST_BEGIN macro!
    1.64 - * 
    1.65 - * <b>Attention:</b> Do not call own functions within a test, that use
    1.66 - * UCX_TEST_ASSERT() macros and are not defined by using UCX_TEST_SUBROUTINE().
    1.67 - * 
    1.68 - *
    1.69 - * @author Mike Becker
    1.70 - * @author Olaf Wintermann
    1.71 - *
    1.72 - */
    1.73 -
    1.74 -#ifndef UCX_TEST_H
    1.75 -#define	UCX_TEST_H
    1.76 -
    1.77 -#include "ucx.h"
    1.78 -#include <stdio.h>
    1.79 -#include <string.h>
    1.80 -#include <setjmp.h>
    1.81 -
    1.82 -#ifdef	__cplusplus
    1.83 -extern "C" {
    1.84 -#endif
    1.85 -
    1.86 -#ifndef __FUNCTION__
    1.87 -
    1.88 -/**
    1.89 - * Alias for the <code>__func__</code> preprocessor macro.
    1.90 - * Some compilers use <code>__func__</code> and others use __FUNCTION__.
    1.91 - * We use __FUNCTION__ so we define it for those compilers which use
    1.92 - * <code>__func__</code>.
    1.93 - */
    1.94 -#define __FUNCTION__ __func__
    1.95 -#endif
    1.96 -
    1.97 -/** Type for the UcxTestSuite. */
    1.98 -typedef struct UcxTestSuite UcxTestSuite;
    1.99 -
   1.100 -/** Pointer to a test function. */
   1.101 -typedef void(*UcxTest)(UcxTestSuite*,FILE*);
   1.102 -
   1.103 -/** Type for the internal list of test cases. */
   1.104 -typedef struct UcxTestList UcxTestList;
   1.105 -
   1.106 -/** Structure for the internal list of test cases. */
   1.107 -struct UcxTestList {
   1.108 -    
   1.109 -    /** Test case. */
   1.110 -    UcxTest test;
   1.111 -    
   1.112 -    /** Pointer to the next list element. */
   1.113 -    UcxTestList *next;
   1.114 -};
   1.115 -
   1.116 -/**
   1.117 - * A test suite containing multiple test cases.
   1.118 - */
   1.119 -struct UcxTestSuite {
   1.120 -    
   1.121 -    /** The number of successful tests after the suite has been run. */
   1.122 -    unsigned int success;
   1.123 -    
   1.124 -    /** The number of failed tests after the suite has been run. */
   1.125 -    unsigned int failure;
   1.126 -    
   1.127 -    /**
   1.128 -     * Internal list of test cases.
   1.129 -     * Use ucx_test_register() to add tests to this list.
   1.130 -     */
   1.131 -    UcxTestList *tests;
   1.132 -};
   1.133 -
   1.134 -/**
   1.135 - * Creates a new test suite.
   1.136 - * @return a new test suite
   1.137 - */
   1.138 -UcxTestSuite* ucx_test_suite_new();
   1.139 -
   1.140 -/**
   1.141 - * Destroys a test suite.
   1.142 - * @param suite the test suite to destroy
   1.143 - */
   1.144 -void ucx_test_suite_free(UcxTestSuite* suite);
   1.145 -
   1.146 -/**
   1.147 - * Registers a test function with the specified test suite.
   1.148 - * 
   1.149 - * @param suite the suite, the test function shall be added to
   1.150 - * @param test the test function to register
   1.151 - * @return <code>EXIT_SUCCESS</code> on success or
   1.152 - * <code>EXIT_FAILURE</code> on failure
   1.153 - */
   1.154 -int ucx_test_register(UcxTestSuite* suite, UcxTest test);
   1.155 -
   1.156 -/**
   1.157 - * Runs a test suite and writes the test log to the specified stream.
   1.158 - * @param suite the test suite to run
   1.159 - * @param outstream the stream the log shall be written to
   1.160 - */
   1.161 -void ucx_test_run(UcxTestSuite* suite, FILE* outstream);
   1.162 -
   1.163 -/**
   1.164 - * Macro for a #UcxTest function header.
   1.165 - * 
   1.166 - * Use this macro to declare and/or define a #UcxTest function.
   1.167 - * 
   1.168 - * @param name the name of the test function
   1.169 - */
   1.170 -#define UCX_TEST(name) void name(UcxTestSuite* _suite_,FILE *_output_)
   1.171 -
   1.172 -/**
   1.173 - * Marks the begin of a test.
   1.174 - * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>after</b>
   1.175 - * #UCX_TEST_BEGIN.
   1.176 - * 
   1.177 - * @see #UCX_TEST_END
   1.178 - */
   1.179 -#define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\
   1.180 -        fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\
   1.181 -        fwrite("... ", 1, 4, _output_);\
   1.182 -        jmp_buf _env_; \
   1.183 -        if (!setjmp(_env_)) {
   1.184 -
   1.185 -/**
   1.186 - * Checks a test assertion.
   1.187 - * If the assertion is correct, the test carries on. If the assertion is not
   1.188 - * correct, the specified message (terminated by a dot and a line break) is
   1.189 - * written to the test suites output stream.
   1.190 - * @param condition the condition to check
   1.191 - * @param message the message that shall be printed out on failure
   1.192 - */
   1.193 -#define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \
   1.194 -        fwrite(message".\n", 1, 2+strlen(message), _output_); \
   1.195 -        _suite_->failure++; \
   1.196 -        longjmp(_env_, 1);\
   1.197 -    }
   1.198 -
   1.199 -/**
   1.200 - * Macro for a test subroutine function header.
   1.201 - * 
   1.202 - * Use this to declare and/or define a subroutine that can be called by using
   1.203 - * UCX_TEST_CALL_SUBROUTINE().
   1.204 - * 
   1.205 - * @param name the name of the subroutine
   1.206 - * @param ... the parameter list
   1.207 - * 
   1.208 - * @see UCX_TEST_CALL_SUBROUTINE()
   1.209 - */
   1.210 -#define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\
   1.211 -        FILE *_output_, jmp_buf _env_, __VA_ARGS__)
   1.212 -
   1.213 -/**
   1.214 - * Macro for calling a test subroutine.
   1.215 - * 
   1.216 - * Subroutines declared with UCX_TEST_SUBROUTINE() can be called by using this
   1.217 - * macro.
   1.218 - * 
   1.219 - * <b>Note:</b> You may <b>only</b> call subroutines within a #UCX_TEST_BEGIN-
   1.220 - * #UCX_TEST_END-block.
   1.221 - * 
   1.222 - * @param name the name of the subroutine
   1.223 - * @param ... the argument list
   1.224 - * 
   1.225 - * @see UCX_TEST_SUBROUTINE()
   1.226 - */
   1.227 -#define UCX_TEST_CALL_SUBROUTINE(name,...) \
   1.228 -        name(_suite_,_output_,_env_,__VA_ARGS__);
   1.229 -
   1.230 -/**
   1.231 - * Marks the end of a test.
   1.232 - * <b>Note:</b> Any UCX_TEST_ASSERT() calls must be performed <b>before</b>
   1.233 - * #UCX_TEST_END.
   1.234 - * 
   1.235 - * @see #UCX_TEST_BEGIN
   1.236 - */
   1.237 -#define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;}
   1.238 -
   1.239 -#ifdef	__cplusplus
   1.240 -}
   1.241 -#endif
   1.242 -
   1.243 -#endif	/* UCX_TEST_H */
   1.244 -

mercurial