feisty meow concerns codebase  2.140
test_registry_configurator.cpp
Go to the documentation of this file.
1 /*****************************************************************************\
2 * *
3 * Name : test_registry_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_REGISTRY_CONFIGURATOR_TEST
18  // uncomment for debugging version.
19 
21 #include <basis/functions.h>
22 #include <basis/guards.h>
23 #include <basis/astring.h>
26 #include <geometric/rectangle.h>
28 #include <loggers/console_logger.h>
32 #include <textual/byte_formatter.h>
33 
34 #include <stdio.h>
35 
37 
38 using namespace basis;
39 using namespace configuration;
40 using namespace geometric;
41 using namespace loggers;
42 using namespace mathematics;
43 //using namespace textual;
44 
46 
47 #define WHERE __WHERE__.s()
48 
49 //hmmm: ugly old main() without using the hoople machinery. ack.
50 astring static_class_name() { return "test_registry_configurator"; }
51 
53  = registry_configurator::hkey_current_user;
54 
55 const astring REGISTRY_SECTION = "boobo/tahini/nunkshus";
56 
57 #define LOG(s) program_wide_logger::get().log(s)
58 
59 int main(int formal(argc), char *formal(argv)[])
60 {
61 #ifdef __WIN32__
62  // this test is only needed for win32 OSes.
63  registry_configurator ini(THE_HIVE, registry_configurator::AUTO_STORE);
64 
65 console_logger out;
66 
67  for (int i = 0; i < test_iterations; i++) { // outer loop bracket.
68  // beginning of test sets.
69 
70  {
71  const char *TEST_NAME = "zeroth test: cleaning section";
74  deadly_error(REGISTRY_SECTION, TEST_NAME,
75  "section still exists after deleting!");
76  }
77 
78  {
79  // first test set.
80  const char *TEST_NAME = "first test: rectangle";
81  // this tests whether rectangle storage works.
82  screen_rectangle default_rectangle(10, 289, 388, 191);
83  ini.delete_entry(REGISTRY_SECTION, "window_size");
84  screen_rectangle win_size;
85  astring tmp = ini.load(REGISTRY_SECTION, "window_size",
86  default_rectangle.text_form());
87  win_size.from_text(tmp);
88  if (win_size != default_rectangle)
89  deadly_error(REGISTRY_SECTION, TEST_NAME,
90  "rectangle not equal to default");
91  screen_rectangle new_size(23, 49, 129, 93);
92  ini.store(REGISTRY_SECTION, "window_size", new_size.text_form());
93  screen_rectangle current_size;
94  tmp = ini.load(REGISTRY_SECTION, "window_size",
95  default_rectangle.text_form());
96  current_size.from_text(tmp);
97  if (current_size != new_size)
98  deadly_error(REGISTRY_SECTION, TEST_NAME,
99  "rectangle not equal to second size stored");
100  }
101  {
102  // second test set.
103  const char *TEST_NAME = "second test: string";
104  astring junk("this is a junky string to be stored as bytes....");
105  byte_array to_store(junk.length() + 1, (byte *)junk.observe());
106  astring as_bytes;
107  byte_formatter::bytes_to_string(to_store, as_bytes);
108  ini.store(REGISTRY_SECTION + "/test_of_byte_store", "test1", as_bytes);
109  astring blort = "blort_fest!";
110  astring rettle = ini.load(REGISTRY_SECTION + "/test_of_byte_store",
111  "test1", blort);
112  byte_array found_byte;
113  byte_formatter::string_to_bytes(rettle, found_byte);
114  astring found_junk((const char *)found_byte.observe());
115  if (rettle == blort)
116  deadly_error(REGISTRY_SECTION, TEST_NAME,
117  "registry_configurator load failed: default was used");
118  else if (found_junk != junk)
119  deadly_error(REGISTRY_SECTION, TEST_NAME,
120  "registry_configurator load failed: result differed from original");
121  }
122  {
123  // third test set.
124  const char *TEST_NAME = "third test: frunkle";
125  frunkle def_frunkle(3.14159265358);
126  astring def_text(astring::SPRINTF, "%f", def_frunkle.value());
127  ini.store(REGISTRY_SECTION, TEST_NAME, def_text);
128  astring found_string = ini.load(REGISTRY_SECTION, TEST_NAME, "9949494.3");
129  frunkle found_frunkle = found_string.convert(0.0);
130  if (found_frunkle == frunkle(9949494.3))
131  deadly_error(REGISTRY_SECTION, TEST_NAME,
132  "registry_configurator load failed: default was used");
133  if (found_frunkle != def_frunkle)
134  deadly_error(REGISTRY_SECTION, TEST_NAME,
135  "registry_configurator load failed: saved value differed");
136  }
137  {
138  // fourth test set.
139  const char *TEST_NAME = "fourth test: frunkle";
140  frunkle def_frunkle(1487335673.1415926535834985987);
141  astring def_text(astring::SPRINTF, "%f", def_frunkle.value());
142  ini.store(REGISTRY_SECTION + "/test", "frunkle_test", def_text);
143  astring found_string = ini.load(REGISTRY_SECTION + "/test",
144  "frunkle_test", "9949494.3");
145  frunkle found_frunkle = found_string.convert(0.0);
146  if (found_frunkle == frunkle(9949494.3))
147  deadly_error(REGISTRY_SECTION, TEST_NAME,
148  "registry_configurator load failed: wrong default was used");
149  if (found_frunkle != def_frunkle)
150  deadly_error(REGISTRY_SECTION, TEST_NAME,
151  "registry_configurator load failed: saved value differed");
152  }
153  {
154  // fifth test set.
155  const char *TEST_NAME = "fifth test: bytes";
156  astring urp("urp");
157  astring junk("this is a junky string to be stored as bytes....");
158  byte_array default_bytes(urp.length() + 1, (byte *)urp.observe());
159  astring defbytes_string;
160  byte_formatter::bytes_to_string(default_bytes, defbytes_string);
161  byte_array found;
162  astring tmp = ini.load(REGISTRY_SECTION + "/test_of_byte_store", "test1", defbytes_string);
163  byte_formatter::string_to_bytes(tmp, found);
164  astring string_found = (char *)found.observe();
165  if (string_found == urp)
166  deadly_error(REGISTRY_SECTION, TEST_NAME,
167  "registry_configurator load_bytes failed: default was used");
168  if (string_found.length() != junk.length())
169  deadly_error(REGISTRY_SECTION, TEST_NAME,
170  "registry_configurator load_bytes failed: array lengths differ");
171  if (string_found != junk)
172  deadly_error(REGISTRY_SECTION, TEST_NAME,
173  "registry_configurator load_bytes failed: arrays differ");
174  }
175  {
176  // sixth test set.
177  const char *TEST_NAME = "sixth test: blank string";
178  ini.delete_entry(REGISTRY_SECTION + "/test_of_blank_string", "test1");
179  astring blank("");
180  astring fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", blank);
181  if (fund != blank)
182  deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string "
183  "with blank default failed: didn't return blank");
184  ini.delete_entry(REGISTRY_SECTION + "/test_of_blank_string", "test1");
185  astring non_blank("blinkblankblunk");
186  fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", non_blank);
187  if (fund != non_blank)
188  deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string "
189  "with non-blank default failed: didn't return default");
190  fund = ini.load(REGISTRY_SECTION + "/test_of_blank_string", "test1", blank);
191  if (fund != non_blank)
192  deadly_error(REGISTRY_SECTION, TEST_NAME, "registry_configurator load string "
193  "with blank default failed: returned wrong string");
194  }
195  {
196  // 7th set, test get_section.
197  registry_configurator ini(registry_configurator::HKCU,
198  registry_configurator::AUTO_STORE);
199  const char *TEST_NAME = "seventh test: get_section";
200  string_table sect;
201  bool worked = ini.get_section("Environment", sect);
202  if (!worked)
203  deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to get sample section (environment)");
204 //to see if it worked...
205 // LOG(astring("got section:\n") + sect.text_form());
206  }
207  {
208  // 8th set, test put_section.
209  const char *TEST_NAME = "eighth test: put_section";
210  string_table sect;
211  sect.add("dooby", "mastiff");
212  sect.add("flammix", "pontiff");
213  sect.add("gruntlix", "sagawilla");
214  sect.add("toast", "megaspoony");
215  bool worked = ini.put_section(REGISTRY_SECTION + "\\turnips", sect);
216  if (!worked)
217  deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to put sample section");
218  string_table sect8;
219  worked = ini.get_section(REGISTRY_SECTION + "\\turnips", sect8);
220  // since it should read in the order written, these should come back
221  // directly comparable.
222  if (sect != sect8)
223  deadly_error(REGISTRY_SECTION, TEST_NAME, "registry contents didn't compare as expected");
224  worked = ini.delete_section(REGISTRY_SECTION + "\\turnips");
225  if (!worked)
226  deadly_error(REGISTRY_SECTION, TEST_NAME, "failed to delete new section");
227  worked = ini.get_section(REGISTRY_SECTION + "\\turnips", sect8);
228  if (worked)
229  deadly_error(REGISTRY_SECTION, TEST_NAME, "new section could be read still!");
230  }
231  }
232 
233  // clean up after the test.
235 
236 #endif // win32.
237 
238  critical_events::alert_message(astring(static_class_name()) + ": works for those functions tested.");
239 
240  return 0;
241 }
242 
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 the configurator class interface on the windows registry.
virtual bool put_section(const basis::astring &section, const structures::string_table &info)
writes a table called "info" into the "section" in the INI file.
virtual bool section_exists(const basis::astring &section)
returns true if the "section" was found in the file.
virtual bool get_section(const basis::astring &section, structures::string_table &info)
reads the entire "section" into a table called "info".
virtual bool delete_entry(const basis::astring &section, const basis::astring &entry)
removes the entry specified by the "section" and "entry" name.
registry_hives
the hives are major partitions of the registry.
virtual bool delete_section(const basis::astring &section)
removes the entire "section" specified.
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
Provides macros that implement the 'main' program of an application.
The guards collection helps in testing preconditions and reporting errors.
Definition: array.h:30
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
astring static_class_name()
const registry_configurator::registry_hives THE_HIVE
double_plus frunkle
int main(int formal(argc), char *formal(argv)[])
const int test_iterations
const astring REGISTRY_SECTION