The brief makes about a dozen promises. test_tracker.cpp is where each one is checked, and a good way to read a test file is to go promise by promise and find the direct evidence: the one assertion that would fail if that promise were broken. If you cannot find it, the promise is untested, however many other assertions pass.
Promises and their evidence
Factory boundaries.create rejects ID 0 and 10000, an empty title, a title with an outer space or a |, 0 and 181 minutes, and a 41-character title, and accepts the extreme valid task: ID 9999, 40 characters, 180 minutes. Each limit is tested on both sides.
Exact export.original == "1|10|done|Review\n2|25|todo|Read\n3|25|todo|Practice\n" is written by hand from the format, so a mistake in export cannot hide behind a matching mistake in import (Day 6).
Duplicates preserve the original. The second add(*first) must return Error::duplicateand leave the export equal to original.
Rejected edits change nothing.edit(2, "Changed", 0, State::done) has a valid title and an invalid duration; the export must still equal original (Day 4).
Missing IDs are reported.edit and complete on ID 99 return Error::missing.
Lookups are copies. Editing the optional returned by find(2) leaves the tracker's title as "Read" (Day 5).
Report order. IDs 2, 3, 1: duration descending, then ID ascending on the tie between 2 and 3.
Predicate totals. The "all tasks" total is 60 and the todo total is 50. Two different numbers are what make an ignored predicate detectable.
Idempotent completion. Completing task 2 twice leaves the todo total at 25.
Round trip. Export, parse every line into a new tracker, export again, and compare.
Parser rejections. Eight invalid lines, including a 21-digit number and a negative ID, produce no task, and bad is malformed while 0|25|todo|Read is invalid.
Owned titles. A task parsed from a temporary string keeps its title after the string is gone.
Stable IDs after removal. Removing task 1 leaves tasks 2 and 3 findable under the same IDs; removing it again reports missing.
Capacity. 1000 tasks are accepted, the 1001st is full, and the total reaches 180,000.
Assertions are programmer checks. Keep them enabled: building with -DNDEBUG removes every assert, and the suite would print "Tracker checks passed" without checking anything. A failed assertion prints a message whose wording differs between compilers and standard libraries, so depend only on pass or fail.
How mutations use this evidence
The course checker applies each mutation from manifest.json and requires the suite to fail. Each one is caught by a line from the list above: accepting zero minutes by the factory boundary, a reversed tie-breaker or duration order by the report order, editing before validation by the rejected-edit export, an ignored predicate by the todo total, a skipped duplicate check by the duplicate result, and toggling by idempotent completion. A mutation that survived would mean a promise without direct evidence.
Predict and run
complete sets the flag, and it is called twice. What happens?
Change the body to done = !done;. The second call flips the flag back, the assertion fails, and the program stops before printing. That one line is the whole idea of the project's repeated-completion check.
Run the project tests
Before you run it, predict what it prints: should any assertion stop the unmodified project, and if not, what is the last line?
Task tracker testsC++#include "tracker.h"
#include <cassert>
#include <iostream>
#include <sstream>
int main() {
assert(!Task::create(0, "Read", 25, State::todo));
assert(!Task::create(10000, "Read", 25, State::todo));
assert(!Task::create(1, "", 25, State::todo));
assert(!Task::create(1, " Read", 25, State::todo));
assert(!Task::create(1, "Read ", 25, State::todo));
assert(!Task::create(1, "Bad|title", 25, State::todo));
assert(!Task::create(1, "Read", 0, State::todo));
assert(!Task::create(1, "Read", 181, State::todo));
assert(Task::create(9999, std::string(40, 'x'), 180, State::done));
assert(!Task::create(1, std::string(41, 'x'), 25, State::todo));
Tracker tracker;
assert(tracker.size() == 0 && !tracker.find(1));
auto first = Task::create(2, "Read", 25, State::todo);
auto second = Task::create(1, "Review", 10, State::done);
auto third = Task::create(3, "Practice", 25, State::todo);
assert(first && second && third);
assert(tracker.add(*first) == Error::none);
assert(tracker.add(*second) == Error::none);
assert(tracker.add(*third) == Error::none);
const std::string original = export_tasks(tracker);
assert(original == "1|10|done|Review\n2|25|todo|Read\n3|25|todo|Practice\n");
assert(tracker.add(*first) == Error::duplicate && export_tasks(tracker) == original);
assert(tracker.edit(2, "Changed", 0, State::done) == Error::invalid);
assert(export_tasks(tracker) == original);
assert(tracker.edit(99, "Missing", 20, State::todo) == Error::missing);
assert(tracker.complete(99) == Error::missing);
auto copy = tracker.find(2);
assert(copy && copy->edit("Copy", 50, State::done));
assert(tracker.find(2)->title() == "Read");
auto ordered = report_order(tracker);
assert(ordered[0].id() == 2 && ordered[1].id() == 3 && ordered[2].id() == 1);
assert(export_tasks(tracker) == original);
assert(total_minutes_if(ordered, [](const Task&) { return true; }) == 60);
assert(total_minutes_if(ordered, [](const Task& t) { return t.state() == State::todo; }) == 50);
assert(tracker.complete(2) == Error::none);
assert(tracker.complete(2) == Error::none);
assert(total_minutes_if(tracker.snapshot(), [](const Task& t) { return t.state() == State::todo; }) == 25);
assert(tracker.edit(2, "Updated", 40, State::todo) == Error::none);
assert(tracker.find(2)->title() == "Updated" && tracker.find(2)->minutes() == 40);
assert(tracker.find(2)->state() == State::todo);
const std::string canonical = export_tasks(tracker);
Tracker restored;
std::istringstream input(canonical);
std::string line;
while (std::getline(input, line)) {
auto result = parse_task(line);
assert(result.task && result.error == Error::none);
assert(restored.add(*result.task) == Error::none);
}
assert(export_tasks(restored) == canonical);
const std::vector<std::string> invalid{"", "1|25|todo", "1|25|later|Read", "1|25x|todo|Read",
"1|25|todo|Read|extra", "1|999999999999999999999|todo|Read", "-1|25|todo|Read", "1|25|todo|"};
for (const auto& text : invalid) { assert(!parse_task(text).task); }
assert(parse_task("bad").error == Error::malformed);
assert(parse_task("0|25|todo|Read").error == Error::invalid);
Task owned = *parse_task(std::string("9|1|todo|Temporary")).task;
assert(owned.title() == "Temporary");
assert(tracker.remove(1) == Error::none && !tracker.find(1));
assert(tracker.remove(1) == Error::missing);
assert(tracker.find(2)->id() == 2 && tracker.find(3)->id() == 3);
// A removed ID is free again: the brief promises no renumbering, not that IDs are retired.
assert(tracker.add(*Task::create(1, "Plan", 15, State::todo)) == Error::none);
assert(tracker.find(1)->title() == "Plan" && tracker.size() == 3);
assert(tracker.remove(1) == Error::none);
Tracker full;
for (int id = 1; id <= 1000; ++id) { assert(full.add(*Task::create(id, "Task", 180, State::todo)) == Error::none); }
assert(full.add(*Task::create(1001, "Extra", 1, State::todo)) == Error::full && full.size() == 1000);
assert(total_minutes_if(full.snapshot(), [](const Task&) { return true; }) == 180000L);
assert(export_tasks(Tracker{}).empty());
std::cout << "Tracker checks passed\n";
}
#include "tracker.h"
#include <algorithm>
#include <sstream>
#include <utility>
static bool valid_fields(const std::string& title, int minutes, State state) {
if (title.empty() || title.size() > 40 || minutes < 1 || minutes > 180 ||
(state != State::todo && state != State::done)) { return false; }
if (title.front() == ' ' || title.back() == ' ') { return false; }
for (unsigned char ch : title) {
if (ch < 32 || ch > 126 || ch == '|') { return false; }
}
return true;
}
Task::Task(int id, std::string title, int minutes, State state)
: id_(id), title_(std::move(title)), minutes_(minutes), state_(state) { }
std::optional<Task> Task::create(int id, std::string title, int minutes, State state) {
if (id < 1 || id > 9999 || !valid_fields(title, minutes, state)) { return std::nullopt; }
return Task(id, std::move(title), minutes, state);
}
bool Task::edit(std::string title, int minutes, State state) {
if (!valid_fields(title, minutes, state)) { return false; }
title_ = std::move(title);
minutes_ = minutes;
state_ = state;
return true;
}
static std::optional<int> decimal(std::string_view text) {
if (text.empty()) { return std::nullopt; }
for (char ch : text) { if (ch < '0' || ch > '9') { return std::nullopt; } }
std::istringstream input{std::string(text)};
int value = 0;
if (!(input >> value)) { return std::nullopt; }
return value;
}
ParseResult parse_task(std::string_view line) {
const auto first = line.find('|');
if (first == std::string_view::npos) { return {std::nullopt, Error::malformed}; }
const auto second = line.find('|', first + 1);
if (second == std::string_view::npos) { return {std::nullopt, Error::malformed}; }
const auto third = line.find('|', second + 1);
if (third == std::string_view::npos || line.find('|', third + 1) != std::string_view::npos) {
return {std::nullopt, Error::malformed};
}
auto id = decimal(line.substr(0, first));
auto minutes = decimal(line.substr(first + 1, second - first - 1));
auto state_text = line.substr(second + 1, third - second - 1);
if (!id || !minutes || (state_text != "todo" && state_text != "done")) {
return {std::nullopt, Error::invalid};
}
auto task = Task::create(*id, std::string(line.substr(third + 1)), *minutes,
state_text == "todo" ? State::todo : State::done);
if (!task) { return {std::nullopt, Error::invalid}; }
return {std::move(task), Error::none};
}
Error Tracker::add(const Task& task) {
if (!Task::create(task.id(), task.title(), task.minutes(), task.state())) { return Error::invalid; }
if (tasks_.find(task.id()) != tasks_.end()) { return Error::duplicate; }
if (tasks_.size() >= 1000) { return Error::full; }
tasks_.emplace(task.id(), task);
return Error::none;
}
Error Tracker::edit(int id, std::string title, int minutes, State state) {
auto it = tasks_.find(id);
if (it == tasks_.end()) { return Error::missing; }
return it->second.edit(std::move(title), minutes, state) ? Error::none : Error::invalid;
}
Error Tracker::complete(int id) {
auto it = tasks_.find(id);
if (it == tasks_.end()) { return Error::missing; }
it->second.edit(it->second.title(), it->second.minutes(), State::done);
return Error::none;
}
Error Tracker::remove(int id) {
return tasks_.erase(id) == 0 ? Error::missing : Error::none;
}
std::optional<Task> Tracker::find(int id) const {
auto it = tasks_.find(id);
if (it == tasks_.end()) { return std::nullopt; }
return it->second;
}
std::vector<Task> Tracker::snapshot() const {
std::vector<Task> result;
for (const auto& entry : tasks_) { result.push_back(entry.second); }
return result;
}
std::vector<Task> report_order(const Tracker& tracker) {
auto tasks = tracker.snapshot();
std::sort(tasks.begin(), tasks.end(), [](const Task& a, const Task& b) {
if (a.minutes() != b.minutes()) { return a.minutes() > b.minutes(); }
return a.id() < b.id();
});
return tasks;
}
std::string export_tasks(const Tracker& tracker) {
std::ostringstream output;
for (const Task& task : tracker.snapshot()) {
output << task.id() << '|' << task.minutes() << '|'
<< (task.state() == State::todo ? "todo" : "done") << '|' << task.title() << '\n';
}
return output.str();
}
#ifndef TASK_TRACKER_H
#define TASK_TRACKER_H
#include <map>
#include <optional>
#include <string>
#include <string_view>
#include <vector>
enum class State { todo, done };
enum class Error { none, malformed, invalid, duplicate, missing, full };
class Task {
public:
static std::optional<Task> create(int id, std::string title, int minutes, State state);
int id() const { return id_; }
const std::string& title() const { return title_; } // Borrowed until this task changes/dies.
int minutes() const { return minutes_; }
State state() const { return state_; }
bool edit(std::string title, int minutes, State state); // Invalid edits preserve all fields.
private:
Task(int id, std::string title, int minutes, State state);
int id_;
std::string title_;
int minutes_;
State state_;
};
struct ParseResult { std::optional<Task> task; Error error = Error::none; };
// id|minutes|todo-or-done|title. Numeric fields contain decimal digits only.
// CRLF stripping belongs to the input adapter. No embedded control characters.
ParseResult parse_task(std::string_view line);
class Tracker {
public:
Error add(const Task& task); // 1000 tasks maximum; duplicates leave existing task unchanged.
Error edit(int id, std::string title, int minutes, State state);
Error complete(int id); // Repeating completion succeeds without changing totals twice.
Error remove(int id);
std::optional<Task> find(int id) const; // Returns an owning copy.
std::vector<Task> snapshot() const; // ID order; owning values.
std::size_t size() const { return tasks_.size(); }
private:
std::map<int, Task> tasks_;
};
// Owns its return value. Input text/view may be destroyed after parsing.
std::string export_tasks(const Tracker& tracker);
std::vector<Task> report_order(const Tracker& tracker); // Minutes descending, then ID ascending.
template<class Predicate>
long total_minutes_if(const std::vector<Task>& tasks, Predicate matches) {
long total = 0;
for (const Task& task : tasks) {
if (matches(task)) { total += task.minutes(); }
}
return total;
}
#endif
Check your prediction
Text
Tracker checks passed
Now try two mutations by hand in tracker.cpp, one at a time. First change the tie-breaker return a.id() < b.id(); to >, predict which assertion stops the program, and run. Reset, then change the duration check to minutes < 0. The first is caught by the report-order assertion and the second by the factory boundary checks at the top of main; compare how far each run gets before stopping.
Practice
Exercise
Your turn
A comparator for std::sort must be irreflexive and asymmetric (Day 3). Write bool report_before(const Row& a, const Row& b) for struct Row { int id; int minutes; }; and a checker bool strict(Compare, const std::vector<Row>&) (with using Compare = bool (*)(const Row&, const Row&);) that tests both properties on every pair of rows without sorting. Check your comparator and a broken one that uses <= for the ID tie-breaker, then sort with the correct one only. Print:
#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; };
using Compare = bool (*)(const Row&, const Row&);
// Write report_before, broken and strict here.
int main() {
std::vector<Row> rows{{3, 25}, {1, 10}, {2, 25}};
return 0;
}
Hint 1: the idea
A strict ordering is false for equal values, and two rows cannot each come before the other. Both properties can be checked by calling the comparator directly; never pass a suspect comparator to std::sort, because a broken one is undefined behavior there.
Hint 2: a first step
Write strict to check only the first property: loop over every row a and return false if less(a, a) is true. Run it on your comparator and on the broken one. Note which one it already catches and why: a tie-breaker using <= makes a row come before itself. Then add the pairwise asymmetry check with a second, nested loop.
Show one solution
Run the solutionC++
#include <algorithm>
#include <iostream>
#include <vector>
struct Row { int id; int minutes; };
using Compare = bool (*)(const Row&, const Row&);
bool report_before(const Row& a, const Row& b) {
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id < b.id;
}
bool broken(const Row& a, const Row& b) {
if (a.minutes != b.minutes) { return a.minutes > b.minutes; }
return a.id <= b.id;
}
bool strict(Compare less, const std::vector<Row>& rows) {
for (const Row& a : rows) {
if (less(a, a)) { return false; }
for (const Row& b : rows) {
if (less(a, b) && less(b, a)) { return false; }
}
}
return true;
}
int main() {
std::vector<Row> rows{{3, 25}, {1, 10}, {2, 25}};
std::cout << "report_before strict: " << (strict(report_before, rows) ? "yes" : "no") << "\n";
std::cout << "broken strict: " << (strict(broken, rows) ? "yes" : "no") << "\n";
std::sort(rows.begin(), rows.end(), report_before);
std::cout << "order:";
for (const Row& row : rows) { std::cout << ' ' << row.id; }
std::cout << "\n";
return 0;
}
The broken comparator fails on the very first check, less(a, a), because a.id <= a.id is true. Testing the contract directly is safe, while sorting with the broken comparator could read outside the vector. The correct comparator then produces the same 2, 3, 1 order the project's test asserts. The next lesson extends the tracker with a new report and shows how to build it on your own machine.
Completion is saved in this browser only. It does not measure mastery.