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. */
14 XBT_LOG_EXTERNAL_CATEGORY(dict);
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 static void fill(xbt_dict_t *head);
18 static void debuged_add(xbt_dict_t head,const char*key);
19 static xbt_error_t search(xbt_dict_t head,const char*key);
20 static xbt_error_t debuged_remove(xbt_dict_t head,const char*key);
21 static xbt_error_t traverse(xbt_dict_t head);
23 static void print_str(void *str);
24 static void print_str(void *str) {
25 printf("%s",(char*)str);
28 static void fill(xbt_dict_t *head) {
29 printf("\n Fill in the dictionnary\n");
31 *head = xbt_dict_new();
32 debuged_add(*head,"12");
33 debuged_add(*head,"12a");
34 debuged_add(*head,"12b");
35 debuged_add(*head,"123");
36 debuged_add(*head,"123456");
37 /* Child becomes child of what to add */
38 debuged_add(*head,"1234");
39 /* Need of common ancestor */
40 debuged_add(*head,"123457");
44 static void debuged_add(xbt_dict_t head,const char*key)
46 char *data=xbt_strdup(key);
48 printf(" - Add %s\n",key);
49 xbt_dict_set(head,key,data,&free);
50 if (XBT_LOG_ISENABLED(dict,xbt_log_priority_debug)) {
51 xbt_dict_dump(head,(void (*)(void*))&printf);
56 static xbt_error_t search(xbt_dict_t head,const char*key) {
61 errcode=xbt_dict_get(head,key,&data);
62 printf(" - Search %s. Found %s\n",key,data?(char*)data:"NULL");fflush(stdout);
65 if (strcmp((char*)data,key))
66 return mismatch_error;
70 static xbt_error_t debuged_remove(xbt_dict_t head,const char*key)
74 printf(" Remove '%s'\n",key);fflush(stdout);
75 errcode=xbt_dict_remove(head,key);
76 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
81 static xbt_error_t traverse(xbt_dict_t head) {
82 xbt_dict_cursor_t cursor=NULL;
86 xbt_dict_foreach(head,cursor,key,data) {
87 printf(" - Seen: %s->%s\n",key,data);
88 xbt_assert2(!data || !strcmp(key,data),
89 "Key(%s) != value(%s). Abording\n",key,data);
94 int main(int argc,char **argv) {
99 xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
101 printf("\nGeneric dictionnary: USAGE test:\n");
103 printf(" Traverse the empty dictionnary\n");
104 TRYFAIL(traverse(head));
107 printf(" Free the dictionnary (twice)\n");
108 xbt_dict_free(&head);
109 xbt_dict_free(&head);
113 /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
114 printf(" - Test that it works with NULL data\n");
115 printf(" - Store NULL under 'null'\n");fflush(stdout);
116 xbt_dict_set(head,"null",NULL,NULL);
117 TRYFAIL(search(head,"null"));
118 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
119 printf(" Check whether I see it while traversing\n");fflush(stdout);
121 xbt_dict_cursor_t cursor=NULL;
125 xbt_dict_foreach(head,cursor,key,data) {
126 printf(" - Seen: %s->%s\n",key,data);fflush(stdout);
127 if (!strcmp(key,"null"))
130 xbt_assert0(found,"the key 'null', associated to NULL is not found");
133 printf(" - Change some values\n");
134 printf(" - Change 123 to 'Changed 123'\n");
135 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
136 printf(" - Change 123 back to '123'\n");
137 xbt_dict_set(head,"123",strdup("123"),&free);
138 printf(" - Change 12a to 'Dummy 12a'\n");
139 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
140 printf(" - Change 12a to '12a'\n");
141 xbt_dict_set(head,"12a",strdup("12a"),&free);
143 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
144 printf(" - Traverse the resulting dictionnary\n");
145 TRYFAIL(traverse(head));
147 printf(" - Retrive values\n");
148 TRYFAIL(xbt_dict_get(head,"123",(void**)&data));
150 TRYFAIL(strcmp("123",data));
152 TRYEXPECT(xbt_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
153 TRYEXPECT(xbt_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
154 TRYEXPECT(xbt_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
156 TRYFAIL(search(head,"12a"));
157 TRYFAIL(search(head,"12b"));
158 TRYFAIL(search(head,"12"));
159 TRYFAIL(search(head,"123456"));
160 TRYFAIL(search(head,"1234"));
161 TRYFAIL(search(head,"123457"));
163 printf(" - Traverse the resulting dictionnary\n");
164 TRYFAIL(traverse(head));
166 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
168 printf(" Free the dictionnary twice\n");
169 xbt_dict_free(&head);
170 xbt_dict_free(&head);
172 printf(" - Traverse the resulting dictionnary\n");
173 TRYFAIL(traverse(head));
177 printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
178 TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
179 TRYFAIL(traverse(head));
181 xbt_dict_free(&head);
183 printf(" - Remove data from the NULL dict (error message expected)\n");
184 TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
186 printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
188 TRYFAIL(debuged_remove(head,"12a")); TRYFAIL(traverse(head));
189 TRYFAIL(debuged_remove(head,"12b")); TRYFAIL(traverse(head));
190 TRYFAIL(debuged_remove(head,"12")); TRYFAIL(traverse(head));
191 TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
192 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
193 TRYFAIL(debuged_remove(head,"1234")); TRYFAIL(traverse(head));
194 TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
195 TRYFAIL(debuged_remove(head,"123")); TRYFAIL(traverse(head));
196 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
198 printf(" - Free the dictionnary twice\n");
199 xbt_dict_free(&head);
200 xbt_dict_free(&head);