Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix memleaks
[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 XBT_LOG_NEW_CATEGORY(test,"Logging for this test");
14
15 /*====[ Code ]===============================================================*/
16 static xbt_cfg_t make_set(){
17   xbt_cfg_t set=NULL; 
18
19   set = xbt_cfg_new();
20   xbt_cfg_register_str(set,"speed:1_to_2_int");
21   xbt_cfg_register_str(set,"hostname:1_to_1_string");
22   xbt_cfg_register_str(set,"user:1_to_10_string");
23
24   return set;
25 } /* end_of_make_set */
26  
27 int main(int argc, char **argv) {
28   xbt_ex_t e;
29   xbt_cfg_t set;
30
31   char *str;
32   
33   xbt_init(&argc,argv);
34
35   fprintf(stderr,"==== Alloc and free a config set.\n");
36   set=make_set();
37   xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
38   xbt_cfg_dump("test set","",set);
39   xbt_cfg_free(&set);
40   xbt_cfg_free(&set);
41
42   fprintf(stderr, "==== Validation test with too few values of 'speed'\n");
43   set=make_set();
44   xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
45   TRY {
46     xbt_cfg_check(set);
47   } CATCH(e) {
48     if (e.category != mismatch_error || 
49         strncmp(e.msg,"Config elem speed needs",strlen("Config elem speed needs")))
50       RETHROW;
51     xbt_ex_free(e);
52   }
53   xbt_cfg_free(&set);
54   xbt_cfg_free(&set);
55
56   fprintf(stderr,"==== Validation test with too much values of 'speed'\n");
57   set=make_set(); 
58     xbt_cfg_set_parse(set,"hostname:toto:42 user:alegrand");
59   TRY {
60     xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
61   } CATCH(e) {
62     if (e.category != mismatch_error ||
63         strncmp(e.msg,"Cannot add value 34 to the config elem speed",
64                 strlen("Config elem speed needs")))
65       RETHROW;
66     xbt_ex_free(e);
67   }
68   xbt_cfg_check(set);
69   xbt_cfg_free(&set);
70   xbt_cfg_free(&set);
71
72   fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n");
73   {     
74   /* get_single_value */
75   int ival;
76   xbt_cfg_t myset=make_set();
77      
78   xbt_cfg_set_parse(myset,"hostname:toto:42 speed:42");
79   ival = xbt_cfg_get_int(myset,"speed"); 
80   fprintf(stderr,"speed value: %d\n",ival); /* Prints: "speed value: 42" */
81   xbt_cfg_free(&myset);
82   }
83    
84   fprintf(stderr,"==== Get multiple values (Expected: 'Count: 3; Options: mquinson;ecaron;alegrand;')\n");
85   {     
86   /* get_multiple_value */
87   xbt_dynar_t dyn; 
88   int ival;
89   xbt_cfg_t myset=make_set();
90      
91   xbt_cfg_set_parse(myset, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
92   xbt_cfg_set_parse(myset,"speed:42");
93   xbt_cfg_check(myset); 
94   dyn = xbt_cfg_get_dynar(myset,"user");
95   fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn));
96   xbt_dynar_foreach(dyn,ival,str) {
97     fprintf(stderr,"%s;",str); 
98   }
99   fprintf(stderr,"\n");
100   /* This prints: "Count: 3; Options: mquinson;ecaron;alegrand;" */
101   xbt_cfg_free(&myset);
102   }
103    
104   fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n");
105   {
106   xbt_cfg_t myset=make_set();
107   TRY {
108     xbt_cfg_set_parse(myset,"color:blue");
109     THROW1(mismatch_error,0,"Found an option which shouldn't be there (%s)","color:blue");
110   } CATCH(e) {
111     if (e.category != not_found_error)
112       RETHROW;
113     xbt_ex_free(e);
114   }
115   /* This spits an error: 'color' not registered */
116   xbt_cfg_free(&myset);
117   }
118
119   fprintf(stderr,"==== Success\n");
120   xbt_exit();
121   return 0;
122 }
123