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 void search_name(xbt_set_t set,const char*key);
34 static void search_id(xbt_set_t head,int id,
35 const char*expected_key);
36 static void 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 void search_name(xbt_set_t head,const char*key) {
90 my_elem_t elm = (my_elem_t)xbt_set_get_by_name(head,key);
92 printf(" - Search by name %s. Found %s (under ID %d)\n",
94 elm? elm->data:"(null)",
96 if (strcmp(key,elm->name))
97 THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
99 if (strcmp(elm->name,elm->data))
100 THROW2(mismatch_error,0,"The name (%s) != data (%s)",
105 static void search_id(xbt_set_t head,int id,const char*key) {
106 my_elem_t elm = (my_elem_t) xbt_set_get_by_id(head,id);
108 printf(" - Search by id %d. Found %s (data %s)\n",
110 elm? elm->name:"(null)",
111 elm? elm->data:"(null)");
113 THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
115 if (strcmp(key,elm->name))
116 THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
118 if (strcmp(elm->name,elm->data))
119 THROW2(mismatch_error,0,"The name (%s) != data (%s)",
120 elm->name,elm->data);
125 static void traverse(xbt_set_t set) {
126 xbt_set_cursor_t cursor=NULL;
129 xbt_set_foreach(set,cursor,elm) {
130 xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
131 printf(" - Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
132 xbt_assert2(!strcmp(elm->name,elm->data),
133 "Key(%s) != value(%s). Abording",
134 elm->name,elm->data);
138 static void search_not_found(xbt_set_t set, const char *data) {
142 xbt_set_get_by_name(set,data);
143 THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
145 if (e.category == mismatch_error) {
153 int main(int argc,char **argv) {
157 xbt_init(&argc,argv);
159 printf("\nData set: USAGE test:\n");
161 printf(" Traverse the empty set\n");
165 printf(" Free the data set\n");
167 printf(" Free the data set again\n");
172 printf(" - Change some values\n");
173 printf(" - Change 123 to 'Changed 123'\n");
174 debuged_add_with_data(set,"123","Changed 123");
175 printf(" - Change 123 back to '123'\n");
176 debuged_add_with_data(set,"123","123");
177 printf(" - Change 12a to 'Dummy 12a'\n");
178 debuged_add_with_data(set,"12a","Dummy 12a");
179 printf(" - Change 12a to '12a'\n");
180 debuged_add_with_data(set,"12a","12a");
182 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
183 printf(" - Traverse the resulting data set\n");
186 printf(" - Retrive values\n");
187 elm = (my_elem_t) xbt_set_get_by_name(set,"123");
189 strcmp("123",elm->data);
191 search_not_found(set,"Can't be found");
192 search_not_found(set,"123 Can't be found");
193 search_not_found(set,"12345678 NOT");
195 search_name(set,"12");
196 search_name(set,"12a");
197 search_name(set,"12b");
198 search_name(set,"123");
199 search_name(set,"123456");
200 search_name(set,"1234");
201 search_name(set,"123457");
203 search_id(set,0,"12");
204 search_id(set,1,"12a");
205 search_id(set,2,"12b");
206 search_id(set,3,"123");
207 search_id(set,4,"123456");
208 search_id(set,5,"1234");
209 search_id(set,6,"123457");
211 printf(" - Traverse the resulting data set\n");
214 /* xbt_dict_dump(head,(void (*)(void*))&printf); */
216 printf(" Free the data set (twice)\n");
220 printf(" - Traverse the resulting data set\n");