feisty meow concerns codebase  2.140
test_ini_configurator.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_ini_configurator *
4 * Author : Chris Koeritz *
5 * *
6 *******************************************************************************
7 * Copyright (c) 1998-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
14 
15 const int test_iterations = 10;
16 
17 //#define DEBUG_INI_CONFIGURATOR_TEST
18  // uncomment for debugging version.
19 
21 #include <basis/functions.h>
22 #include <basis/guards.h>
23 #include <basis/astring.h>
24 #include <geometric/rectangle.h>
27 #include <loggers/console_logger.h>
31 #include <textual/byte_formatter.h>
32 
33 #ifdef DEBUG_INI_CONFIGURATOR_TEST
34  #include <stdio.h>
35 #endif
36 
37 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
38 
39 #define UNWANTED_DEFAULT_VALUE 9949494.3
40 
42 
43 using namespace basis;
44 using namespace configuration;
45 using namespace geometric;
46 using namespace loggers;
47 using namespace mathematics;
48 using namespace textual;
49 
51 
52 #define WHERE __WHERE__.s()
53 
54 const char *INI_SECTION = "t_ini_configurator";
55 
56 //hmmm: ugly old main() without using the hoople machinery. ack.
57 #define static_class_name() "test_ini_configurator"
58 
59 #define MACRO_AS_STRING(s) #s
60 
61 int main(int formal(argc), char *formal(argv)[])
62 {
63  FUNCDEF("test ini config main")
64 
65  ini_configurator ini("t_ini_configurator.ini", ini_configurator::AUTO_STORE);
66 
67 LOG(astring("ini file resides in: ") + ini.name());
68 
69 LOG(astring("exe directory is currently: ") + application_configuration::application_directory());
70 
71  for (int i = 0; i < test_iterations; i++) { // outer loop bracket.
72  // beginning of test sets.
73  {
74  // first test set.
75  const char *TEST_NAME = "first test: rectangle";
76  // this tests whether rectangle storage works.
77  screen_rectangle default_rectangle(10, 289, 388, 191);
78  ini.delete_entry(INI_SECTION, "window_size");
79  screen_rectangle win_size;
80  astring tmp = ini.load(INI_SECTION, "window_size",
81  default_rectangle.text_form());
82  win_size.from_text(tmp);
83  if (win_size != default_rectangle)
84  deadly_error(INI_SECTION, TEST_NAME,
85  "rectangle not equal to default");
86  screen_rectangle new_size(23, 49, 129, 93);
87  ini.store(INI_SECTION, "window_size", new_size.text_form());
88  screen_rectangle current_size;
89  tmp = ini.load(INI_SECTION, "window_size",
90  default_rectangle.text_form());
91  current_size.from_text(tmp);
92  if (current_size != new_size)
93  deadly_error(INI_SECTION, TEST_NAME,
94  "rectangle not equal to second size stored");
95  }
96  {
97  // second test set.
98  const char *TEST_NAME = "second test: string";
99  astring junk("this is a junky string to be stored as bytes....");
100  byte_array to_store(junk.length() + 1, (abyte *)junk.observe());
101  astring as_bytes;
102  byte_formatter::bytes_to_string(to_store, as_bytes);
103  ini.store("test_of_byte_store", "test1", as_bytes);
104  astring blort = "blort_fest!";
105  astring rettle = ini.load("test_of_byte_store", "test1", blort);
106  byte_array found_byte;
107  byte_formatter::string_to_bytes(rettle, found_byte);
108  astring found_junk((const char *)found_byte.observe());
109  if (rettle == blort)
110  deadly_error(INI_SECTION, TEST_NAME,
111  "ini_configurator load failed: default was used");
112  else if (found_junk != junk)
113  deadly_error(INI_SECTION, TEST_NAME,
114  "ini_configurator load failed: result differed from original");
115  }
116  {
117  // third test set.
118  const char *TEST_NAME = "third test: frunkle";
119  frunkle def_frunkle(3.14159265358);
120  astring def_text(astring::SPRINTF, "%f", def_frunkle.value());
121  ini.store(INI_SECTION, TEST_NAME, def_text);
122  astring found_string = ini.load(INI_SECTION, TEST_NAME, MACRO_AS_STRING(UNWANTED_DEFAULT_VALUE));
123  frunkle found_frunkle = found_string.convert(0.0);
124  if (found_frunkle == frunkle(UNWANTED_DEFAULT_VALUE))
125  deadly_error(INI_SECTION, TEST_NAME,
126  "ini_configurator load failed: default was used");
127  if (found_frunkle != def_frunkle)
128  deadly_error(INI_SECTION, TEST_NAME,
129  "ini_configurator load failed: saved value differed");
130  }
131  {
132  // fourth test set.
133  const char *TEST_NAME = "fourth test: frunkle";
134  frunkle def_frunkle(1487335673.1415926535834985987);
135  astring def_text(astring::SPRINTF, "%f", def_frunkle.value());
136  ini.store("test", "frunkle_test", def_text);
137  astring found_string = ini.load("test", "frunkle_test", MACRO_AS_STRING(UNWANTED_DEFAULT_VALUE));
138  frunkle found_frunkle = found_string.convert(0.0);
139  if (found_frunkle == frunkle(0.0))
140  deadly_error(INI_SECTION, TEST_NAME,
141  "ini_configurator load failed: float conversion failed--got zero");
142  if (found_frunkle == frunkle(UNWANTED_DEFAULT_VALUE))
143  deadly_error(INI_SECTION, TEST_NAME,
144  "ini_configurator load failed: wrong unwanted default was used");
145  if (found_frunkle != def_frunkle)
146  deadly_error(INI_SECTION, TEST_NAME,
147  "ini_configurator load failed: saved value differed");
148  }
149  {
150  // fifth test set.
151  const char *TEST_NAME = "fifth test: bytes";
152  astring urp("urp");
153  astring junk("this is a junky string to be stored as bytes....");
154  byte_array default_bytes(urp.length() + 1, (abyte *)urp.observe());
155  astring defbytes_string;
156  byte_formatter::bytes_to_string(default_bytes, defbytes_string);
157  byte_array found;
158  astring tmp = ini.load("test_of_byte_store", "test1", defbytes_string);
159  byte_formatter::string_to_bytes(tmp, found);
160  astring string_found = (char *)found.observe();
161  if (string_found == urp)
162  deadly_error(INI_SECTION, TEST_NAME,
163  "ini_configurator load_bytes failed: default was used");
164  if (string_found.length() != junk.length())
165  deadly_error(INI_SECTION, TEST_NAME,
166  "ini_configurator load_bytes failed: array lengths differ");
167  if (string_found != junk)
168  deadly_error(INI_SECTION, TEST_NAME,
169  "ini_configurator load_bytes failed: arrays differ");
170  }
171  {
172  // sixth test set.
173  const char *TEST_NAME = "sixth test: blank string";
174  ini.delete_entry("test_of_blank_string", "test1");
175  astring blank("");
176  astring fund = ini.load("test_of_blank_string", "test1", blank);
177  if (fund != blank)
178  deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
179  "with blank default failed: didn't return blank");
180  ini.delete_entry("test_of_blank_string", "test1");
181  astring non_blank("blinkblankblunk");
182  fund = ini.load("test_of_blank_string", "test1", non_blank);
183  if (fund != non_blank)
184  deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
185  "with non-blank default failed: didn't return default");
186  fund = ini.load("test_of_blank_string", "test1", blank);
187  if (fund != non_blank)
188  deadly_error(INI_SECTION, TEST_NAME, "ini_configurator load string "
189  "with blank default failed: returned wrong string");
190  }
191  }
192 
193  critical_events::alert_message(astring(static_class_name()) + ": works for those functions tested.");
194 
195  return 0;
196 }
197 
const contents * observe() const
Returns a pointer to the underlying C array of data.
Definition: array.h:172
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
int convert(int default_value) const
Converts the string into a corresponding integer.
Definition: astring.cpp:760
int length() const
Returns the current length of the string.
Definition: astring.cpp:132
virtual const char * observe() const
observes the underlying pointer to the zero-terminated string.
Definition: astring.cpp:140
A very common template for a dynamic array of bytes.
Definition: byte_array.h:36
bool store(const basis::astring &section, const basis::astring &entry, const basis::astring &to_store)
a synonym for put.
basis::astring load(const basis::astring &section, const basis::astring &entry, const basis::astring &default_value)
a synonym for get that implements the auto-store behavior.
Supports a configurator-based interface on text initialization files.
virtual bool delete_entry(const basis::astring &section, const basis::astring &entry)
removes the entry specified by the "section" and "entry" name.
basis::astring name() const
observes the name of the file used for ini entries.
basis::astring text_form() const
Prints out the contents of the rectangle.
Definition: rectangle.h:352
bool from_text(const basis::astring &text)
Returns true if the "text" is parsed into this rectangle.
Definition: rectangle.h:359
Represents a rectangle as interpreted on display screens.
double value() const
observes the value held in this.
Definition: double_plus.h:59
#define deadly_error(c, f, i)
#define formal(parameter)
This macro just eats what it's passed; it marks unused formal parameters.
Definition: definitions.h:48
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Definition: enhance_cpp.h:54
Provides macros that implement the 'main' program of an application.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51
Contains all of our objects for geometry and avoids name clashes.
Definition: angle.h:25
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
Definition: averager.h:21
#define LOG(s)
double_plus frunkle
#define UNWANTED_DEFAULT_VALUE
int main(int formal(argc), char *formal(argv)[])
const int test_iterations
#define static_class_name()
const char * INI_SECTION
#define MACRO_AS_STRING(s)