Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Modifying the API so as to prevent a use of the context that would make valgrind...
[simgrid.git] / testsuite / xbt / set_usage.c
1 /* $Id$ */
2
3 /* set_usage - A test of normal usage of a set                              */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdio.h>
11
12 #include "gras.h"
13
14 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
15 XBT_LOG_EXTERNAL_CATEGORY(set);
16
17 typedef struct  {
18   /* headers */
19   unsigned int ID;
20   char        *name;
21   unsigned int name_len;
22
23   /* payload */
24   char         *data;
25 } s_my_elem_t,*my_elem_t;
26
27 static void fill(xbt_set_t *set);
28 static void debuged_add(xbt_set_t set,const char*key);
29 static void debuged_add_with_data(xbt_set_t  set,
30                                   const char *name,
31                                   const char *data);
32 static xbt_error_t search_name(xbt_set_t set,const char*key);
33 static xbt_error_t search_id(xbt_set_t head,
34                               int id,
35                               const char*expected_key);
36 static xbt_error_t traverse(xbt_set_t set);
37
38 static void my_elem_free(void *e) {
39   my_elem_t elm=(my_elem_t)e;
40
41   if (elm) {
42     free(elm->name);
43     free(elm->data);
44     free(elm);
45   }
46 }
47
48 static void debuged_add_with_data(xbt_set_t  set,
49                                   const char *name,
50                                   const char *data) {
51
52   my_elem_t    elm;
53
54   elm = xbt_new(s_my_elem_t,1);
55   elm->name=xbt_strdup(name);
56   elm->name_len=0;
57
58   elm->data=xbt_strdup(data);
59
60   printf("   - Add %s ",name);
61   if (strcmp(name,data)) {
62     printf("(->%s)",data);
63   }
64   printf("\n");
65   xbt_set_add(set, (xbt_set_elm_t)elm,
66                &my_elem_free);
67 }
68
69 static void debuged_add(xbt_set_t  set,
70                         const char *name) {
71   debuged_add_with_data(set, name, name);
72 }
73
74 static void fill(xbt_set_t *set) {
75   printf("\n Fill in the data set\n");
76
77   *set=xbt_set_new();
78   debuged_add(*set,"12");
79   debuged_add(*set,"12a");
80   debuged_add(*set,"12b");
81   debuged_add(*set,"123");
82   debuged_add(*set,"123456");
83   /* Child becomes child of what to add */
84   debuged_add(*set,"1234");
85   /* Need of common ancestor */
86   debuged_add(*set,"123457");
87 }
88
89 static xbt_error_t search_name(xbt_set_t head,const char*key) {
90   xbt_error_t    errcode;
91   my_elem_t       elm;
92   
93   errcode=xbt_set_get_by_name(head,key,(xbt_set_elm_t*)&elm);
94   printf("   - Search by name %s. Found %s (under ID %d)\n",
95          key, 
96          elm? elm->data:"(null)",
97          elm? elm->ID:-1);
98   if (strcmp(key,elm->name)) {
99     printf("    The key (%s) is not the one expected (%s)\n",
100            key,elm->name);
101     return mismatch_error;
102   }
103   if (strcmp(elm->name,elm->data)) {
104     printf("    The name (%s) != data (%s)\n",
105            elm->name,elm->data);
106     return mismatch_error;
107   }
108   fflush(stdout);
109   return errcode;
110 }
111
112 static xbt_error_t search_id(xbt_set_t head,int id,const char*key) {
113   xbt_error_t errcode;
114   my_elem_t    elm;
115   
116   errcode=xbt_set_get_by_id(head,id,(xbt_set_elm_t*)&elm);
117   printf("   - Search by id %d. Found %s (data %s)\n",
118          id, 
119          elm? elm->name:"(null)",
120          elm? elm->data:"(null)");
121   if (id != elm->ID) {
122     printf("    The found ID (%d) is not the one expected (%d)\n",
123            elm->ID,id);
124     return mismatch_error;
125   }
126   if (strcmp(key,elm->name)) {
127     printf("    The key (%s) is not the one expected (%s)\n",
128            elm->name,key);
129     return mismatch_error;
130   }
131   if (strcmp(elm->name,elm->data)) {
132     printf("    The name (%s) != data (%s)\n",
133            elm->name,elm->data);
134     return mismatch_error;
135   }
136   fflush(stdout);
137   return errcode;
138 }
139
140
141 static xbt_error_t traverse(xbt_set_t set) {
142   xbt_set_cursor_t cursor=NULL;
143   my_elem_t         elm=NULL;
144
145   xbt_set_foreach(set,cursor,elm) {
146     xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
147     printf("   - Id(%d):  %s->%s\n",elm->ID,elm->name,elm->data);
148     xbt_assert2(!strcmp(elm->name,elm->data),
149                  "Key(%s) != value(%s). Abording",
150                  elm->name,elm->data);
151   }
152   return no_error;
153 }
154
155 int main(int argc,char **argv) {
156   xbt_error_t errcode;
157   xbt_set_t set=NULL;
158   my_elem_t  elm;
159
160   xbt_init_defaultlog(&argc,argv,"set.thresh=verbose");
161    
162   printf("\nData set: USAGE test:\n");
163
164   printf(" Traverse the empty set\n");
165   TRYFAIL(traverse(set));
166
167   fill(&set);
168   printf(" Free the data set\n");
169   xbt_set_free(&set);
170   printf(" Free the data set again\n");
171   xbt_set_free(&set);
172   
173   fill(&set);
174
175   printf(" - Change some values\n");
176   printf("   - Change 123 to 'Changed 123'\n");
177   debuged_add_with_data(set,"123","Changed 123");
178   printf("   - Change 123 back to '123'\n");
179   debuged_add_with_data(set,"123","123");
180   printf("   - Change 12a to 'Dummy 12a'\n");
181   debuged_add_with_data(set,"12a","Dummy 12a");
182   printf("   - Change 12a to '12a'\n");
183   debuged_add_with_data(set,"12a","12a");
184
185   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
186   printf(" - Traverse the resulting data set\n");
187   TRYFAIL(traverse(set));
188
189   printf(" - Retrive values\n");
190   xbt_set_get_by_name(set,"123",(xbt_set_elm_t*)&elm);
191   xbt_assert(elm);
192   TRYFAIL(strcmp("123",elm->data));
193
194   TRYEXPECT(xbt_set_get_by_name(set,"Can't be found",(xbt_set_elm_t*)&elm),
195             mismatch_error);
196   TRYEXPECT(xbt_set_get_by_name(set,"123 Can't be found",(xbt_set_elm_t*)&elm),
197             mismatch_error);
198   TRYEXPECT(xbt_set_get_by_name(set,"12345678 NOT",(xbt_set_elm_t*)&elm),
199             mismatch_error);
200
201   TRYFAIL(search_name(set,"12"));
202   TRYFAIL(search_name(set,"12a"));
203   TRYFAIL(search_name(set,"12b"));
204   TRYFAIL(search_name(set,"123"));
205   TRYFAIL(search_name(set,"123456"));
206   TRYFAIL(search_name(set,"1234"));
207   TRYFAIL(search_name(set,"123457"));
208
209   TRYFAIL(search_id(set,0,"12"));
210   TRYFAIL(search_id(set,1,"12a"));
211   TRYFAIL(search_id(set,2,"12b"));
212   TRYFAIL(search_id(set,3,"123"));
213   TRYFAIL(search_id(set,4,"123456"));
214   TRYFAIL(search_id(set,5,"1234"));
215   TRYFAIL(search_id(set,6,"123457"));
216
217   printf(" - Traverse the resulting data set\n");
218   TRYFAIL(traverse(set));
219
220   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
221
222   printf(" Free the data set (twice)\n");
223   xbt_set_free(&set);
224   xbt_set_free(&set);
225
226   printf(" - Traverse the resulting data set\n");
227   TRYFAIL(traverse(set));
228
229   xbt_exit();
230   return 0;
231 }