Logo AND Algorithmique Numérique Distribuée

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