UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_shared.cpp
Go to the documentation of this file.
1 
6 /*
7 Copyright (C) 2002-2020 UFO: Alien Invasion.
8 
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17 
18 See the GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 
24 */
25 
26 #include "test_shared.h"
27 #include "../client/cl_renderer.h"
28 #include "../client/cl_lua.h"
29 #include "../client/battlescape/cl_particle.h"
30 #include "../client/ui/ui_main.h"
31 #include <stdio.h>
32 #include <stdarg.h>
33 
34 void TEST_Shutdown (void)
35 {
38  SV_Shutdown("test shutdown", false);
39  FS_Shutdown();
40  UI_Shutdown();
42  Cmd_Shutdown();
43  developer = nullptr;
44  Cvar_Shutdown();
45  Mem_Shutdown();
46  Com_Shutdown();
47  Cbuf_Shutdown();
48  NET_Shutdown();
49 
50  com_aliasSysPool = nullptr;
51  com_cmdSysPool = nullptr;
52  com_cmodelSysPool = nullptr;
53  com_cvarSysPool = nullptr;
54  com_fileSysPool = nullptr;
55  com_genericPool = nullptr;
56 }
57 
58 void TEST_vPrintfSilent (const char* fmt, va_list argptr)
59 {
60 }
61 
62 void TEST_vPrintf (const char* fmt, va_list argptr)
63 {
64  char text[1024];
65  Q_vsnprintf(text, sizeof(text), fmt, argptr);
66 
67  fprintf(stderr, "%s", text);
68 
69  fflush(stderr);
70 }
71 
72 void TEST_Init (void)
73 {
74  try {
75  com_aliasSysPool = Mem_CreatePool("Common: Alias system");
76  com_cmdSysPool = Mem_CreatePool("Common: Command system");
77  com_cmodelSysPool = Mem_CreatePool("Common: Collision model");
78  com_cvarSysPool = Mem_CreatePool("Common: Cvar system");
79  com_fileSysPool = Mem_CreatePool("Common: File system");
80  com_genericPool = Mem_CreatePool("Generic");
81 
82  Mem_Init();
83  Cbuf_Init();
84  Cmd_Init();
85  Cvar_Init();
86  FS_InitFilesystem(true);
87  FS_AddGameDirectory("./unittest", false);
88  FS_AddHomeAsGameDirectory("unittest", true);
89  Swap_Init();
90  SV_Init();
91  NET_Init();
92 
94 
95  OBJZERO(csi);
96  } catch (comDrop_t const&) {
97  Sys_Error("Error during initialization");
98  }
99 
100  http_timeout = Cvar_Get("noname");
101  http_proxy = Cvar_Get("noname");
102  hwclass = Cvar_Get("hwclass", "5");
103 }
104 
105 
106 #define PROPERTY_HASH_SIZE 32
107 
108 #define MAX_PROPERTY_NAME 32
109 
110 typedef struct test_property_s {
112  const char* value;
116 
119 
125 void TEST_RegisterProperty (const char* name, const char* value)
126 {
127  unsigned int hash;
128  test_property_t* element;
129 
130  /* if the alias already exists, reuse it */
131  hash = Com_HashKey(name, PROPERTY_HASH_SIZE);
132  for (element = test_property_hash[hash]; element; element = element->hash_next) {
133  if (Q_streq(name, element->name)) {
134  break;
135  }
136  }
137 
138  if (!element) {
139  element = (test_property_t*) malloc(sizeof(*element));
140  Q_strncpyz(element->name, name, sizeof(element->name));
142  element->value = value;
143  element->next = test_property;
144  element->hash_next = test_property_hash[hash];
145  test_property_hash[hash] = element;
146  test_property = element;
147  Com_Printf("Register test property \"%s\" = \"%s\"\n", name, value);
148  }
149 }
150 
156 static const test_property_t* TEST_GetProperty (const char* name)
157 {
158  unsigned int hash;
159  const test_property_t* element;
160 
161  hash = Com_HashKey(name, PROPERTY_HASH_SIZE);
162  for (element = test_property_hash[hash]; element; element = element->hash_next) {
163  if (!Q_strcasecmp(name, element->name)) {
164  return element;
165  }
166  }
167  return nullptr;
168 }
169 
175 bool TEST_ExistsProperty (const char* name)
176 {
177  return TEST_GetProperty(name) != nullptr;
178 }
179 
185 const char* TEST_GetStringProperty (const char* name)
186 {
187  const test_property_t* element = TEST_GetProperty(name);
188  if (element == nullptr) {
189  Com_Printf("WARNING: Test property \"%s\" not found. nullptr returned.\n", name);
190  return nullptr;
191  }
192  return element->value;
193 }
194 
200 int TEST_GetIntProperty (const char* name)
201 {
202  const test_property_t* element = TEST_GetProperty(name);
203  if (element == nullptr) {
204  Com_Printf("WARNING: Test property \"%s\" not found. 0 returned.\n", name);
205  return 0;
206  }
207  return atoi(element->value);
208 }
209 
215 long TEST_GetLongProperty (const char* name)
216 {
217  const test_property_t* element = TEST_GetProperty(name);
218  if (element == nullptr) {
219  Com_Printf("WARNING: Test property \"%s\" not found. 0 returned.\n", name);
220  return 0;
221  }
222  return atol(element->value);
223 }
void Cmd_Init(void)
Definition: cmd.cpp:1127
void FS_AddGameDirectory(const char *dir, bool write)
Adds the directory to the head of the search path.
Definition: files.cpp:495
int Q_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Safe (null terminating) vsnprintf implementation.
Definition: shared.cpp:535
void Sys_Error(const char *error,...)
Definition: g_main.cpp:421
void Mem_Shutdown(void)
Definition: mem.cpp:603
void FS_ExecAutoexec(void)
void NET_Shutdown(void)
Definition: net.cpp:337
void NET_Init(void)
Definition: net.cpp:305
const char * value
void UI_Shutdown(void)
Reset and free the UI data hunk.
Definition: ui_main.cpp:205
void Swap_Init(void)
Definition: byte.cpp:31
csi_t csi
Definition: common.cpp:39
memPool_t * com_aliasSysPool
Definition: common.cpp:68
#define PROPERTY_HASH_SIZE
static const test_property_t * TEST_GetProperty(const char *name)
void CL_ShutdownLua(void)
Shutdown the ui-lua interfacing environment.
Definition: cl_lua.cpp:151
void R_ShutdownImages(void)
Definition: r_image.cpp:796
void Cvar_Shutdown(void)
Definition: cvar.cpp:1100
void Com_Printf(const char *const fmt,...)
Definition: common.cpp:386
memPool_t * com_cmdSysPool
Definition: common.cpp:69
cvar_t * http_proxy
Definition: common.cpp:47
void Q_strncpyz(char *dest, const char *src, size_t destsize)
Safe strncpy that ensures a trailing zero.
Definition: shared.cpp:457
void TEST_vPrintf(const char *fmt, va_list argptr)
Definition: test_shared.cpp:62
cvar_t * Cvar_Get(const char *var_name, const char *var_value, int flags, const char *desc)
Init or return a cvar.
Definition: cvar.cpp:342
void Cvar_Init(void)
Reads in all archived cvars.
Definition: cvar.cpp:1087
#define OBJZERO(obj)
Definition: shared.h:178
cvar_t * hwclass
Definition: common.cpp:62
void TEST_RegisterProperty(const char *name, const char *value)
void TEST_Shutdown(void)
Definition: test_shared.cpp:34
#define MAX_PROPERTY_NAME
static wrapCache_t * hash[MAX_WRAP_HASH]
Definition: r_font.cpp:86
memPool_t * com_genericPool
Definition: common.cpp:73
void FS_AddHomeAsGameDirectory(const char *dir, bool write)
Definition: files.cpp:655
void FS_InitFilesystem(bool writeToHomeDir)
Definition: files.cpp:888
#define Mem_CreatePool(name)
Definition: mem.h:32
cvar_t * http_timeout
Definition: common.cpp:48
#define Q_strcasecmp(a, b)
Definition: shared.h:131
static test_property_t * test_property
memPool_t * com_cvarSysPool
Definition: common.cpp:71
void SV_Init(void)
Only called once at startup, not for each game.
Definition: sv_main.cpp:960
void Cbuf_Shutdown(void)
Definition: cmd.cpp:117
static test_property_t * test_property_hash[PROPERTY_HASH_SIZE]
cvar_t * developer
Definition: common.cpp:46
bool TEST_ExistsProperty(const char *name)
memPool_t * com_cmodelSysPool
Definition: common.cpp:70
void Cbuf_Init(void)
allocates an initial text buffer that will grow as needed
Definition: cmd.cpp:109
long TEST_GetLongProperty(const char *name)
void Cmd_Shutdown(void)
Definition: cmd.cpp:1145
struct test_property_s * hash_next
void Mem_Init(void)
Definition: mem.cpp:588
struct test_property_s * next
const char * TEST_GetStringProperty(const char *name)
void TEST_Init(void)
Definition: test_shared.cpp:72
void TEST_vPrintfSilent(const char *fmt, va_list argptr)
Definition: test_shared.cpp:58
QGL_EXTERN GLuint GLsizei GLsizei GLint GLenum GLchar * name
Definition: r_gl.h:110
unsigned int Com_HashKey(const char *name, int hashsize)
returns hash key for a string
Definition: shared.cpp:336
void Com_Shutdown(void)
Definition: scripts.cpp:3754
memPool_t * com_fileSysPool
Definition: common.cpp:72
void PTL_InitStartup(void)
Clears particle data.
char name[MAX_PROPERTY_NAME]
#define Q_streq(a, b)
Definition: shared.h:136
struct test_property_s test_property_t
int TEST_GetIntProperty(const char *name)
void FS_Shutdown(void)
Cleanup function.
Definition: files.cpp:1602
void SV_Shutdown(const char *finalmsg, bool reconnect)
Called when each game quits, before Sys_Quit or Sys_Error.
Definition: sv_main.cpp:1042