/*****************************************************************************\ * * * Name : test_registry_configurator * * Author : Chris Koeritz * * * ******************************************************************************* * Copyright (c) 1998-$now By Author. This program is free software; you can * * redistribute it and/or modify it under the terms of the GNU General Public * * License as published by the Free Software Foundation; either version 2 of * * the License or (at your option) any later version. This is online at: * * http://www.fsf.org/copyleft/gpl.html * * Please send any updates to: fred@gruntose.com * \*****************************************************************************/ const int test_iterations = 10; //#define DEBUG_REGISTRY_CONFIGURATOR_TEST // uncomment for debugging version. #include #include #include #include #include #include #include #include #include #include #include #include #include #include HOOPLE_STARTUP_CODE; using namespace basis; using namespace configuration; using namespace geometric; using namespace loggers; using namespace mathematics; //using namespace textual; typedef double_plus frunkle; #define WHERE __WHERE__.s() //hmmm: ugly old main() without using the hoople machinery. ack. astring static_class_name() { return "test_registry_configurator"; } const registry_configurator::registry_hives THE_HIVE = registry_configurator::hkey_current_user; const astring REGISTRY_SECTION = "boobo/tahini/nunkshus"; #define LOG(s) program_wide_logger::get().log(s) int main(int formal(argc), char *formal(argv)[]) { #ifdef __WIN32__ // this test is only needed for win32 OSes. registry_configurator ini(THE_HIVE, registry_configurator::AUTO_STORE); console_logger out; for (int i = 0; i < test_iterations; i++) { // outer loop bracket. // beginning of test sets. { const char *TEST_NAME = "zeroth test: cleaning section"; ini.delete_section(REGISTRY_SECTION); if (ini.section_exists(REGISTRY_SECTION)) deadly_error(REGISTRY_SECTION, TEST_NAME, "section still exists after deleting!"); } { // first test set. const char *TEST_NAME = "first test: rectangle"; // this tests whether rectangle storage works. screen_rectangle default_rectangle(10, 289, 388, 191); ini.delete_entry(REGISTRY_SECTION, "window_size"); screen_rectangle win_size; astring tmp = ini.load(REGISTRY_SECTION, "window_size", default_rectangle.text_form()); win_size.from_text(tmp); if (win_size != default_rectangle) deadly_error(REGISTRY_SECTION, TEST_NAME, "rectangle not equal to default"); screen_rectangle new_size(23, 49, 129, 93); ini.store(REGISTRY_SECTION, "window_size", new_size.text_form()); screen_rectangle current_size; tmp = ini.load(REGISTRY_SECTION, "window_size", default_rectangle.text_form()); current_size.from_text(tmp); if (current_size != new_size) deadly_error(REGISTRY_SECTION, TEST_NAME, "rectangle not equal to second size stored"); } { // second test set. const char *TEST_NAME = "second test: string"; astring junk("this is a junky string to be stored as bytes...."); byte_array to_store(junk.length() + 1, (byte *)junk.observe()); astring as_bytes; byte_formatter::bytes_to_string(to_store, as_bytes); ini.store(REGISTRY_SECTION + "/test_of_byte_store", "test1", as_bytes); astring blort = "blort_fest!"; astring rettle = ini.load(REGISTRY_SECTION + "/test_of_byte_store", "test1", blort); byte_array found_byte; byte_formatter::string_to_bytes(rettle, found_byte); astring found_junk((const char *)found_byte.observe()); if (rettle == blort) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: default was used"); else if (found_junk != junk) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: result differed from original"); } { // third test set. const char *TEST_NAME = "third test: frunkle"; frunkle def_frunkle(3.14159265358); astring def_text(astring::SPRINTF, "%f", def_frunkle.value()); ini.store(REGISTRY_SECTION, TEST_NAME, def_text); astring found_string = ini.load(REGISTRY_SECTION, TEST_NAME, "9949494.3"); frunkle found_frunkle = found_string.convert(0.0); if (found_frunkle == frunkle(9949494.3)) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: default was used"); if (found_frunkle != def_frunkle) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: saved value differed"); } { // fourth test set. const char *TEST_NAME = "fourth test: frunkle"; frunkle def_frunkle(1487335673.1415926535834985987); astring def_text(astring::SPRINTF, "%f", def_frunkle.value()); ini.store(REGISTRY_SECTION + "/test", "frunkle_test", def_text); astring found_string = ini.load(REGISTRY_SECTION + "/test", "frunkle_test", "9949494.3"); frunkle found_frunkle = found_string.convert(0.0); if (found_frunkle == frunkle(9949494.3)) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: wrong default was used"); if (found_frunkle != def_frunkle) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load failed: saved value differed"); } { // fifth test set. const char *TEST_NAME = "fifth test: bytes"; astring urp("urp"); astring junk("this is a junky string to be stored as bytes...."); byte_array default_bytes(urp.length() + 1, (byte *)urp.observe()); astring defbytes_string; byte_formatter::bytes_to_string(default_bytes, defbytes_string); byte_array found; astring tmp = ini.load(REGISTRY_SECTION + "/test_of_byte_store", "test1", defbytes_string); byte_formatter::string_to_bytes(tmp, found); astring string_found = (char *)found.observe(); if (string_found == urp) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load_bytes failed: default was used"); if (string_found.length() != junk.length()) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load_bytes failed: array lengths differ"); if (string_found != junk) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load_bytes failed: arrays differ"); } { // sixth test set. const char *TEST_NAME = "sixth test: blank string"; ini.delete_entry(REGISTRY_SECTION + "/test_of_blank_string", "test1"); astring blank(""); astring fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", blank); if (fund != blank) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string " "with blank default failed: didn't return blank"); ini.delete_entry(REGISTRY_SECTION + "/test_of_blank_string", "test1"); astring non_blank("blinkblankblunk"); fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", non_blank); if (fund != non_blank) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string " "with non-blank default failed: didn't return default"); fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", blank); if (fund != non_blank) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string " "with blank default failed: returned wrong string"); } { // 7th set, test get_section. registry_configurator ini(registry_configurator::HKCU, registry_configurator::AUTO_STORE); const char *TEST_NAME = "seventh test: get_section"; string_table sect; bool worked = ini.get_section("Environment", sect); if (!worked) deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to get sample section (environment)"); //to see if it worked... // LOG(astring("got section:\n") + sect.text_form()); } { // 8th set, test put_section. const char *TEST_NAME = "eighth test: put_section"; string_table sect; sect.add("dooby", "mastiff"); sect.add("flammix", "pontiff"); sect.add("gruntlix", "sagawilla"); sect.add("toast", "megaspoony"); bool worked = ini.put_section(REGISTRY_SECTION + "\\turnips", sect); if (!worked) deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to put sample section"); string_table sect8; worked = ini.get_section(REGISTRY_SECTION + "\\turnips", sect8); // since it should read in the order written, these should come back // directly comparable. if (sect != sect8) deadly_error(REGISTRY_SECTION, TEST_NAME, "registry contents didn't compare as expected"); worked = ini.delete_section(REGISTRY_SECTION + "\\turnips"); if (!worked) deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to delete new section"); worked = ini.get_section(REGISTRY_SECTION + "\\turnips", sect8); if (worked) deadly_error(REGISTRY_SECTION, TEST_NAME, "new section could be read still!"); } } // clean up after the test. ini.delete_section(REGISTRY_SECTION); #endif // win32. critical_events::alert_message(astring(static_class_name()) + ": works for those functions tested."); return 0; }