Logo AND Algorithmique Numérique Distribuée

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