Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Port set datacontainer to unit testing
[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 void 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 }
36
37 static int countelems(xbt_dict_t head) {
38   xbt_dict_cursor_t cursor;
39   char *key;
40   void *data;
41   int res = 0;
42
43   xbt_dict_foreach(head,cursor,key,data) {
44     res++;
45   }
46   return res;
47 }
48
49 int main(int argc,char **argv) {
50   xbt_dict_t head=NULL;
51   int i,j,k, nb;
52   char *key;
53   void *data;
54
55   xbt_init(&argc,argv);
56   srand((unsigned int)time(NULL));
57
58   printf("Dictionnary: CRASH test:\n");
59   printf(" Fill the struct, count its elems and frees the structure (x20)\n");
60   printf(" using 1000 elements with %d chars long randomized keys.\n",SIZEOFKEY);
61   printf(" (a point is a test)\n");
62
63   for (i=0;i<20;i++) {
64     head=xbt_dict_new();
65     if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
66     nb=0;
67     for (j=0;j<1000;j++) {
68       if (!(key=malloc(SIZEOFKEY))) {
69         fprintf(stderr,"Out of memory\n");
70         return 1;
71       }
72
73       for (k=0;k<SIZEOFKEY-1;k++)
74         key[k]=rand() % ('z' - 'a') + 'a';
75       key[k]='\0';
76       /*      printf("[%d %s]\n",j,key); */
77       xbt_dict_set(head,key,key,&free);
78     }
79     /*    xbt_dict_dump(head,(void (*)(void*))&printf); */
80     nb = countelems(head);
81     if (nb != 1000) {
82        printf ("\nI found %d elements, and not 1000\n",nb);
83        abort();
84     }     
85     traverse(head);
86     xbt_dict_free(&head);
87     xbt_dict_free(&head);
88   }
89
90
91   head=xbt_dict_new();
92   printf("\n Fill 20 000 elements, with keys being the number of element\n");
93   printf("  (a point is 1 000 elements)\n");
94   for (j=0;j<NB_ELM;j++) {
95     if (!(j%1000)) {
96       printf("."); 
97       fflush(stdout);
98     }
99     if (!(key=malloc(10))) {
100       fprintf(stderr,"Out of memory\n");
101       abort();
102     }
103     
104     sprintf(key,"%d",j);
105     xbt_dict_set(head,key,key,&free);
106   }
107
108   printf("\n Count the elements (retrieving the key and data for each): \n");
109   i = countelems(head);
110
111   printf(" There is %d elements\n",i);
112   printf("\n Search my 20 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       data = xbt_dict_get(head,key);
123       if (strcmp(key,(char*)data)) {
124         printf("key=%s != data=%s\n",key,(char*)data);
125         abort();
126       }
127     }
128   }
129   free(key);
130
131   printf("\n Remove my 20 000 elements. (a point is 10 000 elements)\n");
132   if (!(key=malloc(10))) {
133     fprintf(stderr,"Out of memory\n");
134     abort();
135   }
136   for (j=0;j<NB_ELM;j++) {
137     if (!(j%10000)) printf("."); fflush(stdout);
138     
139     sprintf(key,"%d",j);
140     xbt_dict_remove(head,key);
141   }
142   printf("\n");
143   free(key);
144
145   
146   printf("\n Free the structure (twice)\n");
147   xbt_dict_free(&head);
148   xbt_dict_free(&head);
149   
150   xbt_exit();
151   return 0;
152 }