src/debugger/test/parser.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
10struct LPTest {
11 struct LexVector lv;
12 struct ParseTree tree;
13};
14
15#define PARSE(STR) \
16 struct LPTest* lp = *state; \
17 lexFree(&lp->lv); \
18 LexVectorClear(&lp->lv); \
19 size_t adjusted = lexExpression(&lp->lv, STR, strlen(STR), ""); \
20 assert_false(adjusted > strlen(STR)); \
21 struct ParseTree* tree = &lp->tree; \
22 parseLexedExpression(tree, &lp->lv)
23
24M_TEST_SUITE_SETUP(Parser) {
25 struct LPTest* lp = malloc(sizeof(struct LPTest));
26 LexVectorInit(&lp->lv, 0);
27 *state = lp;
28 return 0;
29}
30
31M_TEST_SUITE_TEARDOWN(Parser) {
32 struct LPTest* lp = *state;
33 parseFree(&lp->tree); \
34 lexFree(&lp->lv);
35 LexVectorDeinit(&lp->lv);
36 free(lp);
37 return 0;
38}
39
40M_TEST_DEFINE(parseEmpty) {
41 PARSE("");
42
43 assert_int_equal(tree->token.type, TOKEN_ERROR_TYPE);
44}
45
46M_TEST_DEFINE(parseInt) {
47 PARSE("0");
48
49 assert_int_equal(tree->token.type, TOKEN_UINT_TYPE);
50 assert_int_equal(tree->token.uintValue, 0);
51}
52
53M_TEST_DEFINE(parseLexError) {
54 PARSE("@");
55
56 assert_int_equal(tree->token.type, TOKEN_ERROR_TYPE);
57}
58
59M_TEST_DEFINE(parseSimpleExpression) {
60 PARSE("1+2");
61
62 assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
63 assert_int_equal(tree->token.operatorValue, OP_ADD);
64 assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
65 assert_int_equal(tree->lhs->token.uintValue, 1);
66 assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
67 assert_int_equal(tree->rhs->token.uintValue, 2);
68}
69
70M_TEST_DEFINE(parseAddMultplyExpression) {
71 PARSE("1+2*3");
72
73 assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
74 assert_int_equal(tree->token.operatorValue, OP_ADD);
75 assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
76 assert_int_equal(tree->lhs->token.uintValue, 1);
77 assert_int_equal(tree->rhs->token.type, TOKEN_OPERATOR_TYPE);
78 assert_int_equal(tree->rhs->token.uintValue, OP_MULTIPLY);
79 assert_int_equal(tree->rhs->lhs->token.type, TOKEN_UINT_TYPE);
80 assert_int_equal(tree->rhs->lhs->token.uintValue, 2);
81 assert_int_equal(tree->rhs->rhs->token.type, TOKEN_UINT_TYPE);
82 assert_int_equal(tree->rhs->rhs->token.uintValue, 3);
83}
84
85M_TEST_DEFINE(parseParentheticalExpression) {
86 PARSE("(1+2)");
87
88 assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
89 assert_int_equal(tree->token.operatorValue, OP_ADD);
90 assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
91 assert_int_equal(tree->lhs->token.uintValue, 1);
92 assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
93 assert_int_equal(tree->rhs->token.uintValue, 2);
94}
95
96M_TEST_DEFINE(parseParentheticalAddMultplyExpression) {
97 PARSE("(1+2)*3");
98
99 assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
100 assert_int_equal(tree->token.operatorValue, OP_MULTIPLY);
101 assert_int_equal(tree->lhs->token.type, TOKEN_OPERATOR_TYPE);
102 assert_int_equal(tree->lhs->token.uintValue, OP_ADD);
103 assert_int_equal(tree->lhs->lhs->token.type, TOKEN_UINT_TYPE);
104 assert_int_equal(tree->lhs->lhs->token.uintValue, 1);
105 assert_int_equal(tree->lhs->lhs->token.type, TOKEN_UINT_TYPE);
106 assert_int_equal(tree->lhs->rhs->token.uintValue, 2);
107 assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
108 assert_int_equal(tree->rhs->token.uintValue, 3);
109}
110
111M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(Parser,
112 cmocka_unit_test(parseEmpty),
113 cmocka_unit_test(parseInt),
114 cmocka_unit_test(parseLexError),
115 cmocka_unit_test(parseSimpleExpression),
116 cmocka_unit_test(parseAddMultplyExpression),
117 cmocka_unit_test(parseParentheticalExpression),
118 cmocka_unit_test(parseParentheticalAddMultplyExpression))