A passing test suite tells you the code agrees with the tests. It does not tell you the tests would notice if the code were wrong. Mutation testing answers that second question. A mutation is a small deliberate bug: reverse a tie-breaker, accept a zero duration, change a title before validating, toggle completion instead of setting it. Apply one, run the tests, and at least one check must fail. If every test still passes, the suite has a blind spot exactly where the mutation is, and you have found the test to write next. Restore the code after each experiment.
The project ships a list of mutations in manifest.json, and the course checker applies each one to tracker.cpp or tracker.h and confirms the tests fail. In this checkpoint you build the same kind of harness for a small function, then use it on the real tests.
Setting a value twice leaves it set: the operation is idempotent. Replace both assignments with done = !done; and the state flips back to false, so the assert stops the program with a diagnostic instead of printing. A user who clicks "complete" twice should not find the task reopened, which is why the project tests repeated completion explicitly.
What you will use
Function pointers to pass several implementations of the same operation to one test function.
A test suite as a function returning bool, so it can report "caught" instead of stopping the program the way assert does.
One case per promise: repeated completion stays done, a missing ID is reported and inserts nothing, and other entries do not change.
The project's mutation list, to compare your reasoning with real assertions.
assert is the right tool in the project's tests: a failure stops immediately, and the tests must be built without NDEBUG, which would remove every assert. Do not rely on the exact wording of an assertion failure; it differs between compilers. The checker looks only at pass or fail.
Plan before you type
Write the correct complete(std::map<int, bool>& done, int id): find, return false at end(), set true, return true.
Write three mutations: one toggles, one uses operator[] (so a missing ID is inserted), one marks every task done.
Write suite(Complete): build a fresh map in each case, run the operation, and check both the return value and the whole map.
Before running, predict which case catches each mutation. Then run all four implementations through the suite.
Prove it can fail
The harness itself can be wrong. Delete the "missing ID" case from your suite and run again: inserts missing must now print passes, which shows that case was the only one guarding that promise. Do the same with the other cases. Then open the project tests below, apply a mutation from the list by hand in tracker.cpp, predict which assertion stops the program, and run.
Mistakes worth checking for
Every mutation "passes": the suite reuses one map across cases, so an early case changes the starting state of later ones. Build fresh data per case.
The toggle mutation survives: no case completes the same ID twice.
The insertion mutation survives: the missing-ID case checks the return value but not size().
"Completes all" survives: no case looks at an entry other than the one being completed.
Tests pass locally and fail elsewhere: an expected value depends on the wording of an assertion message or on unordered output. Check values, not messages.
Practice
Exercise
Your turn
Write the correct complete, the three mutations from the plan, and bool suite(Complete), with using Complete = bool (*)(std::map<int, bool>&, int);. Start each case from {2, false}, {3, false}. Print:
#include <iostream>
#include <map>
#include <utility>
using Complete = bool (*)(std::map<int, bool>&, int);
// Write complete, the mutations and suite here.
int main() {
return 0;
}
Hint 1: the idea
Completion is setting a state, not reversing it, and it touches exactly one existing entry. Each promise in that sentence needs its own check, and each mutation breaks exactly one of them.
Hint 2: a first step
Write each case as its own small block: std::map<int, bool> tasks{{2, false}, {3, false}};, then the call, then a condition such as !(op(tasks, 2) && op(tasks, 2) && tasks.find(2)->second). Return false from suite as soon as one case fails. Reading through find is safe here because every fresh map contains IDs 2 and 3; avoid tasks[2] in a test, since it would insert a key if a mutation had removed it.
Each mutation fails a different case: toggling fails the repeat, operator[] fails the missing ID (it reports success and grows the map), and "completes all" fails the check on task 3. The find(...)->second reads are safe because those keys are in every fresh map. Day 7 assembles the full tracker, whose tests apply this same discipline to every promise in the brief.
Run and challenge 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?
Tracker regression 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
Each mutation in the project's manifest is caught by a specific assertion. Find them in test_tracker.cpp:
Accept zero duration (minutes < 0): assert(!Task::create(1, "Read", 0, State::todo));.
Reverse the ID tie-breaker or the duration order: the assertion that the report order is IDs 2, 3, 1.
Edit before validation:export_tasks(tracker) == original after the rejected edit.
Ignore the predicate in total_minutes_if: the pending total of 50, which differs from the total of all tasks, 60.
Skip the duplicate check:tracker.add(*first) == Error::duplicate.
Toggle completion: completing task 2 twice and checking that pending minutes drop to 25 rather than returning to 50.
Change one rule in tracker.cpp, predict which line stops the program, run, and restore it.
Completion is saved in this browser only. It does not measure mastery.