Stress Tester

A template harness for stress-testing competitive programming solutions — compare a fast solution against a brute-force reference on random inputs.

Stress Tester

A template harness for stress testing any competitive-programming task: you write a fast solution and a slow-but-obviously-correct brute, generate random tests, and stop at the first mismatch.


How the Pieces Fit

File Purpose
solution.cpp Your optimized implementation for the task you are debugging
brute.cpp A simple, trusted reference solution (may be slower)
gen.cpp Input generator — customize to emit random or edge-case inputs; accepts an optional seed for reproducibility
tester.py Orchestrates the loop: compile → generate → run solution vs. brute → report first difference
gen_notes.md Documentation for the reusable RNG helpers inside gen.cpp
gen_approach.md Broader notes on designing generators and edge cases
gen_tree.cpp / gen_tree2.cpp Standalone tree generators for problems that need random trees
input.txt Overwritten with the latest generated test case

Quick Start

Requirements: C++17 compiler (g++) and Python 3

# Run the harness
python tester.py

This will:

  1. Compile solution.cpp, brute.cpp, and gen.cpp with -std=c++17 -O2
  2. For each seed, generate an input, run both binaries, compare outputs (whitespace-agnostic)
  3. Stop on the first mismatch with the failing case echoed from input.txt

Customizing for Your Problem

  1. Edit gen.cpp to emit inputs matching your problem statement (sizes, distributions, edge cases)
  2. Keep the brute simple and obviously correct — speed is secondary
  3. Tweak START_SEED and MAX_TESTS in tester.py to skip early seeds or cap runs

Manual reproduction

# Generate a specific test case
gen 123 > input.txt

# Run both solutions
solution < input.txt
brute < input.txt

Workflow Diagram

┌──────────┐     ┌──────────┐     ┌──────────────┐
│  gen.cpp  │────▶│ input.txt │────▶│ solution.cpp │──┐
│  (seed N) │     └──────────┘     └──────────────┘  │
└──────────┘           │           ┌──────────────┐  │  Compare
                       └──────────▶│  brute.cpp   │──┘  outputs
                                   └──────────────┘
                                          │
                                   Match? ──▶ Next seed
                                   Mismatch? ──▶ STOP & report

Tips

  • When a mismatch appears, inspect input.txt along with both outputs printed by the tester
  • Use the tree generators directly when the task needs random trees; otherwise ignore them
  • Recompile happens automatically each run; fix compile errors before rerunning

Tech Stack

  • C++17 — Solution, brute, and generator code
  • Python 3 — Test orchestration and comparison logic