ucx/test.h

Sat, 20 Jul 2013 11:13:26 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 20 Jul 2013 11:13:26 +0200
changeset 120
8170f658f017
parent 103
08018864fb91
child 128
b79b1ce438dd
permissions
-rw-r--r--

some fixes

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

mercurial