Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
datadesc.big32_2 got regenerated on m68k
[simgrid.git] / testsuite / xbt / config_usage.c
1 /* $Id$ */
2
3 /* test config - test code to the config set */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h>
11 #include "gras.h"
12
13 /*====[ Code ]===============================================================*/
14 static xbt_cfg_t make_set(){
15   xbt_cfg_t set=NULL; 
16   xbt_error_t errcode;
17
18   set = xbt_cfg_new();
19   TRYFAIL(xbt_cfg_register_str(set,"speed:1_to_2_int"));
20   TRYFAIL(xbt_cfg_register_str(set,"hostname:1_to_1_string"));
21   TRYFAIL(xbt_cfg_register_str(set,"user:1_to_10_string"));
22
23   return set;
24 } /* end_of_make_set */
25  
26 int main(int argc, char **argv) {
27   xbt_error_t errcode;
28   xbt_cfg_t set;
29
30   char *str;
31   
32   xbt_init_defaultlog(&argc,argv,"config.thresh=debug root.thresh=info");
33
34   fprintf(stderr,"==== Alloc and free a config set.\n");
35   set=make_set();
36   TRYFAIL(xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand"));
37   xbt_cfg_dump("test set","",set);
38   xbt_cfg_free(&set);
39   xbt_cfg_free(&set);
40
41   fprintf(stderr,
42           "==== Validation test. (ERROR EXPECTED: not enough values of 'speed')\n");
43   set=make_set();
44   xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
45   xbt_cfg_check(set);
46   xbt_cfg_free(&set);
47   xbt_cfg_free(&set);
48
49   fprintf(stderr,"==== Validation test (ERROR EXPECTED: too many elements)\n");
50   set=make_set();
51   xbt_cfg_set_parse(set,"hostname:toto:42");
52   xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
53   xbt_cfg_check(set);
54   xbt_cfg_free(&set);
55   xbt_cfg_free(&set);
56
57   fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n");
58   {     
59   /* get_single_value */
60   int ival;
61   xbt_cfg_t myset=make_set();
62      
63   xbt_cfg_set_parse(myset,"hostname:toto:42 speed:42");
64   xbt_cfg_get_int(myset,"speed",&ival); 
65   fprintf(stderr,"speed value: %d\n",ival); /* Prints: "speed value: 42" */
66   xbt_cfg_free(&myset);
67   }
68    
69   fprintf(stderr,"==== Get multiple values (Expected: 'Count: 3; Options: mquinson;ecaron;alegrand;')\n");
70   {     
71   /* get_multiple_value */
72   xbt_dynar_t dyn; 
73   int ival;
74   xbt_cfg_t myset=make_set();
75      
76   xbt_cfg_set_parse(myset, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
77   xbt_cfg_set_parse(myset,"speed:42");
78   xbt_cfg_check(myset); 
79   xbt_cfg_get_dynar(myset,"user",&dyn);
80   fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn));
81   xbt_dynar_foreach(dyn,ival,str) {
82     fprintf(stderr,"%s;",str); 
83   }
84   fprintf(stderr,"\n");
85   /* This prints: "Count: 3; Options: mquinson;ecaron;alegrand;" */
86   xbt_cfg_free(&myset);
87   }
88    
89   fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n");
90   {
91   xbt_cfg_t myset=make_set();
92   TRYEXPECT(mismatch_error,xbt_cfg_set_parse(myset,"color:blue"));
93   /* This spits an error: 'color' not registered */
94   xbt_cfg_free(&myset);
95   }
96
97   fprintf(stderr,"==== Success\n");
98   xbt_exit();
99   return 0;
100 }
101