all repos — mgba @ ab2437fcb8e2216b1504f60bf3835d255ccff0d4

mGBA Game Boy Advance Emulator

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.lhs); \
 34	parseFree(lp->tree.rhs); \
 35	lexFree(&lp->lv);
 36	LexVectorDeinit(&lp->lv);
 37	free(lp);
 38	return 0;
 39}
 40
 41M_TEST_DEFINE(parseEmpty) {
 42	PARSE("");
 43
 44	assert_int_equal(tree->token.type, TOKEN_ERROR_TYPE);
 45}
 46
 47M_TEST_DEFINE(parseInt) {
 48	PARSE("0");
 49
 50	assert_int_equal(tree->token.type, TOKEN_UINT_TYPE);
 51	assert_int_equal(tree->token.uintValue, 0);
 52}
 53
 54M_TEST_DEFINE(parseLexError) {
 55	PARSE("@");
 56
 57	assert_int_equal(tree->token.type, TOKEN_ERROR_TYPE);
 58}
 59
 60M_TEST_DEFINE(parseSimpleExpression) {
 61	PARSE("1+2");
 62
 63	assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
 64	assert_int_equal(tree->token.operatorValue, OP_ADD);
 65	assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
 66	assert_int_equal(tree->lhs->token.uintValue, 1);
 67	assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
 68	assert_int_equal(tree->rhs->token.uintValue, 2);
 69}
 70
 71M_TEST_DEFINE(parseAddMultplyExpression) {
 72	PARSE("1+2*3");
 73
 74	assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
 75	assert_int_equal(tree->token.operatorValue, OP_ADD);
 76	assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
 77	assert_int_equal(tree->lhs->token.uintValue, 1);
 78	assert_int_equal(tree->rhs->token.type, TOKEN_OPERATOR_TYPE);
 79	assert_int_equal(tree->rhs->token.uintValue, OP_MULTIPLY);
 80	assert_int_equal(tree->rhs->lhs->token.type, TOKEN_UINT_TYPE);
 81	assert_int_equal(tree->rhs->lhs->token.uintValue, 2);
 82	assert_int_equal(tree->rhs->rhs->token.type, TOKEN_UINT_TYPE);
 83	assert_int_equal(tree->rhs->rhs->token.uintValue, 3);
 84}
 85
 86M_TEST_DEFINE(parseParentheticalExpression) {
 87	PARSE("(1+2)");
 88
 89	assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
 90	assert_int_equal(tree->token.operatorValue, OP_ADD);
 91	assert_int_equal(tree->lhs->token.type, TOKEN_UINT_TYPE);
 92	assert_int_equal(tree->lhs->token.uintValue, 1);
 93	assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
 94	assert_int_equal(tree->rhs->token.uintValue, 2);
 95}
 96
 97M_TEST_DEFINE(parseParentheticalAddMultplyExpression) {
 98	PARSE("(1+2)*3");
 99
100	assert_int_equal(tree->token.type, TOKEN_OPERATOR_TYPE);
101	assert_int_equal(tree->token.operatorValue, OP_MULTIPLY);
102	assert_int_equal(tree->lhs->token.type, TOKEN_OPERATOR_TYPE);
103	assert_int_equal(tree->lhs->token.uintValue, OP_ADD);
104	assert_int_equal(tree->lhs->lhs->token.type, TOKEN_UINT_TYPE);
105	assert_int_equal(tree->lhs->lhs->token.uintValue, 1);
106	assert_int_equal(tree->lhs->lhs->token.type, TOKEN_UINT_TYPE);
107	assert_int_equal(tree->lhs->rhs->token.uintValue, 2);
108	assert_int_equal(tree->rhs->token.type, TOKEN_UINT_TYPE);
109	assert_int_equal(tree->rhs->token.uintValue, 3);
110}
111
112M_TEST_SUITE_DEFINE_SETUP_TEARDOWN(Parser,
113	cmocka_unit_test(parseEmpty),
114	cmocka_unit_test(parseInt),
115	cmocka_unit_test(parseLexError),
116	cmocka_unit_test(parseSimpleExpression),
117	cmocka_unit_test(parseAddMultplyExpression),
118	cmocka_unit_test(parseParentheticalExpression),
119	cmocka_unit_test(parseParentheticalAddMultplyExpression))