This is the transfer test. The domain is new, a small venue that takes seat reservations, but every design decision is one you have made this week: closed events, owned policies, atomic batches, results that say what failed and where, and canonical text that tests can compare exactly. If you can build it from a blank editor, the ideas have moved from the inventory engine into your own toolkit.
The specification.
- Events.
Book{id, seats}andCancel{id}, in a closedstd::variant, applied with one explicit visitor overload per event. - Rules each event enforces. IDs are positive and a booking holds 1..20 seats (
invalid); booking an existing ID fails (duplicate); at most 100 reservations exist (full); cancelling a missing ID fails (missing). - Rules checked after every event. The total of occupied seats is at most 20 (
capacity). Then every owned policy must allow the staged state (policy). - Policies. The venue owns a
std::vector<std::unique_ptr<Policy>>, given to its constructor.Policyhas one pure function,bool allows(const Reservations& proposed) const. Write one policy,MaxPerBooking, that rejects any reservation over its configured number of seats; the acceptance cases use a maximum of 12. - Atomic batches. A batch either applies completely or changes nothing.
- Results. Applying a batch returns the first error, or
none, together with the zero-based index of the failing event, which is empty on success. - Canonical export. One
book|id|seatsline per reservation, in ID order, each ending in a newline.
Print one line per batch, then the final export. These ten batches, applied in order to an empty venue with MaxPerBooking(12):
Book{7,10}, Book{2,5}Book{9,4}, Book{10,4}Cancel{7}, Book{9,10}Cancel{99}- an empty batch
Book{3,6}, Cancel{9}Book{3,5}Book{4,0}Cancel{9}, Cancel{2}, Book{8,13}Book{2,1}
must print exactly:
batch 1: ok, 15 seats
batch 2: capacity at event 1, 15 seats
batch 3: ok, 15 seats
batch 4: missing at event 0, 15 seats
batch 5: ok, 15 seats
batch 6: capacity at event 0, 15 seats
batch 7: ok, 20 seats
batch 8: invalid at event 0, 20 seats
batch 9: policy at event 2, 20 seats
batch 10: duplicate at event 0, 20 seats
book|2|5
book|3|5
book|9|10Predict and run
#include <iostream>
int main() {
const int capacity = 20;
int occupied = 15; // After batch 3: booking 2 (5 seats), booking 9 (10).
const int changes[] = {6, -10}; // Batch 6: Book{3,6}, then Cancel{9}.
bool ok = true;
for (int change : changes) {
occupied += change;
std::cout << "after " << change << ": " << occupied << "\n";
if (occupied > capacity) { ok = false; }
}
std::cout << (ok ? "accepted" : "rejected") << "\n";
return 0;
}Check your prediction
after 6: 21
after -10: 11
rejectedBatch 6 ends at 11 seats, within capacity, but passes through 21 on the way. Evaluating only the final total would accept it; the specification says capacity holds after every event, so it must fail, exactly like the compensating batch in the inventory engine.
Before you type
There is no plan here on purpose: the specification is the plan. Work out, for each batch, which event fails and why, before writing any code. Batch 9 is the interesting one. Its total stays within capacity, so only the policy can reject it, and only after two cancels have succeeded on the staged copy. Batch 6 fails at event 0 even though its final total would be 11.
Practice
Your turn
Build the booking engine from the specification and print the ten lines and the export above. Assert, for every rejected batch, that the export is identical before and after it, so atomicity is proved by full-state comparison rather than by the seat total alone.
#include <cassert>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <utility>
#include <variant>
#include <vector>
struct Book { int id; int seats; };
struct Cancel { int id; };
using Event = std::variant<Book, Cancel>;
using Reservations = std::map<int, int>;
int main() {
// Write the engine from the specification, then run the ten batches.
return 0;
}Hint 1: the idea
Every check runs against a staged copy, and the live reservations change in exactly one place, a swap after the last event. Each check that fails returns its error together with the loop index, so the index comes for free if the loop counts events instead of using a range-for.
Hint 2: a first step
Start without policies or results: a visitor whose overloads return bool, and apply returning bool. Get batches 1 to 8 printing ok or rejected with the right seat totals. Then change the overloads to return an Error, add the index, and add the policy last; only batch 9 depends on it.
Show one solution
#include <cassert>
#include <iostream>
#include <map>
#include <memory>
#include <optional>
#include <sstream>
#include <string>
#include <utility>
#include <variant>
#include <vector>
struct Book { int id; int seats; };
struct Cancel { int id; };
using Event = std::variant<Book, Cancel>;
using Reservations = std::map<int, int>;
enum class Error { none, invalid, duplicate, missing, full, capacity, policy };
struct BatchResult {
Error error = Error::none;
std::optional<std::size_t> index; // The failing event; empty on success.
};
class Policy {
public:
virtual ~Policy() = default;
virtual bool allows(const Reservations& proposed) const = 0;
};
class MaxPerBooking final : public Policy {
public:
explicit MaxPerBooking(int maximum) : maximum_(maximum) {}
bool allows(const Reservations& proposed) const override {
for (const auto& [id, seats] : proposed) {
if (seats > maximum_) { return false; }
}
return true;
}
private:
int maximum_;
};
struct Apply {
Reservations& state;
Error operator()(const Book& e) const {
if (e.id < 1 || e.seats < 1 || e.seats > 20) { return Error::invalid; }
if (state.contains(e.id)) { return Error::duplicate; }
if (state.size() >= 100) { return Error::full; }
state.emplace(e.id, e.seats);
return Error::none;
}
Error operator()(const Cancel& e) const {
if (e.id < 1) { return Error::invalid; }
return state.erase(e.id) == 1 ? Error::none : Error::missing;
}
};
long total(const Reservations& reservations) {
long seats = 0;
for (const auto& entry : reservations) { seats += entry.second; }
return seats;
}
class Venue {
public:
explicit Venue(std::vector<std::unique_ptr<Policy>> policies) : policies_(std::move(policies)) {}
BatchResult apply(const std::vector<Event>& events) {
Reservations staged = live_;
for (std::size_t i = 0; i < events.size(); ++i) {
const Error error = std::visit(Apply{staged}, events[i]);
if (error != Error::none) { return {error, i}; }
if (total(staged) > 20) { return {Error::capacity, i}; }
for (const auto& policy : policies_) {
if (!policy->allows(staged)) { return {Error::policy, i}; }
}
}
live_.swap(staged);
return {};
}
long occupied() const { return total(live_); }
// One "book|id|seats" line per reservation, in ID order.
std::string export_text() const {
std::ostringstream out;
for (const auto& [id, seats] : live_) { out << "book|" << id << '|' << seats << '\n'; }
return out.str();
}
private:
Reservations live_;
std::vector<std::unique_ptr<Policy>> policies_;
};
const char* name(Error error) {
switch (error) {
case Error::none: return "ok";
case Error::invalid: return "invalid";
case Error::duplicate: return "duplicate";
case Error::missing: return "missing";
case Error::full: return "full";
case Error::capacity: return "capacity";
case Error::policy: return "policy";
}
return "unknown";
}
int main() {
std::vector<std::unique_ptr<Policy>> policies;
policies.push_back(std::make_unique<MaxPerBooking>(12));
Venue venue(std::move(policies));
const std::vector<std::vector<Event>> batches{
{Book{7, 10}, Book{2, 5}},
{Book{9, 4}, Book{10, 4}},
{Cancel{7}, Book{9, 10}},
{Cancel{99}},
{},
{Book{3, 6}, Cancel{9}},
{Book{3, 5}},
{Book{4, 0}},
{Cancel{9}, Cancel{2}, Book{8, 13}},
{Book{2, 1}}};
for (std::size_t i = 0; i < batches.size(); ++i) {
const std::string before = venue.export_text();
const BatchResult result = venue.apply(batches[i]);
// A rejected batch must leave every reservation exactly as it was.
assert(result.error == Error::none || venue.export_text() == before);
assert((result.error == Error::none) == !result.index.has_value());
std::cout << "batch " << i + 1 << ": " << name(result.error);
if (result.index) { std::cout << " at event " << *result.index; }
std::cout << ", " << venue.occupied() << " seats\n";
}
std::cout << venue.export_text();
return 0;
}batch 1: ok, 15 seats
batch 2: capacity at event 1, 15 seats
batch 3: ok, 15 seats
batch 4: missing at event 0, 15 seats
batch 5: ok, 15 seats
batch 6: capacity at event 0, 15 seats
batch 7: ok, 20 seats
batch 8: invalid at event 0, 20 seats
batch 9: policy at event 2, 20 seats
batch 10: duplicate at event 0, 20 seats
book|2|5
book|3|5
book|9|10Batch 2 fails at event 1, after event 0 succeeded on the staged copy, and the export assertion proves reservation 9 never reached the live map. Batch 6 fails at its first event, when the total reaches 21, even though cancelling 9 would have brought it back to 11: capacity holds after every event, not just at the end. Batch 9 passes capacity at 18 seats and fails only the owned policy, after two successful cancels, so a wrong order of checks or a policy run only at the end would both show up here. The visitor's overloads return Error values, so apply reports the reason and the index without knowing anything about the individual rules. Venue owns its policies through std::unique_ptr, so it is movable but not copyable, and Policy's virtual destructor makes deleting a MaxPerBooking through the base pointer correct. This core handles ordinary domain rejection only. It does not recover from allocation failure or roll back effects outside the map.
Check your own work
Answer each honestly. A "no" names the day to revisit.
- Does your output match the ten lines and the export exactly, character for character? (Day 5)
- Is atomicity proved by comparing the whole export before and after every rejected batch, not only the seat total? (Day 4, Day 6)
- Does a total of exactly 20 pass (batch 7) while 21 fails (batch 6)? (Day 4)
- Did you kill a mutant? Move the capacity check after the loop, or apply events to the live map instead of the staged copy, and confirm an assertion or an output line changes. (Day 6)
- Is the policy owned by the venue through
std::unique_ptr, with a virtual destructor inPolicy, and could you add a second policy without changingapply? (Day 2) - Does each event type have its own visitor overload, so adding a third event would fail to compile until you handle it? (Day 1)
Completion is saved in this browser only. It does not measure mastery.