Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Redirect tests stderr to &1 to be usable in a |less
[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=debug");
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,strdup(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)) printf("."); fflush(stdout);
101     if (!(key=malloc(10))) {
102       fprintf(stderr,"Out of memory\n");
103       abort();
104     }
105     
106     sprintf(key,"%d",j);
107     TRYFAIL(gras_dict_insert(head,key,key,&free));
108   }
109
110   printf("\n Count the elements (retrieving the key and data for each): \n");
111   TRYFAIL(countelems(head,&i));
112
113   printf(" There is %d elements\n",i);
114   printf("\n Search my 200 000 elements 20 times. (a point is a test)\n");
115   if (!(key=malloc(10))) {
116     fprintf(stderr,"Out of memory\n");
117     abort();
118   } 
119   for (i=0;i<20;i++) {
120     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
121     for (j=0;j<NB_ELM;j++) {
122       
123       sprintf(key,"%d",j);
124       TRYFAIL(gras_dict_retrieve(head,key,&data));
125       if (strcmp(key,(char*)data)) {
126         printf("key=%s != data=%s\n",key,(char*)data);
127         abort();
128       }
129     }
130   }
131
132   printf("\n Remove my 200 000 elements. (a point is 10 000 elements)\n");
133   if (!(key=malloc(10))) {
134     fprintf(stderr,"Out of memory\n");
135     return 3;
136   }
137   for (j=0;j<NB_ELM;j++) {
138     if (!(j%10000)) printf("."); fflush(stdout);
139     
140     sprintf(key,"%d",j);
141     TRYFAIL(gras_dict_remove(head,key));
142   }
143
144   gras_dict_free(&head);
145   gras_dict_free(&head);
146   printf("\n");
147   return 0;
148 }