Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
We don't need you anymore. You are weak. Goodbye !
[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, 2004 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 "gras.h"
13
14 #define NB_ELM 10/*00*/
15 #define MULTICACHE_DEPTH 5
16 #define KEY_SIZE 12 /*512*/
17 #define NB_TEST 20
18 int verbose=0;
19
20 static xbt_error_t test1();
21
22 static xbt_error_t test1() {
23   xbt_error_t errcode;
24   xbt_dict_t *head=NULL;
25   int i,j,k,l;
26   char **key = NULL;
27   char **val = NULL;
28   void *data;
29
30   printf("\nGeneric multicache: CRASH test:\n");
31   printf(" Fill the struct and frees it %d times, using %d elements, depth of multicache=%d\n",NB_TEST,NB_ELM,MULTICACHE_DEPTH);
32   printf(" with %d chars long randomized keys. (a point is a test)\n",KEY_SIZE);
33
34   for (i=0;i<NB_TEST;i++) {
35     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
36     if (!(key=malloc(sizeof(char*)*MULTICACHE_DEPTH)))
37       RAISE_MALLOC;
38     if (!(val=malloc(sizeof(char*)*MULTICACHE_DEPTH)))
39       RAISE_MALLOC;
40     for (l=0 ; l<MULTICACHE_DEPTH ; l++)
41       if (!(val[l]=malloc(sizeof(char)*KEY_SIZE)))
42         RAISE_MALLOC;
43
44     for (j=0;j<NB_ELM;j++) {
45       if (verbose) printf ("Add ");
46       for (l=0 ; l<MULTICACHE_DEPTH ; l++){
47         for (k=0;k<KEY_SIZE-1;k++) {
48           val[l][k]=rand() % ('z' - 'a') + 'a';
49         }
50         val[l][k]='\0';
51         if (verbose) printf("%s ; ",val[l]);
52         key[l]=val[l];/*  NOWADAYS, no need to strdup the key. */
53       }
54       if (verbose) printf("in multitree %p.\n",head);
55       TRYFAIL(xbt_multidict_set(&head,MULTICACHE_DEPTH,key,
56                                  strdup(val[0]),&free));
57
58       TRYFAIL(xbt_multidict_get(head,
59                                  MULTICACHE_DEPTH,(const char **)val,
60                                  &data));
61       if (!data || strcmp((char*)data,val[0])) {
62         fprintf(stderr,"Retrieved value (%s) does not match the entrered one (%s)\n",
63                 (char*)data,val[0]);
64         abort();
65       }
66     }
67     xbt_dict_dump(head,&xbt_dict_print);
68     xbt_dict_free(&head);
69
70     for (l=0 ; l<MULTICACHE_DEPTH ; l++)
71       if (val[l]) free(val[l]);
72     free(val);
73     free(key);
74
75   }
76
77   printf("\n");
78   return no_error;
79 }
80
81 int main(int argc, char *argv[]) {
82   xbt_error_t errcode;
83
84   xbt_init(argc,argv,"root.thresh=debug"));
85   TRYFAIL(test1());
86    
87   xbt_exit();
88   return 0;
89 }