// App-wide OPTIONS persistence KAT (io/options_ini.hpp). Rewriting one key must not drop the others, // string values must coexist with integer preferences, and retired preferences must be removable. #include "sc2k/io/options_ini.hpp" #include #include #include #include "test_harness.hpp" static std::string slurp(const std::string& p) { std::ifstream f(p); return std::string((std::istreambuf_iterator(f)), std::istreambuf_iterator()); } int main() { using namespace sc2k::io; const std::string path = "/tmp/sc2k_options_ini_test.ini"; std::remove(path.c_str()); // 1) Missing file → every get() returns its default (no throw, no crash). { OptionsIni o; o.load(path); CHECK(o.get("SOUND", 1) == 1); CHECK(o.get("MUSIC", 1) == 1); CHECK(o.get("AUTOSAVE", 0) == 0); CHECK(o.get("NOPE", 42) == 42); } // 2) set + save + reload round-trips each value. { OptionsIni o; o.load(path); o.set("SOUND", 0); o.set("MUSIC", 1); o.set("AUTOSAVE", 3); CHECK(o.save()); OptionsIni r; r.load(path); CHECK(r.get("SOUND", 1) == 0); CHECK(r.get("MUSIC", 0) == 1); CHECK(r.get("AUTOSAVE", 0) == 3); } // 3) NO-CLOBBER: persisting one key (the toggle path) preserves the others already on disk. { OptionsIni o; o.load(path); // has SOUND=0 MUSIC=1 AUTOSAVE=3 from step 2 CHECK(o.set_and_save("SOUND", 1)); // player re-enables sound OptionsIni r; r.load(path); CHECK(r.get("SOUND", 9) == 1); // changed CHECK(r.get("MUSIC", 9) == 1); // untouched CHECK(r.get("AUTOSAVE", 9) == 3); // untouched } // 4) An unknown key hand-added to the file survives a save of a different key. { { std::ofstream f(path, std::ios::trunc); f << "[OPTIONS]\nDISPLAY_SCALE=2\nAUTOSAVE=5\nSOUND=0\n"; } OptionsIni o; o.load(path); CHECK(o.get("AUTOSAVE", -1) == 5); o.set_and_save("SOUND", 1); // flips SOUND, must keep AUTOSAVE OptionsIni r; r.load(path); CHECK(r.get("AUTOSAVE", -1) == 5); CHECK(r.get("SOUND", -1) == 1); CHECK(r.get("DISPLAY_SCALE", -1) == 2); } // 5) Robust parsing: section header case-insensitive, key case-folded, comments/blank lines ignored, // surrounding whitespace trimmed, non-integer values skipped (fall back to default). { { std::ofstream f(path, std::ios::trunc); f << "; options\n\n[options]\n Sound = 1 \n# a comment\nMUSIC=notanumber\nAUTOSAVE=4\n"; } OptionsIni o; o.load(path); CHECK(o.get("SOUND", -1) == 1); // "Sound" folds to SOUND, spaces trimmed CHECK(o.get("MUSIC", 7) == 7); // garbage value ignored → default CHECK(o.get("AUTOSAVE", -1) == 4); } // 6) The written file uses the exact [OPTIONS] section the original registry section is named after. { OptionsIni o; o.load(path); o.set("SOUND", 1); CHECK(o.save()); CHECK(slurp(path).rfind("[OPTIONS]", 0) == 0); // begins with the section header } // 7) String-valued keys (LANGUAGE): round-trip, defaulting, and preservation alongside int keys — // a save must not drop them (the earlier int-only parser silently discarded non-integer lines). { { std::ofstream f(path, std::ios::trunc); f << "[OPTIONS]\nAUTOSAVE=2\nLANGUAGE=uk\n"; } OptionsIni o; o.load(path); CHECK(o.get_str("LANGUAGE", "en") == "uk"); CHECK(o.get_str("NOPE", "en") == "en"); // absent → default CHECK(o.get("LANGUAGE", 7) == 7); // int accessor on a string value → default o.set_and_save("AUTOSAVE", 4); // an int save must keep the string key OptionsIni r; r.load(path); CHECK(r.get_str("LANGUAGE", "en") == "uk"); CHECK(r.get("AUTOSAVE", -1) == 4); CHECK(r.set_and_save_str("LANGUAGE", "en")); // and the string path round-trips OptionsIni r2; r2.load(path); CHECK(r2.get_str("LANGUAGE", "xx") == "en"); } // 8) A retired speed preference can be stripped without disturbing supported preferences. { { std::ofstream f(path, std::ios::trunc); f << "[OPTIONS]\nSPEED=4\nSOUND=1\nLANGUAGE=uk\n"; } OptionsIni o; o.load(path); CHECK(o.erase("speed")); // removal is case-insensitive like lookup CHECK(!o.erase("SPEED")); // already absent CHECK(o.save()); CHECK(slurp(path).find("SPEED=") == std::string::npos); OptionsIni r; r.load(path); CHECK(r.get("SOUND", -1) == 1); CHECK(r.get_str("LANGUAGE", "en") == "uk"); } // 9) Autosave cadence (AUTOSAVE = interval in years; fires every N played years, off when <=0). { CHECK(!autosave_due(0, 1)); // year 0 (just started) never saves CHECK(autosave_due(1, 1)); // N=1 → every year CHECK(autosave_due(5, 1)); CHECK(!autosave_due(1, 3)); // N=3 → years 3,6,9… CHECK(!autosave_due(2, 3)); CHECK(autosave_due(3, 3)); CHECK(autosave_due(6, 3)); CHECK(!autosave_due(4, 3)); CHECK(!autosave_due(5, 0)); // 0 = off, never fires CHECK(!autosave_due(5, -2)); // negative = off } std::remove(path.c_str()); if (failures == 0) { std::printf("options_ini: [OPTIONS] persistence + autosave cadence OK\n"); return 0; } std::printf("options_ini: %d failure(s)\n", failures); return 1; }