t_ini_configurator.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_ini_configurator                                             *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1998-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 const int test_iterations = 10;
00016 
00017 //#define DEBUG_INI_CONFIGURATOR_TEST
00018   // uncomment for debugging version.
00019 
00020 #include <basis/function.h>
00021 #include <basis/guards.h>
00022 #include <basis/istring.h>
00023 #include <geometric/rectangle.cpp>
00024 #include <geometric/screen_rectangle.h>
00025 #include <mathematics/float_plus.h>
00026 #include <loggers/console_logger.h>
00027 #include <opsystem/ini_config.h>
00028 #include <opsystem/path_configuration.h>
00029 #include <data_struct/static_memory_gremlin.h>
00030 #include <textual/byte_format.h>
00031 
00032 #ifdef DEBUG_INI_CONFIGURATOR_TEST
00033   #include <stdio.h>
00034 #endif
00035 
00036 HOOPLE_STARTUP_CODE;
00037 
00038 using namespace geometric;
00039 
00040 typedef float_plus<double> frunkle;
00041 
00042 #define WHERE __WHERE__.s()
00043 
00044 const char *INI_SECTION = "t_ini_configurator";
00045 
00046 int main(int formal(argc), char *formal(argv)[])
00047 {
00048   ini_configurator ini("t_ini_configurator.ini", ini_configurator::AUTO_STORE);
00049 
00050 console_logger out;
00051 out.log(istring("ini file resides in: ") + ini.name());
00052 
00053 out.log(istring("exe directory is currently: ") + path_configuration::application_directory());
00054 
00055   for (int i = 0; i < test_iterations; i++) {  // outer loop bracket.
00056     // beginning of test sets.
00057     {
00058       // first test set.
00059       const char *TEST_NAME = "first test: rectangle";
00060       // this tests whether rectangle storage works.
00061       screen_rectangle default_rectangle(10, 289, 388, 191);
00062       ini.delete_entry(INI_SECTION, "window_size");
00063       screen_rectangle win_size;
00064       istring tmp = ini.load(INI_SECTION, "window_size",
00065           default_rectangle.text_form());
00066       win_size.from_text(tmp);
00067       if (win_size != default_rectangle)
00068         deadly_error(INI_SECTION, TEST_NAME,
00069               "rectangle not equal to default");
00070       screen_rectangle new_size(23, 49, 129, 93);
00071       ini.store(INI_SECTION, "window_size", new_size.text_form());
00072       screen_rectangle current_size;
00073       tmp = ini.load(INI_SECTION, "window_size",
00074           default_rectangle.text_form());
00075       current_size.from_text(tmp);
00076       if (current_size != new_size)
00077         deadly_error(INI_SECTION, TEST_NAME,
00078               "rectangle not equal to second size stored");
00079     }
00080     {
00081       // second test set.
00082       const char *TEST_NAME = "second test: string";
00083       istring junk("this is a junky string to be stored as bytes....");
00084       byte_array to_store(junk.length() + 1, (byte *)junk.observe());
00085       istring as_bytes;
00086       byte_format::bytes_to_string(to_store, as_bytes);
00087       ini.store("test_of_byte_store", "test1", as_bytes);
00088       istring blort = "blort_fest!";
00089       istring rettle = ini.load("test_of_byte_store", "test1", blort);
00090       byte_array found_byte;
00091       byte_format::string_to_bytes(rettle, found_byte);
00092       istring found_junk((const char *)found_byte.observe());
00093       if (rettle == blort)
00094         deadly_error(INI_SECTION, TEST_NAME,
00095            "ini_configurator load failed: default was used");
00096       else if (found_junk != junk)
00097         deadly_error(INI_SECTION, TEST_NAME,
00098            "ini_configurator load failed: result differed from original");
00099     }
00100     {
00101       // third test set.
00102       const char *TEST_NAME = "third test: frunkle";
00103       frunkle def_frunkle(3.14159265358);
00104       istring def_text(istring::SPRINTF, "%f", def_frunkle.value());
00105       ini.store(INI_SECTION, TEST_NAME, def_text);
00106       istring found_string = ini.load(INI_SECTION, TEST_NAME, "9949494.3");
00107       frunkle found_frunkle = found_string.convert(0.0);
00108       if (found_frunkle == frunkle(9949494.3))
00109         deadly_error(INI_SECTION, TEST_NAME, 
00110            "ini_configurator load failed: default was used");
00111       if (found_frunkle != def_frunkle)
00112         deadly_error(INI_SECTION, TEST_NAME, 
00113            "ini_configurator load failed: saved value differed");
00114     }
00115     {
00116       // fourth test set.
00117       const char *TEST_NAME = "fourth test: frunkle";
00118       frunkle def_frunkle(1487335673.1415926535834985987);
00119       istring def_text(istring::SPRINTF, "%f", def_frunkle.value());
00120       ini.store("test", "frunkle_test", def_text);
00121       istring found_string = ini.load("test", "frunkle_test", "9949494.3");
00122       frunkle found_frunkle = found_string.convert(0.0);
00123       if (found_frunkle == frunkle(9949494.3))
00124         deadly_error(INI_SECTION, TEST_NAME,
00125            "ini_configurator load failed: wrong default was used");
00126       if (found_frunkle != def_frunkle)
00127         deadly_error(INI_SECTION, TEST_NAME, 
00128            "ini_configurator load failed: saved value differed");
00129     }
00130     {
00131       // fifth test set.
00132       const char *TEST_NAME = "fifth test: bytes";
00133       istring urp("urp");
00134       istring junk("this is a junky string to be stored as bytes....");
00135       byte_array default_bytes(urp.length() + 1, (byte *)urp.observe());
00136       istring defbytes_string;
00137       byte_format::bytes_to_string(default_bytes, defbytes_string);
00138       byte_array found;
00139       istring tmp = ini.load("test_of_byte_store", "test1", defbytes_string);
00140       byte_format::string_to_bytes(tmp, found);
00141       istring string_found = (char *)found.observe();
00142       if (string_found == urp)
00143         deadly_error(INI_SECTION, TEST_NAME,
00144             "ini_configurator load_bytes failed: default was used");
00145       if (string_found.length() != junk.length())
00146         deadly_error(INI_SECTION, TEST_NAME,
00147             "ini_configurator load_bytes failed: array lengths differ");
00148       if (string_found != junk)
00149         deadly_error(INI_SECTION, TEST_NAME,
00150             "ini_configurator load_bytes failed: arrays differ");
00151     }
00152     {
00153       // sixth test set.
00154       const char *TEST_NAME = "sixth test: blank string";
00155       ini.delete_entry("test_of_blank_string", "test1");
00156       istring blank("");
00157       istring fund = ini.load("test_of_blank_string", "test1", blank);
00158       if (fund != blank)
00159         deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
00160                "with blank default failed: didn't return blank");
00161       ini.delete_entry("test_of_blank_string", "test1");
00162       istring non_blank("blinkblankblunk");
00163       fund = ini.load("test_of_blank_string", "test1", non_blank);
00164       if (fund != non_blank)
00165         deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
00166                "with non-blank default failed: didn't return default");
00167       fund = ini.load("test_of_blank_string", "test1", blank);
00168       if (fund != non_blank)
00169         deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
00170                "with blank default failed: returned wrong string");
00171     }
00172   }
00173 
00174   istring to_print("ini_configurator:: works for those functions tested.");
00175   guards::alert_message(to_print.s());
00176   return 0;
00177 }
00178 

Generated on Fri Nov 28 04:29:39 2008 for HOOPLE Libraries by  doxygen 1.5.1