Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Compile pas avec les warnings, sinon
[simgrid.git] / testsuite / xbt / dict_crash.c
1 /* $Id$ */
2
3 /* dict_crash - A crash test for dictionnaries                              */
4
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved.            */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "gras.h"
11 #include <time.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14
15 #define NB_ELM 20000
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 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
24
25 static xbt_error_t traverse(xbt_dict_t head) {
26   xbt_dict_cursor_t cursor=NULL;
27   char *key;
28   char *data;
29
30   xbt_dict_foreach(head,cursor,key,data) {
31     /*    printf("   Seen:  %s=%s\n",key,data); */
32     xbt_assert2 (!strcmp(key,data),
33       "Key(%s) != value(%s). Abording\n",key,data);
34   }
35   return no_error;
36 }
37
38 static xbt_error_t countelems(xbt_dict_t head,int*count) {
39   xbt_dict_cursor_t cursor;
40   char *key;
41   void *data;
42   *count=0;
43
44   xbt_dict_foreach(head,cursor,key,data) {
45     (*count)++;
46   }
47   return no_error;
48 }
49
50 int main(int argc,char **argv) {
51   xbt_error_t errcode;
52   xbt_dict_t head=NULL;
53   int i,j,k, nb;
54   char *key;
55   void *data;
56
57   xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
58   srand((unsigned int)time(NULL));
59
60   printf("Dictionnary: CRASH test:\n");
61   printf(" Fill the struct, count its elems and frees the structure (x20)\n");
62   printf(" using 1000 elements with %d chars long randomized keys.\n",SIZEOFKEY);
63   printf(" (a point is a test)\n");
64
65   for (i=0;i<20;i++) {
66     head=xbt_dict_new();
67     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
68     nb=0;
69     for (j=0;j<1000;j++) {
70       if (!(key=malloc(SIZEOFKEY))) {
71         fprintf(stderr,"Out of memory\n");
72         return 1;
73       }
74
75       for (k=0;k<SIZEOFKEY-1;k++)
76         key[k]=rand() % ('z' - 'a') + 'a';
77       key[k]='\0';
78       /*      printf("[%d %s]\n",j,key); */
79       xbt_dict_set(head,key,key,&free);
80     }
81     nb=0;
82     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
83     TRYFAIL(countelems(head,&nb));
84     if (nb != 1000) {
85        printf ("\nI found %d elements, and not 1000\n",nb);
86        abort();
87     }     
88     TRYFAIL(traverse(head));
89     xbt_dict_free(&head);
90     xbt_dict_free(&head);
91   }
92
93
94   head=xbt_dict_new();
95   printf("\n Fill 20 000 elements, with keys being the number of element\n");
96   printf("  (a point is 1 000 elements)\n");
97   for (j=0;j<NB_ELM;j++) {
98     if (!(j%1000)) {
99       printf("."); 
100       fflush(stdout);
101     }
102     if (!(key=malloc(10))) {
103       fprintf(stderr,"Out of memory\n");
104       abort();
105     }
106     
107     sprintf(key,"%d",j);
108     xbt_dict_set(head,key,key,&free);
109   }
110
111   printf("\n Count the elements (retrieving the key and data for each): \n");
112   TRYFAIL(countelems(head,&i));
113
114   printf(" There is %d elements\n",i);
115   printf("\n Search my 20 000 elements 20 times. (a point is a test)\n");
116   if (!(key=malloc(10))) {
117     fprintf(stderr,"Out of memory\n");
118     abort();
119   } 
120   for (i=0;i<20;i++) {
121     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
122     for (j=0;j<NB_ELM;j++) {
123       
124       sprintf(key,"%d",j);
125       TRYFAIL(xbt_dict_get(head,key,&data));
126       if (strcmp(key,(char*)data)) {
127         printf("key=%s != data=%s\n",key,(char*)data);
128         abort();
129       }
130     }
131   }
132   free(key);
133
134   printf("\n Remove my 20 000 elements. (a point is 10 000 elements)\n");
135   if (!(key=malloc(10))) {
136     fprintf(stderr,"Out of memory\n");
137     abort();
138   }
139   for (j=0;j<NB_ELM;j++) {
140     if (!(j%10000)) printf("."); fflush(stdout);
141     
142     sprintf(key,"%d",j);
143     TRYFAIL(xbt_dict_remove(head,key));
144   }
145   printf("\n");
146   free(key);
147
148   
149   printf("\n Free the structure (twice)\n");
150   xbt_dict_free(&head);
151   xbt_dict_free(&head);
152   
153   xbt_exit();
154   return 0;
155 }