X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/d9dde74e0e8fcaacdd7ad844be2a5b152d893731..a63d3d85e1818679541ec2d28613e2f04a15a3d0:/testsuite/xbt/config_usage.c diff --git a/testsuite/xbt/config_usage.c b/testsuite/xbt/config_usage.c index e47e2a16d7..95a82f5750 100644 --- a/testsuite/xbt/config_usage.c +++ b/testsuite/xbt/config_usage.c @@ -2,93 +2,122 @@ /* test config - test code to the config set */ -#include -#include +/* Copyright (c) 2004 Martin Quinson. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ -/*====[ Prototypes ]=========================================================*/ -gras_cfg_t *make_set(void); /* build a minimal set */ -int test3(void); /* validate=>not enought */ -int test4(void); /* validate=> too many */ -int test5(void); /* get users list */ +#include +#include "gras.h" +XBT_LOG_NEW_CATEGORY(test,"Logging for this test"); /*====[ Code ]===============================================================*/ -gras_cfg_t *make_set(){ - gras_cfg_t *set=NULL; - gras_error_t errcode; +static xbt_cfg_t make_set(){ + xbt_cfg_t set=NULL; - TRYFAIL(gras_cfg_new(&set)); - gras_cfg_register_str(set,"hostname:1_to_1_string"); - gras_cfg_register_str(set,"user:1_to_10_string"); - gras_cfg_register_str(set,"speed:1_to_1_int"); + 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"); - gras_cfg_set_parse(set, - "hostname:veloce " - "user:mquinson\nuser:ecaron\tuser:fsuter"); return set; -} - - -/*----[ get users list ]-----------------------------------------------------*/ -int test5() -{ - gras_dynar_t *dyn; - char *str; - int i; - - gras_cfg_t *set=make_set(); - gras_cfg_set_parse(set,"speed:42"); - gras_cfg_check(set); - gras_cfg_get_dynar(set,"user",&dyn); - printf("Count: %d; Options: \n",gras_dynar_length(dyn)); - gras_dynar_foreach(dyn,i,str) { - printf("%s\n",str); - } - gras_cfg_free(&set); - return 1; -} +} /* end_of_make_set */ -void parse_log_opt(int argc, char **argv,const char *deft); - int main(int argc, char **argv) { - gras_error_t errcode; - gras_cfg_t *set; - int ival; + xbt_ex_t e; + xbt_cfg_t set; + + char *str; - parse_log_opt(argc,argv,"config.thresh=debug root.thresh=info"); + xbt_init(&argc,argv); fprintf(stderr,"==== Alloc and free a config set.\n"); set=make_set(); - gras_cfg_dump("test set","",set); - gras_cfg_free(&set); - + 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 (err msg expected).\n"); + fprintf(stderr, "==== Validation test with too few values of 'speed'\n"); set=make_set(); - TRYEXPECT(mismatch_error,gras_cfg_set_parse(set,"color:blue")); - gras_cfg_free(&set); - - - fprintf(stderr, - "\n==== Validation test (err msg about not enough values expected)\n"); - set=make_set(); - gras_cfg_check(set); - gras_cfg_free(&set); - - fprintf(stderr,"\n==== Validation test (too many elements)\n"); - set=make_set(); - gras_cfg_set_parse(set,"hostname:toto:42"); - gras_cfg_set_parse(set,"speed:42 speed:24"); - gras_cfg_check(set); - gras_cfg_get_int(set,"speed",&ival); - printf("speed value: %d\n",ival); - gras_cfg_free(&set); + 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"))) + RETHROW; + xbt_ex_free(e); + } + xbt_cfg_free(&set); + xbt_cfg_free(&set); + + fprintf(stderr,"==== Validation test with too much values of 'speed'\n"); + 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"))) + RETHROW; + xbt_ex_free(e); + } + xbt_cfg_check(set); + xbt_cfg_free(&set); + xbt_cfg_free(&set); - fprintf(stderr,"\n§§§§§§§§§ %s §§§§§§§§§\n§§§ Expected: %s\n", - "TEST5", - "Count: 3; Options:\\nmquinson\\necaron\\nfsuter"); - test5(); + fprintf(stderr,"==== Get single value (Expected: 'speed value: 42')\n"); + { + /* 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"); + 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"); + { + /* 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); + dyn = xbt_cfg_get_dynar(myset,"user"); + fprintf(stderr,"Count: %lu; Options: ",xbt_dynar_length(dyn)); + xbt_dynar_foreach(dyn,ival,str) { + fprintf(stderr,"%s;",str); + } + fprintf(stderr,"\n"); + /* 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(); + TRY { + xbt_cfg_set_parse(myset,"color:blue"); + THROW1(mismatch_error,0,"Found an option which shouldn't be there (%s)","color:blue"); + } CATCH(e) { + if (e.category != not_found_error) + RETHROW; + xbt_ex_free(e); + } + /* This spits an error: 'color' not registered */ + xbt_cfg_free(&myset); + } + fprintf(stderr,"==== Success\n"); + xbt_exit(); return 0; }