Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
75fdb101eec26c3dd1ea92ff933fb6fd5ea37b36
[simgrid.git] / testsuite / xbt / dict_crash.c
1 /* $Id$ */
2
3 /* dict_crash - A crash test for 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 <gras.h>
12 #include <time.h>
13 #include <stdio.h>
14
15 #define NB_ELM 200000
16 #define SIZEOFKEY 1024
17
18 static void print_str(void *str);
19 static void print_str(void *str) {
20   printf("%s",(char*)str);
21 }
22
23
24 static gras_error_t traverse(gras_dict_t *head) {
25   gras_dict_cursor_t *cursor=NULL;
26   char *key;
27   char *data;
28
29   gras_dict_foreach(head,cursor,key,data) {
30     //    printf("   Seen:  %s=%s\n",key,data);
31     if (strcmp(key,data)) {
32       printf("Key(%s) != value(%s). Abording\n",key,data);
33       abort();
34     }
35   }
36   return no_error;
37 }
38
39 static gras_error_t countelems(gras_dict_t *head,int*count) {
40   gras_dict_cursor_t *cursor;
41   char *key;
42   void *data;
43   *count=0;
44
45   gras_dict_foreach(head,cursor,key,data) {
46     (*count)++;
47   }
48   return no_error;
49 }
50
51 int main() {
52   gras_error_t errcode;
53   gras_dict_t *head=NULL;
54   int i,j,k, nb;
55   char *key;
56   void *data;
57
58   TRY(gras_log_control_set("root.thresh=info"));
59   srand((unsigned int)time(NULL));
60
61   printf("Dictionnary: CRASH test:\n");
62   printf(" Fill the struct, count its elems and frees the structure (x20)\n");
63   printf(" using 1000 elements with %d chars long randomized keys.\n",SIZEOFKEY);
64   printf(" (a point is a test)\n");
65
66   for (i=0;i<20;i++) {
67     TRYFAIL(gras_dict_new(&head));
68     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
69     nb=0;
70     for (j=0;j<1000;j++) {
71       if (!(key=malloc(SIZEOFKEY))) {
72         fprintf(stderr,"Out of memory\n");
73         return 1;
74       }
75
76       for (k=0;k<SIZEOFKEY-1;k++)
77         key[k]=rand() % ('z' - 'a') + 'a';
78       key[k]='\0';
79       //      printf("[%d %s]\n",j,key);
80       TRYFAIL(gras_dict_insert(head,strdup(key),key,&free));
81     }
82     nb=0;
83     //    gras_dict_dump(head,(void (*)(void*))&printf);
84     TRYFAIL(countelems(head,&nb));
85     if (nb != 1000) {
86        printf ("\nI found %d elements, and not 1000\n",nb);
87        abort();
88     }     
89     TRYFAIL(traverse(head));
90     gras_dict_free(&head);
91   }
92
93
94   TRYFAIL(gras_dict_new(&head));
95   printf("\n Fill 200 000 elements, with keys being the number of element\n");
96   printf("  (a point is 10 000 elements)\n");
97   for (j=0;j<NB_ELM;j++) {
98     if (!(j%10000)) printf("."); fflush(stdout);
99     if (!(key=malloc(10))) {
100       fprintf(stderr,"Out of memory\n");
101       abort();
102     }
103     
104     sprintf(key,"%d",j);
105     TRYFAIL(gras_dict_insert(head,key,key,&free));
106   }
107
108   printf("\n Count the elements (retrieving the key and data for each): \n");
109   TRYFAIL(countelems(head,&i));
110
111   printf(" There is %d elements\n",i);
112   printf("\n Search my 200 000 elements 20 times. (a point is a test)\n");
113   if (!(key=malloc(10))) {
114     fprintf(stderr,"Out of memory\n");
115     abort();
116   } 
117   for (i=0;i<20;i++) {
118     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
119     for (j=0;j<NB_ELM;j++) {
120       
121       sprintf(key,"%d",j);
122       TRYFAIL(gras_dict_retrieve(head,key,&data));
123       if (strcmp(key,(char*)data)) {
124         printf("key=%s != data=%s\n",key,(char*)data);
125         abort();
126       }
127     }
128   }
129
130   printf("\n Remove my 200 000 elements. (a point is 10 000 elements)\n");
131   if (!(key=malloc(10))) {
132     fprintf(stderr,"Out of memory\n");
133     return 3;
134   }
135   for (j=0;j<NB_ELM;j++) {
136     if (!(j%10000)) printf("."); fflush(stdout);
137     
138     sprintf(key,"%d",j);
139     TRYFAIL(gras_dict_remove(head,key));
140   }
141
142   gras_dict_free(&head);
143   gras_dict_free(&head);
144   printf("\n");
145   return 0;
146 }