|
#include "test_utils.h"
|
|
#include "../ir/ir.h"
|
|
#include "../runtime/labeltable.h"
|
|
|
|
UnittestTestResult_t* test_create_empty(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_create_empty");
|
|
|
|
RavaInstructionList_t* list = rava_instruction_list_create();
|
|
LabelTable_t* table = rava_labeltable_create(list);
|
|
UNITTEST_ASSERT_NOT_NULL(_unittest_result, table, "table should not be NULL");
|
|
rava_labeltable_destroy(table);
|
|
rava_instruction_list_destroy(list);
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_create_null(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_create_null");
|
|
|
|
LabelTable_t* table = rava_labeltable_create(NULL);
|
|
UNITTEST_ASSERT_NULL(_unittest_result, table, "table should be NULL for NULL input");
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_single_label(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_single_label");
|
|
|
|
RavaInstructionList_t* list = rava_instruction_list_create();
|
|
|
|
RavaInstruction_t instr1 = {.opcode = RAVA_OP_LOAD_CONST, .operand.int_value = 10};
|
|
RavaInstruction_t instr2 = {.opcode = RAVA_OP_LABEL, .operand.label_id = 0};
|
|
RavaInstruction_t instr3 = {.opcode = RAVA_OP_LOAD_CONST, .operand.int_value = 20};
|
|
|
|
rava_instruction_list_add(list, instr1);
|
|
rava_instruction_list_add(list, instr2);
|
|
rava_instruction_list_add(list, instr3);
|
|
|
|
LabelTable_t* table = rava_labeltable_create(list);
|
|
UNITTEST_ASSERT_NOT_NULL(_unittest_result, table, "table should not be NULL");
|
|
|
|
size_t pc = rava_labeltable_lookup(table, 0);
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 2, (int)pc, "label 0 should point to PC 2");
|
|
|
|
rava_labeltable_destroy(table);
|
|
rava_instruction_list_destroy(list);
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_multiple_labels(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_multiple_labels");
|
|
|
|
RavaInstructionList_t* list = rava_instruction_list_create();
|
|
|
|
RavaInstruction_t instr0 = {.opcode = RAVA_OP_LOAD_CONST, .operand.int_value = 0};
|
|
RavaInstruction_t label0 = {.opcode = RAVA_OP_LABEL, .operand.label_id = 0};
|
|
RavaInstruction_t instr1 = {.opcode = RAVA_OP_LOAD_CONST, .operand.int_value = 1};
|
|
RavaInstruction_t label1 = {.opcode = RAVA_OP_LABEL, .operand.label_id = 1};
|
|
RavaInstruction_t instr2 = {.opcode = RAVA_OP_LOAD_CONST, .operand.int_value = 2};
|
|
RavaInstruction_t label2 = {.opcode = RAVA_OP_LABEL, .operand.label_id = 2};
|
|
|
|
rava_instruction_list_add(list, instr0);
|
|
rava_instruction_list_add(list, label0);
|
|
rava_instruction_list_add(list, instr1);
|
|
rava_instruction_list_add(list, label1);
|
|
rava_instruction_list_add(list, instr2);
|
|
rava_instruction_list_add(list, label2);
|
|
|
|
LabelTable_t* table = rava_labeltable_create(list);
|
|
UNITTEST_ASSERT_NOT_NULL(_unittest_result, table, "table should not be NULL");
|
|
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 2, (int)rava_labeltable_lookup(table, 0), "label 0 should point to PC 2");
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 4, (int)rava_labeltable_lookup(table, 1), "label 1 should point to PC 4");
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 6, (int)rava_labeltable_lookup(table, 2), "label 2 should point to PC 6");
|
|
|
|
rava_labeltable_destroy(table);
|
|
rava_instruction_list_destroy(list);
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_missing_label(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_missing_label");
|
|
|
|
RavaInstructionList_t* list = rava_instruction_list_create();
|
|
|
|
RavaInstruction_t label = {.opcode = RAVA_OP_LABEL, .operand.label_id = 5};
|
|
rava_instruction_list_add(list, label);
|
|
|
|
LabelTable_t* table = rava_labeltable_create(list);
|
|
UNITTEST_ASSERT_NOT_NULL(_unittest_result, table, "table should not be NULL");
|
|
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 0, (int)rava_labeltable_lookup(table, 0), "missing label 0 should return 0");
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 0, (int)rava_labeltable_lookup(table, 1), "missing label 1 should return 0");
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 1, (int)rava_labeltable_lookup(table, 5), "label 5 should point to PC 1");
|
|
|
|
rava_labeltable_destroy(table);
|
|
rava_instruction_list_destroy(list);
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
UnittestTestResult_t* test_out_of_range(void) {
|
|
UNITTEST_BEGIN_TEST("TestLabelTable", "test_out_of_range");
|
|
|
|
RavaInstructionList_t* list = rava_instruction_list_create();
|
|
|
|
RavaInstruction_t label = {.opcode = RAVA_OP_LABEL, .operand.label_id = 2};
|
|
rava_instruction_list_add(list, label);
|
|
|
|
LabelTable_t* table = rava_labeltable_create(list);
|
|
UNITTEST_ASSERT_NOT_NULL(_unittest_result, table, "table should not be NULL");
|
|
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 0, (int)rava_labeltable_lookup(table, 100), "out of range label should return 0");
|
|
UNITTEST_ASSERT_EQUAL(_unittest_result, 0, (int)rava_labeltable_lookup(table, -1), "negative label should return 0");
|
|
|
|
rava_labeltable_destroy(table);
|
|
rava_instruction_list_destroy(list);
|
|
|
|
UNITTEST_END_TEST();
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
UnittestConfig_t *config = unittest_config_create();
|
|
config->verbosity = 2;
|
|
|
|
if (argc > 1 && strcmp(argv[1], "--json") == 0) {
|
|
config->output_format = UNITTEST_FORMAT_JSON;
|
|
config->use_colors = false;
|
|
}
|
|
|
|
UnittestTestSuite_t *suite = unittest_test_suite_create("Label Table Tests");
|
|
|
|
UnittestTestCase_t *tc = unittest_test_case_create("TestLabelTable");
|
|
unittest_test_case_add_result(tc, test_create_empty());
|
|
unittest_test_case_add_result(tc, test_create_null());
|
|
unittest_test_case_add_result(tc, test_single_label());
|
|
unittest_test_case_add_result(tc, test_multiple_labels());
|
|
unittest_test_case_add_result(tc, test_missing_label());
|
|
unittest_test_case_add_result(tc, test_out_of_range());
|
|
unittest_test_suite_add_test_case(suite, tc);
|
|
|
|
unittest_generate_report(suite, config);
|
|
|
|
int failures = suite->total_failed + suite->total_errors;
|
|
unittest_test_suite_destroy(suite);
|
|
unittest_config_destroy(config);
|
|
|
|
return failures > 0 ? 1 : 0;
|
|
}
|