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);
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 = (my_elem_t*)malloc(sizeof(my_elem_t));
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 if (strcmp(elm->name,elm->data)) {
157 printf("Key(%s) != value(%s). Abording\n",elm->name,elm->data);
164 int main(int argc,char **argv) {
165 gras_error_t errcode;
166 gras_set_t *set=NULL;
169 gras_init_defaultlog(&argc,argv,"set.thresh=verbose");
171 printf("\nData set: USAGE test:\n");
173 printf(" Traverse the empty set\n");
174 TRYFAIL(traverse(set));
177 printf(" Free the data set\n");
179 printf(" Free the data set again\n");
184 printf(" - Change some values\n");
185 printf(" - Change 123 to 'Changed 123'\n");
186 TRYFAIL(debuged_add_with_data(set,"123","Changed 123"));
187 printf(" - Change 123 back to '123'\n");
188 TRYFAIL(debuged_add_with_data(set,"123","123"));
189 printf(" - Change 12a to 'Dummy 12a'\n");
190 TRYFAIL(debuged_add_with_data(set,"12a","Dummy 12a"));
191 printf(" - Change 12a to '12a'\n");
192 TRYFAIL(debuged_add_with_data(set,"12a","12a"));
194 // gras_dict_dump(head,(void (*)(void*))&printf);
195 printf(" - Traverse the resulting data set\n");
196 TRYFAIL(traverse(set));
198 printf(" - Retrive values\n");
199 TRYFAIL(gras_set_get_by_name(set,"123",(gras_set_elm_t**)&elm));
201 TRYFAIL(strcmp("123",elm->data));
203 TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t**)&elm),
205 TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t**)&elm),
207 TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t**)&elm),
210 TRYFAIL(search_name(set,"12"));
211 TRYFAIL(search_name(set,"12a"));
212 TRYFAIL(search_name(set,"12b"));
213 TRYFAIL(search_name(set,"123"));
214 TRYFAIL(search_name(set,"123456"));
215 TRYFAIL(search_name(set,"1234"));
216 TRYFAIL(search_name(set,"123457"));
218 TRYFAIL(search_id(set,0,"12"));
219 TRYFAIL(search_id(set,1,"12a"));
220 TRYFAIL(search_id(set,2,"12b"));
221 TRYFAIL(search_id(set,3,"123"));
222 TRYFAIL(search_id(set,4,"123456"));
223 TRYFAIL(search_id(set,5,"1234"));
224 TRYFAIL(search_id(set,6,"123457"));
226 printf(" - Traverse the resulting data set\n");
227 TRYFAIL(traverse(set));
229 // gras_dict_dump(head,(void (*)(void*))&printf);
231 printf(" Free the data set (twice)\n");
235 printf(" - Traverse the resulting data set\n");
236 TRYFAIL(traverse(set));