Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
include libex
[simgrid.git] / testsuite / xbt / multidict_crash.c
1 /* $Id$ */
2
3 /* multidict_crash - A crash test for multi-level dictionnaries             */
4
5 /* Copyright (c) 2003-2005 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
12 #include "xbt.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(Test,"this test");
15
16 #define NB_ELM 100 /*00*/
17 #define DEPTH 5
18 #define KEY_SIZE 512
19 #define NB_TEST 20 /*20*/
20 int verbose=0;
21
22 static void str_free(void *s) {
23   char *c=*(char**)s;
24   free(c);
25 }
26
27 int main(int argc, char *argv[]) {
28   xbt_error_t errcode;
29   xbt_dict_t mdict = NULL;
30   int i,j,k,l;
31   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
32   void *data;
33   char *key;
34
35   xbt_init(&argc,argv);
36
37   printf("\nGeneric multicache: CRASH test:\n");
38   printf(" Fill the struct and frees it %d times, using %d elements, depth of multicache=%d\n",NB_TEST,NB_ELM,DEPTH);
39   printf(" with %d chars long randomized keys. (a point is a test)\n",KEY_SIZE);
40
41   for (l=0 ; l<DEPTH ; l++) {
42     key=xbt_malloc(KEY_SIZE);
43     xbt_dynar_push(keys,&key);
44   }     
45
46
47   for (i=0;i<NB_TEST;i++) {
48     mdict = xbt_dict_new();
49     VERB1("mdict=%p",mdict);
50     if (verbose>0) {
51       printf("Test %d\n",i);
52     } else if (verbose==0) {
53       if (i%10) printf("."); else printf("%d",i/10);
54     }
55     fflush(stdout);
56     
57     for (j=0;j<NB_ELM;j++) {
58       if (verbose>0) printf ("  Add {");
59       
60       for (l=0 ; l<DEPTH ; l++) {
61         key=*(char**)xbt_dynar_get_ptr(keys,l);
62         
63         for (k=0;k<KEY_SIZE-1;k++) 
64           key[k]=rand() % ('z' - 'a') + 'a';
65           
66         key[k]='\0';
67         
68         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
69       }
70       if (verbose>0) printf("in multitree %p.\n",mdict);
71                                                         
72       TRYFAIL(xbt_multidict_set(mdict,keys,xbt_strdup(key),free));
73
74       TRYFAIL(xbt_multidict_get(mdict,keys,&data));
75
76       xbt_assert2(data && !strcmp((char*)data,key),
77                   "Retrieved value (%s) does not match the entrered one (%s)\n",
78                   (char*)data,key);
79     }
80     xbt_dict_free(&mdict);
81   }
82   
83   xbt_dynar_free(&keys);
84
85 /*  if (verbose>0)
86     xbt_dict_dump(mdict,&xbt_dict_print);*/
87     
88   xbt_dict_free(&mdict);
89   xbt_dynar_free(&keys);
90   printf("\n");
91
92   xbt_exit();
93   return 0;
94 }