2026-04-13 07:57:36 -06:00
|
|
|
#include "arena.h"
|
2026-04-30 09:58:27 -06:00
|
|
|
#include "arraylist.h"
|
2026-03-25 07:43:00 -06:00
|
|
|
#include "lexer.h"
|
|
|
|
|
#include "parser.h"
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
#include <cmocka.h>
|
2026-02-28 13:59:02 -06:00
|
|
|
|
2026-03-25 07:43:00 -06:00
|
|
|
static void test_parsing_basic_expression(void **state) {
|
|
|
|
|
(void) state;
|
|
|
|
|
|
|
|
|
|
char expr[256] = "2 + 3 / 66 * 789";
|
2026-04-30 09:58:27 -06:00
|
|
|
TokenizeResult tokens = tokenize(expr);
|
2026-03-25 07:43:00 -06:00
|
|
|
|
2026-04-30 09:58:27 -06:00
|
|
|
assert_true(tokens.is_valid);
|
|
|
|
|
assert_int_equal(arraylist_size(tokens.arr), 7);
|
|
|
|
|
|
|
|
|
|
ParseResult result = parse(tokens);
|
2026-03-25 10:22:54 -06:00
|
|
|
// Assert head is +
|
2026-04-13 08:44:30 -06:00
|
|
|
assert_int_equal(result.tree->type, NODE_BINARY_OP);
|
|
|
|
|
assert_int_equal(result.tree->data.binary.op, OP_ADD);
|
2026-03-25 10:22:54 -06:00
|
|
|
|
2026-04-13 08:44:30 -06:00
|
|
|
assert_int_equal(result.tree->data.binary.left->type, NODE_INTEGER);
|
|
|
|
|
assert_int_equal(result.tree->data.binary.left->data.integer, 2);
|
2026-03-25 10:22:54 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->type,
|
2026-03-25 10:22:54 -06:00
|
|
|
NODE_BINARY_OP
|
|
|
|
|
);
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.op,
|
2026-03-25 10:22:54 -06:00
|
|
|
OP_MUL
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.right->type,
|
2026-03-25 10:22:54 -06:00
|
|
|
NODE_INTEGER);
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.right->data.integer,
|
2026-03-25 10:22:54 -06:00
|
|
|
789);
|
|
|
|
|
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->type,
|
2026-03-25 10:22:54 -06:00
|
|
|
NODE_BINARY_OP
|
|
|
|
|
);
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->data.binary.op,
|
2026-03-25 10:22:54 -06:00
|
|
|
OP_DIV
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->data.binary.right->type,
|
2026-03-25 10:22:54 -06:00
|
|
|
NODE_INTEGER
|
|
|
|
|
);
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->data.binary.right->data.integer,
|
2026-03-25 10:22:54 -06:00
|
|
|
66
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->data.binary.left->type,
|
2026-03-25 10:22:54 -06:00
|
|
|
NODE_INTEGER
|
|
|
|
|
);
|
|
|
|
|
assert_int_equal(
|
2026-04-13 08:44:30 -06:00
|
|
|
result.tree->data.binary.right->data.binary.left->data.binary.left->data.integer,
|
2026-03-25 10:22:54 -06:00
|
|
|
3
|
|
|
|
|
);
|
2026-04-13 08:44:30 -06:00
|
|
|
arena_destroy(&result.arena);
|
2026-03-25 07:43:00 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
|
const struct CMUnitTest tests [] = {
|
|
|
|
|
cmocka_unit_test(test_parsing_basic_expression),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
2026-02-28 13:59:02 -06:00
|
|
|
}
|