Automated unit test generation for C remains a formidable challenge due to the semantic gap between high-level program intent and the rigid syntactic constraints of pointer arithmetic and manual memory management. While Large Language Models (LLMs) exhibit strong generative capabilities, direct intent-to-code synthesis frequently suffers from the leap-to-code failure mode, where models prematurely emit code without grounding in program structure, constraints, and semantics. This will result in non-compilable tests, hallucinated function signatures, low branch coverage, and semantically irrelevant assertions that cannot properly capture bugs. We introduce SPARC, a neuro-symbolic, scenario-based framework that bridges this gap through four stages: (1) Control Flow Graph (CFG) analysis, (2) an Operation Map that grounds LLM reasoning in validated utility helpers, (3) Path-targeted test synthesis, and (4) an iterative, self-correction validation loop using compiler and runtime feedback. We evaluate SPARC on 59 real-world and algorithmic subjects, where it outperforms the vanilla prompt generation baseline by 31.36% in line coverage, 26.01% in branch coverage, and 20.78% in mutation score, matching or exceeding the symbolic execution tool KLEE on complex subjects. SPARC retains 94.3% of tests through iterative repair and produces code with significantly higher developer-rated readability and maintainability. By aligning LLM reasoning with program structure, SPARC provides a scalable path for industrial-grade testing of legacy C codebases.
翻译:针对C语言的自动化单元测试生成,由于高层程序意图与指针运算和手动内存管理的严格语法约束之间存在语义鸿沟,仍是一项艰巨挑战。尽管大语言模型展现出强大的生成能力,但直接从意图到代码的合成常受“跳跃至代码”故障模式的影响,即模型在未基于程序结构、约束和语义的情况下过早生成代码。这将导致测试无法编译、函数签名幻觉、分支覆盖率低以及无法正确捕获错误的语义无关断言。我们提出SPARC,一种神经符号化、基于场景的框架,通过四个阶段弥合这一鸿沟:(1) 控制流图分析,(2) 将LLM推理锚定于已验证实用辅助函数的操作映射,(3) 路径导向的测试合成,以及(4) 利用编译器和运行时反馈的迭代自校正验证循环。我们在59个真实世界和算法主题上评估SPARC,其在线覆盖率、分支覆盖率和变异分数上分别比原始提示生成基线高出31.36%、26.01%和20.78%,在复杂主题上达到或超越了符号执行工具KLEE的性能。SPARC通过迭代修复保留了94.3%的测试,并生成具有显著更高开发者评价可读性和可维护性的代码。通过将LLM推理与程序结构对齐,SPARC为遗留C代码库的工业级测试提供了可扩展的路径。