Compare commits

..

6 Commits

Author SHA1 Message Date
ff1927a5c6 Add symbols tests
All checks were successful
Validate the build / validate-build (push) Successful in 30s
2025-04-09 00:14:53 +02:00
7223c31154 initial symbol table implementation 2025-04-09 00:14:44 +02:00
8025f7f8e8 Add .import and .export to the input test file 2025-04-09 00:14:44 +02:00
41867694e2 Make main properly return with failure on parsing errors 2025-04-09 00:14:44 +02:00
7596e54191 Add .import and .export directive to the grammar and parser 2025-04-09 00:14:44 +02:00
c43aab3a2d fix parse_immediate to accept label_reference instead of identifier 2025-04-09 00:14:44 +02:00
2 changed files with 144 additions and 0 deletions

View File

@ -0,0 +1,65 @@
lbl_0: ; 65 symbols used for testing growing the symbols table
lbl_1:
lbl_2:
lbl_3:
lbl_4:
lbl_5:
lbl_6:
lbl_7:
lbl_8:
lbl_9:
lbl_10:
lbl_11:
lbl_12:
lbl_13:
lbl_14:
lbl_15:
lbl_16:
lbl_17:
lbl_18:
lbl_19:
lbl_20:
lbl_21:
lbl_22:
lbl_23:
lbl_24:
lbl_25:
lbl_26:
lbl_27:
lbl_28:
lbl_29:
lbl_30:
lbl_31:
lbl_32:
lbl_33:
lbl_34:
lbl_35:
lbl_36:
lbl_37:
lbl_38:
lbl_39:
lbl_40:
lbl_41:
lbl_42:
lbl_43:
lbl_44:
lbl_45:
lbl_46:
lbl_47:
lbl_48:
lbl_49:
lbl_50:
lbl_51:
lbl_52:
lbl_53:
lbl_54:
lbl_55:
lbl_56:
lbl_57:
lbl_58:
lbl_59:
lbl_60:
lbl_61:
lbl_62:
lbl_63:
lbl_64:

View File

@ -259,6 +259,83 @@ MunitResult test_symbol_add_export(const MunitParameter params[], void *data) {
return MUNIT_OK;
}
MunitResult test_symbol_table_growth(const MunitParameter params[], void *data) {
(void)params;
(void)data;
ast_node_t *root;
tokenlist_t *list;
symbol_table_t *table = nullptr;
// Set up with our manysymbols.asm file
symbols_setup_test(&root, &list, "tests/input/manysymbols.asm");
symbol_table_alloc(&table);
// Initial capacity should be the default (64)
munit_assert_size(table->cap, ==, 64);
munit_assert_size(table->len, ==, 0);
// Add the first 64 labels (indices 0-63)
size_t initial_cap = table->cap;
for (size_t i = 0; i < 64; i++) {
ast_node_t *label = root->children[i];
munit_assert_int(label->id, ==, NODE_LABEL);
error_t *err = symbol_table_update(table, label);
munit_assert_null(err);
munit_assert_size(table->len, ==, i + 1);
// Capacity should remain the same for the first 64 labels
munit_assert_size(table->cap, ==, initial_cap);
}
// Now add the 65th label (index 64), which should trigger growth
ast_node_t *final_label = root->children[64];
munit_assert_int(final_label->id, ==, NODE_LABEL);
error_t *err = symbol_table_update(table, final_label);
munit_assert_null(err);
munit_assert_size(table->len, ==, 65);
// Capacity should have doubled
munit_assert_size(table->cap, ==, initial_cap * 2);
// Validate we can look up all the symbols
for (size_t i = 0; i <= 64; i++) {
char name[10];
sprintf(name, "lbl_%zu", i);
symbol_t *symbol = symbol_table_lookup(table, name);
munit_assert_not_null(symbol);
munit_assert_int(SYMBOL_LOCAL, ==, symbol->kind);
munit_assert_string_equal(symbol->name, name);
}
symbol_table_free(table);
ast_node_free(root);
tokenlist_free(list);
return MUNIT_OK;
}
MunitResult test_symbol_invalid_node(const MunitParameter params[], void *data) {
(void)params;
(void)data;
ast_node_t *root;
tokenlist_t *list;
symbol_table_t *table = nullptr;
symbols_setup_test(&root, &list, "tests/input/symbols.asm");
symbol_table_alloc(&table);
munit_assert_size(table->len, ==, 0);
error_t *err = symbol_table_update(table, root);
munit_assert_ptr_equal(err, err_symbol_table_invalid_node);
munit_assert_size(table->len, ==, 0);
symbol_table_free(table);
ast_node_free(root);
tokenlist_free(list);
return MUNIT_OK;
}
MunitTest symbols_tests[] = {
{"/table_alloc", test_symbol_table_alloc, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{"/table_lookup_empty", test_symbol_table_lookup_empty, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
@ -268,5 +345,7 @@ MunitTest symbols_tests[] = {
{"/add_export", test_symbol_add_export, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{"/upgrade_valid", test_symbol_upgrade_valid, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{"/upgrade_invalid", test_symbol_upgrade_invalid, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{"/table_growth", test_symbol_table_growth, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{"/invalid_node", test_symbol_invalid_node, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr},
{nullptr, nullptr, nullptr, nullptr, MUNIT_TEST_OPTION_NONE, nullptr}
};