Logo AND Algorithmique Numérique Distribuée

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