3 /* set_usage - A test of normal usage of a set */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2004 the OURAGAN project. */
8 /* This program is free software; you can redistribute it and/or modify it
9 under the terms of the license (GNU LGPL) which comes with this package. */
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 XBT_LOG_EXTERNAL_CATEGORY(set);
23 unsigned int name_len;
27 } s_my_elem_t,*my_elem_t;
29 static void fill(xbt_set_t *set);
30 static void debuged_add(xbt_set_t set,const char*key);
31 static void debuged_add_with_data(xbt_set_t set,
34 static xbt_error_t search_name(xbt_set_t set,const char*key);
35 static xbt_error_t search_id(xbt_set_t head,
37 const char*expected_key);
38 static xbt_error_t traverse(xbt_set_t set);
40 static void my_elem_free(void *e) {
41 my_elem_t elm=(my_elem_t)e;
50 static void debuged_add_with_data(xbt_set_t set,
56 elm = xbt_new(s_my_elem_t,1);
57 elm->name=xbt_strdup(name);
60 elm->data=xbt_strdup(data);
62 printf(" - Add %s ",name);
63 if (strcmp(name,data)) {
64 printf("(->%s)",data);
67 xbt_set_add(set, (xbt_set_elm_t)elm,
71 static void debuged_add(xbt_set_t set,
73 debuged_add_with_data(set, name, name);
76 static void fill(xbt_set_t *set) {
77 printf("\n Fill in the data set\n");
80 debuged_add(*set,"12");
81 debuged_add(*set,"12a");
82 debuged_add(*set,"12b");
83 debuged_add(*set,"123");
84 debuged_add(*set,"123456");
85 /* Child becomes child of what to add */
86 debuged_add(*set,"1234");
87 /* Need of common ancestor */
88 debuged_add(*set,"123457");
91 static xbt_error_t search_name(xbt_set_t head,const char*key) {
95 errcode=xbt_set_get_by_name(head,key,(xbt_set_elm_t*)&elm);
96 printf(" - Search by name %s. Found %s (under ID %d)\n",
98 elm? elm->data:"(null)",
100 if (strcmp(key,elm->name)) {
101 printf(" The key (%s) is not the one expected (%s)\n",
103 return mismatch_error;
105 if (strcmp(elm->name,elm->data)) {
106 printf(" The name (%s) != data (%s)\n",
107 elm->name,elm->data);
108 return mismatch_error;
114 static xbt_error_t search_id(xbt_set_t head,int id,const char*key) {
118 errcode=xbt_set_get_by_id(head,id,(xbt_set_elm_t*)&elm);
119 printf(" - Search by id %d. Found %s (data %s)\n",
121 elm? elm->name:"(null)",
122 elm? elm->data:"(null)");
124 printf(" The found ID (%d) is not the one expected (%d)\n",
126 return mismatch_error;
128 if (strcmp(key,elm->name)) {
129 printf(" The key (%s) is not the one expected (%s)\n",
131 return mismatch_error;
133 if (strcmp(elm->name,elm->data)) {
134 printf(" The name (%s) != data (%s)\n",
135 elm->name,elm->data);
136 return mismatch_error;
143 static xbt_error_t traverse(xbt_set_t set) {
144 xbt_set_cursor_t cursor=NULL;
147 xbt_set_foreach(set,cursor,elm) {
148 xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
149 printf(" - Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
150 xbt_assert2(!strcmp(elm->name,elm->data),
151 "Key(%s) != value(%s). Abording",
152 elm->name,elm->data);
157 int main(int argc,char **argv) {
162 xbt_init_defaultlog(&argc,argv,"set.thresh=verbose");
164 printf("\nData set: USAGE test:\n");
166 printf(" Traverse the empty set\n");
167 TRYFAIL(traverse(set));
170 printf(" Free the data set\n");
172 printf(" Free the data set again\n");
177 printf(" - Change some values\n");
178 printf(" - Change 123 to 'Changed 123'\n");
179 debuged_add_with_data(set,"123","Changed 123");
180 printf(" - Change 123 back to '123'\n");
181 debuged_add_with_data(set,"123","123");
182 printf(" - Change 12a to 'Dummy 12a'\n");
183 debuged_add_with_data(set,"12a","Dummy 12a");
184 printf(" - Change 12a to '12a'\n");
185 debuged_add_with_data(set,"12a","12a");
187 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
188 printf(" - Traverse the resulting data set\n");
189 TRYFAIL(traverse(set));
191 printf(" - Retrive values\n");
192 xbt_set_get_by_name(set,"123",(xbt_set_elm_t*)&elm);
194 TRYFAIL(strcmp("123",elm->data));
196 TRYEXPECT(xbt_set_get_by_name(set,"Can't be found",(xbt_set_elm_t*)&elm),
198 TRYEXPECT(xbt_set_get_by_name(set,"123 Can't be found",(xbt_set_elm_t*)&elm),
200 TRYEXPECT(xbt_set_get_by_name(set,"12345678 NOT",(xbt_set_elm_t*)&elm),
203 TRYFAIL(search_name(set,"12"));
204 TRYFAIL(search_name(set,"12a"));
205 TRYFAIL(search_name(set,"12b"));
206 TRYFAIL(search_name(set,"123"));
207 TRYFAIL(search_name(set,"123456"));
208 TRYFAIL(search_name(set,"1234"));
209 TRYFAIL(search_name(set,"123457"));
211 TRYFAIL(search_id(set,0,"12"));
212 TRYFAIL(search_id(set,1,"12a"));
213 TRYFAIL(search_id(set,2,"12b"));
214 TRYFAIL(search_id(set,3,"123"));
215 TRYFAIL(search_id(set,4,"123456"));
216 TRYFAIL(search_id(set,5,"1234"));
217 TRYFAIL(search_id(set,6,"123457"));
219 printf(" - Traverse the resulting data set\n");
220 TRYFAIL(traverse(set));
222 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
224 printf(" Free the data set (twice)\n");
228 printf(" - Traverse the resulting data set\n");
229 TRYFAIL(traverse(set));