Skip to content

Commit

Permalink
test: update tester
Browse files Browse the repository at this point in the history
  • Loading branch information
zpl-zak committed Feb 21, 2021
1 parent b192c0c commit 6c0b0b6
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 38 deletions.
8 changes: 4 additions & 4 deletions code/tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
int main() {
UNIT_CREATE("librg");

UNIT_CASE(general);
UNIT_CASE(entity);
UNIT_CASE(query);
UNIT_CASE(packing);
UNIT_MODULE(general);
UNIT_MODULE(entity);
UNIT_MODULE(query);
UNIT_MODULE(packing);

return UNIT_RUN();
}
190 changes: 156 additions & 34 deletions code/tests/unit.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,119 @@
static int _g_modules = 0;
static int _g_modules_err = 0;
static int _g_total = 0;
static int _g_errors = 0;
/**
ZPL - Unit testing framework
Usage:
#include "unit.h" in EXACTLY one source file, usually the one containing your testing app's entry point.
There really is no need to include this file multiple times within a project, unless you wish to run
multiple tests within a single executable or split test cases to multiple compilation units, in such case
define UNIT_STATIC to ensure the library won't leak symbols outside compilation units.
and cover your beautiful code already!
GitHub:
https://github.com/zpl-c/tester
Version History:
1.0.1 - Small tweaks
1.0.0 - Where it all started... (not really)
License:
This Software is dual licensed under the following licenses:
Unlicense
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
BSD 3-Clause
Copyright (c) 2016-2021 Dominik Madarász. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/* Adjust it to your needs, preferably to the number of modules you wish to test against. */
#ifndef UNIT_MAX_MODULES
#define UNIT_MAX_MODULES 256
#endif

#ifndef UNIT_SKIP_MAGIC
#define UNIT_SKIP_MAGIC 0xFF
#endif

#ifndef UNIT_JOIN2
#define UNIT_JOIN2(a,b) a##b
#endif

/* Isolates test results within compilation units, this allows for running
multiple test suites per single binary. */
#ifdef UNIT_STATIC
#define UNIT_DEF static
#else
#define UNIT_DEF
#endif

#include <string.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>

#define MODULE(name, scope) \
int ZPL_JOIN2(module__,name)() { \
zpl_printf("--------------------------------------\n"); \
zpl_printf(" module: %s\n", #name); \
zpl_printf("--------------------------------------\n"); \
int32_t UNIT_JOIN2(module__,name)() { \
printf("--------------------------------------\n"); \
printf(" module: %s\n", #name); \
printf("--------------------------------------\n"); \
fflush(stdout); \
_g_modules++; \
int _total = 0; \
int _errors = 0; \
int _lasterr = 0; \
int32_t _total = 0; \
int32_t _errors = 0; \
int32_t _lasterr = 0; \
char *_errstr = 0; \
scope; \
fflush(stdout); \
zpl_printf("\n results: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _total, _errors>0?"\x1B[31m":"", _errors, _errors==0?"\x1B[32m":"", _total - _errors); \
printf("\n results: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _total, _errors>0?"\x1B[31m":"", _errors, _errors==0?"\x1B[32m":"", _total - _errors); \
_g_total += _total; \
_g_errors += _errors; \
if (_errors) _g_modules_err++; \
Expand All @@ -28,19 +125,25 @@ static int _g_errors = 0;
_errstr = ""; \
_total += 1; \
do scope while(0); \
_errors += _lasterr; \
zpl_printf(" * [%s]: It %s %s\n", (_lasterr) ? "\x1B[31mFAIL\x1B[0m" : "\x1B[32mPASS\x1B[0m", desc, _errstr);
if (_lasterr != UNIT_SKIP_MAGIC) _errors += _lasterr; \
printf(" * [%s]: It %s %s\n", (_lasterr == UNIT_SKIP_MAGIC) ? "\x1B[33mSKIP\x1B[0m" : (_lasterr) ? "\x1B[31mFAIL\x1B[0m" : "\x1B[32mPASS\x1B[0m", desc, _errstr);

#define FAIL(a, b) { _errstr = zpl_bprintf("\n\n\tassert: \x1B[31m%s:%d %s %s:%d\x1B[0m\n\tat %s:%d\n", #a, (int)((intptr_t)a), (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define STRFAIL(a, b) { _errstr = zpl_bprintf("\n\n\tassert: \x1B[31m%s:%s %s %s:%s\x1B[0m\n\tat %s:%d\n", #a, (char *)a, (!strcmp(a,b))?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
/* TEST CHECKS */
#define FAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%lld %s %s:%lld\x1B[0m\n\tat %s:%d\n", #a, a, (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define UFAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%llu %s %s:%llu\x1B[0m\n\tat %s:%d\n", #a, a, (a == b)?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define STRFAIL(a, b) { _errstr = unit__bprintf("\n\n\tassert: \x1B[31m%s:%s %s %s:%s\x1B[0m\n\tat %s:%d\n", #a, (char *)a, (!strcmp(a,b))?"==":"!=", #b, b, __FILE__, __LINE__); _lasterr = 1; break; }
#define EQUALS(a, b) if (a != b) { FAIL(a, b); }
#define UEQUALS(a, b) if (a != b) { UFAIL(a, b); }
#define STREQUALS(a, b) if (!!strcmp(a,b)) { STRFAIL(a, b); }
#define STRCEQUALS(a, b, c) if (!!strncmp(a,b, c)) { STRFAIL(a, b); }
#define STRCNEQUALS(a, b, c) if (!strncmp(a,b, c)) { STRFAIL(a, b); }
#define STRNEQUALS(a, b) if (!strcmp(a,b)) { STRFAIL(a, b); }
#define NEQUALS(a, b) if (a == b) { FAIL(a, b); }
#define LESSER(a, b) if (a >= b) { FAIL(a, b); }
#define GREATER(a, b) if (a <=b) { FAIL(a, b); }
#define LESSEREQ(a, b) if (a < b) { FAIL(a, b); }
#define GREATEREQ(a, b) if (a > b) { FAIL(a, b); }
#define SKIP() { _lasterr = UNIT_SKIP_MAGIC; break; }

#if defined(__GCC__) || defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
Expand All @@ -54,6 +157,7 @@ static int _g_errors = 0;
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wmissing-braces"
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif

#if defined(_MSC_VER)
Expand All @@ -62,40 +166,58 @@ static int _g_errors = 0;
#pragma warning(disable : 4127) // Conditional expression is constant
#endif

typedef int (*unit_case)();
typedef int32_t (*unit_case)();

#define UNIT_CREATE(name) \
const char *unit_name = name; \
unit_case unit_cases[256] = {0}; \
int unit_count = 0;
unit_case unit_modules[UNIT_MAX_MODULES] = {0}; \
int32_t unit_count = 0;

#define UNIT_CASE(name) \
unit_cases[unit_count++] = ZPL_JOIN2(module__,name)
#define UNIT_MODULE(name) \
unit_modules[unit_count++] = UNIT_JOIN2(module__,name)

#define UNIT_RUN() \
unit_main(unit_name, unit_cases, unit_count)
unit_main(unit_name, unit_modules, unit_count)

/* INTERNALS */

int unit_main(const char *name, unit_case *cases, int count) {
int err = 0, cnt = count;
zpl_printf("> testing suite:\n\n");
zpl_printf(" * library: %s\n", name);
zpl_printf(" * modules: %d\n", cnt);
zpl_printf("\n", cnt);
UNIT_DEF int32_t _g_modules = 0;
UNIT_DEF int32_t _g_modules_err = 0;
UNIT_DEF int32_t _g_total = 0;
UNIT_DEF int32_t _g_errors = 0;

for (int i = 0; i < count; ++i) {
UNIT_DEF int32_t unit_main(const char *name, unit_case *cases, int32_t count) {
int32_t err = 0, cnt = count;
printf("> testing suite:\n\n");
printf(" * suite: %s\n", name);
printf(" * modules: %d\n", cnt);
printf("\n");

for (int32_t i = 0; i < count; ++i) {
err += cases[i]();
}

fflush(stdout);
zpl_printf("--------------------------------------\n"); \
zpl_printf("> total:\n\n");
zpl_printf(" * modules: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_modules, _g_modules_err>0?"\x1B[31m":"" ,_g_modules_err, _g_modules_err==0?"\x1B[32m":"", _g_modules - _g_modules_err);
zpl_printf(" * tests: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_total, _g_errors>0?"\x1B[31m":"" ,_g_errors, _g_errors==0?"\x1B[32m":"", _g_total - _g_errors);
zpl_printf("\n", cnt);
printf("--------------------------------------\n"); \
printf("> total:\n\n");
printf(" * modules: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_modules, _g_modules_err>0?"\x1B[31m":"" ,_g_modules_err, _g_modules_err==0?"\x1B[32m":"", _g_modules - _g_modules_err);
printf(" * tests: %d total, %s%d failed\x1B[0m, %s%d passed\x1B[0m\n", _g_total, _g_errors>0?"\x1B[31m":"" ,_g_errors, _g_errors==0?"\x1B[32m":"", _g_total - _g_errors);
printf("\n");

return -err;
}

// Locally persisting buffer
static inline char* unit__bprintf(const char* fmt, ...)
{
static char buf[128];
va_list args;
va_start(args, fmt);
vsprintf(buf, fmt, args);
va_end(args);
return buf;
}

#if defined(ZPL_COMPILER_MSVC)
#pragma warning(pop)
#endif
Expand Down

0 comments on commit 6c0b0b6

Please sign in to comment.