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 #define STRING(str) (str)?:"(null)"
25 static void print_str(void *str);
26 static void print_str(void *str) {
27 printf("%s",(char*)STRING(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",STRING(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 xbt_error_t search(xbt_dict_t head,const char*key) {
63 errcode=xbt_dict_get(head,key,&data);
64 printf(" - Search %s. Found %s\n",STRING(key),(char*) STRING(data));fflush(stdout);
67 if (strcmp((char*)data,key))
68 return mismatch_error;
72 static xbt_error_t debuged_remove(xbt_dict_t head,const char*key)
76 printf(" Remove '%s'\n",STRING(key));fflush(stdout);
77 errcode=xbt_dict_remove(head,key);
78 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
83 static xbt_error_t traverse(xbt_dict_t head) {
84 xbt_dict_cursor_t cursor=NULL;
88 xbt_dict_foreach(head,cursor,key,data) {
89 printf(" - Seen: %s->%s\n",STRING(key),STRING(data));
90 xbt_assert2(!data || !strcmp(key,data),
91 "Key(%s) != value(%s). Abording\n",key,data);
96 int main(int argc,char **argv) {
101 xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
103 printf("\nGeneric dictionnary: USAGE test:\n");
105 printf(" Traverse the empty dictionnary\n");
106 TRYFAIL(traverse(head));
109 printf(" Free the dictionnary (twice)\n");
110 xbt_dict_free(&head);
111 xbt_dict_free(&head);
115 /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
116 printf(" - Test that it works with NULL data\n");
117 printf(" - Store NULL under 'null'\n");fflush(stdout);
118 xbt_dict_set(head,"null",NULL,NULL);
119 TRYFAIL(search(head,"null"));
120 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
121 printf(" Check whether I see it while traversing\n");fflush(stdout);
123 xbt_dict_cursor_t cursor=NULL;
127 xbt_dict_foreach(head,cursor,key,data) {
128 printf(" - Seen: %s->%s\n",STRING(key),STRING(data));fflush(stdout);
129 if (!strcmp(key,"null"))
132 xbt_assert0(found,"the key 'null', associated to NULL is not found");
135 printf(" - Change some values\n");
136 printf(" - Change 123 to 'Changed 123'\n");
137 xbt_dict_set(head,"123",strdup("Changed 123"),&free);
138 printf(" - Change 123 back to '123'\n");
139 xbt_dict_set(head,"123",strdup("123"),&free);
140 printf(" - Change 12a to 'Dummy 12a'\n");
141 xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
142 printf(" - Change 12a to '12a'\n");
143 xbt_dict_set(head,"12a",strdup("12a"),&free);
145 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
146 printf(" - Traverse the resulting dictionnary\n");
147 TRYFAIL(traverse(head));
149 printf(" - Retrive values\n");
150 TRYFAIL(xbt_dict_get(head,"123",(void**)&data));
152 TRYFAIL(strcmp("123",data));
154 TRYEXPECT(xbt_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
155 TRYEXPECT(xbt_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
156 TRYEXPECT(xbt_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
158 TRYFAIL(search(head,"12a"));
159 TRYFAIL(search(head,"12b"));
160 TRYFAIL(search(head,"12"));
161 TRYFAIL(search(head,"123456"));
162 TRYFAIL(search(head,"1234"));
163 TRYFAIL(search(head,"123457"));
165 printf(" - Traverse the resulting dictionnary\n");
166 TRYFAIL(traverse(head));
168 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
170 printf(" Free the dictionnary twice\n");
171 xbt_dict_free(&head);
172 xbt_dict_free(&head);
174 printf(" - Traverse the resulting dictionnary\n");
175 TRYFAIL(traverse(head));
179 printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
180 TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
181 TRYFAIL(traverse(head));
183 xbt_dict_free(&head);
185 printf(" - Remove data from the NULL dict (error message expected)\n");
186 TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
188 printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
190 TRYFAIL(debuged_remove(head,"12a")); TRYFAIL(traverse(head));
191 TRYFAIL(debuged_remove(head,"12b")); TRYFAIL(traverse(head));
192 TRYFAIL(debuged_remove(head,"12")); TRYFAIL(traverse(head));
193 TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
194 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
195 TRYFAIL(debuged_remove(head,"1234")); TRYFAIL(traverse(head));
196 TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
197 TRYFAIL(debuged_remove(head,"123")); TRYFAIL(traverse(head));
198 TRYEXPECT(debuged_remove(head,"12346"),mismatch_error); TRYFAIL(traverse(head));
200 printf(" - Free the dictionnary twice\n");
201 xbt_dict_free(&head);
202 xbt_dict_free(&head);