Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Only use the C comment style, not the C++ one
[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,"Logging specific to this 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     gras_free(elm->name);
45     gras_free(elm->data);
46     gras_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 = gras_new(my_elem_t,1);
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     gras_assert2(!strcmp(elm->name,elm->data),
157                  "Key(%s) != value(%s). Abording",
158                  elm->name,elm->data);
159   }
160   return no_error;
161 }
162
163 int main(int argc,char **argv) {
164   gras_error_t errcode;
165   gras_set_t *set=NULL;
166   my_elem_t *elm;
167
168   gras_init_defaultlog(&argc,argv,"set.thresh=verbose");
169    
170   printf("\nData set: USAGE test:\n");
171
172   printf(" Traverse the empty set\n");
173   TRYFAIL(traverse(set));
174
175   TRYFAIL(fill(&set));
176   printf(" Free the data set\n");
177   gras_set_free(&set);
178   printf(" Free the data set again\n");
179   gras_set_free(&set);
180   
181   TRYFAIL(fill(&set));
182
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"));
192
193   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
194   printf(" - Traverse the resulting data set\n");
195   TRYFAIL(traverse(set));
196
197   printf(" - Retrive values\n");
198   TRYFAIL(gras_set_get_by_name(set,"123",(gras_set_elm_t**)&elm));
199   assert(elm);
200   TRYFAIL(strcmp("123",elm->data));
201
202   TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t**)&elm),
203             mismatch_error);
204   TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t**)&elm),
205             mismatch_error);
206   TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t**)&elm),
207             mismatch_error);
208
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"));
216
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"));
224
225   printf(" - Traverse the resulting data set\n");
226   TRYFAIL(traverse(set));
227
228   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
229
230   printf(" Free the data set (twice)\n");
231   gras_set_free(&set);
232   gras_set_free(&set);
233
234   printf(" - Traverse the resulting data set\n");
235   TRYFAIL(traverse(set));
236
237   gras_exit();
238   return 0;
239 }