Program Sketching by Automatically Generating Mocks from Tests

Nate F. F. Bragg, Jeffrey S. Foster, Cody Roux, Armando Solar-Lezama

Abstract

Program synthesis tools like Sketch face significant scalability challenges because they typically inline all function calls, leading to an exponential explosion in the underlying SAT formulas. We present Sketcham, a technique that introduces modularity into the synthesis process by automatically transforming existing test harnesses into "mocks." By decoupling function dependencies, Sketcham allows the solver to focus on individual components. Our evaluation across ten benchmarks shows that Sketcham achieves up to a 5x median speedup and successfully synthesizes programs that were previously unsolvable due to timeouts.

1. Introduction

Program synthesis—the automated generation of code from high-level specifications—is a powerful paradigm for software development. The Sketch synthesis tool is a prominent example, allowing developers to provide "sketches" (code with holes) and "harnesses" (tests). However, Sketch is inherently monolithic: it solves for all unknowns simultaneously by inlining function calls. As program complexity grows, the resulting search space becomes intractable.

While human developers often use "mocks" to isolate components during testing, manually writing mocks for synthesis is labor-intensive. Sketcham addresses this by leveraging existing test harnesses to generate mocks automatically, effectively turning tests into partial specifications that guide the synthesis of calling functions.

2. Methodology: From Tests to Mocks

Sketcham implements a three-stage pipeline to modularize the synthesis task:

  1. BuildAssertMap: The tool identifies assertions within test harnesses and maps them to the specific functions they constrain. Currently, the system focuses on assertions involving single function calls to maintain simplicity.
  2. GenerateMocks: For a target function, Sketcham creates a mock version that utilizes an uninterpreted function. This mock is constrained by assume statements derived from the original tests. For instance, a test case assert g(10) == 20 results in a mock that assumes an output of 20 whenever the input is 10.
  3. MockHarnesses: The program's call graph is restructured. New function variants are created that call these simplified mocks instead of the full, complex implementations.
Original harness call graph
Mocked harness call graph

Importantly, Sketcham maintains correctness by using a "solve-then-verify" approach. It first finds a candidate solution using the mocked version and then verifies that solution against the original, full implementation.

3. Experimental Evaluation

We evaluated Sketcham on ten benchmarks, including algorithmic tasks like Fibonacci sequence generation, string searching (Boyer-Moore), and complex data processing like array deduplication and date-time parsing.

3.1 Key Results

The results indicate that decoupling the search space significantly improves performance:

  • Performance Gains: Sketcham improved median performance on 6 out of 10 benchmarks, with speedups reaching 5x.
  • Scalability: The datetime benchmark, which represents a simplified version of the C strptime function, timed out in standard Sketch but was solved in seconds using Sketcham.
  • Synthesis Efficiency: In the spellcheck benchmark, the time required for the synthesis phase was reduced by over 300x.
  • Low Overhead: The mock generation process is highly efficient, typically accounting for less than 0.4% of the total execution time.

4. Discussion and Conclusion

The core insight of Sketcham is that tests serve as implicit specifications. By treating them as the source of truth for mocks, the tool prevents "hole interference," where incorrect guesses in low-level functions hinder the synthesis of high-level logic.

While Sketcham shows great promise, it currently faces limitations with multi-function assertions and deep recursion. Future work will focus on expanding the types of supported assertions and optimizing mock construction for even larger-scale software projects. By bridging the gap between testing and synthesis, Sketcham provides a viable path toward more scalable automated programming.

Summary updated

BibTeX
@inproceedings{bragg2021program,
  author = {Nate F. F. Bragg and Jeffrey S. Foster and Cody Roux and Armando Solar-Lezama},
  title = {{Program Sketching by Automatically Generating Mocks from Tests}},
  booktitle = {CAV},
  year = {2021},
  doi = {10.1007/978-3-030-81685-8\_38},
}
Figure