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. */
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
15 XBT_LOG_EXTERNAL_CATEGORY(set);
21 unsigned int name_len;
25 } s_my_elem_t,*my_elem_t;
27 static void fill(xbt_set_t *set);
28 static void debuged_add(xbt_set_t set,const char*key);
29 static void debuged_add_with_data(xbt_set_t set,
32 static xbt_error_t search_name(xbt_set_t set,const char*key);
33 static xbt_error_t search_id(xbt_set_t head,
35 const char*expected_key);
36 static xbt_error_t traverse(xbt_set_t set);
38 static void my_elem_free(void *e) {
39 my_elem_t elm=(my_elem_t)e;
48 static void debuged_add_with_data(xbt_set_t set,
54 elm = xbt_new(s_my_elem_t,1);
55 elm->name=xbt_strdup(name);
58 elm->data=xbt_strdup(data);
60 printf(" - Add %s ",name);
61 if (strcmp(name,data)) {
62 printf("(->%s)",data);
65 xbt_set_add(set, (xbt_set_elm_t)elm,
69 static void debuged_add(xbt_set_t set,
71 debuged_add_with_data(set, name, name);
74 static void fill(xbt_set_t *set) {
75 printf("\n Fill in the data set\n");
78 debuged_add(*set,"12");
79 debuged_add(*set,"12a");
80 debuged_add(*set,"12b");
81 debuged_add(*set,"123");
82 debuged_add(*set,"123456");
83 /* Child becomes child of what to add */
84 debuged_add(*set,"1234");
85 /* Need of common ancestor */
86 debuged_add(*set,"123457");
89 static xbt_error_t search_name(xbt_set_t head,const char*key) {
93 errcode=xbt_set_get_by_name(head,key,(xbt_set_elm_t*)&elm);
94 printf(" - Search by name %s. Found %s (under ID %d)\n",
96 elm? elm->data:"(null)",
98 if (strcmp(key,elm->name)) {
99 printf(" The key (%s) is not the one expected (%s)\n",
101 return mismatch_error;
103 if (strcmp(elm->name,elm->data)) {
104 printf(" The name (%s) != data (%s)\n",
105 elm->name,elm->data);
106 return mismatch_error;
112 static xbt_error_t search_id(xbt_set_t head,int id,const char*key) {
116 errcode=xbt_set_get_by_id(head,id,(xbt_set_elm_t*)&elm);
117 printf(" - Search by id %d. Found %s (data %s)\n",
119 elm? elm->name:"(null)",
120 elm? elm->data:"(null)");
122 printf(" The found ID (%d) is not the one expected (%d)\n",
124 return mismatch_error;
126 if (strcmp(key,elm->name)) {
127 printf(" The key (%s) is not the one expected (%s)\n",
129 return mismatch_error;
131 if (strcmp(elm->name,elm->data)) {
132 printf(" The name (%s) != data (%s)\n",
133 elm->name,elm->data);
134 return mismatch_error;
141 static xbt_error_t traverse(xbt_set_t set) {
142 xbt_set_cursor_t cursor=NULL;
145 xbt_set_foreach(set,cursor,elm) {
146 xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
147 printf(" - Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
148 xbt_assert2(!strcmp(elm->name,elm->data),
149 "Key(%s) != value(%s). Abording",
150 elm->name,elm->data);
155 int main(int argc,char **argv) {
160 xbt_init_defaultlog(&argc,argv,"set.thresh=verbose");
162 printf("\nData set: USAGE test:\n");
164 printf(" Traverse the empty set\n");
165 TRYFAIL(traverse(set));
168 printf(" Free the data set\n");
170 printf(" Free the data set again\n");
175 printf(" - Change some values\n");
176 printf(" - Change 123 to 'Changed 123'\n");
177 debuged_add_with_data(set,"123","Changed 123");
178 printf(" - Change 123 back to '123'\n");
179 debuged_add_with_data(set,"123","123");
180 printf(" - Change 12a to 'Dummy 12a'\n");
181 debuged_add_with_data(set,"12a","Dummy 12a");
182 printf(" - Change 12a to '12a'\n");
183 debuged_add_with_data(set,"12a","12a");
185 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
186 printf(" - Traverse the resulting data set\n");
187 TRYFAIL(traverse(set));
189 printf(" - Retrive values\n");
190 xbt_set_get_by_name(set,"123",(xbt_set_elm_t*)&elm);
192 TRYFAIL(strcmp("123",elm->data));
194 TRYEXPECT(xbt_set_get_by_name(set,"Can't be found",(xbt_set_elm_t*)&elm),
196 TRYEXPECT(xbt_set_get_by_name(set,"123 Can't be found",(xbt_set_elm_t*)&elm),
198 TRYEXPECT(xbt_set_get_by_name(set,"12345678 NOT",(xbt_set_elm_t*)&elm),
201 TRYFAIL(search_name(set,"12"));
202 TRYFAIL(search_name(set,"12a"));
203 TRYFAIL(search_name(set,"12b"));
204 TRYFAIL(search_name(set,"123"));
205 TRYFAIL(search_name(set,"123456"));
206 TRYFAIL(search_name(set,"1234"));
207 TRYFAIL(search_name(set,"123457"));
209 TRYFAIL(search_id(set,0,"12"));
210 TRYFAIL(search_id(set,1,"12a"));
211 TRYFAIL(search_id(set,2,"12b"));
212 TRYFAIL(search_id(set,3,"123"));
213 TRYFAIL(search_id(set,4,"123456"));
214 TRYFAIL(search_id(set,5,"1234"));
215 TRYFAIL(search_id(set,6,"123457"));
217 printf(" - Traverse the resulting data set\n");
218 TRYFAIL(traverse(set));
220 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
222 printf(" Free the data set (twice)\n");
226 printf(" - Traverse the resulting data set\n");
227 TRYFAIL(traverse(set));