3 /* dict_usage - A test of normal usage of a dictionnary */
5 /* Copyright (c) 2003,2004 Martin Quinson. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
16 XBT_LOG_EXTERNAL_CATEGORY(dict);
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
19 static void fill(xbt_dict_t *head);
20 static void debuged_add(xbt_dict_t head,const char*key);
21 static void search(xbt_dict_t head,const char*key);
22 static void debuged_remove(xbt_dict_t head,const char*key);
23 static void traverse(xbt_dict_t head);
25 static void print_str(void *str);
26 static void print_str(void *str) {
27 printf("%s",(char*)PRINTF_STR(str));
30 static void fill(xbt_dict_t *head) {
31 printf("\n Fill in the dictionnary\n");
33 *head = xbt_dict_new();
34 debuged_add(*head,"12");
35 debuged_add(*head,"12a");
36 debuged_add(*head,"12b");
37 debuged_add(*head,"123");
38 debuged_add(*head,"123456");
39 /* Child becomes child of what to add */
40 debuged_add(*head,"1234");
41 /* Need of common ancestor */
42 debuged_add(*head,"123457");
46 static void debuged_add(xbt_dict_t head,const char*key)
48 char *data=xbt_strdup(key);
50 printf(" - Add %s\n",PRINTF_STR(key));
51 xbt_dict_set(head,key,data,&free);
52 if (XBT_LOG_ISENABLED(dict,xbt_log_priority_debug)) {
53 xbt_dict_dump(head,(void (*)(void*))&printf);
58 static void search(xbt_dict_t head,const char*key) {
61 data=xbt_dict_get(head,key);
62 printf(" - Search %s. Found %s\n",PRINTF_STR(key),(char*) PRINTF_STR(data));fflush(stdout);
63 if (data && strcmp((char*)data,key))
64 THROW2(mismatch_error,0,"Found %s while looking for %s",(char*)data,key);
67 static void debuged_remove(xbt_dict_t head,const char*key) {
69 printf(" Remove '%s'\n",PRINTF_STR(key));fflush(stdout);
70 xbt_dict_remove(head,key);
71 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
75 static void traverse(xbt_dict_t head) {
76 xbt_dict_cursor_t cursor=NULL;
80 xbt_dict_foreach(head,cursor,key,data) {
81 printf(" - Seen: %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));
82 xbt_assert2(!data || !strcmp(key,data),
83 "Key(%s) != value(%s). Abording\n",key,data);
87 static void search_not_found(xbt_dict_t head, const char *data) {
91 data = xbt_dict_get(head,"Can't be found");
92 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
94 if (e.category == mismatch_error) {
102 int main(int argc,char **argv) {
104 xbt_dict_t head=NULL;
107 xbt_init(&argc,argv);
109 printf("\nGeneric dictionnary: USAGE test:\n");
111 printf(" Traverse the empty dictionnary\n");
114 printf(" Traverse the full dictionnary\n");
117 xbt_dict_dump(head,(void (*)(void*))&printf);
120 printf(" Free the dictionnary (twice)\n");
121 xbt_dict_free(&head);
122 xbt_dict_free(&head);
126 /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
127 printf(" - Test that it works with NULL data\n");
128 printf(" - Store NULL under 'null'\n");fflush(stdout);
129 xbt_dict_set(head,"null",NULL,NULL);
131 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
132 printf(" Check whether I see it while traversing...\n");fflush(stdout);
134 xbt_dict_cursor_t cursor=NULL;
138 xbt_dict_foreach(head,cursor,key,data) {
139 printf(" - Seen: %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));fflush(stdout);
140 if (!strcmp(key,"null"))
143 xbt_assert0(found,"the key 'null', associated to NULL is not found");
145 printf(" OK, I did found the searched NULL\n");
147 printf(" - Change some values\n");
148 printf(" - Change 123 to 'Changed 123'\n");
149 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
150 printf(" - Change 123 back to '123'\n");
151 xbt_dict_set(head,"123",strdup("123"),&free);
152 printf(" - Change 12a to 'Dummy 12a'\n");
153 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
154 printf(" - Change 12a to '12a'\n");
155 xbt_dict_set(head,"12a",strdup("12a"),&free);
157 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
158 printf(" - Traverse the resulting dictionnary\n");
161 printf(" - Retrive values\n");
162 data = xbt_dict_get(head,"123");
166 search_not_found(head,"Can't be found");
167 search_not_found(head,"123 Can't be found");
168 search_not_found(head,"12345678 NOT");
173 search(head,"123456");
175 search(head,"123457");
177 printf(" - Traverse the resulting dictionnary\n");
180 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
182 printf(" Free the dictionnary twice\n");
183 xbt_dict_free(&head);
184 xbt_dict_free(&head);
186 printf(" - Traverse the resulting dictionnary\n");
191 printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
193 debuged_remove(head,"Does not exist");
195 if (e.category != mismatch_error)
201 xbt_dict_free(&head);
203 printf(" - Remove data from the NULL dict (error message expected)\n");
205 debuged_remove(head,"12345");
207 if (e.category != arg_error)
212 printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
214 debuged_remove(head,"12a"); traverse(head);
215 debuged_remove(head,"12b"); traverse(head);
216 debuged_remove(head,"12"); traverse(head);
217 debuged_remove(head,"123456"); traverse(head);
219 debuged_remove(head,"12346");
221 if (e.category != mismatch_error)
223 xbt_ex_free(e); traverse(head);
225 debuged_remove(head,"1234"); traverse(head);
226 debuged_remove(head,"123457"); traverse(head);
227 debuged_remove(head,"123"); traverse(head);
229 debuged_remove(head,"12346");
231 if (e.category != mismatch_error)
233 xbt_ex_free(e); traverse(head);
236 printf(" - Free the dictionnary twice\n");
237 xbt_dict_free(&head);
238 xbt_dict_free(&head);