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) {
25 gras_dict_cursor_t *cursor=NULL;
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);
39 static gras_error_t countelems(gras_dict_t *head,int*count) {
40 gras_dict_cursor_t *cursor;
45 gras_dict_foreach(head,cursor,key,data) {
51 void parse_log_opt(int argc, char **argv, const char *deft);
53 int main(int argc,char **argv) {
55 gras_dict_t *head=NULL;
60 parse_log_opt(argc,argv,"dict.thresh=verbose");
61 srand((unsigned int)time(NULL));
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");
69 TRYFAIL(gras_dict_new(&head));
70 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
72 for (j=0;j<1000;j++) {
73 if (!(key=malloc(SIZEOFKEY))) {
74 fprintf(stderr,"Out of memory\n");
78 for (k=0;k<SIZEOFKEY-1;k++)
79 key[k]=rand() % ('z' - 'a') + 'a';
81 // printf("[%d %s]\n",j,key);
82 TRYFAIL(gras_dict_insert(head,key,key,&free));
85 // gras_dict_dump(head,(void (*)(void*))&printf);
86 TRYFAIL(countelems(head,&nb));
88 printf ("\nI found %d elements, and not 1000\n",nb);
91 TRYFAIL(traverse(head));
92 gras_dict_free(&head);
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++) {
104 if (!(key=malloc(10))) {
105 fprintf(stderr,"Out of memory\n");
110 TRYFAIL(gras_dict_insert(head,key,key,&free));
113 printf("\n Count the elements (retrieving the key and data for each): \n");
114 TRYFAIL(countelems(head,&i));
116 printf(" There is %d elements\n",i);
117 printf("\n Search my 200 000 elements 20 times. (a point is a test)\n");
118 if (!(key=malloc(10))) {
119 fprintf(stderr,"Out of memory\n");
123 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
124 for (j=0;j<NB_ELM;j++) {
127 TRYFAIL(gras_dict_retrieve(head,key,&data));
128 if (strcmp(key,(char*)data)) {
129 printf("key=%s != data=%s\n",key,(char*)data);
135 printf("\n Remove my 200 000 elements. (a point is 10 000 elements)\n");
136 if (!(key=malloc(10))) {
137 fprintf(stderr,"Out of memory\n");
140 for (j=0;j<NB_ELM;j++) {
141 if (!(j%10000)) printf("."); fflush(stdout);
144 TRYFAIL(gras_dict_remove(head,key));
149 printf("\n Free the structure (twice)\n");
150 gras_dict_free(&head);
151 gras_dict_free(&head);