Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
2a0e4b031f7f85f92cfd42bb9f9519a727de0d9c
[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_dict_t mdict = NULL;
29   int i,j,k,l;
30   xbt_dynar_t keys = xbt_dynar_new(sizeof(char*),str_free);
31   void *data;
32   char *key;
33
34   xbt_init(&argc,argv);
35
36   printf("\nGeneric multicache: CRASH test:\n");
37   printf(" Fill the struct and frees it %d times, using %d elements, depth of multicache=%d\n",NB_TEST,NB_ELM,DEPTH);
38   printf(" with %d chars long randomized keys. (a point is a test)\n",KEY_SIZE);
39
40   for (l=0 ; l<DEPTH ; l++) {
41     key=xbt_malloc(KEY_SIZE);
42     xbt_dynar_push(keys,&key);
43   }     
44
45
46   for (i=0;i<NB_TEST;i++) {
47     mdict = xbt_dict_new();
48     VERB1("mdict=%p",mdict);
49     if (verbose>0) {
50       printf("Test %d\n",i);
51     } else if (verbose==0) {
52       if (i%10) printf("."); else printf("%d",i/10);
53     }
54     fflush(stdout);
55     
56     for (j=0;j<NB_ELM;j++) {
57       if (verbose>0) printf ("  Add {");
58       
59       for (l=0 ; l<DEPTH ; l++) {
60         key=*(char**)xbt_dynar_get_ptr(keys,l);
61         
62         for (k=0;k<KEY_SIZE-1;k++) 
63           key[k]=rand() % ('z' - 'a') + 'a';
64           
65         key[k]='\0';
66         
67         if (verbose>0) printf("%p=%s %s ",key, key,(l<DEPTH-1?";":"}"));
68       }
69       if (verbose>0) printf("in multitree %p.\n",mdict);
70                                                         
71       xbt_multidict_set(mdict,keys,xbt_strdup(key),free);
72
73       data = xbt_multidict_get(mdict,keys);
74
75       xbt_assert2(data && !strcmp((char*)data,key),
76                   "Retrieved value (%s) does not match the entrered one (%s)\n",
77                   (char*)data,key);
78     }
79     xbt_dict_free(&mdict);
80   }
81   
82   xbt_dynar_free(&keys);
83
84 /*  if (verbose>0)
85     xbt_dict_dump(mdict,&xbt_dict_print);*/
86     
87   xbt_dict_free(&mdict);
88   xbt_dynar_free(&keys);
89   printf("\n");
90
91   xbt_exit();
92   return 0;
93 }