#include "Util/NewConfig.h" #include "Util/ConfigBuilders.h" #include static void PrintTestResults(std::vector> results) { std::cout << "TEST RESULTS" << std::endl; std::cout << "------------" << std::endl; for (auto v: results) std::cout << v.first << ": " << (v.second ? "passed" : "FAILED") << std::endl; } int main() { std::vector> test_results; /* * Creates a config tree corresponding to the following: * * * scud * * * foo.bin * 0 * * * bar.bin * 2 * * * */ Util::Config::Node root("global"); auto &game = root.Add("game"); game.Add("name", "scud"); auto &roms = game.Add("roms"); auto &crom1 = roms.Add("crom"); crom1.Add("name", "foo.bin"); crom1.Add("offset", "0"); auto &crom2 = roms.Add("crom"); crom2.Add("name", "bar.bin"); crom2.Add("offset", "2"); const char *expected_output = "global=\"\" children={ game }\n" " game=\"\" children={ name roms }\n" " name=\"scud\" children={ }\n" " roms=\"\" children={ crom }\n" " crom=\"\" children={ name offset }\n" " name=\"foo.bin\" children={ }\n" " offset=\"0\" children={ }\n" " crom=\"\" children={ name offset }\n" " name=\"bar.bin\" children={ }\n" " offset=\"2\" children={ }\n"; std::cout << "Expected output:" << std::endl << std::endl << expected_output << std::endl; std::cout << "Actual config tree:" << std::endl << std::endl; root.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "Manual", root.ToString() == expected_output }); // Expect second crom: bar.bin std::cout << "game/roms/crom/name=" << root["game/roms/crom/name"].Value() << " (expected: bar.bin)" << std::endl << std::endl; test_results.push_back({ "Lookup", root["game/roms/crom/name"].Value() == "bar.bin" }); // Make a copy using copy constructor std::cout << "Copy constructed:" << std::endl << std::endl; Util::Config::Node copy(root); copy.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "Copy constructed", copy.ToString() == root.ToString() }); // Make a deep copy using assignment operator, check equality, then modify a // value and ensure it does not change in the copy std::cout << "Copy by assignment:" << std::endl << std::endl; Util::Config::Node copy2("foo", "bar"); copy2.Add("x/y/z", "test"); copy2.Add("dummy", "value"); std::cout << "Initially set to:" << std::endl; copy2.Serialize(&std::cout); copy2 = root; std::cout << "Copy of \"global\":" << std::endl; copy2.Serialize(&std::cout); std::cout << std::endl; bool copies_are_equivalent = copy2.ToString() == root.ToString(); copy2.Get("game/name").SetValue("vf3"); bool copies_are_unique = copy2["game/name"].Value() == "vf3" && root["game/name"].Value() == "scud"; test_results.push_back({ "Copy by assignment", copies_are_equivalent && copies_are_unique }); // Parse from XML const char *xml = " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n" " \n"; const char *expected_xml_config_tree = "xml=\"\" children={ game }\n" " game=\"\" children={ name roms }\n" " name=\"scud\" children={ }\n" " roms=\"\" children={ region }\n" " region=\"\" children={ byte_swap file name stride }\n" " name=\"crom\" children={ }\n" " stride=\"8\" children={ }\n" " byte_swap=\"true\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"0\" children={ }\n" " name=\"epr-19734.20\" children={ }\n" " crc32=\"0xBE897336\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"2\" children={ }\n" " name=\"epr-19733.19\" children={ }\n" " crc32=\"0x6565E29A\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"4\" children={ }\n" " name=\"epr-19732.18\" children={ }\n" " crc32=\"0x23E864BB\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"6\" children={ }\n" " name=\"epr-19731.17\" children={ }\n" " crc32=\"0x3EE6447E\" children={ }\n" " region=\"\" children={ byte_swap file name stride }\n" " name=\"banked_crom\" children={ }\n" " stride=\"8\" children={ }\n" " byte_swap=\"true\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"0x0000000\" children={ }\n" " name=\"mpr-20364.4\" children={ }\n" " crc32=\"0xA2A68EF2\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"0x0000002\" children={ }\n" " name=\"mpr-20363.3\" children={ }\n" " crc32=\"0x3E3CC6FF\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"0x0000004\" children={ }\n" " name=\"mpr-20362.2\" children={ }\n" " crc32=\"0xF7E60DFD\" children={ }\n" " file=\"\" children={ crc32 name offset }\n" " offset=\"0x0000006\" children={ }\n" " name=\"mpr-20361.1\" children={ }\n" " crc32=\"0xDDB66C2F\" children={ }\n"; std::cout << "Expected output:" << std::endl << std::endl << expected_xml_config_tree << std::endl; std::cout << "Actual config tree:" << std::endl << std::endl; Util::Config::Node xml_config("xml"); Util::Config::FromXML(&xml_config, xml); xml_config.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "XML", xml_config.ToString() == expected_xml_config_tree }); // Create a nested key { Util::Config::Node config("global"); config.Add("foo/bar/baz", "bart"); config.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "Nested key 1", config["foo/bar/baz"].Value() == "bart" }); config.Get("foo/bar/baz").Set("x", "xx"); config.Get("foo/bar/baz").Set("y/z", "zz"); config.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "Nested key 2", config["foo/bar/baz/x"].Value() == "xx" }); test_results.push_back({ "Nested key 3", config["foo/bar/baz/y/z"].Value() == "zz" }); config.Add("a/b/c"); config.Get("a/b/c").SetValue("d"); config.Serialize(&std::cout); std::cout << std::endl; test_results.push_back({ "Nested key 4", config["a/b/c"].Value() == "d" }); } PrintTestResults(test_results); return 0; }