Skip to Content
Read

Stress testing and the trace debugger

@alt, Stress, shrinking and Trace.

A wrong answer on test 7 says nothing about why. Two tools turn that doubt into a concrete input: Stress finds a small case where your solution and an obviously correct one disagree, and Trace shows every step of a run.

A brute force in the same file

Write the solution, then the slow, obviously correct version below it as @alt(program). It reads the same input, already read for it, and prints its own answer. cpl build strips it from the C++ you submit.

Here the fast version claims the best profit is always between two neighbours. Press Stress:

Output
Press Run (Ctrl+Enter).

Stress makes random inputs from the types (n: 1..=8, values 1..=20), so no generator is needed. On the first disagreement it shrinks the input to the smallest one that still fails, and shows both answers, here three numbers where the best pair is not adjacent. The sample passes; the idea is wrong.

Small ranges in the types are what make the cases small. Check with n: 1..=8, then put the statement's limits back.

The fix, checked

Keep the smallest value so far, and the best profit is the best a[i] - low:

Output
Press Run (Ctrl+Enter).

Stress now reports that every random case agrees.

Trace

Trace runs the program and records every step. The viewer opens in a new tab, where you step backward and forward through the run and see arrays, grids and tables change. It is the quickest way to see where a loop first does something you did not expect.

On your machine the same tools are cpl stress file.cp and cpl run file.cp --trace; the stress guide covers custom generators (@gen) and checkers for problems with several right answers (@check).

Problem: Best stretch

Print the largest sum of a non-empty run of consecutive elements of an array (values from −10⁹ to 10⁹). For -1 3 -2 4 -9 the answer is 5.

5 tests · Ctrl+Enter
Hints

A linear scan keeps the best run ending here. To check it, add the O(n²) version as @alt(program), shrink the ranges and press Stress.

That is the course. The guides go further, and the practice track has more problems.

On this page