Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
API cleanup: s/dict_insert/dict_set/ and s/dict_retrieve/dict_get/ for consistency...
[simgrid.git] / testsuite / xbt / set_usage.c
1 /* $Id$ */
2
3 /* set_usage - A test of normal usage of a set                              */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 the OURAGAN project.                                  */
7
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. */
10
11 #include <stdio.h>
12 #include <assert.h>
13
14 #include <gras.h>
15
16 GRAS_LOG_NEW_DEFAULT_CATEGORY(test);
17 GRAS_LOG_EXTERNAL_CATEGORY(set);
18
19 typedef struct  {
20   /* headers */
21   unsigned int ID;
22   char        *name;
23   unsigned int name_len;
24
25   /* payload */
26   char         *data;
27 }my_elem_t;
28
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,
32                                           const char *name,
33                                           const char *data);
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,
36                               int id,
37                               const char*expected_key);
38 static gras_error_t traverse(gras_set_t *set);
39
40 static void my_elem_free(void *e) {
41   my_elem_t *elm=(my_elem_t*)e;
42
43   if (elm) {
44     free(elm->name);
45     free(elm->data);
46     free(elm);
47   }
48 }
49
50 static gras_error_t debuged_add_with_data(gras_set_t *set,
51                                           const char *name,
52                                           const char *data) {
53
54   gras_error_t  errcode;
55   my_elem_t    *elm;
56
57   elm = (my_elem_t*)malloc(sizeof(my_elem_t));
58   elm->name=strdup(name);
59   elm->name_len=0;
60
61   elm->data=strdup(data);
62
63   printf("   - Add %s ",name);
64   if (strcmp(name,data)) {
65     printf("(->%s)",data);
66   }
67   printf("\n");
68   errcode=gras_set_add(set,
69                        (gras_set_elm_t*)elm,
70                        &my_elem_free);
71   return errcode;
72 }
73
74 static gras_error_t debuged_add(gras_set_t *set,
75                                 const char *name) {
76   return debuged_add_with_data(set, name, name);
77 }
78
79 static gras_error_t fill(gras_set_t **set) {
80   gras_error_t errcode;
81   printf("\n Fill in the data set\n");
82
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"));
93
94   return no_error;
95 }
96
97 static gras_error_t search_name(gras_set_t *head,const char*key) {
98   gras_error_t    errcode;
99   my_elem_t      *elm;
100   
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",
103          key, 
104          elm? elm->data:"(null)",
105          elm? elm->ID:-1);
106   if (strcmp(key,elm->name)) {
107     printf("    The key (%s) is not the one expected (%s)\n",
108            key,elm->name);
109     return mismatch_error;
110   }
111   if (strcmp(elm->name,elm->data)) {
112     printf("    The name (%s) != data (%s)\n",
113            elm->name,elm->data);
114     return mismatch_error;
115   }
116   fflush(stdout);
117   return errcode;
118 }
119
120 static gras_error_t search_id(gras_set_t *head,int id,const char*key) {
121   gras_error_t    errcode;
122   my_elem_t      *elm;
123   
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",
126          id, 
127          elm? elm->name:"(null)",
128          elm? elm->data:"(null)");
129   if (id != elm->ID) {
130     printf("    The found ID (%d) is not the one expected (%d)\n",
131            elm->ID,id);
132     return mismatch_error;
133   }
134   if (strcmp(key,elm->name)) {
135     printf("    The key (%s) is not the one expected (%s)\n",
136            elm->name,key);
137     return mismatch_error;
138   }
139   if (strcmp(elm->name,elm->data)) {
140     printf("    The name (%s) != data (%s)\n",
141            elm->name,elm->data);
142     return mismatch_error;
143   }
144   fflush(stdout);
145   return errcode;
146 }
147
148
149 static gras_error_t traverse(gras_set_t *set) {
150   gras_set_cursor_t *cursor=NULL;
151   my_elem_t *elm=NULL;
152
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);
158       abort();
159     }
160   }
161   return no_error;
162 }
163
164 int main(int argc,char **argv) {
165   gras_error_t errcode;
166   gras_set_t *set=NULL;
167   my_elem_t *elm;
168
169   gras_init_defaultlog(&argc,argv,"set.thresh=verbose");
170    
171   printf("\nData set: USAGE test:\n");
172
173   printf(" Traverse the empty set\n");
174   TRYFAIL(traverse(set));
175
176   TRYFAIL(fill(&set));
177   printf(" Free the data set\n");
178   gras_set_free(&set);
179   printf(" Free the data set again\n");
180   gras_set_free(&set);
181   
182   TRYFAIL(fill(&set));
183
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"));
193
194   //  gras_dict_dump(head,(void (*)(void*))&printf);
195   printf(" - Traverse the resulting data set\n");
196   TRYFAIL(traverse(set));
197
198   printf(" - Retrive values\n");
199   TRYFAIL(gras_set_get_by_name(set,"123",(gras_set_elm_t**)&elm));
200   assert(elm);
201   TRYFAIL(strcmp("123",elm->data));
202
203   TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t**)&elm),
204             mismatch_error);
205   TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t**)&elm),
206             mismatch_error);
207   TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t**)&elm),
208             mismatch_error);
209
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"));
217
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"));
225
226   printf(" - Traverse the resulting data set\n");
227   TRYFAIL(traverse(set));
228
229   //  gras_dict_dump(head,(void (*)(void*))&printf);
230
231   printf(" Free the data set (twice)\n");
232   gras_set_free(&set);
233   gras_set_free(&set);
234
235   printf(" - Traverse the resulting data set\n");
236   TRYFAIL(traverse(set));
237
238   gras_exit();
239   return 0;
240 }