Logo AND Algorithmique Numérique Distribuée

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