3 /* set_usage - A test of normal usage of a set */
5 /* Copyright (c) 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. */
15 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
16 XBT_LOG_EXTERNAL_CATEGORY(set);
22 unsigned int name_len;
26 } s_my_elem_t,*my_elem_t;
28 static void fill(xbt_set_t *set);
29 static void debuged_add(xbt_set_t set,const char*key);
30 static void debuged_add_with_data(xbt_set_t set,
33 static xbt_error_t search_name(xbt_set_t set,const char*key);
34 static xbt_error_t search_id(xbt_set_t head,
36 const char*expected_key);
37 static xbt_error_t traverse(xbt_set_t set);
39 static void my_elem_free(void *e) {
40 my_elem_t elm=(my_elem_t)e;
49 static void debuged_add_with_data(xbt_set_t set,
55 elm = xbt_new(s_my_elem_t,1);
56 elm->name=xbt_strdup(name);
59 elm->data=xbt_strdup(data);
61 printf(" - Add %s ",name);
62 if (strcmp(name,data)) {
63 printf("(->%s)",data);
66 xbt_set_add(set, (xbt_set_elm_t)elm,
70 static void debuged_add(xbt_set_t set,
72 debuged_add_with_data(set, name, name);
75 static void fill(xbt_set_t *set) {
76 printf("\n Fill in the data set\n");
79 debuged_add(*set,"12");
80 debuged_add(*set,"12a");
81 debuged_add(*set,"12b");
82 debuged_add(*set,"123");
83 debuged_add(*set,"123456");
84 /* Child becomes child of what to add */
85 debuged_add(*set,"1234");
86 /* Need of common ancestor */
87 debuged_add(*set,"123457");
90 static xbt_error_t search_name(xbt_set_t head,const char*key) {
94 errcode=xbt_set_get_by_name(head,key,(xbt_set_elm_t*)&elm);
95 printf(" - Search by name %s. Found %s (under ID %d)\n",
97 elm? elm->data:"(null)",
99 if (strcmp(key,elm->name)) {
100 printf(" The key (%s) is not the one expected (%s)\n",
102 return mismatch_error;
104 if (strcmp(elm->name,elm->data)) {
105 printf(" The name (%s) != data (%s)\n",
106 elm->name,elm->data);
107 return mismatch_error;
113 static xbt_error_t search_id(xbt_set_t head,int id,const char*key) {
117 errcode=xbt_set_get_by_id(head,id,(xbt_set_elm_t*)&elm);
118 printf(" - Search by id %d. Found %s (data %s)\n",
120 elm? elm->name:"(null)",
121 elm? elm->data:"(null)");
123 printf(" The found ID (%d) is not the one expected (%d)\n",
125 return mismatch_error;
127 if (strcmp(key,elm->name)) {
128 printf(" The key (%s) is not the one expected (%s)\n",
130 return mismatch_error;
132 if (strcmp(elm->name,elm->data)) {
133 printf(" The name (%s) != data (%s)\n",
134 elm->name,elm->data);
135 return mismatch_error;
142 static xbt_error_t traverse(xbt_set_t set) {
143 xbt_set_cursor_t cursor=NULL;
146 xbt_set_foreach(set,cursor,elm) {
147 xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
148 printf(" - Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
149 xbt_assert2(!strcmp(elm->name,elm->data),
150 "Key(%s) != value(%s). Abording",
151 elm->name,elm->data);
156 int main(int argc,char **argv) {
161 xbt_init_defaultlog(&argc,argv,"set.thresh=verbose");
163 printf("\nData set: USAGE test:\n");
165 printf(" Traverse the empty set\n");
166 TRYFAIL(traverse(set));
169 printf(" Free the data set\n");
171 printf(" Free the data set again\n");
176 printf(" - Change some values\n");
177 printf(" - Change 123 to 'Changed 123'\n");
178 debuged_add_with_data(set,"123","Changed 123");
179 printf(" - Change 123 back to '123'\n");
180 debuged_add_with_data(set,"123","123");
181 printf(" - Change 12a to 'Dummy 12a'\n");
182 debuged_add_with_data(set,"12a","Dummy 12a");
183 printf(" - Change 12a to '12a'\n");
184 debuged_add_with_data(set,"12a","12a");
186 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
187 printf(" - Traverse the resulting data set\n");
188 TRYFAIL(traverse(set));
190 printf(" - Retrive values\n");
191 xbt_set_get_by_name(set,"123",(xbt_set_elm_t*)&elm);
193 TRYFAIL(strcmp("123",elm->data));
195 TRYEXPECT(xbt_set_get_by_name(set,"Can't be found",(xbt_set_elm_t*)&elm),
197 TRYEXPECT(xbt_set_get_by_name(set,"123 Can't be found",(xbt_set_elm_t*)&elm),
199 TRYEXPECT(xbt_set_get_by_name(set,"12345678 NOT",(xbt_set_elm_t*)&elm),
202 TRYFAIL(search_name(set,"12"));
203 TRYFAIL(search_name(set,"12a"));
204 TRYFAIL(search_name(set,"12b"));
205 TRYFAIL(search_name(set,"123"));
206 TRYFAIL(search_name(set,"123456"));
207 TRYFAIL(search_name(set,"1234"));
208 TRYFAIL(search_name(set,"123457"));
210 TRYFAIL(search_id(set,0,"12"));
211 TRYFAIL(search_id(set,1,"12a"));
212 TRYFAIL(search_id(set,2,"12b"));
213 TRYFAIL(search_id(set,3,"123"));
214 TRYFAIL(search_id(set,4,"123456"));
215 TRYFAIL(search_id(set,5,"1234"));
216 TRYFAIL(search_id(set,6,"123457"));
218 printf(" - Traverse the resulting data set\n");
219 TRYFAIL(traverse(set));
221 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
223 printf(" Free the data set (twice)\n");
227 printf(" - Traverse the resulting data set\n");
228 TRYFAIL(traverse(set));