From 6ea25b819435cc85a8a060f7dd1cba4d29b9cdcf Mon Sep 17 00:00:00 2001 From: Alibek Omarov Date: Sun, 20 Jun 2021 19:55:31 +0300 Subject: [PATCH] engine: add simple unit-testing (v3?) --- engine/common/host.c | 32 +++++++++++++++++++++++++++++++- engine/common/tests.h | 28 ++++++++++++++++++++++++++++ engine/wscript | 14 ++++++++++++-- 3 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 engine/common/tests.h diff --git a/engine/common/host.c b/engine/common/host.c index 2465bbb0..12ffa724 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -40,12 +40,16 @@ GNU General Public License for more details. #include "input.h" #include "enginefeatures.h" #include "render_api.h" // decallist_t - +#include "tests.h" pfnChangeGame pChangeGame = NULL; host_parm_t host; // host parms sysinfo_t SI; +#ifdef XASH_ENGINE_TESTS +struct tests_stats_s tests_stats; +#endif + CVAR_DEFINE( host_developer, "developer", "0", 0, "engine is in development-mode" ); CVAR_DEFINE_AUTO( sys_ticrate, "100", 0, "framerate in dedicated mode" ); @@ -771,6 +775,19 @@ void Host_Userconfigd_f( void ) Mem_Free( t ); } +#if XASH_ENGINE_TESTS +static void Host_RunTests( void ) +{ + memset( &tests_stats, 0, sizeof( tests_stats )); + + Test_RunLibCommon(); + + Msg( "Done! %d passed, %d failed\n", tests_stats.passed, tests_stats.failed ); + + Sys_Quit(); +} +#endif + /* ================= Host_InitCommon @@ -827,6 +844,14 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha } } +#if XASH_ENGINE_TESTS + if( Sys_CheckParm( "-runtests" )) + { + host.allow_console = true; + developer = DEV_EXTENDED; + } +#endif + host.con_showalways = true; #if XASH_DEDICATED @@ -896,6 +921,11 @@ void Host_InitCommon( int argc, char **argv, const char *progname, qboolean bCha Con_Init(); // early console running to catch all the messages +#if XASH_ENGINE_TESTS + if( Sys_CheckParm( "-runtests" )) + Host_RunTests(); +#endif + Platform_Init(); baseDir = getenv( "XASH3D_BASEDIR" ); diff --git a/engine/common/tests.h b/engine/common/tests.h new file mode 100644 index 00000000..6eff238d --- /dev/null +++ b/engine/common/tests.h @@ -0,0 +1,28 @@ +#ifndef TESTS_H +#define TESTS_H + +#if XASH_ENGINE_TESTS + +struct tests_stats_s +{ + uint passed; + uint failed; +}; + +extern struct tests_stats_s tests_stats; + +#define TRUN( x ) Msg( "Running " #x "\n" ); x + +#define TASSERT( exp ) \ + if(!( exp )) \ + { \ + tests_stats.failed++; \ + Msg( "assert failed at %s:%i\n", __FILE__, __LINE__ ) \ + } \ + else tests_stats.passed++; + +void Test_RunLibCommon( void ); + +#endif + +#endif /* TESTS_H */ diff --git a/engine/wscript b/engine/wscript index 38022b5a..1e080128 100644 --- a/engine/wscript +++ b/engine/wscript @@ -29,6 +29,9 @@ def options(opt): grp.add_option('--enable-static-binary', action = 'store_true', dest = 'STATIC', default = False, help = 'build static binary(not recommended, --single-binary required) [default: %default]') + grp.add_option('--enable-engine-tests', action = 'store_true', dest = 'ENGINE_TESTS', default = False, + help = 'embed tests into the engine, jump into them by -runtests command line switch [default: %default]') + opt.load('sdl2') def configure(conf): @@ -79,6 +82,9 @@ def configure(conf): if hasattr(conf.options, 'DLLEMU'): conf.define_cond('XASH_DLL_LOADER', conf.options.DLLEMU) + conf.env.ENGINE_TESTS = conf.options.ENGINE_TESTS + + conf.define_cond('XASH_ENGINE_TESTS', conf.env.ENGINE_TESTS) conf.define_cond('XASH_STATIC_LIBS', conf.env.STATIC_LINKING) conf.define_cond('XASH_CUSTOM_SWAP', conf.options.CUSTOM_SWAP) conf.define_cond('SINGLE_BINARY', conf.env.SINGLE_BINARY) @@ -92,6 +98,8 @@ def configure(conf): def build(bld): is_cxx_link = False libs = [ 'public', 'dllemu' ] + + # basic build: dedicated only source = bld.path.ant_glob([ 'common/*.c', 'common/imagelib/*.c', @@ -99,7 +107,9 @@ def build(bld): 'common/soundlib/libmpg/*.c', 'server/*.c']) - # basic build: dedicated only, no dependencies + if bld.env.ENGINE_TESTS: + source += bld.path.ant_glob(['tests/*.c']) + if bld.env.DEST_OS == 'win32': libs += ['USER32', 'SHELL32', 'GDI32', 'ADVAPI32', 'DBGHELP', 'PSAPI', 'WS2_32' ] source += bld.path.ant_glob(['platform/win32/*.c']) @@ -146,7 +156,7 @@ def build(bld): 'client/vgui/*.c', 'client/avi/*.c']) - includes = ['common', 'server', 'client', 'client/vgui', '.', '../public', '../common', '../pm_shared' ] + includes = ['common', 'server', 'client', 'client/vgui', 'tests', '.', '../public', '../common', '../pm_shared' ] if bld.env.SINGLE_BINARY: install_path = bld.env.BINDIR