Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Reduce overengeneering around datadesc, put stubs in place so that the kernel compile...
[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 void parse_log_opt(int argc, char **argv, const char *deft);
52
53 int main(int argc,char **argv) {
54   gras_error_t errcode;
55   gras_dict_t *head=NULL;
56   int i,j,k, nb;
57   char *key;
58   void *data;
59
60   parse_log_opt(argc,argv,"dict.thresh=verbose");
61   srand((unsigned int)time(NULL));
62
63   printf("Dictionnary: CRASH test:\n");
64   printf(" Fill the struct, count its elems and frees the structure (x20)\n");
65   printf(" using 1000 elements with %d chars long randomized keys.\n",SIZEOFKEY);
66   printf(" (a point is a test)\n");
67
68   for (i=0;i<20;i++) {
69     TRYFAIL(gras_dict_new(&head));
70     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
71     nb=0;
72     for (j=0;j<1000;j++) {
73       if (!(key=malloc(SIZEOFKEY))) {
74         fprintf(stderr,"Out of memory\n");
75         return 1;
76       }
77
78       for (k=0;k<SIZEOFKEY-1;k++)
79         key[k]=rand() % ('z' - 'a') + 'a';
80       key[k]='\0';
81       //      printf("[%d %s]\n",j,key);
82       TRYFAIL(gras_dict_insert(head,key,key,&free));
83     }
84     nb=0;
85     //    gras_dict_dump(head,(void (*)(void*))&printf);
86     TRYFAIL(countelems(head,&nb));
87     if (nb != 1000) {
88        printf ("\nI found %d elements, and not 1000\n",nb);
89        abort();
90     }     
91     TRYFAIL(traverse(head));
92     gras_dict_free(&head);
93   }
94
95
96   TRYFAIL(gras_dict_new(&head));
97   printf("\n Fill 200 000 elements, with keys being the number of element\n");
98   printf("  (a point is 10 000 elements)\n");
99   for (j=0;j<NB_ELM;j++) {
100     if (!(j%10000)) {
101       printf("."); 
102       fflush(stdout);
103     }
104     if (!(key=malloc(10))) {
105       fprintf(stderr,"Out of memory\n");
106       abort();
107     }
108     
109     sprintf(key,"%d",j);
110     TRYFAIL(gras_dict_insert(head,key,key,&free));
111   }
112
113   printf("\n Count the elements (retrieving the key and data for each): \n");
114   TRYFAIL(countelems(head,&i));
115
116   printf(" There is %d elements\n",i);
117   printf("\n Search my 200 000 elements 20 times. (a point is a test)\n");
118   if (!(key=malloc(10))) {
119     fprintf(stderr,"Out of memory\n");
120     abort();
121   } 
122   for (i=0;i<20;i++) {
123     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
124     for (j=0;j<NB_ELM;j++) {
125       
126       sprintf(key,"%d",j);
127       TRYFAIL(gras_dict_retrieve(head,key,&data));
128       if (strcmp(key,(char*)data)) {
129         printf("key=%s != data=%s\n",key,(char*)data);
130         abort();
131       }
132     }
133   }
134   free(key);
135
136   printf("\n Remove my 200 000 elements. (a point is 10 000 elements)\n");
137   if (!(key=malloc(10))) {
138     fprintf(stderr,"Out of memory\n");
139     abort();
140   }
141   for (j=0;j<NB_ELM;j++) {
142     if (!(j%10000)) printf("."); fflush(stdout);
143     
144     sprintf(key,"%d",j);
145     TRYFAIL(gras_dict_remove(head,key));
146   }
147   printf("\n");
148   free(key);
149
150   
151   printf("\n Free the structure (twice)\n");
152   gras_dict_free(&head);
153   gras_dict_free(&head);
154   return 0;
155 }