Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Starting the network module.
[simgrid.git] / testsuite / xbt / config_usage.c
1 /* $Id$ */
2
3 /* test config - test code to the config set */
4
5 #include <stdio.h>
6 #include <gras.h>
7
8 /*====[ Prototypes ]=========================================================*/
9 xbt_cfg_t make_set(void); /* build a minimal set */
10
11 /*====[ Code ]===============================================================*/
12 xbt_cfg_t make_set(){
13   xbt_cfg_t set=NULL; 
14   xbt_error_t errcode;
15
16   set = xbt_cfg_new();
17   TRYFAIL(xbt_cfg_register_str(set,"speed:1_to_2_int"));
18   TRYFAIL(xbt_cfg_register_str(set,"hostname:1_to_1_string"));
19   TRYFAIL(xbt_cfg_register_str(set,"user:1_to_10_string"));
20
21   TRYFAIL(xbt_cfg_set_parse(set, "hostname:veloce "
22                              "user:mquinson\nuser:oaumage\tuser:alegrand"));
23   return set;
24 }
25  
26 int main(int argc, char **argv) {
27   xbt_error_t errcode;
28   xbt_cfg_t set;
29
30   xbt_dynar_t dyn;
31   char *str;
32   int ival;
33   
34   xbt_init_defaultlog(&argc,argv,"config.thresh=debug root.thresh=info");
35
36   fprintf(stderr,"==== Alloc and free a config set.\n");
37   set=make_set();
38   xbt_cfg_dump("test set","",set);
39   xbt_cfg_free(&set);
40   xbt_cfg_free(&set);
41
42
43   fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n");
44   set=make_set();
45   TRYEXPECT(mismatch_error,xbt_cfg_set_parse(set,"color:blue"));
46   xbt_cfg_free(&set);
47   xbt_cfg_free(&set);
48
49
50   fprintf(stderr,
51           "==== Validation test. (ERROR EXPECTED: not enough values of 'speed')\n");
52   set=make_set();
53   xbt_cfg_check(set);
54   xbt_cfg_free(&set);
55   xbt_cfg_free(&set);
56
57   fprintf(stderr,"==== Validation test (ERROR EXPECTED: too many elements)\n");
58   set=make_set();
59   xbt_cfg_set_parse(set,"hostname:toto:42");
60   xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
61   xbt_cfg_check(set);
62   xbt_cfg_free(&set);
63   xbt_cfg_free(&set);
64
65   fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n");
66   set=make_set();
67   xbt_cfg_set_parse(set,"hostname:toto:42 speed:42");
68   xbt_cfg_get_int(set,"speed",&ival);
69   fprintf(stderr,"speed value: %d\n",ival); 
70   xbt_cfg_free(&set);
71   xbt_cfg_free(&set);
72
73   fprintf(stderr,"==== Get multiple values (Expected: 'Count: 3; Options: mquinson;ecaron;alegrand;')\n");
74   set=make_set();
75   xbt_cfg_set_parse(set,"speed:42");
76   xbt_cfg_check(set);
77   xbt_cfg_get_dynar(set,"user",&dyn);
78   fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn));
79   xbt_dynar_foreach(dyn,ival,str) {
80     fprintf(stderr,"%s;",str);
81   }
82   fprintf(stderr,"\n");
83   xbt_cfg_free(&set);
84   xbt_cfg_free(&set);
85
86   xbt_exit();
87   return 0;
88 }
89