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