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. */
17 #define SIZEOFKEY 1024
19 static void print_str(void *str);
20 static void print_str(void *str) {
21 printf("%s",(char*)str);
24 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
26 static xbt_error_t traverse(xbt_dict_t head) {
27 xbt_dict_cursor_t cursor=NULL;
31 xbt_dict_foreach(head,cursor,key,data) {
32 /* printf(" Seen: %s=%s\n",key,data); */
33 xbt_assert2 (!strcmp(key,data),
34 "Key(%s) != value(%s). Abording\n",key,data);
39 static xbt_error_t countelems(xbt_dict_t head,int*count) {
40 xbt_dict_cursor_t cursor;
45 xbt_dict_foreach(head,cursor,key,data) {
51 int main(int argc,char **argv) {
58 xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
59 srand((unsigned int)time(NULL));
61 printf("Dictionnary: CRASH test:\n");
62 printf(" Fill the struct, count its elems and frees the structure (x20)\n");
63 printf(" using 1000 elements with %d chars long randomized keys.\n",SIZEOFKEY);
64 printf(" (a point is a test)\n");
68 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
70 for (j=0;j<1000;j++) {
71 if (!(key=malloc(SIZEOFKEY))) {
72 fprintf(stderr,"Out of memory\n");
76 for (k=0;k<SIZEOFKEY-1;k++)
77 key[k]=rand() % ('z' - 'a') + 'a';
79 /* printf("[%d %s]\n",j,key); */
80 xbt_dict_set(head,key,key,&free);
83 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
84 TRYFAIL(countelems(head,&nb));
86 printf ("\nI found %d elements, and not 1000\n",nb);
89 TRYFAIL(traverse(head));
96 printf("\n Fill 20 000 elements, with keys being the number of element\n");
97 printf(" (a point is 1 000 elements)\n");
98 for (j=0;j<NB_ELM;j++) {
103 if (!(key=malloc(10))) {
104 fprintf(stderr,"Out of memory\n");
109 xbt_dict_set(head,key,key,&free);
112 printf("\n Count the elements (retrieving the key and data for each): \n");
113 TRYFAIL(countelems(head,&i));
115 printf(" There is %d elements\n",i);
116 printf("\n Search my 20 000 elements 20 times. (a point is a test)\n");
117 if (!(key=malloc(10))) {
118 fprintf(stderr,"Out of memory\n");
122 if (i%10) printf("."); else printf("%d",i/10); fflush(stdout);
123 for (j=0;j<NB_ELM;j++) {
126 TRYFAIL(xbt_dict_get(head,key,&data));
127 if (strcmp(key,(char*)data)) {
128 printf("key=%s != data=%s\n",key,(char*)data);
135 printf("\n Remove my 20 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(xbt_dict_remove(head,key));
150 printf("\n Free the structure (twice)\n");
151 xbt_dict_free(&head);
152 xbt_dict_free(&head);