Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Current state. See changelog, sorry, I'm out of time
[simgrid.git] / testsuite / xbt / multidict_crash.c
1 /* $Id$ */
2
3 /* multidict_crash - A crash test for multi-level dictionnaries             */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <assert.h>
13
14 #include <gras.h>
15
16 #define NB_ELM 10/*00*/
17 #define MULTICACHE_DEPTH 5
18 #define KEY_SIZE 12 /*512*/
19 #define NB_TEST 20
20 int verbose=0;
21
22 static gras_error_t test1();
23
24 static gras_error_t test1() {
25   gras_error_t errcode;
26   gras_dict_t *head=NULL;
27   int i,j,k,l;
28   char **key = NULL;
29   char **val = NULL;
30   void *data;
31
32   printf("\nGeneric multicache: CRASH test:\n");
33   printf(" Fill the struct and frees it %d times, using %d elements, depth of multicache=%d\n",NB_TEST,NB_ELM,MULTICACHE_DEPTH);
34   printf(" with %d chars long randomized keys. (a point is a test)\n",KEY_SIZE);
35
36   for (i=0;i<NB_TEST;i++) {
37     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
38     if (!(key=malloc(sizeof(char*)*MULTICACHE_DEPTH)))
39       RAISE_MALLOC;
40     if (!(val=malloc(sizeof(char*)*MULTICACHE_DEPTH)))
41       RAISE_MALLOC;
42     for (l=0 ; l<MULTICACHE_DEPTH ; l++)
43       if (!(val[l]=malloc(sizeof(char)*KEY_SIZE)))
44         RAISE_MALLOC;
45
46     for (j=0;j<NB_ELM;j++) {
47       if (verbose) printf ("Add ");
48       for (l=0 ; l<MULTICACHE_DEPTH ; l++){
49         for (k=0;k<KEY_SIZE-1;k++) {
50           val[l][k]=rand() % ('z' - 'a') + 'a';
51         }
52         val[l][k]='\0';
53         if (verbose) printf("%s ; ",val[l]);
54         key[l]=val[l];/*  NOWADAYS, no need to strdup the key. */
55       }
56       if (verbose) printf("in multitree %p.\n",head);
57       TRYFAIL(gras_multidict_set(&head,MULTICACHE_DEPTH,key,
58                                  strdup(val[0]),&free));
59
60       TRYFAIL(gras_multidict_get(head,
61                                  MULTICACHE_DEPTH,(const char **)val,
62                                  &data));
63       if (!data || strcmp((char*)data,val[0])) {
64         fprintf(stderr,"Retrieved value (%s) does not match the entrered one (%s)\n",
65                 (char*)data,val[0]);
66         abort();
67       }
68     }
69     gras_dict_dump(head,&gras_dict_print);
70     gras_dict_free(&head);
71
72     for (l=0 ; l<MULTICACHE_DEPTH ; l++)
73       if (val[l]) free(val[l]);
74     free(val);
75     free(key);
76
77   }
78
79   printf("\n");
80   return no_error;
81 }
82
83 int main(int argc, char *argv[]) {
84   gras_error_t errcode;
85
86   gras_init(argc,argv,"root.thresh=debug"));
87   TRYFAIL(test1());
88    
89   gras_exit();
90   return 0;
91 }