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=debug");
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,strdup(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++) {
100 if (!(j%10000)) printf("."); fflush(stdout);
101 if (!(key=malloc(10))) {
102 fprintf(stderr,"Out of memory\n");
107 TRYFAIL(gras_dict_insert(head,key,key,&free));
110 printf("\n Count the elements (retrieving the key and data for each): \n");
111 TRYFAIL(countelems(head,&i));
113 printf(" There is %d elements\n",i);
114 printf("\n Search my 200 000 elements 20 times. (a point is a test)\n");
115 if (!(key=malloc(10))) {
116 fprintf(stderr,"Out of memory\n");
120 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
121 for (j=0;j<NB_ELM;j++) {
124 TRYFAIL(gras_dict_retrieve(head,key,&data));
125 if (strcmp(key,(char*)data)) {
126 printf("key=%s != data=%s\n",key,(char*)data);
132 printf("\n Remove my 200 000 elements. (a point is 10 000 elements)\n");
133 if (!(key=malloc(10))) {
134 fprintf(stderr,"Out of memory\n");
137 for (j=0;j<NB_ELM;j++) {
138 if (!(j%10000)) printf("."); fflush(stdout);
141 TRYFAIL(gras_dict_remove(head,key));
144 gras_dict_free(&head);
145 gras_dict_free(&head);