#include <stdio.h>
#include "gras.h"
-/*====[ Prototypes ]=========================================================*/
-xbt_cfg_t make_set(void); /* build a minimal set */
-
/*====[ Code ]===============================================================*/
-xbt_cfg_t make_set(){
+static xbt_cfg_t make_set(){
xbt_cfg_t set=NULL;
xbt_error_t errcode;
TRYFAIL(xbt_cfg_register_str(set,"hostname:1_to_1_string"));
TRYFAIL(xbt_cfg_register_str(set,"user:1_to_10_string"));
- TRYFAIL(xbt_cfg_set_parse(set, "hostname:veloce "
- "user:mquinson\nuser:oaumage\tuser:alegrand"));
return set;
-}
+} /* end_of_make_set */
int main(int argc, char **argv) {
xbt_error_t errcode;
xbt_cfg_t set;
- xbt_dynar_t dyn;
char *str;
- int ival;
xbt_init_defaultlog(&argc,argv,"config.thresh=debug root.thresh=info");
fprintf(stderr,"==== Alloc and free a config set.\n");
set=make_set();
+ TRYFAIL(xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand"));
xbt_cfg_dump("test set","",set);
xbt_cfg_free(&set);
xbt_cfg_free(&set);
-
- fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n");
- set=make_set();
- TRYEXPECT(mismatch_error,xbt_cfg_set_parse(set,"color:blue"));
- xbt_cfg_free(&set);
- xbt_cfg_free(&set);
-
-
fprintf(stderr,
"==== Validation test. (ERROR EXPECTED: not enough values of 'speed')\n");
set=make_set();
+ xbt_cfg_set_parse(set, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
xbt_cfg_check(set);
xbt_cfg_free(&set);
xbt_cfg_free(&set);
xbt_cfg_free(&set);
fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n");
- set=make_set();
- xbt_cfg_set_parse(set,"hostname:toto:42 speed:42");
- xbt_cfg_get_int(set,"speed",&ival);
- fprintf(stderr,"speed value: %d\n",ival);
- xbt_cfg_free(&set);
- xbt_cfg_free(&set);
-
+ {
+ /* get_single_value */
+ int ival;
+ xbt_cfg_t myset=make_set();
+
+ xbt_cfg_set_parse(myset,"hostname:toto:42 speed:42");
+ xbt_cfg_get_int(myset,"speed",&ival);
+ fprintf(stderr,"speed value: %d\n",ival); /* Prints: "speed value: 42" */
+ xbt_cfg_free(&myset);
+ }
+
fprintf(stderr,"==== Get multiple values (Expected: 'Count: 3; Options: mquinson;ecaron;alegrand;')\n");
- set=make_set();
- xbt_cfg_set_parse(set,"speed:42");
- xbt_cfg_check(set);
- xbt_cfg_get_dynar(set,"user",&dyn);
+ {
+ /* get_multiple_value */
+ xbt_dynar_t dyn;
+ int ival;
+ xbt_cfg_t myset=make_set();
+
+ xbt_cfg_set_parse(myset, "hostname:veloce user:mquinson\nuser:oaumage\tuser:alegrand");
+ xbt_cfg_set_parse(myset,"speed:42");
+ xbt_cfg_check(myset);
+ xbt_cfg_get_dynar(myset,"user",&dyn);
fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn));
xbt_dynar_foreach(dyn,ival,str) {
- fprintf(stderr,"%s;",str);
+ fprintf(stderr,"%s;",str);
}
fprintf(stderr,"\n");
- xbt_cfg_free(&set);
- xbt_cfg_free(&set);
+ /* This prints: "Count: 3; Options: mquinson;ecaron;alegrand;" */
+ xbt_cfg_free(&myset);
+ }
+
+ fprintf(stderr,"==== Try to use an unregistered option. (ERROR EXPECTED: 'color' not registered)\n");
+ {
+ xbt_cfg_t myset=make_set();
+ TRYEXPECT(mismatch_error,xbt_cfg_set_parse(myset,"color:blue"));
+ /* This spits an error: 'color' not registered */
+ xbt_cfg_free(&myset);
+ }
+ fprintf(stderr,"==== Success\n");
xbt_exit();
return 0;
}
XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
int main(int argc,char *argv[]) {
+ /* Vars_decl [doxygen cruft] */
xbt_dynar_t d;
xbt_error_t errcode;
int i,cpt,cursor;
INFO1("==== Push %d int, set them again 3 times, traverse them, shift them",
NB_ELEM);
+ /* Populate_ints [doxygen cruft] */
+ /* 1. Populate the dynar */
d=xbt_dynar_new(sizeof(int),NULL);
for (cpt=0; cpt< NB_ELEM; cpt++) {
- xbt_dynar_push_as(d,int,cpt);
+ xbt_dynar_push_as(d,int,cpt); /* This is faster (and possible only with scalars) */
+ /* xbt_dynar_push(d,&cpt); This would also work */
DEBUG2("Push %d, length=%lu",cpt, xbt_dynar_length(d));
}
+
+ /* 2. Traverse manually the dynar */
for (cursor=0; cursor< NB_ELEM; cursor++) {
iptr=xbt_dynar_get_ptr(d,cursor);
xbt_assert2(cursor == *iptr,
- "The retrieved value is not the same than the injected one (%d!=%d)",
- cursor,cpt);
+ "The retrieved value is not the same than the injected one (%d!=%d)",
+ cursor,cpt);
}
+
+ /* 3. Traverse the dynar using the neat macro to that extend */
xbt_dynar_foreach(d,cursor,cpt){
xbt_assert2(cursor == cpt,
- "The retrieved value is not the same than the injected one (%d!=%d)",
- cursor,cpt);
+ "The retrieved value is not the same than the injected one (%d!=%d)",
+ cursor,cpt);
}
+ /* end_of_traversal */
+
for (cpt=0; cpt< NB_ELEM; cpt++)
*(int*)xbt_dynar_get_ptr(d,cpt) = cpt;
"Cannot retrieve my %d values. Last got one is %d",
NB_ELEM, cpt);
+ /* shifting [doxygen cruft] */
+ /* 4. Shift all the values */
for (cpt=0; cpt< NB_ELEM; cpt++) {
xbt_dynar_shift(d,&i);
xbt_assert2(i == cpt,
i,cpt);
DEBUG2("Pop %d, length=%lu",cpt, xbt_dynar_length(d));
}
+
+ /* 5. Free the resources */
xbt_dynar_free(&d);
xbt_dynar_free(&d);
#define NB_ELEM 5000
XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
-void free_string(void *d);
+/* doxygen_first_cruft*/
-void free_string(void *d){
+/* The function we will use to free the data */
+static void free_string(void *d){
free(*(void**)d);
}
INFO1("==== Push %d strings, set them again 3 times, shift them",NB_ELEM);
d=xbt_dynar_new(sizeof(char*),&free_string);
+ /* Populate_str [doxygen cruft] */
+ /* 1. Populate the dynar */
for (cpt=0; cpt< NB_ELEM; cpt++) {
sprintf(buf,"%d",cpt);
s1=strdup(buf);
s1=strdup(buf);
xbt_dynar_unshift(d,&s1);
}
+ /* 2. Traverse the dynar with the macro */
xbt_dynar_foreach(d,cpt,s1) {
sprintf(buf,"%d",NB_ELEM - cpt -1);
xbt_assert2 (!strcmp(buf,s1),
"The retrieved value is not the same than the injected one (%s!=%s)",
buf,s1);
}
+ /* 3. Traverse the dynar with the macro */
for (cpt=0; cpt< NB_ELEM; cpt++) {
sprintf(buf,"%d",cpt);
xbt_dynar_pop(d,&s2);
buf,s2);
free(s2);
}
+ /* 4. Free the resources */
xbt_dynar_free(&d);
xbt_dynar_free(&d);