Hand-written tests check the cases you thought of. The bugs that reach production are usually in the cases you did not: an off-by-one at a boundary you never tested, an interaction between two events you never combined. You cannot list every batch by hand, but you can let a program compare the engine's answers against another program's answers, over far more inputs than you would ever write. That second program is a reference model, and the technique is called differential testing.
The model's value comes from being independent and obviously right. The engine uses a map of items, a variant of events, visitors, owned rules and a staged swap. The project's model, inside test_engine.cpp, is one int and one loop: for a batch of adjustments to a single item, add each delta, and if any intermediate value leaves 0..1000, keep the old value. It covers a small slice of the engine's behavior, but within that slice it is short enough to check by eye, and it shares no code with what it tests.
Predict and run
#include <iostream>
#include <initializer_list>
int main() {
int reference = 10;
for (int delta : {-3,2}) { reference += delta; }
std::cout << reference << "\n";
return 0;
}Check your prediction
9This model has no rejection yet: every delta is applied. To match the engine, it needs the same all-or-nothing rule, written its own way. The natural form keeps the previous value and a candidate, updates only the candidate, and returns the previous value the moment the candidate leaves the valid range. That is the same idea as staging, expressed with one integer instead of a copied map, which is exactly the kind of difference that lets the two catch each other's mistakes.
A model catches what examples miss
Here is an engine function with a plausible bug: it checks only the final quantity, not each intermediate one. A short list of hand-picked batches agrees with it, and the model disagrees on the one batch that matters:
#include <iostream>
#include <vector>
struct Outcome { bool accepted; int quantity; };
Outcome model(int quantity, const std::vector<int>& deltas) {
int candidate = quantity;
for (int delta : deltas) {
candidate += delta;
if (candidate < 0 || candidate > 1000) { return {false, quantity}; }
}
return {true, candidate};
}
Outcome final_only(int quantity, const std::vector<int>& deltas) {
int candidate = quantity;
for (int delta : deltas) { candidate += delta; }
if (candidate < 0 || candidate > 1000) { return {false, quantity}; }
return {true, candidate};
}
int main() {
const std::vector<std::vector<int>> batches{{-3, 2}, {5, -99}, {}, {-20, 15}};
for (std::size_t i = 0; i < batches.size(); ++i) {
const Outcome expected = model(10, batches[i]);
const Outcome actual = final_only(10, batches[i]);
const bool same = expected.accepted == actual.accepted && expected.quantity == actual.quantity;
std::cout << "batch " << i << ": " << (same ? "agree" : "DISAGREE") << "\n";
}
return 0;
}batch 0: agree
batch 1: agree
batch 2: agree
batch 3: DISAGREEBatch 3 dips to -10 before recovering to 5. The model rejects it; the buggy function accepts it. Three of four batches agreed, so a test suite made only of the first three would have passed. A generator that produces hundreds of batches, as the next lesson builds, makes it very likely that some batch dips below zero and recovers.
What independence means
- Different representation. The model tracks an
int; the engine tracksstd::map<int, Item>. A bug in map handling cannot appear in the model. - No shared helpers. If both called the same
in_rangefunction, a wrong bound would make both wrong in the same way, and they would agree. The model writes its own comparison. - Smaller scope. The model handles adjustments to one item, nothing else. Covering less is fine; being unsure is not.
- Compare everything observable. The project's loop compares both whether the batch was accepted and the resulting quantity. Comparing only acceptance would miss an engine that accepts correctly but applies the wrong delta.
When the model and the engine disagree, do not assume the engine is wrong. Either may be. A disagreement is a question to investigate, and sometimes the answer is that the specification was unclear and both readings were reasonable.
What agreement does not prove
Agreement over 500 batches is strong evidence about adjustments to one item in that range of deltas. It says nothing about creates, renames, duplicate IDs, names or the item limit, which the model never generates. That is why the project keeps its hand-written cases beside the model: the late-failure rollback test, the exact error indices, the boundary at exactly 1000. Generated tests explore; hand-written tests pin down known requirements. Neither replaces the other.
Practice
Your turn
Write Outcome model(int quantity, const std::vector<int>& deltas) that also rejects any delta outside -1000..1000 before adding it. Then compare the model with the two engine functions in the starter over the batches given, carrying each one's own state from batch to batch, starting at 10. Report the first disagreement for each engine, or how many batches agreed:
correct engine: agrees on 5 batches
strict engine: first disagreement at batch 2, model accepts, engine rejects#include <iostream>
#include <vector>
struct Outcome { bool accepted; int quantity; };
Outcome correct_engine(int quantity, const std::vector<int>& deltas) {
int staged = quantity;
for (int delta : deltas) {
if (delta < -1000 || delta > 1000) { return {false, quantity}; }
const int next = staged + delta;
if (next < 0 || next > 1000) { return {false, quantity}; }
staged = next;
}
return {true, staged};
}
Outcome strict_engine(int quantity, const std::vector<int>& deltas) {
int staged = quantity;
for (int delta : deltas) {
if (delta < -1000 || delta > 1000) { return {false, quantity}; }
const int next = staged + delta;
if (next < 0 || next >= 1000) { return {false, quantity}; } // Bug: 1000 is valid.
staged = next;
}
return {true, staged};
}
int main() {
const std::vector<std::vector<int>> batches{{-3, 2}, {5, -99}, {991}, {-1000}, {0, 2000}};
// Write model and compare it with both engines.
return 0;
}Hint 1: the idea
Each side carries its own quantity forward. After a disagreement the states may differ, so report only the first one.
Hint 2: a first step
Write template<class Engine> void compare(const char* name, Engine engine, const std::vector<std::vector<int>>& batches) that keeps int expected = 10, actual = 10; and loops over the batches.
Show one solution
#include <iostream>
#include <vector>
struct Outcome { bool accepted; int quantity; };
Outcome correct_engine(int quantity, const std::vector<int>& deltas) {
int staged = quantity;
for (int delta : deltas) {
if (delta < -1000 || delta > 1000) { return {false, quantity}; }
const int next = staged + delta;
if (next < 0 || next > 1000) { return {false, quantity}; }
staged = next;
}
return {true, staged};
}
Outcome strict_engine(int quantity, const std::vector<int>& deltas) {
int staged = quantity;
for (int delta : deltas) {
if (delta < -1000 || delta > 1000) { return {false, quantity}; }
const int next = staged + delta;
if (next < 0 || next >= 1000) { return {false, quantity}; } // Bug: 1000 is valid.
staged = next;
}
return {true, staged};
}
Outcome model(int quantity, const std::vector<int>& deltas) {
int candidate = quantity;
for (int delta : deltas) {
if (delta > 1000 || delta < -1000) { return {false, quantity}; }
candidate += delta;
if (candidate > 1000 || candidate < 0) { return {false, quantity}; }
}
return {true, candidate};
}
template<class Engine>
void compare(const char* name, Engine engine, const std::vector<std::vector<int>>& batches) {
int expected = 10, actual = 10;
for (std::size_t i = 0; i < batches.size(); ++i) {
const Outcome want = model(expected, batches[i]);
const Outcome got = engine(actual, batches[i]);
if (want.accepted != got.accepted || want.quantity != got.quantity) {
std::cout << name << ": first disagreement at batch " << i
<< ", model " << (want.accepted ? "accepts" : "rejects")
<< ", engine " << (got.accepted ? "accepts" : "rejects") << "\n";
return;
}
expected = want.quantity;
actual = got.quantity;
}
std::cout << name << ": agrees on " << batches.size() << " batches\n";
}
int main() {
const std::vector<std::vector<int>> batches{{-3, 2}, {5, -99}, {991}, {-1000}, {0, 2000}};
compare("correct engine", correct_engine, batches);
compare("strict engine", strict_engine, batches);
return 0;
}correct engine: agrees on 5 batches
strict engine: first disagreement at batch 2, model accepts, engine rejectsBatch 2 takes the quantity from 9 to exactly 1000, the boundary the strict engine wrongly excludes. The model writes its comparisons in a different order and adds before checking, so a copy-paste of the engine's bug is unlikely to slip in. The batches here were hand-picked to hit that boundary; the next lesson generates batches automatically, with a seed, so a failure found once can be reproduced exactly.
Completion is saved in this browser only. It does not measure mastery.