UFO: Alien Invasion
Toggle main menu visibility
Loading...
Searching...
No Matches
test_parser.cpp
Go to the documentation of this file.
1
5
6
/*
7
Copyright (C) 2002-2025 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 "
../shared/ufotypes.h
"
28
#include "
../shared/parse.h
"
29
30
class
ParserTest
:
public
::testing::Test {
31
protected
:
32
static
void
SetUpTestCase
() {
33
TEST_Init
();
34
}
35
36
static
void
TearDownTestCase
() {
37
TEST_Shutdown
();
38
}
39
};
40
44
TEST_F
(
ParserTest
, Parser)
45
{
46
const
char
*
string
=
"aa \t\n {\"bbb(bbb bbb)bbb {\"{a}\n/* foooo { } \n { } */\n// fooooo\naaaa"
;
47
const
char
* cursor = string;
48
const
char
* token;
49
50
token =
Com_Parse
(&cursor);
51
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
52
ASSERT_STREQ(token,
"aa"
);
53
54
token =
Com_Parse
(&cursor);
55
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
56
ASSERT_STREQ(token,
"{"
);
57
58
token =
Com_Parse
(&cursor);
59
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
60
ASSERT_STREQ(token,
"bbb(bbb bbb)bbb {"
);
61
62
token =
Com_Parse
(&cursor);
63
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
64
ASSERT_STREQ(token,
"{"
);
65
66
token =
Com_Parse
(&cursor);
67
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
68
ASSERT_STREQ(token,
"a"
);
69
70
token =
Com_Parse
(&cursor);
71
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
72
ASSERT_STREQ(token,
"}"
);
73
74
token =
Com_Parse
(&cursor);
75
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
76
ASSERT_STREQ(token,
"aaaa"
);
77
78
token =
Com_Parse
(&cursor);
79
ASSERT_EQ(
Com_GetType
(&cursor),
TT_EOF
);
80
ASSERT_STREQ(token,
"\0"
);
81
}
82
86
TEST_F
(
ParserTest
, ParserWithEntity)
87
{
88
const
char
*
string
=
"\n\taaaa \" \\n \\t \\\" \""
;
89
const
char
* cursor = string;
90
const
char
* token;
91
92
token =
Com_Parse
(&cursor);
93
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
94
ASSERT_STREQ(token,
"aaaa"
);
95
96
token =
Com_Parse
(&cursor);
97
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
98
ASSERT_STREQ(token,
" \n \t \" "
);
99
}
100
104
TEST_F
(
ParserTest
, ParserWithUnParse)
105
{
106
const
char
*
string
=
"aaaaa\n\tbbbbb \"ccccc\""
;
107
const
char
* cursor = string;
108
const
char
* token;
109
110
token =
Com_Parse
(&cursor);
111
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
112
ASSERT_STREQ(token,
"aaaaa"
);
113
114
Com_UnParseLastToken
();
115
116
token =
Com_Parse
(&cursor);
117
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
118
ASSERT_STREQ(token,
"aaaaa"
);
119
120
Com_UnParseLastToken
();
121
122
token =
Com_Parse
(&cursor);
123
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
124
ASSERT_STREQ(token,
"aaaaa"
);
125
126
token =
Com_Parse
(&cursor);
127
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
128
ASSERT_STREQ(token,
"bbbbb"
);
129
130
Com_UnParseLastToken
();
131
132
token =
Com_Parse
(&cursor);
133
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
134
ASSERT_STREQ(token,
"bbbbb"
);
135
136
Com_UnParseLastToken
();
137
138
token =
Com_Parse
(&cursor);
139
ASSERT_NE(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
140
ASSERT_STREQ(token,
"bbbbb"
);
141
142
token =
Com_Parse
(&cursor);
143
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
144
ASSERT_STREQ(token,
"ccccc"
);
145
146
Com_UnParseLastToken
();
147
148
token =
Com_Parse
(&cursor);
149
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
150
ASSERT_STREQ(token,
"ccccc"
);
151
152
Com_UnParseLastToken
();
153
154
token =
Com_Parse
(&cursor);
155
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
156
ASSERT_STREQ(token,
"ccccc"
);
157
}
158
162
TEST_F
(
ParserTest
, ParserWithFunctionScriptToken)
163
{
164
const
char
*
string
=
"aa \t\n aa,({\"bbb(bbb bbb)bbb {\"{a}\n/* foooo { } \n { } */\n// fooooo\naaaa)"
;
165
const
char
* cursor = string;
166
const
char
* token;
167
168
token =
Com_Parse
(&cursor);
169
ASSERT_EQ(
Com_GetType
(&cursor),
TT_WORD
);
170
ASSERT_STREQ(token,
"aa"
);
171
172
token =
Com_Parse
(&cursor);
173
ASSERT_EQ(
Com_GetType
(&cursor),
TT_WORD
);
174
ASSERT_STREQ(token,
"aa"
);
175
176
token =
Com_Parse
(&cursor);
177
ASSERT_EQ(
Com_GetType
(&cursor),
TT_COMMA
);
178
ASSERT_STREQ(token,
","
);
179
180
token =
Com_Parse
(&cursor);
181
ASSERT_EQ(
Com_GetType
(&cursor),
TT_BEGIN_LIST
);
182
ASSERT_STREQ(token,
"("
);
183
184
token =
Com_Parse
(&cursor);
185
ASSERT_EQ(
Com_GetType
(&cursor),
TT_BEGIN_BLOCK
);
186
ASSERT_STREQ(token,
"{"
);
187
188
token =
Com_Parse
(&cursor);
189
ASSERT_EQ(
Com_GetType
(&cursor),
TT_QUOTED_WORD
);
190
ASSERT_STREQ(token,
"bbb(bbb bbb)bbb {"
);
191
192
token =
Com_Parse
(&cursor);
193
ASSERT_EQ(
Com_GetType
(&cursor),
TT_BEGIN_BLOCK
);
194
ASSERT_STREQ(token,
"{"
);
195
196
token =
Com_Parse
(&cursor);
197
ASSERT_EQ(
Com_GetType
(&cursor),
TT_WORD
);
198
ASSERT_STREQ(token,
"a"
);
199
200
token =
Com_Parse
(&cursor);
201
ASSERT_EQ(
Com_GetType
(&cursor),
TT_END_BLOCK
);
202
ASSERT_STREQ(token,
"}"
);
203
204
token =
Com_Parse
(&cursor);
205
ASSERT_EQ(
Com_GetType
(&cursor),
TT_WORD
);
206
ASSERT_STREQ(token,
"aaaa"
);
207
208
token =
Com_Parse
(&cursor);
209
ASSERT_EQ(
Com_GetType
(&cursor),
TT_END_LIST
);
210
ASSERT_STREQ(token,
")"
);
211
212
token =
Com_Parse
(&cursor);
213
ASSERT_EQ(
Com_GetType
(&cursor),
TT_EOF
);
214
ASSERT_STREQ(token,
"\0"
);
215
}
216
220
TEST_F
(
ParserTest
, ParserCommonType)
221
{
222
int
ivalue;
223
bool
bvalue;
224
float
fvalue;
225
align_t
align;
226
blend_t
blend;
227
style_t
style;
228
fade_t
fade;
229
size_t
bytes;
230
int
result;
231
232
/* boolean */
233
234
bytes = 0;
235
result =
Com_ParseValue
(&bvalue,
"true"
,
V_BOOL
, 0,
sizeof
(
bool
), &bytes);
236
ASSERT_EQ(result,
RESULT_OK
);
237
ASSERT_EQ(bvalue, 1);
238
ASSERT_EQ(bytes,
sizeof
(
bool
));
239
240
bytes = 0;
241
result =
Com_ParseValue
(&bvalue,
"false"
,
V_BOOL
, 0,
sizeof
(
bool
), &bytes);
242
ASSERT_EQ(result,
RESULT_OK
);
243
ASSERT_EQ(bvalue, 0);
244
ASSERT_EQ(bytes,
sizeof
(
bool
));
245
246
bytes = 0;
247
result =
Com_ParseValue
(&bvalue,
"foo"
,
V_BOOL
, 0,
sizeof
(
int
), &bytes);
248
ASSERT_EQ(result,
RESULT_ERROR
);
249
ASSERT_EQ(bytes, 0);
250
251
/* int */
252
253
bytes = 0;
254
result =
Com_ParseValue
(&ivalue,
"10"
,
V_INT
, 0,
sizeof
(
int
), &bytes);
255
ASSERT_EQ(result,
RESULT_OK
);
256
ASSERT_EQ(ivalue, 10);
257
ASSERT_EQ(bytes,
sizeof
(
int
));
258
259
bytes = 0;
260
result =
Com_ParseValue
(&ivalue,
"abc"
,
V_INT
, 0,
sizeof
(
int
), &bytes);
261
ASSERT_EQ(result,
RESULT_ERROR
);
262
ASSERT_EQ(bytes, 0);
263
264
/* float */
265
266
bytes = 0;
267
result =
Com_ParseValue
(&fvalue,
"1.1"
,
V_FLOAT
, 0,
sizeof
(
float
), &bytes);
268
ASSERT_EQ(result,
RESULT_OK
);
269
ASSERT_EQ(fvalue, 1.1f);
270
ASSERT_EQ(bytes,
sizeof
(
float
));
271
272
bytes = 0;
273
result =
Com_ParseValue
(&fvalue,
"9.8"
,
V_FLOAT
, 0,
sizeof
(
float
), &bytes);
274
ASSERT_EQ(result,
RESULT_OK
);
275
ASSERT_EQ(fvalue, 9.8f);
276
ASSERT_EQ(bytes,
sizeof
(
float
));
277
278
bytes = 0;
279
result =
Com_ParseValue
(&fvalue,
"abc"
,
V_FLOAT
, 0,
sizeof
(
float
), &bytes);
280
ASSERT_EQ(result,
RESULT_ERROR
);
281
ASSERT_EQ(bytes, 0);
282
289
290
/* align */
291
292
bytes = 0;
293
result =
Com_ParseValue
(&align,
"cc"
,
V_ALIGN
, 0,
sizeof
(
align_t
), &bytes);
294
ASSERT_EQ(result,
RESULT_OK
);
295
ASSERT_EQ(align,
ALIGN_CC
);
296
ASSERT_EQ(bytes,
sizeof
(
align_t
));
297
298
bytes = 0;
299
result =
Com_ParseValue
(&align,
"abc"
,
V_ALIGN
, 0,
sizeof
(
align_t
), &bytes);
300
ASSERT_EQ(result,
RESULT_ERROR
);
301
ASSERT_EQ(bytes, 0);
302
303
/* blend */
304
305
bytes = 0;
306
result =
Com_ParseValue
(&blend,
"blend"
,
V_BLEND
, 0,
sizeof
(
blend_t
), &bytes);
307
ASSERT_EQ(result,
RESULT_OK
);
308
ASSERT_EQ(blend,
BLEND_BLEND
);
309
ASSERT_EQ(bytes,
sizeof
(
blend_t
));
310
311
bytes = 0;
312
result =
Com_ParseValue
(&blend,
"abc"
,
V_BLEND
, 0,
sizeof
(
blend_t
), &bytes);
313
ASSERT_EQ(result,
RESULT_ERROR
);
314
ASSERT_EQ(bytes, 0);
315
316
/* style */
317
318
bytes = 0;
319
result =
Com_ParseValue
(&style,
"rotated"
,
V_STYLE
, 0,
sizeof
(
style_t
), &bytes);
320
ASSERT_EQ(result,
RESULT_OK
);
321
ASSERT_EQ(style,
STYLE_ROTATED
);
322
ASSERT_EQ(bytes,
sizeof
(
style_t
));
323
324
bytes = 0;
325
result =
Com_ParseValue
(&style,
"abc"
,
V_STYLE
, 0,
sizeof
(
style_t
), &bytes);
326
ASSERT_EQ(result,
RESULT_ERROR
);
327
ASSERT_EQ(bytes, 0);
328
329
/* fade */
330
331
bytes = 0;
332
result =
Com_ParseValue
(&fade,
"sin"
,
V_FADE
, 0,
sizeof
(
fade_t
), &bytes);
333
ASSERT_EQ(result,
RESULT_OK
);
334
ASSERT_EQ(fade,
FADE_SIN
);
335
ASSERT_EQ(bytes,
sizeof
(
fade_t
));
336
337
bytes = 0;
338
result =
Com_ParseValue
(&fade,
"abc"
,
V_FADE
, 0,
sizeof
(
fade_t
), &bytes);
339
ASSERT_EQ(result,
RESULT_ERROR
);
340
ASSERT_EQ(bytes, 0);
341
}
342
346
TEST_F
(
ParserTest
, ParserListOk)
347
{
348
const
char
*
string
=
" ( aaa \n \"bbb\" \t ccc \n \n ) "
;
349
const
char
* cursor = string;
350
linkedList_t
* list;
351
352
ASSERT_TRUE(
Com_ParseList
(&cursor, &list)) <<
"List parsing failed"
;
353
354
ASSERT_EQ(
LIST_Count
(list), 3);
355
356
ASSERT_STREQ(
static_cast<
const
char
*
>
(list->
data
),
"aaa"
);
357
ASSERT_STREQ(
static_cast<
const
char
*
>
(list->
next
->
data
),
"bbb"
);
358
ASSERT_STREQ(
static_cast<
const
char
*
>
(list->
next
->
next
->
data
),
"ccc"
);
359
360
LIST_Delete
(&list);
361
}
362
363
367
TEST_F
(
ParserTest
, ParserListOkEmpty)
368
{
369
const
char
*
string
=
" ( \n ) ()"
;
370
const
char
* cursor = string;
371
linkedList_t
* list;
372
373
ASSERT_TRUE(
Com_ParseList
(&cursor, &list)) <<
"List parsing failed"
;
374
375
ASSERT_EQ(
LIST_Count
(list), 0);
376
377
ASSERT_TRUE(
Com_ParseList
(&cursor, &list)) <<
"List parsing failed"
;
378
379
ASSERT_EQ(
LIST_Count
(list), 0);
380
}
381
385
TEST_F
(
ParserTest
, ParserListKoEOF)
386
{
387
const
char
*
string
=
" ( aaa \n bbb \t ccc \n \n"
;
388
const
char
* cursor = string;
389
linkedList_t
* list;
390
391
ASSERT_FALSE(
Com_ParseList
(&cursor, &list)) <<
"List parsing succeed, which is wrong"
;
392
}
393
397
TEST_F
(
ParserTest
, ParserListKoWrongToken)
398
{
399
const
char
*
string
=
" ( aaa \n bbb \t ccc \n \n } "
;
400
const
char
* cursor = string;
401
402
linkedList_t
* list;
403
ASSERT_FALSE(
Com_ParseList
(&cursor, &list)) <<
"List parsing succeed, which is wrong"
;
404
}
405
409
TEST_F
(
ParserTest
, ParserListKoNewList)
410
{
411
const
char
*
string
=
" ( aaa \n bbb \t ccc \n \n ( "
;
412
const
char
* cursor = string;
413
linkedList_t
* list;
414
415
ASSERT_FALSE(
Com_ParseList
(&cursor, &list)) <<
"List parsing succeed, which is wrong"
;
416
}
ParserTest
Definition
test_parser.cpp:30
ParserTest::TearDownTestCase
static void TearDownTestCase()
Definition
test_parser.cpp:36
ParserTest::SetUpTestCase
static void SetUpTestCase()
Definition
test_parser.cpp:32
LIST_Delete
void LIST_Delete(linkedList_t **list)
Definition
list.cpp:195
LIST_Count
int LIST_Count(const linkedList_t *list)
Definition
list.cpp:344
Com_Parse
const char * Com_Parse(const char *data_p[], char *target, size_t size, bool replaceWhitespaces)
Parse a token out of a string.
Definition
parse.cpp:107
Com_UnParseLastToken
void Com_UnParseLastToken(void)
Put back the last token into the parser The next call of Com_Parse will return the same token again.
Definition
parse.cpp:42
Com_GetType
Com_TokenType_t Com_GetType(const char **data_p)
Get the current token type.
Definition
parse.cpp:60
parse.h
Shared parsing functions.
TT_BEGIN_BLOCK
@ TT_BEGIN_BLOCK
Definition
parse.h:35
TT_END_BLOCK
@ TT_END_BLOCK
Definition
parse.h:36
TT_QUOTED_WORD
@ TT_QUOTED_WORD
Definition
parse.h:43
TT_WORD
@ TT_WORD
Definition
parse.h:42
TT_END_LIST
@ TT_END_LIST
Definition
parse.h:39
TT_BEGIN_LIST
@ TT_BEGIN_LIST
Definition
parse.h:38
TT_EOF
@ TT_EOF
Definition
parse.h:34
TT_COMMA
@ TT_COMMA
Definition
parse.h:37
Com_ParseList
bool Com_ParseList(const char **text, linkedList_t **list)
Definition
scripts.cpp:1363
Com_ParseValue
resultStatus_t Com_ParseValue(void *base, const char *token, valueTypes_t type, int ofs, size_t size, size_t *writtenBytes)
Parse a value from a string.
Definition
scripts.cpp:656
style_t
style_t
Definition
scripts.h:124
STYLE_ROTATED
@ STYLE_ROTATED
Definition
scripts.h:126
align_t
align_t
We need this here for checking the boundaries from script values.
Definition
scripts.h:89
ALIGN_CC
@ ALIGN_CC
Definition
scripts.h:94
V_BOOL
@ V_BOOL
Definition
scripts.h:50
V_FLOAT
@ V_FLOAT
Definition
scripts.h:54
V_STYLE
@ V_STYLE
Definition
scripts.h:63
V_BLEND
@ V_BLEND
Definition
scripts.h:62
V_FADE
@ V_FADE
Definition
scripts.h:64
V_ALIGN
@ V_ALIGN
Definition
scripts.h:61
V_INT
@ V_INT
Definition
scripts.h:52
RESULT_ERROR
@ RESULT_ERROR
Definition
scripts.h:185
RESULT_OK
@ RESULT_OK
Definition
scripts.h:187
blend_t
blend_t
Definition
scripts.h:113
BLEND_BLEND
@ BLEND_BLEND
Definition
scripts.h:116
fade_t
fade_t
Definition
scripts.h:135
FADE_SIN
@ FADE_SIN
Definition
scripts.h:139
linkedList_t
Definition
list.h:30
linkedList_t::data
void * data
Definition
list.h:31
linkedList_t::next
linkedList_t * next
Definition
list.h:32
TEST_F
TEST_F(ParserTest, Parser)
unittest around default use of parser
Definition
test_parser.cpp:44
TEST_Shutdown
void TEST_Shutdown(void)
Definition
test_shared.cpp:34
TEST_Init
void TEST_Init(void)
Definition
test_shared.cpp:72
test_shared.h
ufotypes.h
Cross-platform type definitions.
src
tests
test_parser.cpp
Generated on __DATE__ __TIME__ for UFO: Alien Invasion by
1.17.0