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 GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17 GRAS_LOG_EXTERNAL_CATEGORY(set);
23 unsigned int name_len;
29 static gras_error_t fill(gras_set_t **set);
30 static gras_error_t debuged_add(gras_set_t *set,const char*key);
31 static gras_error_t debuged_add_with_data(gras_set_t *set,
34 static gras_error_t search_name(gras_set_t *set,const char*key);
35 static gras_error_t search_id(gras_set_t *head,
37 const char*expected_key);
38 static gras_error_t traverse(gras_set_t *set);
40 static void my_elem_free(void *e) {
41 my_elem_t *elm=(my_elem_t*)e;
50 static gras_error_t debuged_add_with_data(gras_set_t *set,
57 elm = gras_new(my_elem_t,1);
58 elm->name=strdup(name);
61 elm->data=strdup(data);
63 printf(" - Add %s ",name);
64 if (strcmp(name,data)) {
65 printf("(->%s)",data);
68 errcode=gras_set_add(set,
74 static gras_error_t debuged_add(gras_set_t *set,
76 return debuged_add_with_data(set, name, name);
79 static gras_error_t fill(gras_set_t **set) {
81 printf("\n Fill in the data set\n");
83 TRY(gras_set_new(set));
84 TRY(debuged_add(*set,"12"));
85 TRY(debuged_add(*set,"12a"));
86 TRY(debuged_add(*set,"12b"));
87 TRY(debuged_add(*set,"123"));
88 TRY(debuged_add(*set,"123456"));
89 // Child becomes child of what to add
90 TRY(debuged_add(*set,"1234"));
91 // Need of common ancestor
92 TRY(debuged_add(*set,"123457"));
97 static gras_error_t search_name(gras_set_t *head,const char*key) {
101 errcode=gras_set_get_by_name(head,key,(gras_set_elm_t**)&elm);
102 printf(" - Search by name %s. Found %s (under ID %d)\n",
104 elm? elm->data:"(null)",
106 if (strcmp(key,elm->name)) {
107 printf(" The key (%s) is not the one expected (%s)\n",
109 return mismatch_error;
111 if (strcmp(elm->name,elm->data)) {
112 printf(" The name (%s) != data (%s)\n",
113 elm->name,elm->data);
114 return mismatch_error;
120 static gras_error_t search_id(gras_set_t *head,int id,const char*key) {
121 gras_error_t errcode;
124 errcode=gras_set_get_by_id(head,id,(gras_set_elm_t**)&elm);
125 printf(" - Search by id %d. Found %s (data %s)\n",
127 elm? elm->name:"(null)",
128 elm? elm->data:"(null)");
130 printf(" The found ID (%d) is not the one expected (%d)\n",
132 return mismatch_error;
134 if (strcmp(key,elm->name)) {
135 printf(" The key (%s) is not the one expected (%s)\n",
137 return mismatch_error;
139 if (strcmp(elm->name,elm->data)) {
140 printf(" The name (%s) != data (%s)\n",
141 elm->name,elm->data);
142 return mismatch_error;
149 static gras_error_t traverse(gras_set_t *set) {
150 gras_set_cursor_t *cursor=NULL;
153 gras_set_foreach(set,cursor,elm) {
154 gras_assert0(elm,"Dude ! Got a null elm during traversal!");
155 printf(" - Id(%d): %s->%s\n",elm->ID,elm->name,elm->data);
156 gras_assert2(!strcmp(elm->name,elm->data),
157 "Key(%s) != value(%s). Abording",
158 elm->name,elm->data);
163 int main(int argc,char **argv) {
164 gras_error_t errcode;
165 gras_set_t *set=NULL;
168 gras_init_defaultlog(&argc,argv,"set.thresh=verbose");
170 printf("\nData set: USAGE test:\n");
172 printf(" Traverse the empty set\n");
173 TRYFAIL(traverse(set));
176 printf(" Free the data set\n");
178 printf(" Free the data set again\n");
183 printf(" - Change some values\n");
184 printf(" - Change 123 to 'Changed 123'\n");
185 TRYFAIL(debuged_add_with_data(set,"123","Changed 123"));
186 printf(" - Change 123 back to '123'\n");
187 TRYFAIL(debuged_add_with_data(set,"123","123"));
188 printf(" - Change 12a to 'Dummy 12a'\n");
189 TRYFAIL(debuged_add_with_data(set,"12a","Dummy 12a"));
190 printf(" - Change 12a to '12a'\n");
191 TRYFAIL(debuged_add_with_data(set,"12a","12a"));
193 // gras_dict_dump(head,(void (*)(void*))&printf);
194 printf(" - Traverse the resulting data set\n");
195 TRYFAIL(traverse(set));
197 printf(" - Retrive values\n");
198 TRYFAIL(gras_set_get_by_name(set,"123",(gras_set_elm_t**)&elm));
200 TRYFAIL(strcmp("123",elm->data));
202 TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t**)&elm),
204 TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t**)&elm),
206 TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t**)&elm),
209 TRYFAIL(search_name(set,"12"));
210 TRYFAIL(search_name(set,"12a"));
211 TRYFAIL(search_name(set,"12b"));
212 TRYFAIL(search_name(set,"123"));
213 TRYFAIL(search_name(set,"123456"));
214 TRYFAIL(search_name(set,"1234"));
215 TRYFAIL(search_name(set,"123457"));
217 TRYFAIL(search_id(set,0,"12"));
218 TRYFAIL(search_id(set,1,"12a"));
219 TRYFAIL(search_id(set,2,"12b"));
220 TRYFAIL(search_id(set,3,"123"));
221 TRYFAIL(search_id(set,4,"123456"));
222 TRYFAIL(search_id(set,5,"1234"));
223 TRYFAIL(search_id(set,6,"123457"));
225 printf(" - Traverse the resulting data set\n");
226 TRYFAIL(traverse(set));
228 // gras_dict_dump(head,(void (*)(void*))&printf);
230 printf(" Free the data set (twice)\n");
234 printf(" - Traverse the resulting data set\n");
235 TRYFAIL(traverse(set));