A tracker that can write its tasks to text and read them back has a promise to keep: export, then import into an empty tracker, and you get the same tasks. That is a round trip, and testing it is the obvious way to check a file format. It is also easy to get subtly wrong, in two ways. The export may not be unique: the same tasks written in a different order, or with 025 instead of 25, would be different text, so "same text" no longer means "same tasks". And a matching pair of bugs, one in export and one in import, can cancel out perfectly.
This lesson fixes the first problem with a canonical export, exactly one text for each possible state, and the second with tests that compare against a written-out expected string as well as round-tripping.
Predict and run
The map was built with ID 9 listed first. In which order are the lines printed?
#include <iostream>
#include <map>
#include <sstream>
#include <string>
int main() {
std::map<int,std::string> values{{9,"Review"},{2,"Read"}};
std::ostringstream output;
for (const auto& entry : values) { output << entry.first << '|' << entry.second << '\n'; }
std::cout << output.str();
return 0;
}Check your prediction
2|Read
9|ReviewA map iterates in key order whatever order the entries arrived in, so the export comes out sorted by ID without any extra work. std::ostringstream from <sstream> collects output in memory, and str() returns it as an owning std::string, which a function can return or a test can compare.
What makes an export canonical
The tracker's format is id|minutes|state|title, and its export follows fixed rules:
- ID order, one record per line, each ending in
\n, including the last. - Numbers in plain decimal. Input may say
0002|025; export always says2|25. Writing anintwith<<normalizes it automatically. - Exact words for the state,
todoordone. - Nothing extra: no header, no trailing spaces, no blank lines. An empty tracker exports the empty string.
Reports use a different order, longest first. That is deliberate: a report is for people and an export is for programs, and mixing the two would make the file change every time a duration changes. Because input is normalized, two differently written files describing the same tasks export identically:
#include <iostream>
#include <map>
#include <sstream>
#include <string>
std::map<int, int> parse(const std::string& text) {
std::map<int, int> values;
std::istringstream input(text);
int id = 0;
char bar = 0;
int minutes = 0;
while (input >> id >> bar >> minutes) { values.emplace(id, minutes); }
return values;
}
std::string export_text(const std::map<int, int>& values) {
std::ostringstream output;
for (const auto& [id, minutes] : values) { output << id << '|' << minutes << '\n'; }
return output.str();
}
int main() {
const std::string a = export_text(parse("9|10\n2|25\n"));
const std::string b = export_text(parse("0002|025\n09|10\n"));
std::cout << a << "same: " << (a == b) << "\n";
return 0;
}2|25
9|10
same: 1This parse is deliberately loose, to keep the example short: >> skips spaces and accepts signs, which the real parser rejects. The project's parse_task checks every character, as in Day 4.
Why a round trip alone is not enough
Suppose export writes minutes|id by mistake, and import also reads minutes|id. Round-tripping restores the tracker perfectly, every round-trip test passes, and every file the program writes is wrong for anyone else who reads the documented format:
#include <iostream>
#include <map>
#include <sstream>
#include <string>
std::string swapped_export(const std::map<int, int>& values) {
std::ostringstream output;
for (const auto& [id, minutes] : values) { output << minutes << '|' << id << '\n'; }
return output.str();
}
std::map<int, int> swapped_import(const std::string& text) {
std::map<int, int> values;
std::istringstream input(text);
int minutes = 0;
char bar = 0;
int id = 0;
while (input >> minutes >> bar >> id) { values.emplace(id, minutes); }
return values;
}
int main() {
const std::map<int, int> tasks{{2, 25}, {9, 10}};
const std::string text = swapped_export(tasks);
std::cout << "round trip: " << (swapped_import(text) == tasks ? "pass" : "FAIL") << "\n";
std::cout << "expected text: " << (text == "2|25\n9|10\n" ? "pass" : "FAIL") << "\n";
return 0;
}round trip: pass
expected text: FAILThe expected-text test compares with a string you wrote by hand from the specification, so it cannot share the code's mistake. Use both: the exact string pins down the format, and the round trip checks that import and export agree on every field. The project's tests do exactly this, starting with assert(original == "1|10|done|Review\n2|25|todo|Read\n3|25|todo|Practice\n");.
Practice
Your turn
Write std::map<int, int> import_text(const std::string& text, int& rejected) that reads id|minutes lines with std::getline, accepts digits only (one to four characters per field, leading zeros allowed), rejects malformed lines and duplicate IDs by counting them, and std::string export_text(const std::map<int, int>&). Import 09|10\n2|025\nbad\n2|40\n and print:
2|25
9|10
rejected: 2
exact: pass
round trip: pass
empty: pass"exact" compares the export with "2|25\n9|10\n". "round trip" imports the export again and checks that the second export is identical with zero rejections. "empty" checks that importing "" exports "".
#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>
// Write import_text and export_text here.
int main() {
return 0;
}Hint 1: the idea
Serialize in a documented order, independent of input history. Normalizing on import (any leading zeros) and writing plain numbers on export is what makes two spellings meet in one canonical form.
Hint 2: a first step
Write a helper std::optional<int> digits(const std::string& text) that rejects empty text, more than four characters or any non-digit, and builds the number with value * 10 + (ch - '0'). For each line, find the |, require that there is exactly one, and parse the two halves with digits.
Show one solution
#include <iostream>
#include <map>
#include <optional>
#include <sstream>
#include <string>
std::optional<int> digits(const std::string& text) {
if (text.empty() || text.size() > 4) { return std::nullopt; }
int value = 0;
for (char ch : text) {
if (ch < '0' || ch > '9') { return std::nullopt; }
value = value * 10 + (ch - '0');
}
return value;
}
std::map<int, int> import_text(const std::string& text, int& rejected) {
std::map<int, int> values;
std::istringstream input(text);
std::string line;
while (std::getline(input, line)) {
const auto bar = line.find('|');
if (bar == std::string::npos || line.find('|', bar + 1) != std::string::npos) { ++rejected; continue; }
const auto id = digits(line.substr(0, bar));
const auto minutes = digits(line.substr(bar + 1));
if (!id || !minutes || !values.emplace(*id, *minutes).second) { ++rejected; }
}
return values;
}
std::string export_text(const std::map<int, int>& values) {
std::ostringstream output;
for (const auto& [id, minutes] : values) { output << id << '|' << minutes << '\n'; }
return output.str();
}
int main() {
int rejected = 0;
const std::string first = export_text(import_text("09|10\n2|025\nbad\n2|40\n", rejected));
std::cout << first << "rejected: " << rejected << "\n";
std::cout << "exact: " << (first == "2|25\n9|10\n" ? "pass" : "FAIL") << "\n";
int again = 0;
const std::string second = export_text(import_text(first, again));
std::cout << "round trip: " << (second == first && again == 0 ? "pass" : "FAIL") << "\n";
int none = 0;
std::cout << "empty: " << (export_text(import_text("", none)).empty() && none == 0 ? "pass" : "FAIL") << "\n";
return 0;
}2|25
9|10
rejected: 2
exact: pass
round trip: pass
empty: passbad has no separator and the second 2 is a duplicate, so exactly two lines are rejected and the first value for ID 2 is kept. The round trip also checks that re-importing the canonical text rejects nothing: a canonical export must always be valid input. The checkpoint next uses deliberate mutations to prove the project's own tests catch broken rules.
Completion is saved in this browser only. It does not measure mastery.