universe@103: /* universe@103: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. universe@26: * universe@103: * Copyright 2013 Olaf Wintermann. All rights reserved. universe@33: * universe@103: * Redistribution and use in source and binary forms, with or without universe@103: * modification, are permitted provided that the following conditions are met: universe@33: * universe@103: * 1. Redistributions of source code must retain the above copyright universe@103: * notice, this list of conditions and the following disclaimer. universe@103: * universe@103: * 2. Redistributions in binary form must reproduce the above copyright universe@103: * notice, this list of conditions and the following disclaimer in the universe@103: * documentation and/or other materials provided with the distribution. universe@103: * universe@103: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" universe@103: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE universe@103: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE universe@103: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE universe@103: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR universe@103: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF universe@103: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS universe@103: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN universe@103: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) universe@103: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE universe@103: * POSSIBILITY OF SUCH DAMAGE. universe@103: */ universe@103: universe@103: /* universe@33: * universe@33: * Usage of this test framework: universe@33: * universe@33: * **** IN HEADER FILE: **** universe@33: * universe@33: * UCX_TEST_DECLARE(function_name) universe@33: * universe@33: * **** IN SOURCE FILE: **** universe@33: * universe@33: * UCX_TEST_IMPLEMENT(function_name) { universe@33: * universe@33: * UCX_TEST_BEGIN universe@33: * universe@33: * UCX_TEST_END universe@33: * universe@33: * } universe@33: * universe@33: * PLEASE NOTE: if a test fails, a longjump is performed universe@33: * back to the UCX_TEST_BEGIN macro! universe@33: * universe@33: * You may use multiple BEGIN-END blocks if you are aware of the universe@33: * longjmp behaviour. universe@33: * universe@26: */ universe@26: olaf@120: #ifndef UCX_TEST_H olaf@120: #define UCX_TEST_H universe@26: universe@69: #include "ucx.h" universe@26: #include universe@26: #include universe@33: #include universe@26: universe@26: #ifdef __cplusplus universe@26: extern "C" { universe@26: #endif universe@26: universe@69: #ifndef __FUNCTION__ universe@69: #define __FUNCTION__ __func__ universe@69: #endif universe@69: universe@83: typedef struct UcxTestList UcxTestList; universe@83: typedef struct UcxTestSuite UcxTestSuite; universe@83: typedef void(*UcxTest)(UcxTestSuite*,FILE*); universe@83: universe@83: struct UcxTestList{ universe@83: UcxTest test; universe@83: UcxTestList *next; universe@83: }; universe@83: universe@83: struct UcxTestSuite { universe@26: unsigned int success; universe@26: unsigned int failure; universe@83: UcxTestList *tests; universe@83: }; universe@26: universe@26: UcxTestSuite* ucx_test_suite_new(); universe@26: void ucx_test_suite_free(UcxTestSuite*); universe@26: universe@83: int ucx_test_register(UcxTestSuite*, UcxTest); universe@26: void ucx_test_run(UcxTestSuite*, FILE*); universe@26: olaf@70: #define UCX_TEST_DECLARE(name) void name(UcxTestSuite*,FILE *) universe@33: #define UCX_TEST_IMPLEMENT(name) void name(UcxTestSuite* _suite_,FILE *_output_) universe@33: universe@33: #define UCX_TEST_BEGIN fwrite("Running ", 1, 8, _output_);\ universe@69: fwrite(__FUNCTION__, 1, strlen(__FUNCTION__), _output_);\ universe@33: fwrite("... ", 1, 4, _output_);\ universe@33: jmp_buf _env_; \ universe@33: if (!setjmp(_env_)) { universe@26: universe@26: #define UCX_TEST_ASSERT(condition,message) if (!(condition)) { \ universe@26: fwrite(message".\n", 1, 2+strlen(message), _output_); \ universe@26: _suite_->failure++; \ universe@33: longjmp(_env_, 1);\ universe@26: } universe@26: universe@88: #define UCX_TEST_SUBROUTINE(name,...) void name(UcxTestSuite* _suite_,\ universe@88: FILE *_output_, jmp_buf _env_, __VA_ARGS__) universe@88: #define UCX_TEST_CALL_SUBROUTINE(name,...) \ universe@88: name(_suite_,_output_,_env_,__VA_ARGS__); universe@33: universe@33: #define UCX_TEST_END fwrite("success.\n", 1, 9, _output_); _suite_->success++;} universe@26: universe@26: #ifdef __cplusplus universe@26: } universe@26: #endif universe@26: olaf@120: #endif /* UCX_TEST_H */ universe@26: