Skip to content

Commit

Permalink
tests: add tests for the assembler
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Sep 19, 2024
1 parent 8c07c2c commit 8a80a7c
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/cmake-build-debug/
/.idea
/.idea
*~
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ flex_target(lexer src/lexer.l "${LEXER_OUT}" DEFINES_FILE ${LEXER_DIR}/lexer.h)

file(GLOB_RECURSE SOURCE "src/*.c")
file(GLOB UNITY_SOURCE "libs/Unity/src/unity.c")
set(TEST_UTILS ${UNITY_SOURCE} ${SOURCE} ${LEXER_OUT} )

add_executable(HAL64 main.c ${SOURCE} ${LEXER_OUT})
add_executable(TESTS_LEXER test/lexer.c ${UNITY_SOURCE} ${SOURCE} ${LEXER_OUT})
add_executable(TESTS_LEXER test/lexer.c ${TEST_UTILS})
add_executable(TESTS_ASSEMBLER test/assembler.c ${TEST_UTILS})
93 changes: 93 additions & 0 deletions test/assembler.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include "unity.h"
#include "assembler/assembler.h"

void
setUp(void)
{}

void
tearDown(void)
{}

void
compare_instructions(Instruction *expected, Instruction *actual, size_t count)
{
size_t i;
for (i = 0; i < count; i++) {
TEST_ASSERT_EQUAL(expected[i].op, actual[i].op);
switch (expected[i].op) {
case OP_PUSH_LITERAL_STRING:
TEST_ASSERT_EQUAL_STRING(expected[i].data.string.ptr, actual[i].data.string.ptr);
break;
case OP_PUSH_I64:
TEST_ASSERT_EQUAL(expected[i].data.immediate, actual[i].data.immediate);
break;
default:
break;
}
}
}

void
parse_header(void)
{
const char *source =
"---\n"
"globals: 69\n"
"global_pointers: 420\n"
"---";

Program program = assemble(source);

TEST_ASSERT_EQUAL(69, program.globals_count);
TEST_ASSERT_EQUAL(420, program.global_pointers_count);
}

void
parse_function(void)
{
const char *source =
"---\n"
"globals: 0\n"
"global_pointers: 0\n"
"---\n"
":0 { args: 0 ptr_args: 0 locals: 0 local_pointers: 0 } {\n"
" PushI64 30;\n"
" PushI64 12;\n"
" AddI64;\n"
" PrintTopStackI64;\n"
" Exit;\n"
"}\n";

Program program = assemble(source);
Instruction expected[] = {
{.op = OP_PUSH_I64, .data.immediate = 30},
{.op = OP_PUSH_I64, .data.immediate = 12},
{.op = OP_ADD_I64},
{.op = OP_PRINT_TOP_STACK_I64},
{.op = OP_EXIT},
};

TEST_ASSERT_EQUAL(0, program.globals_count);
TEST_ASSERT_EQUAL(0, program.global_pointers_count);
TEST_ASSERT_EQUAL(1, program.functions_count);
TEST_ASSERT_EQUAL(0, program.functions[0].args_count);
TEST_ASSERT_EQUAL(0, program.functions[0].ptr_args_count);
TEST_ASSERT_EQUAL(0, program.functions[0].locals_count);
TEST_ASSERT_EQUAL(0, program.functions[0].local_pointers_count);
TEST_ASSERT_EQUAL(sizeof(expected) / sizeof(expected[0]),
program.functions[0].instructions_count);
compare_instructions(
expected,
program.functions[0].instructions,
program.functions[0].instructions_count);
}

int
main(void)
{
UNITY_BEGIN();
RUN_TEST(parse_header);
RUN_TEST(parse_function);
return UNITY_END();
}

0 comments on commit 8a80a7c

Please sign in to comment.