3 /* dict_crash - A crash test for dictionnaries */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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. */
16 #define SIZEOFKEY 1024
18 static void print_str(void *str);
19 static void print_str(void *str) {
20 printf("%s",(char*)str);
24 static gras_error_t traverse(gras_dict_t *head) {
26 gras_dict_cursor_t *cursor=NULL;
30 //gras_dict_dump(head,(void (*)(void*))&printf);
31 TRY(gras_dict_cursor_new(head,&cursor));
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);
42 gras_dict_cursor_free(cursor);
46 static gras_error_t countelems(gras_dict_t *head,int*count) {
47 gras_dict_cursor_t *cursor;
53 TRY(gras_dict_cursor_new(head,&cursor));
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));
65 gras_dict_t *head=NULL;
70 TRY(gras_log_control_set("root.thresh=info"));
71 srand((unsigned int)time(NULL));
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");
79 TRYFAIL(gras_dict_new(&head));
80 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
82 for (j=0;j<1000;j++) {
83 if (!(key=malloc(SIZEOFKEY))) {
84 fprintf(stderr,"Out of memory\n");
88 for (k=0;k<SIZEOFKEY-1;k++)
89 key[k]=rand() % ('z' - 'a') + 'a';
91 // printf("[%d %s]\n",j,key);
92 TRYFAIL(gras_dict_insert(head,strdup(key),key,&free));
95 // gras_dict_dump(head,(void (*)(void*))&printf);
96 TRYFAIL(countelems(head,&nb));
98 printf ("\nI found %d elements, and not 1000\n",nb);
101 TRYFAIL(traverse(head));
102 gras_dict_free(&head);
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");
117 TRYFAIL(gras_dict_insert(head,key,key,&free));
120 printf("\n Count the elements (retrieving the key and data for each): \n");
121 TRYFAIL(countelems(head,&i));
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");
130 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
131 for (j=0;j<NB_ELM;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);
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");
147 for (j=0;j<NB_ELM;j++) {
148 if (!(j%10000)) printf("."); fflush(stdout);
151 TRYFAIL(gras_dict_remove(head,key));
154 gras_dict_free(&head);
155 gras_dict_free(&head);