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 void parse_log_opt(int argc, char **argv,const char *deft);
166 int main(int argc,char **argv) {
167 gras_error_t errcode;
168 gras_set_t *set=NULL;
171 parse_log_opt(argc,argv,"set.thresh=verbose");
173 printf("\nData set: USAGE test:\n");
175 printf(" Traverse the empty set\n");
176 TRYFAIL(traverse(set));
179 printf(" Free the data set\n");
181 printf(" Free the data set again\n");
186 printf(" - Change some values\n");
187 printf(" - Change 123 to 'Changed 123'\n");
188 TRYFAIL(debuged_add_with_data(set,"123","Changed 123"));
189 printf(" - Change 123 back to '123'\n");
190 TRYFAIL(debuged_add_with_data(set,"123","123"));
191 printf(" - Change 12a to 'Dummy 12a'\n");
192 TRYFAIL(debuged_add_with_data(set,"12a","Dummy 12a"));
193 printf(" - Change 12a to '12a'\n");
194 TRYFAIL(debuged_add_with_data(set,"12a","12a"));
196 // gras_dict_dump(head,(void (*)(void*))&printf);
197 printf(" - Traverse the resulting data set\n");
198 TRYFAIL(traverse(set));
200 printf(" - Retrive values\n");
201 TRYFAIL(gras_set_get_by_name(set,"123",(gras_set_elm_t**)&elm));
203 TRYFAIL(strcmp("123",elm->data));
205 TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t**)&elm),
207 TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t**)&elm),
209 TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t**)&elm),
212 TRYFAIL(search_name(set,"12"));
213 TRYFAIL(search_name(set,"12a"));
214 TRYFAIL(search_name(set,"12b"));
215 TRYFAIL(search_name(set,"123"));
216 TRYFAIL(search_name(set,"123456"));
217 TRYFAIL(search_name(set,"1234"));
218 TRYFAIL(search_name(set,"123457"));
220 TRYFAIL(search_id(set,0,"12"));
221 TRYFAIL(search_id(set,1,"12a"));
222 TRYFAIL(search_id(set,2,"12b"));
223 TRYFAIL(search_id(set,3,"123"));
224 TRYFAIL(search_id(set,4,"123456"));
225 TRYFAIL(search_id(set,5,"1234"));
226 TRYFAIL(search_id(set,6,"123457"));
228 printf(" - Traverse the resulting data set\n");
229 TRYFAIL(traverse(set));
231 // gras_dict_dump(head,(void (*)(void*))&printf);
233 printf(" Free the data set (twice)\n");
237 printf(" - Traverse the resulting data set\n");
238 TRYFAIL(traverse(set));