UseAIHereUseAIHere

How Can We Expose Software Errors Earlier? — Lecture 11 of Nanjing University's Generative Software Engineering

November 3, 2026 · UseAIHere

阅读中文版 →

How Can We Expose Software Errors Earlier? — Lecture 11 of Nanjing University's Generative Software Engineering

Video · Lecture notes · Slides

Lecture 11 at a glance

An agent-built game opens. The scene renders, and the player can move and shoot. Watching the screen tells you little about whether a shot costs exactly one round, a cooldown is enforced, or simultaneous inputs are handled correctly. A useful test starts with an expected behavior, then finds a way to trigger that behavior and observe the result.

1. A fault can survive a successful run

There are two gaps between intent, specification, and implementation. A specification can miss what a user cares about, and an implementation can violate a specification that was clear. Tests provide evidence about the implementation. Acceptance brings the check back to the user's situation.

Consider two nested loops. The inner loop uses j as its counter, but the increment is written as i++. The wrong variable in the code is a fault. When the loop executes and j fails to advance, the program reaches an error, a state that violates the specification. A caller who sees a hang or abnormal termination observes a failure. With a loop bound of zero, the faulty code stays dormant.

An error can also disappear from view. A cart may calculate the wrong cached total and still produce the right bill if checkout recomputes the price from the line items. An assertion after every cart update can check the cached total against their sum. The test supplies conditions that can activate the fault, and the assertion gives the resulting error somewhere to surface.

2. The program takes more inputs than its arguments

The same source can behave differently across operating systems, runtimes, and dependency versions. A lockfile records resolved dependencies, while the runtime and operating system still have to be accounted for separately. Time, files, network responses, and thread scheduling can affect even two runs in the same environment. If two threads both read a counter of zero before either writes, both can store one and lose an increment. Repeating the test on one thread never produces that interleaving.

A test sets up the conditions, performs an operation, and checks its result. One counterexample can disprove a claim about the implementation. Passing runs provide evidence for the conditions exercised. A broken sort that returns its input unchanged passes on an already sorted array. With a uniformly shuffled array of n distinct elements, it has only a 1/n! chance of returning the right order. That number applies to this particular faulty implementation and shows what a discriminating input can do.

3. Choose cases from the specification and check what ran

Black-box tests start with the specification. If an endpoint accepts at most 100 items, choose cases for empty, ordinary, and over-limit inputs, including boundary sizes such as 0, 1, 99, 100, and 101. Whether the endpoint rejects or truncates an oversized request is a decision the specification must settle. A decision table can cover combinations of course capacity and prerequisites. State-transition tests can check withdrawal, duplicate enrollment, and enrollment after withdrawal.

White-box tests look for logic the cases missed. Statement coverage records which lines ran. Branch coverage checks whether conditions took their different outcomes, and path coverage considers combinations of branches. An assertion still has to check what the execution did. A test of add(1, 0) passes even if the implementation subtracts. Checking add(1, 2) against the expected result exposes that fault.

The tests also operate at different scopes. A unit test checks prerequisite logic. An integration test checks that the enrollment service and database agree on identifiers and status. A system test can enroll a student and verify that the result survives a refresh. Acceptance checks that a full course really rejects another enrollment and that the student can understand the outcome. Exercising every condition through the UI raises the cost of both running and diagnosing the tests.

A stub can supply a fixed dependency response, and a mock can check whether an expected interaction occurred. These substitutes make timeouts and rejections easy to exercise. Separate integration checks still have to cover the real dependency. A regression case is most informative when it fails before the fix and passes afterward. Continuous integration reruns those checks as the software changes.

4. Test the rules without rendering the game

In a demonstration involving a CS:GO-style game, the agent repeatedly launched the game, interacted with it, and took screenshots until a request received a 413 response for being too large. A screenshot can show that a scene rendered. It gives weak evidence about ammunition, collision, cooldown, or victory conditions, and every run brings rendering, input handling, and game rules into the same experiment.

Represent game logic as a state transition and a test can pass in the old state and a shooting action, then inspect the resulting state. It can check an empty magazine or a change in health without starting the renderer. A pure function takes its inputs explicitly and has no side effects, so a test does not have to reconstruct the whole global environment. A controllable clock can advance a cooldown immediately, and a controllable source of randomness can replay an encounter. A smaller set of end-to-end cases can still verify how those rules connect to rendering and input.

5. Compare against a reference and search for better inputs

NEMU's differential testing runs two processor implementations from corresponding initial states. An early x86 version used QEMU as a reference. After the same instruction, the test can compare registers and flags. An incorrect parity flag may only affect the final output when a later instruction reads it. Comparing state after each instruction stops the run at the first divergence, with the instruction and prior state still available for inspection. XiangShan's DiffTest applies the same idea to processor verification. XiangShan DiffTest documentation

The comparison needs a well-defined basis. Two implementations can share a misunderstanding of the specification. When testing C compilers against each other, a program with undefined behavior can yield differing results without giving evidence of a compiler bug.

Finding inputs is a search problem. Random bytes often fail the first format check. AFL mutates existing samples, measures coverage, and retains inputs that reach new execution behavior for further mutation. New coverage helps direct the search. A crash, timeout, failed assertion, or difference from a reference is what calls for investigation. The two techniques can work together, with fuzzing searching for inputs and differential testing comparing the results. AFL technical notes

6. Turn a physical action into a repeatable test

Removing an SD card during a read is an input to its driver. In an early Android example, San Mehat wanted a small mechanism that could insert and remove an SD card under software control. With the mechanism in a test loop, a script can schedule removal during I/O, record the conditions of a failure, and repeat the same sequence.

An agent can write the script, operate the test rig, and collect logs. The experiment still needs a trigger, a place to observe the behavior, and a criterion for failure. In a game, a processor, or a device driver, an observation closer to the fault makes a surprising result easier to reproduce and investigate.

More articles →

Loading full version…