Quake II RTX doxygen  1.0 dev
tests.c File Reference
#include "shared/shared.h"
#include "common/bsp.h"
#include "common/cmd.h"
#include "common/common.h"
#include "common/files.h"
#include "common/tests.h"
#include "refresh/refresh.h"
#include "system/system.h"

Go to the source code of this file.

Classes

struct  wildtest_t
 
struct  normtest_t
 
struct  info_validate_test_t
 
struct  info_remove_test_t
 
struct  info_set_test_t
 
struct  snprintf_test_t
 

Macros

#define OV   SIZE_MAX
 

Functions

static void Com_Error_f (void)
 
static void Com_ErrorDrop_f (void)
 
static void Com_Freeze_f (void)
 
static void Com_Crash_f (void)
 
static void Com_PrintJunk_f (void)
 
static void BSP_Test_f (void)
 
static void Com_TestWild_f (void)
 
static void Com_TestNorm_f (void)
 
static void Com_TestInfo_f (void)
 
static void Com_TestSnprintf_f (void)
 
void TST_Init (void)
 

Variables

static const wildtest_t wildtests []
 
static const int numwildtests = q_countof(wildtests)
 
static const normtest_t normtests []
 
static const int numnormtests = q_countof(normtests)
 
static const info_validate_test_t info_validate_tests []
 
static const int num_info_validate_tests = q_countof(info_validate_tests)
 
static const info_remove_test_t info_remove_tests []
 
static const int num_info_remove_tests = q_countof(info_remove_tests)
 
static const info_set_test_t info_set_tests []
 
static const int num_info_set_tests = q_countof(info_set_tests)
 
static const snprintf_test_t snprintf_tests []
 
static const int num_snprintf_tests = q_countof(snprintf_tests)
 

Macro Definition Documentation

◆ OV

#define OV   SIZE_MAX

Definition at line 445 of file tests.c.

Function Documentation

◆ BSP_Test_f()

static void BSP_Test_f ( void  )
static

Definition at line 119 of file tests.c.

120 {
121  void **list;
122  char *name;
123  int i, count, errors;
124  bsp_t *bsp;
125  qerror_t ret;
126  unsigned start, end;
127 
128  list = FS_ListFiles("maps", ".bsp", FS_SEARCH_SAVEPATH, &count);
129  if (!list) {
130  Com_Printf("No maps found\n");
131  return;
132  }
133 
134  start = Sys_Milliseconds();
135 
136  errors = 0;
137  for (i = 0; i < count; i++) {
138  name = list[i];
139  ret = BSP_Load(name, &bsp);
140  if (!bsp) {
141  Com_EPrintf("%s: %s\n", name, Q_ErrorString(ret));
142  errors++;
143  continue;
144  }
145 
146  Com_DPrintf("%s: success\n", name);
147  BSP_Free(bsp);
148  }
149 
150  end = Sys_Milliseconds();
151 
152  Com_Printf("%d msec, %d failures, %d maps tested\n",
153  end - start, errors, count);
154 
155  FS_FreeList(list);
156 }

Referenced by TST_Init().

◆ Com_Crash_f()

static void Com_Crash_f ( void  )
static

Definition at line 61 of file tests.c.

62 {
63  static byte buf1[16];
64  byte buf2[16], *buf3;
65  int i = atoi(Cmd_Argv(1));
66 
67  switch (i) {
68  case 1:
69  // data
70  memset(buf1, 0xcc, 16);
71  buf1[0] = 0xc3;
72  ((void (*)(void))buf1)();
73  break;
74  case 2:
75  // stack
76  memset(buf2, 0xcc, 16);
77  buf2[0] = 0xc3;
78  ((void (*)(void))buf2)();
79  break;
80  case 3:
81  // heap
82  buf3 = Z_Malloc(16);
83  memset(buf3, 0xcc, 16);
84  buf3[0] = 0xc3;
85  ((void (*)(void))buf3)();
86  Z_Free(buf3);
87  break;
88  default:
89  *(uint32_t *)1024 = 0x123456;
90  break;
91  }
92 }

Referenced by TST_Init().

◆ Com_Error_f()

static void Com_Error_f ( void  )
static

Definition at line 29 of file tests.c.

30 {
31  Com_Error(ERR_FATAL, "%s", Cmd_Argv(1));
32 }

Referenced by TST_Init().

◆ Com_ErrorDrop_f()

static void Com_ErrorDrop_f ( void  )
static

Definition at line 34 of file tests.c.

35 {
36  Com_Error(ERR_DROP, "%s", Cmd_Argv(1));
37 }

Referenced by TST_Init().

◆ Com_Freeze_f()

static void Com_Freeze_f ( void  )
static

Definition at line 39 of file tests.c.

40 {
41  unsigned time, msec;
42  float seconds;
43 
44  if (Cmd_Argc() < 2) {
45  Com_Printf("Usage: %s <seconds>\n", Cmd_Argv(0));
46  return;
47  }
48 
49  seconds = atof(Cmd_Argv(1));
50  if (seconds < 0) {
51  return;
52  }
53 
54  time = Sys_Milliseconds();
55  msec = seconds * 1000;
56  while (Sys_Milliseconds() - time < msec)
57  ;
58 }

Referenced by TST_Init().

◆ Com_PrintJunk_f()

static void Com_PrintJunk_f ( void  )
static

Definition at line 95 of file tests.c.

96 {
97  char buf[MAXPRINTMSG * 2];
98  int i, count;
99 
100  // generate some junk
101  for (i = 0; i < sizeof(buf) - 1; i++)
102  buf[i] = 'a' + i % ('z' - 'a' + 1);
103  buf[i] = 0;
104 
105  // randomly break into words
106  for (i = 0; i < 64; i++)
107  buf[rand() % (sizeof(buf) - 1)] = ' ';
108 
109  if (Cmd_Argc() > 1)
110  count = atoi(Cmd_Argv(1));
111  else
112  count = 1;
113 
114  for (i = 0; i < count; i++)
115  Com_Printf("%s", buf);
116  Com_Printf("\n");
117 }

Referenced by TST_Init().

◆ Com_TestInfo_f()

static void Com_TestInfo_f ( void  )
static

Definition at line 389 of file tests.c.

390 {
391  const info_validate_test_t *v;
392  const info_remove_test_t *r;
393  const info_set_test_t *s;
394  char buffer[MAX_INFO_STRING];
395  int i, errors;
396  qboolean result;
397 
398  errors = 0;
399  for (i = 0; i < num_info_validate_tests; i++) {
400  v = &info_validate_tests[i];
401  result = Info_Validate(v->string);
402  if (result != v->result) {
403  Com_EPrintf("Info_Validate( \"%s\" ) == %d, expected %d\n",
404  v->string, result, v->result);
405  errors++;
406  }
407  }
408 
409  for (i = 0; i < num_info_remove_tests; i++) {
410  r = &info_remove_tests[i];
411  Q_strlcpy(buffer, r->string, sizeof(buffer));
412  Info_RemoveKey(buffer, r->key);
413  if (strcmp(buffer, r->result)) {
414  Com_EPrintf("Info_RemoveKey( \"%s\", \"%s\" ) == \"%s\", expected \"%s\"\n",
415  r->string, r->key, buffer, r->result);
416  errors++;
417  }
418  }
419 
420  for (i = 0; i < num_info_set_tests; i++) {
421  s = &info_set_tests[i];
422  Q_strlcpy(buffer, s->string, sizeof(buffer));
423  result = Info_SetValueForKey(buffer, s->key, s->value);
424  if (result != s->result_b || strcmp(buffer, s->result_s)) {
425  Com_EPrintf("Info_SetValueForKey( \"%s\", \"%s\", \"%s\" ) == \"%s\" (%d), expected \"%s\" (%d)\n",
426  s->string, s->key, s->value, buffer, result, s->result_s, s->result_b);
427  errors++;
428  }
429  }
430 
431  Com_Printf("%d failures, %d strings tested\n", errors,
435 }

Referenced by TST_Init().

◆ Com_TestNorm_f()

static void Com_TestNorm_f ( void  )
static

Definition at line 260 of file tests.c.

261 {
262  const normtest_t *n;
263  char buffer[MAX_QPATH];
264  int i, errors, pass;
265 
266  for (pass = 0; pass < 2; pass++) {
267  errors = 0;
268  for (i = 0; i < numnormtests; i++) {
269  n = &normtests[i];
270  if (pass == 0) {
271  FS_NormalizePath(buffer, n->in);
272  } else {
273  // test in place operation
274  strcpy(buffer, n->in);
275  FS_NormalizePath(buffer, buffer);
276  }
277  if (strcmp(n->out, buffer)) {
278  Com_EPrintf(
279  "FS_NormalizePath( \"%s\" ) == \"%s\", expected \"%s\" (pass %d)\n",
280  n->in, buffer, n->out, pass);
281  errors++;
282  }
283  }
284  if (errors)
285  break;
286  }
287 
288  Com_Printf("%d failures, %d paths tested (%d passes)\n",
289  errors, numnormtests, pass);
290 }

Referenced by TST_Init().

◆ Com_TestSnprintf_f()

static void Com_TestSnprintf_f ( void  )
static

Definition at line 459 of file tests.c.

460 {
461  const snprintf_test_t *t;
462  char buf[16], *ptr;
463  size_t len;
464  int i, errors;
465  qboolean overflow;
466 
467  errors = 0;
468  for (i = 0; i < num_snprintf_tests; i++) {
469  t = &snprintf_tests[i];
470 
471  ptr = t->size ? buf : NULL;
472 
473  memset(buf, 'x', 15); buf[15] = 0;
474  len = Q_snprintf(ptr, t->size, "hello world");
475  overflow = len >= t->size;
476  if (t->len1 != len || strcmp(buf, t->res) || overflow != t->overflow1) {
477  Com_EPrintf("%s( %p, %"PRIz" ) == \"%s\" (%"PRIz") [%d], expected \"%s\" (%"PRIz") [%d]\n",
478  "Q_snprintf", ptr, t->size, buf, len, overflow, t->res, t->len1, t->overflow1);
479  errors++;
480  }
481 
482  memset(buf, 'x', 15); buf[15] = 0;
483  len = Q_scnprintf(ptr, t->size, "hello world");
484  overflow = len >= t->size;
485  if (t->len2 != len || strcmp(buf, t->res) || overflow != t->overflow2) {
486  Com_EPrintf("%s( %p, %"PRIz" ) == \"%s\" (%"PRIz") [%d], expected \"%s\" (%"PRIz") [%d]\n",
487  "Q_scnprintf", ptr, t->size, buf, len, overflow, t->res, t->len2, t->overflow2);
488  errors++;
489  }
490  }
491 
492  Com_Printf("%d failures, %d strings tested\n", errors, num_snprintf_tests * 2);
493 }

Referenced by TST_Init().

◆ Com_TestWild_f()

static void Com_TestWild_f ( void  )
static

Definition at line 185 of file tests.c.

186 {
187  const wildtest_t *w;
188  qboolean match;
189  int i, errors;
190 
191  errors = 0;
192  for (i = 0; i < numwildtests; i++) {
193  w = &wildtests[i];
194  match = Com_WildCmp(w->filter, w->string);
195  if (match != w->result) {
196  Com_EPrintf(
197  "Com_WildCmp( \"%s\", \"%s\" ) == %d, expected %d\n",
198  w->filter, w->string, match, w->result);
199  errors++;
200  }
201  }
202 
203  Com_Printf("%d failures, %d patterns tested\n",
204  errors, numwildtests);
205 }

Referenced by TST_Init().

◆ TST_Init()

void TST_Init ( void  )

Definition at line 527 of file tests.c.

528 {
529  Cmd_AddCommand("error", Com_Error_f);
530  Cmd_AddCommand("errordrop", Com_ErrorDrop_f);
531  Cmd_AddCommand("freeze", Com_Freeze_f);
532  Cmd_AddCommand("crash", Com_Crash_f);
533  Cmd_AddCommand("printjunk", Com_PrintJunk_f);
534  Cmd_AddCommand("bsptest", BSP_Test_f);
535  Cmd_AddCommand("wildtest", Com_TestWild_f);
536  Cmd_AddCommand("normtest", Com_TestNorm_f);
537  Cmd_AddCommand("infotest", Com_TestInfo_f);
538  Cmd_AddCommand("snprintftest", Com_TestSnprintf_f);
539 #if USE_REF
540  Cmd_AddCommand("modeltest", Com_TestModels_f);
541 #endif
542 }

Referenced by Qcommon_Init().

Variable Documentation

◆ info_remove_tests

const info_remove_test_t info_remove_tests[]
static
Initial value:
= {
{ "", "", "" },
{ "\\key\\value", "key", "" },
{ "key\\value", "key", "" },
{ "\\key1\\value1\\key2\\value2", "key1", "\\key2\\value2" },
{ "\\key1\\value1\\key2\\value2", "key2", "\\key1\\value1" },
{ "\\key1\\value1\\key2\\value2\\key1\\value3", "key1", "\\key2\\value2" },
{ "\\key\\value", "", "\\key\\value" },
{ "\\\\value", "", "" },
}

Definition at line 341 of file tests.c.

Referenced by Com_TestInfo_f().

◆ info_set_tests

const info_set_test_t info_set_tests[]
static
Initial value:
= {
{ "", "", "", 1, "" },
{ "\\key\\value1", "key", "value2", 1, "\\key\\value2" },
{ "\\key\\value1", "key", "", 1, "" },
{ "\\key\\value1", "", "value2", 1, "\\key\\value1\\\\value2" },
{ "\\key\\value1", "ke\"y", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "ke\\y", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "ke;y", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "ke\xa2y", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "ke\xdcy", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "ke\xbby", "value2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val\"ue2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val\\ue2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val;ue2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val\xa2ue2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val\xdcue2", 0, "\\key\\value1" },
{ "\\key\\value1", "key", "val\xbbue2", 0, "\\key\\value1" },
{ "\\key1\\value1\\key2\\value2", "key1", "value3", 1, "\\key2\\value2\\key1\\value3" },
{ "\\key1\\value1\\key2\\value2", "key1", "", 1, "\\key2\\value2" },
{ "\\key1\\value1\\key2\\value2\\key1\\value3", "key1", "value4", 1, "\\key2\\value2\\key1\\value4" },
{ "\\key1\\value1\\key2\\value2", "key1", "v\xe1lue3", 1, "\\key2\\value2\\key1\\value3" },
{ "\\key\\value", "key", "\r\n", 1, "\\key\\" },
{ "\\key1\\value1\\key2\\value2", "key1", "\r\n", 1, "\\key2\\value2\\key1\\" },
}

Definition at line 362 of file tests.c.

Referenced by Com_TestInfo_f().

◆ info_validate_tests

const info_validate_test_t info_validate_tests[]
static

Definition at line 297 of file tests.c.

Referenced by Com_TestInfo_f().

◆ normtests

const normtest_t normtests[]
static

Definition at line 212 of file tests.c.

Referenced by Com_TestNorm_f().

◆ num_info_remove_tests

const int num_info_remove_tests = q_countof(info_remove_tests)
static

Definition at line 352 of file tests.c.

Referenced by Com_TestInfo_f().

◆ num_info_set_tests

const int num_info_set_tests = q_countof(info_set_tests)
static

Definition at line 387 of file tests.c.

Referenced by Com_TestInfo_f().

◆ num_info_validate_tests

const int num_info_validate_tests = q_countof(info_validate_tests)
static

Definition at line 333 of file tests.c.

Referenced by Com_TestInfo_f().

◆ num_snprintf_tests

const int num_snprintf_tests = q_countof(snprintf_tests)
static

Definition at line 457 of file tests.c.

Referenced by Com_TestSnprintf_f().

◆ numnormtests

const int numnormtests = q_countof(normtests)
static

Definition at line 258 of file tests.c.

Referenced by Com_TestNorm_f().

◆ numwildtests

const int numwildtests = q_countof(wildtests)
static

Definition at line 183 of file tests.c.

Referenced by Com_TestWild_f().

◆ snprintf_tests

const snprintf_test_t snprintf_tests[]
static
Initial value:
= {
{ 12, 11, 11, 0, 0, "hello world" },
{ 11, OV, 10, 1, 0, "hello worl" },
{ 10, OV, 9, 1, 0, "hello wor" },
{ 0, 11, 0, 1, 1, "xxxxxxxxxxxxxxx" },
}

Definition at line 450 of file tests.c.

Referenced by Com_TestSnprintf_f().

◆ wildtests

const wildtest_t wildtests[]
static
Initial value:
= {
{ "", "", 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 },
}

Definition at line 164 of file tests.c.

Referenced by Com_TestWild_f().

Com_TestWild_f
static void Com_TestWild_f(void)
Definition: tests.c:185
num_info_remove_tests
static const int num_info_remove_tests
Definition: tests.c:352
Com_Error_f
static void Com_Error_f(void)
Definition: tests.c:29
info_set_test_t::result_s
const char * result_s
Definition: tests.c:359
info_set_test_t::value
const char * value
Definition: tests.c:357
info_validate_test_t::string
const char * string
Definition: tests.c:293
Info_Validate
qboolean Info_Validate(const char *s)
Definition: shared.c:1040
normtests
static const normtest_t normtests[]
Definition: tests.c:212
Q_snprintf
size_t Q_snprintf(char *dest, size_t size, const char *fmt,...)
Definition: shared.c:846
num_info_validate_tests
static const int num_info_validate_tests
Definition: tests.c:333
BSP_Test_f
static void BSP_Test_f(void)
Definition: tests.c:119
Cmd_AddCommand
void Cmd_AddCommand(const char *name, xcommand_t function)
Definition: cmd.c:1562
Com_PrintJunk_f
static void Com_PrintJunk_f(void)
Definition: tests.c:95
wildtests
static const wildtest_t wildtests[]
Definition: tests.c:164
wildtest_t::string
const char * string
Definition: tests.c:160
Q_ErrorString
const char * Q_ErrorString(qerror_t error)
Definition: error.c:51
snprintf_test_t::len1
size_t len1
Definition: tests.c:439
FS_FreeList
void FS_FreeList(void **list)
Definition: files.c:2939
info_remove_tests
static const info_remove_test_t info_remove_tests[]
Definition: tests.c:341
info_validate_tests
static const info_validate_test_t info_validate_tests[]
Definition: tests.c:297
info_set_test_t::key
const char * key
Definition: tests.c:356
wildtest_t
Definition: tests.c:158
snprintf_test_t::res
const char * res
Definition: tests.c:441
numnormtests
static const int numnormtests
Definition: tests.c:258
info_set_test_t::result_b
int result_b
Definition: tests.c:358
info_set_test_t
Definition: tests.c:354
Com_Crash_f
static void Com_Crash_f(void)
Definition: tests.c:61
Cmd_Argv
char * Cmd_Argv(int arg)
Definition: cmd.c:899
Sys_Milliseconds
unsigned Sys_Milliseconds(void)
Definition: system.c:644
Cmd_Argc
int Cmd_Argc(void)
Definition: cmd.c:889
num_snprintf_tests
static const int num_snprintf_tests
Definition: tests.c:457
snprintf_test_t::overflow2
qboolean overflow2
Definition: tests.c:440
Com_Error
void Com_Error(error_type_t type, const char *fmt,...)
Definition: g_main.c:258
BSP_Free
void BSP_Free(bsp_t *bsp)
Definition: bsp.c:932
info_remove_test_t::result
const char * result
Definition: tests.c:338
wildtest_t::filter
const char * filter
Definition: tests.c:159
normtest_t
Definition: tests.c:207
Z_Free
void Z_Free(void *ptr)
Definition: zone.c:147
Com_ErrorDrop_f
static void Com_ErrorDrop_f(void)
Definition: tests.c:34
normtest_t::in
const char * in
Definition: tests.c:208
FS_ListFiles
void ** FS_ListFiles(const char *path, const char *filter, unsigned flags, int *count_p)
Definition: files.c:2716
snprintf_test_t::size
size_t size
Definition: tests.c:438
info_remove_test_t
Definition: tests.c:335
snprintf_test_t
Definition: tests.c:437
void
void(APIENTRY *qwglDrawBuffer)(GLenum mode)
Q_strlcpy
size_t Q_strlcpy(char *dst, const char *src, size_t size)
Definition: shared.c:715
OV
#define OV
Definition: tests.c:445
snprintf_test_t::overflow1
qboolean overflow1
Definition: tests.c:440
snprintf_tests
static const snprintf_test_t snprintf_tests[]
Definition: tests.c:450
snprintf_test_t::len2
size_t len2
Definition: tests.c:439
Com_TestSnprintf_f
static void Com_TestSnprintf_f(void)
Definition: tests.c:459
Com_TestNorm_f
static void Com_TestNorm_f(void)
Definition: tests.c:260
Com_TestInfo_f
static void Com_TestInfo_f(void)
Definition: tests.c:389
info_remove_test_t::string
const char * string
Definition: tests.c:336
info_validate_test_t::result
int result
Definition: tests.c:294
num_info_set_tests
static const int num_info_set_tests
Definition: tests.c:387
info_set_test_t::string
const char * string
Definition: tests.c:355
BSP_Load
qerror_t BSP_Load(const char *name, bsp_t **bsp_p)
Definition: bsp.c:1087
Com_Freeze_f
static void Com_Freeze_f(void)
Definition: tests.c:39
Info_SetValueForKey
qboolean Info_SetValueForKey(char *s, const char *key, const char *value)
Definition: shared.c:1137
wildtest_t::result
int result
Definition: tests.c:161
numwildtests
static const int numwildtests
Definition: tests.c:183
Q_scnprintf
size_t Q_scnprintf(char *dest, size_t size, const char *fmt,...)
Definition: shared.c:867
info_validate_test_t
Definition: tests.c:292
FS_NormalizePath
size_t FS_NormalizePath(char *out, const char *in)
Definition: files.c:331
info_set_tests
static const info_set_test_t info_set_tests[]
Definition: tests.c:362
Info_RemoveKey
void Info_RemoveKey(char *s, const char *key)
Definition: shared.c:991
normtest_t::out
const char * out
Definition: tests.c:209
info_remove_test_t::key
const char * key
Definition: tests.c:337