src/debugger/test/lexer.c (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "util/test/suite.h"
7
8#include <mgba/internal/debugger/parser.h>
9
10#define LEX(STR) \
11 struct LexVector* lv = *state; \
12 lexFree(lv); \
13 LexVectorClear(lv); \
14 size_t adjusted = lexExpression(lv, STR, strlen(STR)); \
15 assert_false(adjusted > strlen(STR))
16
17M_TEST_SUITE_SETUP(Lexer) {
18 struct LexVector* lv = malloc(sizeof(struct LexVector));
19 LexVectorInit(lv, 0);
20 *state = lv;
21 return 0;
22}
23
24M_TEST_SUITE_TEARDOWN(Lexer) {
25 struct LexVector* lv = *state;
26 lexFree(lv);
27 LexVectorDeinit(lv);
28 free(lv);
29 return 0;
30}
31
32M_TEST_DEFINE(lexEmpty) {
33 LEX("");
34
35 assert_int_equal(LexVectorSize(lv), 0);
36}
37
38M_TEST_DEFINE(lexInt) {
39 LEX("0");
40
41 assert_int_equal(LexVectorSize(lv), 1);
42 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
43 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 0);
44}
45
46M_TEST_DEFINE(lexDecimal) {
47 LEX("10");
48
49 assert_int_equal(LexVectorSize(lv), 1);
50 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
51 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 10);
52}
53
54M_TEST_DEFINE(lexBinary) {
55 LEX("0b10");
56
57 assert_int_equal(LexVectorSize(lv), 1);
58 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
59 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 2);
60}
61
62M_TEST_DEFINE(lexSigilBinary) {
63 LEX("%10");
64
65 assert_int_equal(LexVectorSize(lv), 1);
66 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
67 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 2);
68}
69
70M_TEST_DEFINE(lexHex) {
71 LEX("0x10");
72
73 assert_int_equal(LexVectorSize(lv), 1);
74 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
75 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 0x10);
76}
77
78M_TEST_DEFINE(lexSigilHex) {
79 LEX("$10");
80
81 assert_int_equal(LexVectorSize(lv), 1);
82 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
83 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 0x10);
84}
85
86M_TEST_DEFINE(lexInvalidDecimal) {
87 LEX("1a");
88
89 assert_int_equal(LexVectorSize(lv), 1);
90 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
91}
92
93M_TEST_DEFINE(lexInvalidBinary) {
94 LEX("0b12");
95
96 assert_int_equal(LexVectorSize(lv), 1);
97 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
98}
99
100M_TEST_DEFINE(lexInvalidHex) {
101 LEX("0x1g");
102
103 assert_int_equal(LexVectorSize(lv), 1);
104 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
105}
106
107M_TEST_DEFINE(lexTruncatedBinary) {
108 LEX("0b");
109
110 assert_int_equal(LexVectorSize(lv), 1);
111 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
112}
113
114M_TEST_DEFINE(lexTruncatedSigilBinary) {
115 LEX("%");
116
117 assert_int_equal(LexVectorSize(lv), 1);
118 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
119}
120
121M_TEST_DEFINE(lexTruncatedSigilHex) {
122 LEX("$");
123
124 assert_int_equal(LexVectorSize(lv), 1);
125 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
126}
127
128M_TEST_DEFINE(lexTruncatedHex) {
129 LEX("0x");
130
131 assert_int_equal(LexVectorSize(lv), 1);
132 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_ERROR_TYPE);
133}
134
135M_TEST_DEFINE(lexSigilSegmentHex) {
136 LEX("$01:0010");
137
138 assert_int_equal(LexVectorSize(lv), 2);
139 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_SEGMENT_TYPE);
140 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
141 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_UINT_TYPE);
142 assert_int_equal(LexVectorGetPointer(lv, 1)->uintValue, 0x10);
143}
144
145M_TEST_DEFINE(lexIdentifier) {
146 LEX("x");
147
148 assert_int_equal(LexVectorSize(lv), 1);
149 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
150 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
151}
152
153M_TEST_DEFINE(lexAddOperator) {
154 LEX("1+");
155
156 assert_int_equal(LexVectorSize(lv), 2);
157 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
158 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
159 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
160 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_ADD);
161}
162
163M_TEST_DEFINE(lexIdentifierAddOperator) {
164 LEX("x+");
165
166 assert_int_equal(LexVectorSize(lv), 2);
167 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
168 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
169 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
170 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_ADD);
171}
172
173M_TEST_DEFINE(lexSubOperator) {
174 LEX("1-");
175
176 assert_int_equal(LexVectorSize(lv), 2);
177 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
178 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
179 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
180 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_SUBTRACT);
181}
182
183M_TEST_DEFINE(lexIdentifierSubOperator) {
184 LEX("x-");
185
186 assert_int_equal(LexVectorSize(lv), 2);
187 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
188 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
189 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
190 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_SUBTRACT);
191}
192
193M_TEST_DEFINE(lexMulOperator) {
194 LEX("1*");
195
196 assert_int_equal(LexVectorSize(lv), 2);
197 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
198 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
199 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
200 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_MULTIPLY);
201}
202
203M_TEST_DEFINE(lexIdentifierMulOperator) {
204 LEX("x*");
205
206 assert_int_equal(LexVectorSize(lv), 2);
207 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
208 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
209 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
210 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_MULTIPLY);
211}
212
213M_TEST_DEFINE(lexDivOperator) {
214 LEX("1/");
215
216 assert_int_equal(LexVectorSize(lv), 2);
217 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
218 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
219 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
220 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_DIVIDE);
221}
222
223M_TEST_DEFINE(lexIdentifierDivOperator) {
224 LEX("x/");
225
226 assert_int_equal(LexVectorSize(lv), 2);
227 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
228 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
229 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
230 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_DIVIDE);
231}
232
233M_TEST_DEFINE(lexAndOperator) {
234 LEX("1&");
235
236 assert_int_equal(LexVectorSize(lv), 2);
237 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
238 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
239 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
240 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_AND);
241}
242
243M_TEST_DEFINE(lexIdentifierAndOperator) {
244 LEX("x&");
245
246 assert_int_equal(LexVectorSize(lv), 2);
247 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
248 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
249 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
250 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_AND);
251}
252
253M_TEST_DEFINE(lexOrOperator) {
254 LEX("1|");
255
256 assert_int_equal(LexVectorSize(lv), 2);
257 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
258 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
259 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
260 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_OR);
261}
262
263M_TEST_DEFINE(lexIdentifierOrOperator) {
264 LEX("x|");
265
266 assert_int_equal(LexVectorSize(lv), 2);
267 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
268 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
269 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
270 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_OR);
271}
272
273M_TEST_DEFINE(lexXorOperator) {
274 LEX("1^");
275
276 assert_int_equal(LexVectorSize(lv), 2);
277 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
278 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
279 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
280 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_XOR);
281}
282
283M_TEST_DEFINE(lexIdentifierXorOperator) {
284 LEX("x^");
285
286 assert_int_equal(LexVectorSize(lv), 2);
287 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
288 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
289 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
290 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_XOR);
291}
292
293M_TEST_DEFINE(lexLessOperator) {
294 LEX("1<");
295
296 assert_int_equal(LexVectorSize(lv), 2);
297 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
298 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
299 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
300 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_LESS);
301}
302
303M_TEST_DEFINE(lexIdentifierLessOperator) {
304 LEX("x<");
305
306 assert_int_equal(LexVectorSize(lv), 2);
307 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
308 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
309 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
310 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_LESS);
311}
312
313M_TEST_DEFINE(lexGreaterOperator) {
314 LEX("1>");
315
316 assert_int_equal(LexVectorSize(lv), 2);
317 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
318 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
319 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
320 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_GREATER);
321}
322
323M_TEST_DEFINE(lexIdentifierGreaterOperator) {
324 LEX("x>");
325
326 assert_int_equal(LexVectorSize(lv), 2);
327 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_IDENTIFIER_TYPE);
328 assert_string_equal(LexVectorGetPointer(lv, 0)->identifierValue, "x");
329 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
330 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_GREATER);
331}
332
333M_TEST_DEFINE(lexSimpleExpression) {
334 LEX("1+1");
335
336 assert_int_equal(LexVectorSize(lv), 3);
337 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_UINT_TYPE);
338 assert_int_equal(LexVectorGetPointer(lv, 0)->uintValue, 1);
339 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_OPERATOR_TYPE);
340 assert_int_equal(LexVectorGetPointer(lv, 1)->operatorValue, OP_ADD);
341 assert_int_equal(LexVectorGetPointer(lv, 2)->type, TOKEN_UINT_TYPE);
342 assert_int_equal(LexVectorGetPointer(lv, 2)->uintValue, 1);
343}
344
345M_TEST_DEFINE(lexOpenParen) {
346 LEX("(");
347
348 assert_int_equal(LexVectorSize(lv), 1);
349 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_OPEN_PAREN_TYPE);
350}
351
352M_TEST_DEFINE(lexCloseParen) {
353 LEX("(0)");
354
355 assert_int_equal(LexVectorSize(lv), 3);
356 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_OPEN_PAREN_TYPE);
357 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_UINT_TYPE);
358 assert_int_equal(LexVectorGetPointer(lv, 1)->uintValue, 0);
359 assert_int_equal(LexVectorGetPointer(lv, 2)->type, TOKEN_CLOSE_PAREN_TYPE);
360}
361
362M_TEST_DEFINE(lexIdentifierCloseParen) {
363 LEX("(x)");
364
365 assert_int_equal(LexVectorSize(lv), 3);
366 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_OPEN_PAREN_TYPE);
367 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_IDENTIFIER_TYPE);
368 assert_string_equal(LexVectorGetPointer(lv, 1)->identifierValue, "x");
369 assert_int_equal(LexVectorGetPointer(lv, 2)->type, TOKEN_CLOSE_PAREN_TYPE);
370}
371
372M_TEST_DEFINE(lexParentheticalExpression) {
373 LEX("(1+1)");
374
375 assert_int_equal(LexVectorSize(lv), 5);
376 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_OPEN_PAREN_TYPE);
377 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_UINT_TYPE);
378 assert_int_equal(LexVectorGetPointer(lv, 1)->uintValue, 1);
379 assert_int_equal(LexVectorGetPointer(lv, 2)->type, TOKEN_OPERATOR_TYPE);
380 assert_int_equal(LexVectorGetPointer(lv, 2)->operatorValue, OP_ADD);
381 assert_int_equal(LexVectorGetPointer(lv, 3)->type, TOKEN_UINT_TYPE);
382 assert_int_equal(LexVectorGetPointer(lv, 3)->uintValue, 1);
383 assert_int_equal(LexVectorGetPointer(lv, 4)->type, TOKEN_CLOSE_PAREN_TYPE);
384}
385
386M_TEST_DEFINE(lexNestedParentheticalExpression) {
387 LEX("(1+(2+3))");
388
389 assert_int_equal(LexVectorSize(lv), 9);
390 assert_int_equal(LexVectorGetPointer(lv, 0)->type, TOKEN_OPEN_PAREN_TYPE);
391 assert_int_equal(LexVectorGetPointer(lv, 1)->type, TOKEN_UINT_TYPE);
392 assert_int_equal(LexVectorGetPointer(lv, 1)->uintValue, 1);
393 assert_int_equal(LexVectorGetPointer(lv, 2)->type, TOKEN_OPERATOR_TYPE);
394 assert_int_equal(LexVectorGetPointer(lv, 2)->operatorValue, OP_ADD);
395 assert_int_equal(LexVectorGetPointer(lv, 3)->type, TOKEN_OPEN_PAREN_TYPE);
396 assert_int_equal(LexVectorGetPointer(lv, 4)->type, TOKEN_UINT_TYPE);
397 assert_int_equal(LexVectorGetPointer(lv, 4)->uintValue, 2);
398 assert_int_equal(LexVectorGetPointer(lv, 5)->type, TOKEN_OPERATOR_TYPE);
399 assert_int_equal(LexVectorGetPointer(lv, 5)->operatorValue, OP_ADD);
400 assert_int_equal(LexVectorGetPointer(lv, 6)->type, TOKEN_UINT_TYPE);
401 assert_int_equal(LexVectorGetPointer(lv, 6)->uintValue, 3);
402 assert_int_equal(LexVectorGetPointer(lv, 7)->type, TOKEN_CLOSE_PAREN_TYPE);
403 assert_int_equal(LexVectorGetPointer(lv, 8)->type, TOKEN_CLOSE_PAREN_TYPE);
404}
405
406M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(Lexer,
407 cmocka_unit_test(lexEmpty),
408 cmocka_unit_test(lexInt),
409 cmocka_unit_test(lexDecimal),
410 cmocka_unit_test(lexBinary),
411 cmocka_unit_test(lexSigilBinary),
412 cmocka_unit_test(lexHex),
413 cmocka_unit_test(lexSigilHex),
414 cmocka_unit_test(lexSigilSegmentHex),
415 cmocka_unit_test(lexInvalidDecimal),
416 cmocka_unit_test(lexInvalidHex),
417 cmocka_unit_test(lexInvalidBinary),
418 cmocka_unit_test(lexTruncatedHex),
419 cmocka_unit_test(lexTruncatedSigilHex),
420 cmocka_unit_test(lexTruncatedBinary),
421 cmocka_unit_test(lexTruncatedSigilBinary),
422 cmocka_unit_test(lexIdentifier),
423 cmocka_unit_test(lexAddOperator),
424 cmocka_unit_test(lexIdentifierAddOperator),
425 cmocka_unit_test(lexSubOperator),
426 cmocka_unit_test(lexIdentifierSubOperator),
427 cmocka_unit_test(lexMulOperator),
428 cmocka_unit_test(lexIdentifierMulOperator),
429 cmocka_unit_test(lexDivOperator),
430 cmocka_unit_test(lexIdentifierDivOperator),
431 cmocka_unit_test(lexAndOperator),
432 cmocka_unit_test(lexIdentifierAndOperator),
433 cmocka_unit_test(lexOrOperator),
434 cmocka_unit_test(lexIdentifierOrOperator),
435 cmocka_unit_test(lexXorOperator),
436 cmocka_unit_test(lexIdentifierXorOperator),
437 cmocka_unit_test(lexLessOperator),
438 cmocka_unit_test(lexIdentifierLessOperator),
439 cmocka_unit_test(lexGreaterOperator),
440 cmocka_unit_test(lexIdentifierGreaterOperator),
441 cmocka_unit_test(lexSimpleExpression),
442 cmocka_unit_test(lexOpenParen),
443 cmocka_unit_test(lexCloseParen),
444 cmocka_unit_test(lexIdentifierCloseParen),
445 cmocka_unit_test(lexParentheticalExpression),
446 cmocka_unit_test(lexNestedParentheticalExpression))