summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrey Nazarov <skuller@skuller.net>2011-03-08 21:59:56 +0300
committerAndrey Nazarov <skuller@skuller.net>2011-03-08 22:00:17 +0300
commit54512a91c39165e44b775e2f8dc183801056cec0 (patch)
tree72bd8e5ec68d1f564f965437490f8221c26bcb58
parentccbfc0b09375a932aa64aea706d059b115328da9 (diff)
Move all testing code into single file.
Add wildcard pattern testing code and NX support testing code.
-rw-r--r--build/common.mk4
-rw-r--r--src/bsp.c45
-rw-r--r--src/common.c57
-rw-r--r--src/common.h4
-rw-r--r--src/tests.c183
5 files changed, 194 insertions, 99 deletions
diff --git a/build/common.mk b/build/common.mk
index cb30fcc..4b1503c 100644
--- a/build/common.mk
+++ b/build/common.mk
@@ -34,6 +34,10 @@ SRCFILES+=cmd.c \
error.c \
fpu.c
+ifdef USE_TESTS
+SRCFILES+=tests.c
+endif
+
ifdef USE_ZLIB
LDFLAGS+=$(ZLIB_LDFLAGS)
CFLAGS+=$(ZLIB_CFLAGS)
diff --git a/src/bsp.c b/src/bsp.c
index 0db6b4e..548dc61 100644
--- a/src/bsp.c
+++ b/src/bsp.c
@@ -1243,55 +1243,10 @@ mmodel_t *BSP_InlineModel( bsp_t *bsp, const char *name ) {
return &bsp->models[num];
}
-//#define BSP_TEST
-
-#ifdef BSP_TEST
-static void BSP_Test_f( void ) {
- void **list;
- char *name;
- int i, count, errors;
- bsp_t *bsp;
- qerror_t ret;
- unsigned start, end;
-
- list = FS_ListFiles( "maps", ".bsp", FS_SEARCH_SAVEPATH, &count );
- if( !list ) {
- Com_Printf( "No maps found\n" );
- return;
- }
-
- start = Sys_Milliseconds();
-
- errors = 0;
- for( i = 0; i < count; i++ ) {
- name = list[i];
- ret = BSP_Load( name, &bsp );
- if( !bsp ) {
- Com_EPrintf( "%s: %s\n", name, Q_ErrorString( ret ) );
- errors++;
- continue;
- }
-
- Com_DPrintf( "%s: success\n", name );
- BSP_Free( bsp );
- }
-
- end = Sys_Milliseconds();
-
- Com_Printf( "%d msec, %d failures, %d maps tested\n",
- end - start, errors, count );
-
- FS_FreeList( list );
-}
-#endif
-
void BSP_Init( void ) {
map_visibility_patch = Cvar_Get( "map_visibility_patch", "1", 0 );
Cmd_AddCommand( "bsplist", BSP_List_f );
-#ifdef BSP_TEST
- Cmd_AddCommand( "bsptest", BSP_Test_f );
-#endif
List_Init( &bsp_cache );
}
diff --git a/src/common.c b/src/common.c
index 85e77d3..e979a2b 100644
--- a/src/common.c
+++ b/src/common.c
@@ -40,9 +40,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "io_sleep.h"
#include "fpu.h"
#include <setjmp.h>
-#if USE_ZLIB
-#include <zlib.h>
-#endif
static jmp_buf abortframe; // an ERR_DROP occured, exit the entire frame
@@ -1663,50 +1660,6 @@ static void Com_Setenv_f( void ) {
}
#endif
-#ifdef _DEBUG
-
-/*
-=============
-Com_Error_f
-
-Just throw a fatal error to
-test error shutdown procedures
-=============
-*/
-static void Com_Error_f( void ) {
- Com_Error( ERR_FATAL, "%s", Cmd_Argv( 1 ) );
-}
-
-static void Com_ErrorDrop_f( void ) {
- Com_Error( ERR_DROP, "%s", Cmd_Argv( 1 ) );
-}
-
-static void Com_Freeze_f( void ) {
- unsigned time, msec;
- float seconds;
-
- if( Cmd_Argc() < 2 ) {
- Com_Printf( "Usage: %s <seconds>\n", Cmd_Argv( 0 ) );
- return;
- }
-
- seconds = atof( Cmd_Argv( 1 ) );
- if( seconds < 0 ) {
- return;
- }
-
- time = Sys_Milliseconds();
- msec = seconds * 1000;
- while( Sys_Milliseconds() - time < msec )
- ;
-}
-
-static void Com_Crash_f( void ) {
- *( uint32_t * )0 = 0x123456;
-}
-
-#endif
-
void Com_Address_g( genctx_t *ctx ) {
int i;
cvar_t *var;
@@ -1989,13 +1942,6 @@ void Qcommon_Init( int argc, char **argv ) {
Com_AddEarlyCommands( qtrue );
-#ifdef _DEBUG
- Cmd_AddCommand( "error", Com_Error_f );
- Cmd_AddCommand( "errordrop", Com_ErrorDrop_f );
- Cmd_AddCommand( "freeze", Com_Freeze_f );
- Cmd_AddCommand( "crash", Com_Crash_f );
-#endif
-
Cmd_AddCommand( "lasterror", Com_LastError_f );
Cmd_AddCommand( "quit", Com_Quit_f );
@@ -2013,6 +1959,9 @@ void Qcommon_Init( int argc, char **argv ) {
#if USE_CLIENT
CL_Init();
#endif
+#if USE_TESTS
+ Com_InitTests();
+#endif
#if USE_SYSCON
Sys_RunConsole();
diff --git a/src/common.h b/src/common.h
index de1c78c..980d85f 100644
--- a/src/common.h
+++ b/src/common.h
@@ -576,6 +576,10 @@ void Com_FlushLogs( void );
#define Com_DDDPrintf(...)
#endif
+#if USE_TESTS
+void Com_InitTests( void );
+#endif
+
#ifdef _DEBUG
extern cvar_t *developer;
#endif
diff --git a/src/tests.c b/src/tests.c
new file mode 100644
index 0000000..ea5ba31
--- /dev/null
+++ b/src/tests.c
@@ -0,0 +1,183 @@
+/*
+Copyright (C) 1997-2001 Id Software, Inc.
+
+This program is free software; you can redistribute it and/or
+modify it under the terms of the GNU General Public License
+as published by the Free Software Foundation; either version 2
+of the License, or (at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+
+See the GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+*/
+
+#include "common.h"
+#include "files.h"
+#include "sys_public.h"
+#include "q_list.h"
+#include "bsp.h"
+
+// test error shutdown procedures
+static void Com_Error_f( void ) {
+ Com_Error( ERR_FATAL, "%s", Cmd_Argv( 1 ) );
+}
+
+static void Com_ErrorDrop_f( void ) {
+ Com_Error( ERR_DROP, "%s", Cmd_Argv( 1 ) );
+}
+
+static void Com_Freeze_f( void ) {
+ unsigned time, msec;
+ float seconds;
+
+ if( Cmd_Argc() < 2 ) {
+ Com_Printf( "Usage: %s <seconds>\n", Cmd_Argv( 0 ) );
+ return;
+ }
+
+ seconds = atof( Cmd_Argv( 1 ) );
+ if( seconds < 0 ) {
+ return;
+ }
+
+ time = Sys_Milliseconds();
+ msec = seconds * 1000;
+ while( Sys_Milliseconds() - time < msec )
+ ;
+}
+
+// test crash dumps and NX support
+static void Com_Crash_f( void ) {
+ static byte buf1[16];
+ byte buf2[16], *buf3;
+ int i = atoi( Cmd_Argv( 1 ) );
+
+ switch( i ) {
+ case 1:
+ // data
+ memset( buf1, 0xcc, 16 );
+ buf1[0] = 0xc3;
+ ((void (*)(void))buf1)();
+ break;
+ case 2:
+ // stack
+ memset( buf2, 0xcc, 16 );
+ buf2[0] = 0xc3;
+ ((void (*)(void))buf2)();
+ break;
+ case 3:
+ // heap
+ buf3 = Z_Malloc( 16 );
+ memset( buf3, 0xcc, 16 );
+ buf3[0] = 0xc3;
+ ((void (*)(void))buf3)();
+ Z_Free( buf3 );
+ break;
+ default:
+ *( uint32_t * )1024 = 0x123456;
+ break;
+ }
+}
+
+static void BSP_Test_f( void ) {
+ void **list;
+ char *name;
+ int i, count, errors;
+ bsp_t *bsp;
+ qerror_t ret;
+ unsigned start, end;
+
+ list = FS_ListFiles( "maps", ".bsp", FS_SEARCH_SAVEPATH, &count );
+ if( !list ) {
+ Com_Printf( "No maps found\n" );
+ return;
+ }
+
+ start = Sys_Milliseconds();
+
+ errors = 0;
+ for( i = 0; i < count; i++ ) {
+ name = list[i];
+ ret = BSP_Load( name, &bsp );
+ if( !bsp ) {
+ Com_EPrintf( "%s: %s\n", name, Q_ErrorString( ret ) );
+ errors++;
+ continue;
+ }
+
+ Com_DPrintf( "%s: success\n", name );
+ BSP_Free( bsp );
+ }
+
+ end = Sys_Milliseconds();
+
+ Com_Printf( "%d msec, %d failures, %d maps tested\n",
+ end - start, errors, count );
+
+ FS_FreeList( list );
+}
+
+typedef struct {
+ const char *filter;
+ const char *string;
+ int result;
+} wildtest_t;
+
+static const wildtest_t wildtests[] = {
+ { "", "", 1 },
+ { "*", "foo", 1 },
+ { "***", "foo", 1 },
+ { "foo*bar", "foobar", 1 },
+ { "foo*bar*baz", "foobaz", 0 },
+ { "bar*", "bar", 1 },
+ { "bar*", "barfoo", 1 },
+ { "???*", "barfoo", 1 },
+ { "???\\*", "bar*", 1 },
+ { "???\\*", "bar*bar", 0 },
+ { "???\\*", "bar?", 0 },
+ { "*\\?", "bar?", 1 },
+ { "\\\\", "\\", 1 },
+ { "\\", "\\", 0 },
+ { "foo*bar\\*baz", "foo*abcbar*baz", 1 },
+ { "\\a\\b\\c", "abc", 1 },
+};
+
+static const int numwildtests = sizeof( wildtests ) / sizeof( wildtests[0] );
+
+static void Com_TestWild_f( void ) {
+ const wildtest_t *w;
+ qboolean match;
+ int i, errors;
+
+ errors = 0;
+ for( i = 0; i < numwildtests; i++ ) {
+ w = &wildtests[i];
+ match = Com_WildCmp( w->filter, w->string );
+ if( match != w->result ) {
+ Com_EPrintf(
+ "Com_WildCmp( \"%s\", \"%s\" ) == %d, expected %d\n",
+ w->filter, w->string, match, w->result );
+ errors++;
+ }
+ }
+
+ Com_Printf( "%d failures, %d patterns tested\n",
+ errors, numwildtests );
+}
+
+void Com_InitTests( void ) {
+ Cmd_AddCommand( "error", Com_Error_f );
+ Cmd_AddCommand( "errordrop", Com_ErrorDrop_f );
+ Cmd_AddCommand( "freeze", Com_Freeze_f );
+ Cmd_AddCommand( "crash", Com_Crash_f );
+ Cmd_AddCommand( "bsptest", BSP_Test_f );
+ Cmd_AddCommand( "wildtest", Com_TestWild_f );
+}
+