/** * @file test_scf.c * @brief SCF format tests */ #include #include #include int main() { printf("Testing SCF format implementation...\n"); // Test 1: Initialization scf_t scf; scf_init(&scf); if (memcmp(scf.header.magic, SCF_MAGIC, 4) != 0) { printf("FAIL: Magic number incorrect\n"); return 1; } if (scf.header.version != SCF_VERSION) { printf("FAIL: Version incorrect\n"); return 1; } if (scf.header.arch != SCF_ARCH_UNKNOWN) { printf("FAIL: Architecture incorrect\n"); return 1; } printf("Test 1 PASSED: Initialization successful\n"); // Test 2: Adding symbols scf_sym_t sym = {0}; sym.name_offset = 0; sym.scf_sym_type = SCF_SYM_TYPE_FUNC; sym.scf_sym_bind = SCF_SYM_BIND_GLOBAL; sym.scf_sym_vis = SCF_SYM_VIS_DEFAULT; sym.scf_sect_type = SCF_SECT_CODE; sym.scf_sect_offset = 0; sym.scf_sym_size = 16; if (!scf_add_sym(&scf, &sym)) { printf("FAIL: Cannot add symbol\n"); return 1; } if (scf.header.sym_count != 1) { printf("FAIL: Symbol count incorrect\n"); return 1; } printf("Test 2 PASSED: Symbol addition successful\n"); // Test 3: Adding relocations scf_reloc_t reloc = {0}; reloc.offset = 0; // 偏移量 reloc.sym_idx = 0; reloc.type = SCF_RELOC_ABS; reloc.sect_type = SCF_SECT_CODE; // 代码段 reloc.addend = 0; if (!scf_add_reloc(&scf, &reloc)) { printf("FAIL: Cannot add relocation\n"); return 1; } if (scf.header.reloc_count != 1) { printf("FAIL: Relocation count incorrect\n"); return 1; } printf("Test 3 PASSED: Relocation addition successful\n"); // Test 4: Checking validity if (!scf_check_valid(&scf)) { printf("FAIL: SCF structure invalid\n"); return 1; } printf("Test 4 PASSED: SCF structure valid\n"); // Test 5: Writing and reading char buffer[1024]; if (!scf_write(&scf, buffer, sizeof(buffer))) { printf("FAIL: Cannot write to buffer\n"); return 1; } scf_t scf2; scf_init(&scf2); if (!scf_parse(&scf2, buffer, sizeof(buffer))) { printf("FAIL: Cannot read from buffer\n"); return 1; } // Compare the two structures if (memcmp(&scf.header, &scf2.header, sizeof(scf_header_t)) != 0) { printf("FAIL: Header mismatch\n"); return 1; } printf("Test 5 PASSED: Write/read successful\n"); printf("All tests passed!\n"); return 0; }