Lumiera  0.pre.03
»edit your freedom«
test-interfaces.c
Go to the documentation of this file.
1 /*
2  TEST-INTERFACES - test interfaces declaration and implementation
3 
4  Copyright (C)
5  2008, Christian Thaeter <ct@pipapo.org>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
21 #include "common/interface.h"
24 #include "common/config.h"
26 
27 #include "lib/test/test.h"
28 #include "interface/say-hello.h"
29 
30 /*
31  define 2 example interfaces
32  */
33 
34 LUMIERA_INTERFACE_DECLARE (lumieraorg_testexample_one, 0,
35  LUMIERA_INTERFACE_SLOT (void, foo1, (const char*)),
36  LUMIERA_INTERFACE_SLOT (void, bar1, (const char*)),
37 );
38 
39 LUMIERA_INTERFACE_DECLARE (lumieraorg_testexample_two, 0,
40  LUMIERA_INTERFACE_SLOT (void, foo2, (const char*)),
41  LUMIERA_INTERFACE_SLOT (void, bar2, (const char*)),
42 );
43 
44 LUMIERA_INTERFACE_DECLARE (lumieraorg_testexample_void, 0
45 );
46 
47 
48 /*
49  now the functions we want to bind to them
50  */
51 
52 void
53 testfunc (const char* message)
54 {
55  ECHO ("Called as '%s'", message);
56 }
57 
58 
59 
60 LumieraInterface
61 testacquire (LumieraInterface self, LumieraInterface interface)
62 {
63  (void) interface;
64  ECHO ("Acquire %s_%d_%s", self->interface, self->version, self->name);
65  return self;
66 }
67 
68 
69 void
70 testrelease (LumieraInterface self)
71 {
72  ECHO ("Release %s_%d_%s", self->interface, self->version, self->name);
73 }
74 
75 /*
76  implementation of some example interfaces
77  */
78 
79 LUMIERA_INTERFACE_INSTANCE (lumieraorg_interfacedescriptor, 0,
80  lumieraorg_tests_descriptor,
81  /*self reference, yay*/
82  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
83  testacquire,
84  testrelease,
85  LUMIERA_INTERFACE_INLINE (name,
86  const char*, (LumieraInterface iface),
87  {(void)iface; return "LumieraTest";}
88  ),
89  LUMIERA_INTERFACE_INLINE (brief,
90  const char*, (LumieraInterface iface),
91  {(void)iface; return "Lumiera Test suite examples";}
92  ),
93  LUMIERA_INTERFACE_INLINE (homepage,
94  const char*, (LumieraInterface iface),
95  {(void)iface; return "http://www.lumiera.org/develompent.html";}
96  ),
97  LUMIERA_INTERFACE_INLINE (version,
98  const char*, (LumieraInterface iface),
99  {(void)iface; return "No Version";}
100  ),
101  LUMIERA_INTERFACE_INLINE (author,
102  const char*, (LumieraInterface iface),
103  {(void)iface; return "Christian Thaeter";}
104  ),
105  LUMIERA_INTERFACE_INLINE (email,
106  const char*, (LumieraInterface iface),
107  {(void)iface; return "ct@pipapo.org";}
108  ),
109  LUMIERA_INTERFACE_INLINE (copyright,
110  const char*, (LumieraInterface iface),
111  {
112  (void)iface;
113  return
114  "Copyright (C)\n"
115  " 2008, Christian Thaeter <ct@pipapo.org>";
116  }
117  ),
118  LUMIERA_INTERFACE_INLINE (license,
119  const char*, (LumieraInterface iface),
120  {
121  (void)iface;
122  return
123  "**Lumiera** is free software; you can redistribute it and/or modify it\n"
124  "under the terms of the GNU General Public License as published by the\n"
125  "Free Software Foundation; either version 2 of the License, or (at your\n"
126  "option) any later version. See the file COPYING for further details."
127  ;
128  }
129  ),
130 
131  LUMIERA_INTERFACE_INLINE (state,
132  int, (LumieraInterface iface),
133  {(void)iface; return LUMIERA_INTERFACE_EXPERIMENTAL;}
134  ),
135 
136  LUMIERA_INTERFACE_INLINE (versioncmp,
137  int, (const char* a, const char* b),
138  {(void)a;(void)b; return 0;}
139  )
140  );
141 
142 
143 
144 
145 
146 /*
147  Now we rig a cross dependency test
148 
149  we have 4 instances, the respective acquire/release operations set following up:
150 
151  one depends on two and three
152  two depends on one and four
153  three depends on two and four
154  four depends on one, two three
155 
156  These all are empty interfaces with no slots
157 */
158 
159 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) one_keeps_two;
160 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) one_keeps_three;
161 
162 LumieraInterface
163 testacquire_one (LumieraInterface self, LumieraInterface interface)
164 {
165  (void) interface;
166  ECHO ("Acquire one %s_%d_%s", self->interface, self->version, self->name);
167  one_keeps_two = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_two);
168  one_keeps_three = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_three);
169  return self;
170 }
171 
172 void
173 testrelease_one (LumieraInterface self)
174 {
175  ECHO ("Release one %s_%d_%s", self->interface, self->version, self->name);
176  lumiera_interface_close ((LumieraInterface)one_keeps_two);
177  lumiera_interface_close ((LumieraInterface)one_keeps_three);
178 }
179 
180 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) two_keeps_one;
181 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) two_keeps_four;
182 
183 LumieraInterface
184 testacquire_two (LumieraInterface self, LumieraInterface interface)
185 {
186  (void) interface;
187  ECHO ("Acquire two %s_%d_%s", self->interface, self->version, self->name);
188  two_keeps_one = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_one);
189  two_keeps_four = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_four);
190  return self;
191 }
192 
193 void
194 testrelease_two (LumieraInterface self)
195 {
196  ECHO ("Release two %s_%d_%s", self->interface, self->version, self->name);
197  lumiera_interface_close ((LumieraInterface)two_keeps_one);
198  lumiera_interface_close ((LumieraInterface)two_keeps_four);
199 }
200 
201 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) three_keeps_two;
202 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) three_keeps_four;
203 
204 LumieraInterface
205 testacquire_three (LumieraInterface self, LumieraInterface interface)
206 {
207  (void) interface;
208  ECHO ("Acquire three %s_%d_%s", self->interface, self->version, self->name);
209  three_keeps_two = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_two);
210  three_keeps_four = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_four);
211  return self;
212 }
213 
214 void
215 testrelease_three (LumieraInterface self)
216 {
217  ECHO ("Release three %s_%d_%s", self->interface, self->version, self->name);
218  lumiera_interface_close ((LumieraInterface)three_keeps_two);
219  lumiera_interface_close ((LumieraInterface)three_keeps_four);
220 }
221 
222 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) four_keeps_one;
223 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) four_keeps_two;
224 static LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) four_keeps_three;
225 
226 LumieraInterface
227 testacquire_four (LumieraInterface self, LumieraInterface interface)
228 {
229  (void) interface;
230  ECHO ("Acquire four %s_%d_%s", self->interface, self->version, self->name);
231  four_keeps_one = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_one);
232  four_keeps_two = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_two);
233  four_keeps_three = LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_three);
234  return self;
235 }
236 
237 void
238 testrelease_four (LumieraInterface self)
239 {
240  ECHO ("Release four %s_%d_%s", self->interface, self->version, self->name);
241  lumiera_interface_close ((LumieraInterface)four_keeps_one);
242  lumiera_interface_close ((LumieraInterface)four_keeps_two);
243  lumiera_interface_close ((LumieraInterface)four_keeps_three);
244 }
245 
246 
248  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_one, 0,
249  lumieraorg_first_test,
250  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
251  testacquire,
252  testrelease,
253  LUMIERA_INTERFACE_MAP (foo1,
254  testfunc),
255  LUMIERA_INTERFACE_MAP (bar1,
256  testfunc)
257  ),
258  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_two, 0,
259  lumieraorg_second_test,
260  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
261  testacquire,
262  testrelease,
263  LUMIERA_INTERFACE_MAP (foo2,
264  testfunc),
265  LUMIERA_INTERFACE_MAP (bar2,
266  testfunc)
267  ),
268  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_void, 0,
269  lumieraorg_dependencytest_one,
270  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
271  testacquire_one,
272  testrelease_one
273  ),
274  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_void, 0,
275  lumieraorg_dependencytest_two,
276  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
277  testacquire_two,
278  testrelease_two
279  ),
280  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_void, 0,
281  lumieraorg_dependencytest_three,
282  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
283  testacquire_three,
284  testrelease_three
285  ),
286  LUMIERA_INTERFACE_DEFINE (lumieraorg_testexample_void, 0,
287  lumieraorg_dependencytest_four,
288  LUMIERA_INTERFACE_REF(lumieraorg_interfacedescriptor, 0, lumieraorg_tests_descriptor),
289  testacquire_four,
290  testrelease_four
291  )
292  );
293 
294 
295 TESTS_BEGIN
296 
297 TEST (basic)
298 {
300 
301  lumiera_interfaceregistry_bulkregister_interfaces (lumiera_plugin_interfaces(), NULL);
302 
303 
304  /* some ugly lowlevel handling tests */
305 
306  LumieraInterface handle1 =
307  lumiera_interfaceregistry_interface_find ("lumieraorg_testexample_one", 0, "lumieraorg_first_test");
308 
309  (LUMIERA_INTERFACE_CAST(lumieraorg_testexample_one, 0)handle1)->bar1 ("this is bar1");
310 
311 
312  LUMIERA_INTERFACE_TYPE(lumieraorg_testexample_two, 0)* handle2 = LUMIERA_INTERFACE_CAST(lumieraorg_testexample_two, 0)
313  lumiera_interfaceregistry_interface_find ("lumieraorg_testexample_two", 0, "lumieraorg_second_test");
314 
315  handle2->foo2 ("this is foo2");
316 
318  lumiera_interfaceregistry_destroy ();
319 }
320 
321 TEST (open_close)
322 {
324  lumiera_interfaceregistry_bulkregister_interfaces (lumiera_plugin_interfaces(), NULL);
325 
326  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_one, 0) handle =
327  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_one, 0, 0, lumieraorg_first_test);
328  CHECK (handle);
329 
330  handle->bar1 ("this is bar1");
331 
332  lumiera_interface_close ((LumieraInterface)handle);
333 
335  lumiera_interfaceregistry_destroy ();
336 }
337 
338 TEST (dependencies_one)
339 {
342 
343  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle =
344  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_one);
345  CHECK (handle);
346 
347  ECHO ("Sucessfully opened");
348 
349  lumiera_interface_close ((LumieraInterface)handle);
350 
352  lumiera_interfaceregistry_destroy ();
353 }
354 
355 
356 TEST (dependencies_two)
357 {
360 
361  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle =
362  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_two);
363  CHECK (handle);
364 
365  ECHO ("Sucessfully opened");
366 
367  lumiera_interface_close ((LumieraInterface)handle);
368 
370  lumiera_interfaceregistry_destroy ();
371 }
372 
373 TEST (dependencies_three)
374 {
377 
378  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle =
379  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_three);
380  CHECK (handle);
381 
382  ECHO ("Sucessfully opened");
383 
384  lumiera_interface_close ((LumieraInterface)handle);
385 
387  lumiera_interfaceregistry_destroy ();
388 }
389 
390 
391 TEST (dependencies_four)
392 {
395 
396  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle =
397  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_four);
398  CHECK (handle);
399 
400  ECHO ("Sucessfully opened");
401 
402  lumiera_interface_close ((LumieraInterface)handle);
403 
405  lumiera_interfaceregistry_destroy ();
406 }
407 
408 
409 
410 TEST (dependencies_all)
411 {
414 
415  ECHO ("OPEN one");
416  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle_one =
417  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_one);
418  CHECK (handle_one);
419 
420  ECHO ("OPEN three");
421  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle_three =
422  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_three);
423  CHECK (handle_three);
424 
425  ECHO ("OPEN two");
426  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle_two =
427  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_two);
428  CHECK (handle_two);
429 
430  ECHO ("OPEN four");
431  LUMIERA_INTERFACE_HANDLE(lumieraorg_testexample_void, 0) handle_four =
432  LUMIERA_INTERFACE_OPEN (lumieraorg_testexample_void, 0, 0, lumieraorg_dependencytest_four);
433  CHECK (handle_four);
434 
435  ECHO ("Sucessfully OPENED");
436 
437  ECHO ("CLOSE four");
438  lumiera_interface_close ((LumieraInterface)handle_four);
439 
440  ECHO ("CLOSE two");
441  lumiera_interface_close ((LumieraInterface)handle_two);
442 
443  ECHO ("CLOSE three");
444  lumiera_interface_close ((LumieraInterface)handle_three);
445 
446  ECHO ("CLOSE one");
447  lumiera_interface_close ((LumieraInterface)handle_one);
448 
449 
451  lumiera_interfaceregistry_destroy ();
452 }
453 
454 
455 TEST (plugin_discover)
456 {
458 
460  {
461  LumieraPlugin p = lumiera_plugin_lookup ("modules/test-c-plugin.lum");
462  printf ("found plugin: %s\n", lumiera_plugin_name (p));
463  p = lumiera_plugin_lookup ("modules/test-cpp-plugin.lum");
464  printf ("found plugin: %s\n", lumiera_plugin_name (p));
466  }
467  else
468  printf ("error: %s\n", lumiera_error ());
469 
470  lumiera_interfaceregistry_destroy ();
471 }
472 
473 
474 TEST (plugin_unload)
475 {
477 
479  LumieraPlugin p = lumiera_plugin_lookup ("modules/test-c-plugin.lum");
480  printf ("plugin discovered before unload: %p\n", p);
481  CHECK (p, "prerequisite: need to load test-c-plugin.lum");
482 
483  lumiera_plugin_unload (lumiera_plugin_lookup ("modules/test-c-plugin.lum"));
484  p = lumiera_plugin_lookup ("test-c-plugin.lum");
485  printf ("plugin discovered after unload: %p\n", p);
486  CHECK (!p, "failed to unload plugin.");
487 
488  lumiera_interfaceregistry_destroy ();
489 }
490 
491 
492 TEST (plugin_exampleplugin)
493 {
496 
497  TODO ("macro to derive minminor version from a slot");
498 
499  LUMIERA_INTERFACE_HANDLE(lumieraorg_testhello, 0) german =
500  LUMIERA_INTERFACE_OPEN (lumieraorg_testhello, 0, 0, lumieraorg_hello_german);
501 
502  LUMIERA_INTERFACE_HANDLE(lumieraorg_testhello, 0) english =
503  LUMIERA_INTERFACE_OPEN (lumieraorg_testhello, 0, 0, lumieraorg_hello_english);
504 
505  german->hello ();
506  german->goodbye ("Welt!");
507 
508  english->hello ();
509  english->goodbye ("World!");
510 
511  LUMIERA_INTERFACE_CLOSE (german);
512  LUMIERA_INTERFACE_CLOSE (english);
513 
514  lumiera_interfaceregistry_destroy ();
515 }
516 
517 TEST (plugin_exampleplugin_nested)
518 {
521  lumiera_config_interface_init ();
522 
523  LUMIERA_INTERFACE_HANDLE(lumieraorg_testtest, 0) test =
524  LUMIERA_INTERFACE_OPEN (lumieraorg_testtest, 0, 0, lumieraorg_test_both);
525 
526  test->testit ();
527 
528  LUMIERA_INTERFACE_CLOSE (test);
529 
530  lumiera_config_interface_destroy ();
531  lumiera_interfaceregistry_destroy ();
532 }
533 
534 
535 TEST (plugin_exampleplugin_cpp)
536 {
539 
540 
541  LUMIERA_INTERFACE_HANDLE(lumieraorg_testhello, 0) german =
542  LUMIERA_INTERFACE_OPEN (lumieraorg_testhello, 0, 0, lumieraorg_hello_german_cpp);
543 
544  LUMIERA_INTERFACE_HANDLE(lumieraorg_testhello, 0) english =
545  LUMIERA_INTERFACE_OPEN (lumieraorg_testhello, 0, 0, lumieraorg_hello_english_cpp);
546 
547  german->hello ();
548  german->goodbye ("schnöde Welt!");
549 
550  english->hello ();
551  english->goodbye ("Vale of Tears!");
552 
553  LUMIERA_INTERFACE_CLOSE (german);
554  LUMIERA_INTERFACE_CLOSE (english);
555 
556  lumiera_interfaceregistry_destroy ();
557 }
558 
559 TESTS_END
560 
561 
562 
563 
564 
565 /*
566 // Local Variables:
567 // mode: C
568 // c-file-style: "gnu"
569 // indent-tabs-mode: nil
570 // End:
571 */
LumieraPlugin lumiera_plugin_lookup(const char *name)
Lookup a plugin handle in the pluginregistry.
Definition: plugin.c:338
const char * lumiera_plugin_name(LumieraPlugin self)
Query the plugin name The name is the path and filename under which it was loaded.
Definition: plugin.c:165
int lumiera_plugin_register(LumieraPlugin plugin)
Register a plugin and its interfaces.
Definition: plugin.c:260
LumieraPlugin lumiera_plugin_load(const char *plugin)
Tries to load a plugin Creates a new plugin structure and tries to load and initialise the plugin...
Definition: plugin.c:241
LUMIERA_EXPORT(LUMIERA_INTERFACE_DEFINE(lumieraorg_interface, 0, lumieraorg_interface, NULL, NULL, NULL, LUMIERA_INTERFACE_MAP(open, lumiera_interface_open), LUMIERA_INTERFACE_MAP(close, lumiera_interface_close), LUMIERA_INTERFACE_MAP(version, lumiera_interface_version),))
Definition of &#39;the mother of all interfaces&#39; since this interface is singleton and required for any c...
Definition: interface.c:309
Definition: run.hpp:40
Helpers and support macros for defining test executables in C.
Interface for a lumiera configuration system (draft).
#define LUMIERA_INTERFACE_DECLARE(name, version,...)
Declare an interface.
Definition: interface.h:154
Not finished development code.
#define LUMIERA_INTERFACE_INSTANCE(iname, version, name, descriptor, acquire, release,...)
Define an interface instance.
Definition: interface.h:185
#define LUMIERA_INTERFACE_REGISTEREXPORTED
Register all exported interfaces when not a plugin This is a no-op when LUMIERA_PLUGIN is defined...
Definition: interface.h:324
Lumiera interface macros and structures.
unsigned lumiera_plugin_unload(LumieraPlugin self)
Tries to unload a plugin.
Definition: plugin.c:297
#define LUMIERA_INTERFACE_CAST(name, version)
Construct a cast to the target interface type Used to cast a generic LumieraInterface to the real typ...
Definition: interface.h:137
A mock interface to support testing of the interface system.
void lumiera_interface_close(LumieraInterface self)
Close an interface after use.
Definition: interface.c:226
#define LUMIERA_INTERFACE_REF(iname, version, dname)
Return a reference (pointer) to an interface implementation.
Definition: interface.h:119
int lumiera_plugin_discover(LumieraPlugin(*callback_load)(const char *plugin), int(*callback_register)(LumieraPlugin))
discover new plugins traverses the configured plugin paths and calls the callback_load function for a...
Definition: plugin.c:187
lumiera_err lumiera_error(void)
Get and clear current error state.
Definition: error-state.c:115
#define LUMIERA_INTERFACE_TYPE(name, version)
Construct the type of the interface.
Definition: interface.h:128
void lumiera_interfaceregistry_init(void)
Initialise the interface registry.
Global registry for interfaces (extension points).
#define LUMIERA_INTERFACE_UNREGISTEREXPORTED
Unregister all exported interfaces when not a plugin This is a no-op when LUMIERA_PLUGIN is defined...
Definition: interface.h:330
ExampleStrategy::Qualifier two(string additionalArg)
definition of another qualifier two(arg), accepting an additional argument
A data record to describe interface, interface instances and plug-in instances.
External interface to the lumiera configuration system.
ExampleStrategy::Qualifier one()
definition of a qualifier one()
#define LUMIERA_INTERFACE_HANDLE(interface, version)
create a handle for a interface (WIP)
Definition: interface.h:347
ElementBoxWidget::Config::Qualifier name(string id)
define the name-ID displayed in the caption