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 != not_found_error)
100 int main(int argc,char **argv) {
102 xbt_dict_t head=NULL;
105 xbt_init(&argc,argv);
107 printf("\nGeneric dictionnary: USAGE test:\n");
109 printf(" Traverse the empty dictionnary\n");
112 printf(" Traverse the full dictionnary\n");
115 xbt_dict_dump(head,(void (*)(void*))&printf);
118 printf(" Free the dictionnary (twice)\n");
119 xbt_dict_free(&head);
120 xbt_dict_free(&head);
124 /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
125 printf(" - Test that it works with NULL data\n");
126 printf(" - Store NULL under 'null'\n");fflush(stdout);
127 xbt_dict_set(head,"null",NULL,NULL);
129 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
130 printf(" Check whether I see it while traversing...\n");fflush(stdout);
132 xbt_dict_cursor_t cursor=NULL;
136 xbt_dict_foreach(head,cursor,key,data) {
137 printf(" - Seen: %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));fflush(stdout);
138 if (!strcmp(key,"null"))
141 xbt_assert0(found,"the key 'null', associated to NULL is not found");
143 printf(" OK, I did found the searched NULL\n");
145 printf(" - Change some values\n");
146 printf(" - Change 123 to 'Changed 123'\n");
147 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
148 printf(" - Change 123 back to '123'\n");
149 xbt_dict_set(head,"123",strdup("123"),&free);
150 printf(" - Change 12a to 'Dummy 12a'\n");
151 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
152 printf(" - Change 12a to '12a'\n");
153 xbt_dict_set(head,"12a",strdup("12a"),&free);
155 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
156 printf(" - Traverse the resulting dictionnary\n");
159 printf(" - Retrive values\n");
160 data = xbt_dict_get(head,"123");
164 search_not_found(head,"Can't be found");
165 search_not_found(head,"123 Can't be found");
166 search_not_found(head,"12345678 NOT");
171 search(head,"123456");
173 search(head,"123457");
175 printf(" - Traverse the resulting dictionnary\n");
178 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
180 printf(" Free the dictionnary twice\n");
181 xbt_dict_free(&head);
182 xbt_dict_free(&head);
184 printf(" - Traverse the resulting dictionnary\n");
189 printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
191 debuged_remove(head,"Does not exist");
193 if (e.category != not_found_error)
199 xbt_dict_free(&head);
201 printf(" - Remove data from the NULL dict (error message expected)\n");
203 debuged_remove(head,"12345");
205 if (e.category != arg_error)
210 printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
212 debuged_remove(head,"12a"); traverse(head);
213 debuged_remove(head,"12b"); traverse(head);
214 debuged_remove(head,"12"); traverse(head);
215 debuged_remove(head,"123456"); traverse(head);
217 debuged_remove(head,"12346");
219 if (e.category != not_found_error)
221 xbt_ex_free(e); traverse(head);
223 debuged_remove(head,"1234"); traverse(head);
224 debuged_remove(head,"123457"); traverse(head);
225 debuged_remove(head,"123"); traverse(head);
227 debuged_remove(head,"12346");
229 if (e.category != not_found_error)
234 printf(" - Free the dictionnary twice\n");
235 xbt_dict_free(&head);
236 xbt_dict_free(&head);