Abstract
Large Language Models (LLMs) excel at code generation, yet their outputs often contain subtle bugs, for which effective test cases are a critical bottleneck. Existing test generation methods, whether based on prompting or supervised fine-tuning, rely on static datasets. This imposes a "fixed-difficulty ceiling", fundamentally limiting their ability to uncover novel or more complex bugs beyond their training scope. To overcome this, we introduce ATGen, a framework that trains a test case generator via adversarial reinforcement learning. ATGen pits a test generator against an adversarial code generator that continuously crafts harder bugs to evade the current policy. This dynamic loop creates a curriculum of increasing difficulty challenging current policy. The test generator is optimized via Reinforcement Learning (RL) to jointly maximize "Output Accuracy" and "Attack Success", enabling it to learn a progressively stronger policy that breaks the fixed-difficulty ceiling of static training. Extensive experiments demonstrate that ATGen significantly outperforms state-of-the-art baselines. We further validate its practical utility, showing it serves as both a more effective filter for Best-of-N inference and a higher-quality reward source for training code generation models. Our work establishes a new, dynamic paradigm for improving the reliability of LLM-generated code.
Introduction
While Large Language Models (LLMs) have become remarkably proficient at writing code, their output is frequently plagued by subtle logic flaws and "corner case" bugs. To catch these, high-quality test cases are critical. However, creating these tests manually is slow, and existing automated methods—usually based on prompting or Supervised Fine-Tuning (SFT)—rely on static datasets.
This reliance creates a "fixed-difficulty ceiling": if a model is only trained on a static set of bugs, it never learns to uncover novel or more complex errors beyond that specific training scope. As code generators get better at hiding their mistakes, test generators require a mechanism to "level up" alongside them.
The ATGen Framework
ATGen addresses this by treating test generation as a dynamic game between two players: a Test Generator and an Adversarial Code Generator.
The Test Generator (Policy) is trained via Reinforcement Learning (specifically the GRPO algorithm) to generate (input, output) pairs. The reward function for the RL agent is multi-component, balancing:
- IO Accuracy: Ensuring the test's expected output is correct according to the problem requirements.
- Attack Success: Rewarding the discovery of bugs in candidate code.
- Format Reward: Encouraging the model to use
<think>tags for internal reasoning, similar to the DeepSeek-R1 approach.
The Adversarial Code Generator is a separate model tasked with producing "hard" buggy code. This code must pass the current test generator's suite but fail against the ground-truth solution. This creates an Adversarial Loop: as the test generator learns to catch simple bugs, the code generator is forced to produce more "natural" and subtle flaws to evade detection. This dynamic curriculum pushes the test generator to reason more deeply about the problem's requirements.
Experimental Setup
The researchers used a subset of 3,000 problems from two popular competitive programming benchmarks, APPS and Codeforces, to generate a training set of 16,822 (Question, buggy code) pairs. A separate test set of 911 pairs was partitioned into Easy, Medium, and Hard tiers based on how difficult it was for a baseline model (Qwen2.5-7B) to find the bugs.
Results and Analysis
ATGen demonstrates state-of-the-art performance across all difficulty tiers, significantly outperforming both open-source and proprietary models.
Key Findings
- State-of-the-Art Performance: ATGen (using a Qwen2.5-7B backbone) achieved an Attack Rate of 36.99%, representing a nearly 60% relative improvement over the strongest proprietary baseline, GPT-4-turbo (23.38%).
- Superiority to SFT: It more than doubled the effectiveness of the previous SFT-based state-of-the-art, UTGen (16.24%).
- Best-of-N Filtering: When used to select the best code from a pool of candidates, ATGen reached a pass@1 of 35.00%, significantly closing the gap to the theoretical "Human Expert" upper bound of 38.33%.
- Adaptive vs. Unconditional Modes: For larger models (7B), an "Adaptive Mode"—only generating new adversarial code when the current bug is "solved"—proved more efficient, whereas smaller models (3B) benefited from a constant stream of new challenges.
Conclusion
The adversarial pressure in ATGen acts as a "synthetic evolution," forcing the model to move beyond simple syntax errors to catch deep logical flaws. By incorporating reasoning traces and RL, the model learns to derive outputs through logic rather than pattern memorization. While computationally expensive during training due to the sampling of adversarial candidates, ATGen-generated tests are high-quality enough to serve as a proxy for human-written test suites in training other code models. Future work will focus on scaling this adversarial RL approach to large-scale repositories with complex dependencies beyond self-contained competitive programming problems.
Summary updated