3 /* dict_crash - A crash test for dictionnaries */
5 /* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
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. */
16 #define SIZEOFKEY 1024
18 static void print_str(void *str);
19 static void print_str(void *str) {
20 printf("%s",(char*)str);
23 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
25 static void traverse(xbt_dict_t head) {
26 xbt_dict_cursor_t cursor=NULL;
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);
37 static int countelems(xbt_dict_t head) {
38 xbt_dict_cursor_t cursor;
43 xbt_dict_foreach(head,cursor,key,data) {
49 int main(int argc,char **argv) {
56 srand((unsigned int)time(NULL));
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");
65 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
67 for (j=0;j<1000;j++) {
68 if (!(key=malloc(SIZEOFKEY))) {
69 fprintf(stderr,"Out of memory\n");
73 for (k=0;k<SIZEOFKEY-1;k++)
74 key[k]=rand() % ('z' - 'a') + 'a';
76 /* printf("[%d %s]\n",j,key); */
77 xbt_dict_set(head,key,key,&free);
79 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
80 nb = countelems(head);
82 printf ("\nI found %d elements, and not 1000\n",nb);
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++) {
99 if (!(key=malloc(10))) {
100 fprintf(stderr,"Out of memory\n");
105 xbt_dict_set(head,key,key,&free);
108 printf("\n Count the elements (retrieving the key and data for each): \n");
109 i = countelems(head);
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");
118 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
119 for (j=0;j<NB_ELM;j++) {
122 data = xbt_dict_get(head,key);
123 if (strcmp(key,(char*)data)) {
124 printf("key=%s != data=%s\n",key,(char*)data);
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");
136 for (j=0;j<NB_ELM;j++) {
137 if (!(j%10000)) printf("."); fflush(stdout);
140 xbt_dict_remove(head,key);
146 printf("\n Free the structure (twice)\n");
147 xbt_dict_free(&head);
148 xbt_dict_free(&head);