Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Revert API breakage about xbt_ex_free since it was not mandatory and since API should...
[simgrid.git] / src / xbt / config.c
index a6c8f6e..f89e94c 100644 (file)
@@ -53,7 +53,7 @@ static void xbt_cfg_str_free(void *d){
   free(*(void**)d);
 }
 static void xbt_cfg_host_free(void *d){
-  xbt_host_t *h=(xbt_host_t*) *(void**)d; 
+  xbt_host_t h=(xbt_host_t) *(void**)d; 
   if (h) {
     if (h->name) free(h->name);
     free(h);
@@ -116,7 +116,7 @@ void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
   int ival;
   char *sval;
   double dval;
-  xbt_host_t *hval;
+  xbt_host_t hval;
 
   if (name)
     printf("%s>> Dumping of the config set '%s':\n",indent,name);
@@ -155,7 +155,7 @@ void xbt_cfg_dump(const char *name,const char *indent,xbt_cfg_t cfg) {
 
     case xbt_cfgelm_host:
       for (i=0; i<size; i++) {
-       hval = xbt_dynar_get_as(variable->content,i,xbt_host_t*);
+       hval = xbt_dynar_get_as(variable->content,i,xbt_host_t);
        printf ("%s    %s:%d\n",indent,hval->name,hval->port);
       }
       break;
@@ -248,7 +248,7 @@ xbt_cfg_register(xbt_cfg_t cfg,
    break;
 
   case xbt_cfgelm_host:
-   res->content = xbt_dynar_new(sizeof(xbt_host_t*),&xbt_cfg_host_free);
+   res->content = xbt_dynar_new(sizeof(xbt_host_t),&xbt_cfg_host_free);
    break;
 
   default:
@@ -541,8 +541,8 @@ xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
       option=NULL; /* don't do next iteration */
 
     } else {
-      DEBUG3("Boundary on '%c'. len=%d;option-name=%d",
-            *option,len,option-name);
+      DEBUG3("Boundary on '%c'. len=%d;option-name=%ld",
+            *option,len,(long)(option-name));
 
       /* Pass the following blank chars */
       *(option++)='\0';
@@ -565,7 +565,7 @@ xbt_cfg_set_parse(xbt_cfg_t cfg, const char *options) {
     if (!val) {
       free(optionlist_cpy);
       xbt_assert1(FALSE,
-            "Malformated option: '%s'; Should be of the form 'name:value'",
+            "Option '%s' badly formated. Should be of the form 'name:value'",
                  name);
     }
     *(val++)='\0';
@@ -760,7 +760,7 @@ void
 xbt_cfg_set_host(xbt_cfg_t cfg,const char*name, 
                  const char *host,int port) {
   xbt_cfgelm_t variable;
-  xbt_host_t *val=xbt_new(xbt_host_t,1);
+  xbt_host_t val=xbt_new(s_xbt_host_t,1);
 
   VERB3("Configuration setting: %s=%s:%d",name,host,port);
 
@@ -895,7 +895,7 @@ void
 xbt_cfg_rm_host(xbt_cfg_t cfg,const char*name, const char *host,int port) {
   xbt_cfgelm_t variable;
   int cpt;
-  xbt_host_t *seen;
+  xbt_host_t seen;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
   
@@ -1060,7 +1060,7 @@ char* xbt_cfg_get_string(xbt_cfg_t  cfg, const char *name) {
 void xbt_cfg_get_host  (xbt_cfg_t   cfg,  const char *name,
                        char      **host, int        *port) {
   xbt_cfgelm_t variable;
-  xbt_host_t  *val;
+  xbt_host_t  val;
 
   variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_host);
 
@@ -1069,7 +1069,7 @@ void xbt_cfg_get_host  (xbt_cfg_t   cfg,  const char *name,
             name, xbt_dynar_length(variable->content));
   }
 
-  val = xbt_dynar_get_as(variable->content, 0, xbt_host_t*);
+  val = xbt_dynar_get_as(variable->content, 0, xbt_host_t);
   *host=val->name;
   *port=val->port;
 }
@@ -1134,8 +1134,131 @@ xbt_cfg_get_host_at(xbt_cfg_t cfg, const char *name, int pos,
                     char **host, int *port) {
                   
   xbt_cfgelm_t variable = xbt_cfgelm_get(cfg,name,xbt_cfgelm_int);
-  xbt_host_t *val = xbt_dynar_get_ptr(variable->content, pos);
+  xbt_host_t val = xbt_dynar_get_ptr(variable->content, pos);
 
   *port = val->port;
   *host = val->name;
 }
+
+
+#ifdef SIMGRID_TEST
+#include "xbt.h"
+#include "xbt/ex.h"
+
+XBT_TEST_SUITE("config","Configuration support");
+
+static xbt_cfg_t make_set(){
+  xbt_cfg_t set=NULL; 
+
+  set = xbt_cfg_new();
+  xbt_cfg_register_str(set,"speed:1_to_2_int");
+  xbt_cfg_register_str(set,"hostname:1_to_1_string");
+  xbt_cfg_register_str(set,"user:1_to_10_string");
+
+  return set;
+} /* end_of_make_set */
+
+XBT_TEST_UNIT("memuse",test_config_memuse,"Alloc and free a config set") {
+  xbt_cfg_t set=make_set();
+  xbt_test_add0("Alloc and free a config set");
+  xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
+  xbt_cfg_free(&set);
+  xbt_cfg_free(&set);
+}
+
+XBT_TEST_UNIT("validation",test_config_validation,"Validation tests") {
+  xbt_cfg_t set = set=make_set();
+  xbt_ex_t e;
+  
+  xbt_test_add0("Having too few elements for speed");
+  xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
+  TRY {
+    xbt_cfg_check(set);
+  } CATCH(e) {
+    if (e.category != mismatch_error || 
+       strncmp(e.msg,"Config elem speed needs",strlen("Config elem speed needs")))
+      xbt_test_fail1("Got an exception. msg=%s",e.msg);
+    xbt_ex_free(e);
+  }
+  xbt_cfg_free(&set);
+  xbt_cfg_free(&set);
+
+
+
+  xbt_test_add0("Having too much values of 'speed'");
+  set=make_set(); 
+  xbt_cfg_set_parse(set,"hostname:toto:42 user:alegrand");
+  TRY {
+    xbt_cfg_set_parse(set,"speed:42 speed:24 speed:34");
+  } CATCH(e) {
+    if (e.category != mismatch_error ||
+       strncmp(e.msg,"Cannot add value 34 to the config elem speed",
+               strlen("Config elem speed needs")))
+      xbt_test_fail1("Got an exception. msg=%s",e.msg);
+    xbt_ex_free(e);
+  }
+  xbt_cfg_check(set);
+  xbt_cfg_free(&set);
+  xbt_cfg_free(&set);
+
+}
+
+XBT_TEST_UNIT("use",test_config_use,"Data retrieving tests") {
+  xbt_cfg_t set = set=make_set();
+
+  xbt_test_add0("Get a single value");
+  {    
+    /* get_single_value */
+    int ival;
+    xbt_cfg_t myset=make_set();
+    
+    xbt_cfg_set_parse(myset,"hostname:toto:42 speed:42");
+    ival = xbt_cfg_get_int(myset,"speed"); 
+    if (ival != 42) 
+      xbt_test_fail1("Speed value = %d, I expected 42",ival);
+    xbt_cfg_free(&myset);
+  }
+
+  xbt_test_add0("Get multiple values");
+  {    
+    /* get_multiple_value */
+    xbt_dynar_t dyn; 
+    xbt_cfg_t myset=make_set();
+    
+    xbt_cfg_set_parse(myset, "hostname:veloce user:foo\nuser:bar\tuser:toto");
+    xbt_cfg_set_parse(myset,"speed:42");
+    xbt_cfg_check(myset); 
+    dyn = xbt_cfg_get_dynar(myset,"user");
+
+    if (xbt_dynar_length(dyn) != 3) 
+      xbt_test_fail1("Dynar length = %d, I expected 3", (int)xbt_dynar_length(dyn));
+
+    if (strcmp(xbt_dynar_get_as(dyn,0,char*),"foo"))
+      xbt_test_fail1("Dynar[0] = %s, I expected foo",   xbt_dynar_get_as(dyn,0,char*));
+
+    if (strcmp(xbt_dynar_get_as(dyn,1,char*),"bar"))
+      xbt_test_fail1("Dynar[1] = %s, I expected bar",   xbt_dynar_get_as(dyn,1,char*));
+
+    if (strcmp(xbt_dynar_get_as(dyn,2,char*),"toto"))
+      xbt_test_fail1("Dynar[2] = %s, I expected toto",  xbt_dynar_get_as(dyn,2,char*));
+    xbt_cfg_free(&myset);    
+  }
+  
+  xbt_test_add0("Access to a non-existant entry");
+  {    
+    /* non-existant_entry */
+    xbt_cfg_t myset=make_set();
+    xbt_ex_t e;
+    
+    TRY {
+      xbt_cfg_set_parse(myset, "color:blue");
+    } CATCH(e) {
+      if (e.category != not_found_error)
+        xbt_test_exception(e);
+      xbt_ex_free(e);
+    }
+    xbt_cfg_free(&myset);    
+  }
+}
+#endif /* SIMGRID_TEST */