Lumiera  0.pre.03
»edit your freedom«
test-configloader.c
Go to the documentation of this file.
1 /*
2  TEST-CONFIGLOADER - test the config system
3 
4  Copyright (C) Lumiera.org
5  2008, Christian Thaeter <ct@pipapo.org>
6  Simeon Voelkel <simeon_voelkel@arcor.de>
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of
11  the License, or (at your option) any later version.
12 
13  This program is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 
22 * *****************************************************/
23 
30 #include "lib/tmpbuf.h"
31 
32 #include "common/config.h"
33 #include "common/configitem.h"
34 
35 #include "lib/test/test.h"
36 
37 TESTS_BEGIN
38 
39 TEST (init)
40 {
41 /* Note: lumiera_config_init and lumiera_config_destroy are
42  * invoked automatically from ConfigFacade
43  */
44  printf ("initialised\n");
46  printf ("destroyed\n");
47  /* there will be a warning about destroying the already
48  * destroyed or uninitialised config system....
49  */
50 }
51 
52 
53 TEST (configitem_simple)
54 {
55  CHECK (argv[2]);
56 
57  LumieraConfigitem item;
58 
59  item = lumiera_configitem_new (argv[2]);
60  CHECK (item);
61 
62  printf ("line = '%s'\n", item->line);
63  if (item->key)
64  printf ("key = '%.*s'\n", (int)item->key_size, item->key);
65  if (item->delim)
66  {
67  printf ("delim = '%c'\n", *item->delim);
68  printf ("value = '%s'\n", item->delim+1);
69  }
70 
71  lumiera_configitem_delete (item, NULL);
72 }
73 
74 
75 TEST (lookup)
76 {
77 
78  lumiera_config_lookup lookup;
80 
81  LumieraConfigitem item = lumiera_configitem_new ("foo.bar = test");
82  lumiera_config_lookup_insert (&lookup, item);
83 
84  LumieraConfigitem found = lumiera_config_lookup_item_find (&lookup, "foo.bar");
85  CHECK (found == item);
86 
87  lumiera_config_lookup_remove (&lookup, found);
88  CHECK (!lumiera_config_lookup_item_find (&lookup, "foo.bar"));
89 
91 }
92 
93 
94 TEST (change_value)
95 {
96  CHECK (argv[2]);
97  CHECK (argv[3]);
98  CHECK (argv[4]);
99 
100  const char* value;
101 
102  if (!lumiera_config_set (argv[2], argv[3]))
103  printf ("failure setting first time '%s%s': %s\n", argv[2], argv[3], lumiera_error ());
104 
105  if (lumiera_config_get (argv[2], &value))
106  printf ("%s\n", value);
107  else
108  printf ("failure retrieving '%s': %s\n", argv[2], lumiera_error ());
109 
110  if (!lumiera_config_set (argv[2], argv[4]))
111  printf ("failure setting second time '%s%s': %s\n", argv[2], argv[4], lumiera_error ());
112 
113  if (lumiera_config_get (argv[2], &value))
114  printf ("%s\n", value);
115  else
116  printf ("failure retrieving '%s': %s\n", argv[2], lumiera_error ());
117 }
118 
119 
120 
121 TEST (basic_set_get)
122 {
123  CHECK (argv[2]);
124  CHECK (argv[3]);
125 
126  if (!lumiera_config_set (argv[2], argv[3]))
127  printf ("failure setting first time '%s%s': %s\n", argv[2], argv[3], lumiera_error ());
128 
129  if (!lumiera_config_set (argv[2], argv[3]))
130  printf ("failure setting second time '%s%s': %s\n", argv[2], argv[3], lumiera_error ());
131 
132  const char* value;
133  if (lumiera_config_get (argv[2], &value))
134  printf ("%s\n", value);
135  else
136  printf ("failure retrieving '%s': %s\n", argv[2], lumiera_error ());
137 }
138 
139 
140 
141 
142 TEST (number_get)
143 {
144  CHECK (argv[2]);
145  CHECK (argv[3]);
146 
147  long long number = 0;
148 
149  lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
150 
151  if (lumiera_config_number_get (argv[2], &number))
152  printf ("%lld\n", number);
153  else
154  printf ("%s, %lld\n", lumiera_error (), number);
155 }
156 
157 
158 TEST (number_get_nodefault)
159 {
160  CHECK (argv[2]);
161 
162  long long number = 0;
163 
164  if (lumiera_config_number_get (argv[2], &number))
165  printf ("%lld\n", number);
166  else
167  printf ("%s\n", lumiera_error ());
168 }
169 
170 
171 TEST (number_set)
172 {
173  CHECK (argv[2]);
174  CHECK (argv[3]);
175 
176  signed long long number = atoll (argv[3]);
177 
178  if (!lumiera_config_number_set (argv[2], &number))
179  printf ("failed setting number '%s=%lld': %s\n", argv[2], number, lumiera_error ());
180 
181  if (lumiera_config_number_get (argv[2], &number))
182  printf ("'%lld'\n", number);
183  else
184  printf ("%s\n", lumiera_error ());
185 }
186 
187 
188 TEST (string_get)
189 {
190  CHECK (argv[2]);
191  CHECK (argv[3]);
192 
193  const char* string;
194 
195  lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
196 
197  if (lumiera_config_string_get (argv[2], &string))
198  printf ("'%s'\n", string);
199  else
200  printf ("%s, '%s'\n", lumiera_error (), string);
201 }
202 
203 
204 TEST (string_set)
205 {
206  CHECK (argv[2]);
207  CHECK (argv[3]);
208 
209  if (!lumiera_config_string_set (argv[2], &argv[3]))
210  printf ("failed setting string '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
211 
212  const char* string;
213  if (lumiera_config_string_get (argv[2], &string))
214  printf ("'%s'\n", string);
215  else
216  printf ("%s\n", lumiera_error ());
217 }
218 
219 
220 TEST (word_get)
221 {
222  CHECK (argv[2]);
223  CHECK (argv[3]);
224 
225  const char* word;
226 
227  lumiera_config_setdefault (lumiera_tmpbuf_snprintf (SIZE_MAX, "%s = %s", argv[2], argv[3]));
228 
229  if (lumiera_config_word_get (argv[2], &word))
230  printf ("'%s'\n", word);
231  else
232  printf ("%s, '%s'\n", lumiera_error (), word);
233 
235 }
236 
237 
238 TEST (word_set)
239 {
240  CHECK (argv[2]);
241  CHECK (argv[3]);
242 
243  if (!lumiera_config_word_set (argv[2], &argv[3]))
244  printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
245 
246  const char* word;
247  if (lumiera_config_word_get (argv[2], &word))
248  printf ("'%s'\n", word);
249  else
250  printf ("%s\n", lumiera_error ());
251 }
252 
253 
254 TEST (configitem_simple_ctor_dtor)
255 {
256  CHECK (argv[2]);
257 
258  LumieraConfigitem item;
259 
260  item = lumiera_configitem_new (argv[2]);
261 }
262 
263 
264 TEST (configitem_simple_content_check)
265 {
266  CHECK (argv[2]);
267 
268  LumieraConfigitem item;
269 
270  item = lumiera_configitem_new (argv[2]);
271 
272  if ( item->line )
273  {
274  printf("item->line = '%s'\n", item->line);
275  }
276  if ( item->key_size )
277  {
278  printf("item->key_size = '%zi'\n", item->key_size);
279  }
280  if ( item->key )
281  {
282  printf("item->key = '%s'\n", item->key);
283  }
284  if ( item->delim )
285  {
286  printf("item->delim = '%s'\n", item->delim);
287  }
288 }
289 
290 
291 TEST (wordlist_get_nth)
292 {
293  CHECK (argv[2]);
294  CHECK (argv[3]);
295  CHECK (argv[4]);
296 
297  if (!lumiera_config_wordlist_set (argv[2], &argv[3]))
298  printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
299 
300  const char* word = lumiera_config_wordlist_get_nth (argv[2], atoi (argv[4]), " \t,;");
301 
302  printf ("'%s'\n", word?word:"NULL");
303 }
304 
305 
306 TEST (wordlist_find)
307 {
308  CHECK (argv[2]);
309  CHECK (argv[3]);
310  CHECK (argv[4]);
311 
312  if (!lumiera_config_wordlist_set (argv[2], &argv[3]))
313  printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
314 
315  int n = lumiera_config_wordlist_find (argv[2], argv[4], " \t,;");
316 
317  printf ("'%d'\n", n);
318 }
319 
320 
321 TEST (wordlist_replace)
322 {
323  CHECK (argv[2]);
324  CHECK (argv[3]);
325  CHECK (argv[4]);
326  CHECK (argv[5]);
327  CHECK (argv[6]);
328 
329  if (!lumiera_config_wordlist_set (argv[2], &argv[3]))
330  printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
331 
332  const char* wordlist = lumiera_config_wordlist_replace (argv[2], argv[4], *argv[5]?argv[5]:NULL, *argv[6]?argv[6]:NULL, " \t,;");
333 
334  if (wordlist)
335  printf ("'%s'\n", wordlist);
336  else
337  printf ("%s\n", lumiera_error ());
338 }
339 
340 
341 TEST (wordlist_add)
342 {
343  CHECK (argv[2]);
344  CHECK (argv[3]);
345  CHECK (argv[4]);
346  CHECK (argv[5]);
347 
348  if (!lumiera_config_wordlist_set (argv[2], &argv[3]))
349  printf ("failed setting word '%s=%s': %s\n", argv[2], argv[3], lumiera_error ());
350 
351  const char* wordlist = lumiera_config_wordlist_add (argv[2], argv[4], " \t,;");
352  if (wordlist)
353  printf ("'%s'\n", wordlist);
354  else
355  printf ("%s\n", lumiera_error ());
356 
357  wordlist = lumiera_config_wordlist_add (argv[2], argv[5], " \t,;");
358  if (wordlist)
359  printf ("'%s'\n", wordlist);
360  else
361  printf ("%s\n", lumiera_error ());
362 }
363 
364 
365 TESTS_END
LumieraConfigLookupentry lumiera_config_lookup_insert(LumieraConfigLookup self, LumieraConfigitem item)
Add a config item to a lookup structure.
Definition: config-lookup.c:78
const char * lumiera_config_wordlist_replace(const char *key, const char *value, const char *subst1, const char *subst2, const char *delims)
Universal word replacement function.
Draft for a configuration system (2008).
LumieraConfigitem lumiera_config_lookup_item_find(LumieraConfigLookup self, const char *key)
Find a the topmost config item stored to a given key.
Helpers and support macros for defining test executables in C.
Interface for a lumiera configuration system (draft).
Round robin temporary buffers.
int lumiera_config_wordlist_find(const char *key, const char *value, const char *delims)
Find the index of a word in a wordlist.
LumieraConfigitem lumiera_config_setdefault(const char *line)
Installs a default value for a config key.
Definition: config.c:274
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:124
const char * lumiera_config_wordlist_get_nth(const char *key, unsigned nth, const char *delims)
return nth word of a wordlist
LumieraConfigitem lumiera_config_lookup_remove(LumieraConfigLookup self, LumieraConfigitem item)
Remove a config item from a lookup structure.
void lumiera_config_destroy()
Destroys the configuration subsystem.
Definition: config.c:114
LumieraConfigLookup lumiera_config_lookup_destroy(LumieraConfigLookup self)
Destroy a lookup structure.
Definition: config-lookup.c:68
const char * lumiera_config_number_get(const char *key, long long *value)
Number signed integer numbers, in different formats (decimal, hex, oct, binary(for masks)) ...
Definition: config-typed.c:69
const char * lumiera_config_wordlist_add(const char *key, const char *value, const char *delims)
Add a word to the end of a wordlist if it doesn&#39;t exist already.
LumieraConfigLookup lumiera_config_lookup_init(LumieraConfigLookup self)
Initialise a lookup structure.
Definition: config-lookup.c:59
LumieraConfigitem lumiera_config_set(const char *key, const char *delim_value)
Definition: config.c:232
char * lumiera_tmpbuf_snprintf(size_t size, const char *fmt,...)
Construct a string in a tmpbuf.
Definition: tmpbuf.c:125