3 /* dict_usage - A test of normal usage of a dictionnary */
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 GRAS_LOG_EXTERNAL_CATEGORY(dict);
18 static gras_error_t fill(gras_dict_t **head);
19 static gras_error_t debuged_add(gras_dict_t *head,const char*key);
20 static gras_error_t search(gras_dict_t *head,const char*key);
21 static gras_error_t debuged_remove(gras_dict_t *head,const char*key);
22 static gras_error_t traverse(gras_dict_t *head);
24 static void print_str(void *str);
25 static void print_str(void *str) {
26 printf("%s",(char*)str);
29 static gras_error_t fill(gras_dict_t **head) {
31 printf("\n Fill in the dictionnary\n");
33 TRY(gras_dict_new(head));
34 TRY(debuged_add(*head,"12"));
35 TRY(debuged_add(*head,"12a"));
36 TRY(debuged_add(*head,"12b"));
37 TRY(debuged_add(*head,"123"));
38 TRY(debuged_add(*head,"123456"));
39 // Child becomes child of what to add
40 TRY(debuged_add(*head,"1234"));
41 // Need of common ancestor
42 TRY(debuged_add(*head,"123457"));
47 static gras_error_t debuged_add(gras_dict_t *head,const char*key)
50 char *data=strdup(key);
52 printf(" - Add %s\n",key);
53 errcode=gras_dict_insert(head,key,data,&free);
54 if (GRAS_LOG_ISENABLED(dict,gras_log_priority_debug)) {
55 gras_dict_dump(head,(void (*)(void*))&printf);
61 static gras_error_t search(gras_dict_t *head,const char*key) {
66 errcode=gras_dict_retrieve(head,key,&data);
67 printf(" - Search %s. Found %s\n",key,data?(char*)data:"(null)");fflush(stdout);
68 if (strcmp((char*)data,key))
69 return mismatch_error;
73 static gras_error_t debuged_remove(gras_dict_t *head,const char*key)
77 printf(" Remove '%s'\n",key);fflush(stdout);
78 errcode=gras_dict_remove(head,key);
79 // gras_dict_dump(head,(void (*)(void*))&printf);
84 static gras_error_t traverse(gras_dict_t *head) {
86 gras_dict_cursor_t *cursor=NULL;
90 // gras_dict_dump(head,&print_str);
91 TRY(gras_dict_cursor_new(head,&cursor));
93 while (gras_dict_cursor_next(cursor) == no_error) {
94 TRY(gras_dict_cursor_get_key(cursor,&key));
95 TRY(gras_dict_cursor_get_data(cursor,(void**)&data));
96 printf(" - Seen: %s->%s\n",key,data);
97 if (strcmp(key,data)) {
98 printf("Key(%s) != value(%s). Abording\n",key,data);
102 gras_dict_cursor_free(cursor);
107 gras_error_t errcode;
108 gras_dict_t *head=NULL;
111 // TRY(gras_log_control_set("root.thresh=info dict_collapse.thresh=debug"));
112 //TRY(gras_log_control_set("root.thresh=info"));
113 // TRY(gras_log_control_set("root.thresh=info dict_search.thresh=info dict.thresh=debug dict_collapse.thresh=debug log.thresh=debug"));
115 printf("\nGeneric dictionnary: USAGE test:\n");
117 printf(" Traverse the empty dictionnary\n");
118 TRYFAIL(traverse(head));
120 TRYFAIL(fill(&head));
121 printf(" Free the dictionnary\n");
122 gras_dict_free(&head);
124 TRYFAIL(fill(&head));
126 printf(" - Change some values\n");
127 printf(" - Change 123 to 'Changed 123'\n");
128 TRYFAIL(gras_dict_insert(head,"123",strdup("Changed 123"),&free));
129 printf(" - Change 123 back to '123'\n");
130 TRYFAIL(gras_dict_insert(head,"123",strdup("123"),&free));
131 printf(" - Change 12a to 'Dummy 12a'\n");
132 TRYFAIL(gras_dict_insert(head,"12a",strdup("Dummy 12a"),&free));
133 printf(" - Change 12a to '12a'\n");
134 TRYFAIL(gras_dict_insert(head,"12a",strdup("12a"),&free));
136 // gras_dict_dump(head,(void (*)(void*))&printf);
137 printf(" - Traverse the resulting dictionnary\n");
138 TRYFAIL(traverse(head));
140 printf(" - Retrive values\n");
141 TRYFAIL(gras_dict_retrieve(head,"123",(void**)&data));
143 TRYFAIL(strcmp("123",data));
145 TRYEXPECT(gras_dict_retrieve(head,"Can't be found",(void**)&data),mismatch_error);
146 TRYEXPECT(gras_dict_retrieve(head,"123 Can't be found",(void**)&data),mismatch_error);
147 TRYEXPECT(gras_dict_retrieve(head,"12345678 NOT",(void**)&data),mismatch_error);
149 TRYFAIL(search(head,"12a"));
150 TRYFAIL(search(head,"12b"));
151 TRYFAIL(search(head,"12"));
152 TRYFAIL(search(head,"123456"));
153 TRYFAIL(search(head,"1234"));
154 TRYFAIL(search(head,"123457"));
156 printf(" - Traverse the resulting dictionnary\n");
157 TRYFAIL(traverse(head));
159 // gras_dict_dump(head,(void (*)(void*))&printf);
161 printf(" Free the dictionnary (twice)\n");
162 gras_dict_free(&head);
163 gras_dict_free(&head); // frees it twice to see if it triggers an error
165 printf(" - Traverse the resulting dictionnary\n");
166 TRYFAIL(traverse(head));
169 TRYFAIL(fill(&head));
170 printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
171 TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
172 TRYFAIL(traverse(head));
174 TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
175 TRYFAIL(traverse(head));
177 TRYFAIL(debuged_remove(head,"12a")); TRYFAIL(traverse(head));
178 TRYFAIL(debuged_remove(head,"12b")); TRYFAIL(traverse(head));
179 TRYFAIL(debuged_remove(head,"12")); TRYFAIL(traverse(head));
180 TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
181 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
182 TRYFAIL(debuged_remove(head,"1234")); TRYFAIL(traverse(head));
183 TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
184 TRYFAIL(debuged_remove(head,"123")); TRYFAIL(traverse(head));
185 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
187 gras_dict_free(&head);
188 gras_dict_free(&head);