Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
star eradication, mainly (check changelog)
[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 } s_my_elem_t,*my_elem_t;
28
29 static void fill(gras_set_t *set);
30 static void debuged_add(gras_set_t set,const char*key);
31 static void 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 void debuged_add_with_data(gras_set_t  set,
51                                   const char *name,
52                                   const char *data) {
53
54   my_elem_t    elm;
55
56   elm = gras_new(s_my_elem_t,1);
57   elm->name=gras_strdup(name);
58   elm->name_len=0;
59
60   elm->data=gras_strdup(data);
61
62   printf("   - Add %s ",name);
63   if (strcmp(name,data)) {
64     printf("(->%s)",data);
65   }
66   printf("\n");
67   gras_set_add(set, (gras_set_elm_t)elm,
68                &my_elem_free);
69 }
70
71 static void debuged_add(gras_set_t  set,
72                         const char *name) {
73   debuged_add_with_data(set, name, name);
74 }
75
76 static void fill(gras_set_t *set) {
77   printf("\n Fill in the data set\n");
78
79   *set=gras_set_new();
80   debuged_add(*set,"12");
81   debuged_add(*set,"12a");
82   debuged_add(*set,"12b");
83   debuged_add(*set,"123");
84   debuged_add(*set,"123456");
85   /* Child becomes child of what to add */
86   debuged_add(*set,"1234");
87   /* Need of common ancestor */
88   debuged_add(*set,"123457");
89 }
90
91 static gras_error_t search_name(gras_set_t head,const char*key) {
92   gras_error_t    errcode;
93   my_elem_t       elm;
94   
95   errcode=gras_set_get_by_name(head,key,(gras_set_elm_t*)&elm);
96   printf("   - Search by name %s. Found %s (under ID %d)\n",
97          key, 
98          elm? elm->data:"(null)",
99          elm? elm->ID:-1);
100   if (strcmp(key,elm->name)) {
101     printf("    The key (%s) is not the one expected (%s)\n",
102            key,elm->name);
103     return mismatch_error;
104   }
105   if (strcmp(elm->name,elm->data)) {
106     printf("    The name (%s) != data (%s)\n",
107            elm->name,elm->data);
108     return mismatch_error;
109   }
110   fflush(stdout);
111   return errcode;
112 }
113
114 static gras_error_t search_id(gras_set_t head,int id,const char*key) {
115   gras_error_t errcode;
116   my_elem_t    elm;
117   
118   errcode=gras_set_get_by_id(head,id,(gras_set_elm_t*)&elm);
119   printf("   - Search by id %d. Found %s (data %s)\n",
120          id, 
121          elm? elm->name:"(null)",
122          elm? elm->data:"(null)");
123   if (id != elm->ID) {
124     printf("    The found ID (%d) is not the one expected (%d)\n",
125            elm->ID,id);
126     return mismatch_error;
127   }
128   if (strcmp(key,elm->name)) {
129     printf("    The key (%s) is not the one expected (%s)\n",
130            elm->name,key);
131     return mismatch_error;
132   }
133   if (strcmp(elm->name,elm->data)) {
134     printf("    The name (%s) != data (%s)\n",
135            elm->name,elm->data);
136     return mismatch_error;
137   }
138   fflush(stdout);
139   return errcode;
140 }
141
142
143 static gras_error_t traverse(gras_set_t set) {
144   gras_set_cursor_t cursor=NULL;
145   my_elem_t         elm=NULL;
146
147   gras_set_foreach(set,cursor,elm) {
148     gras_assert0(elm,"Dude ! Got a null elm during traversal!");
149     printf("   - Id(%d):  %s->%s\n",elm->ID,elm->name,elm->data);
150     gras_assert2(!strcmp(elm->name,elm->data),
151                  "Key(%s) != value(%s). Abording",
152                  elm->name,elm->data);
153   }
154   return no_error;
155 }
156
157 int main(int argc,char **argv) {
158   gras_error_t errcode;
159   gras_set_t set=NULL;
160   my_elem_t  elm;
161
162   gras_init_defaultlog(&argc,argv,"set.thresh=verbose");
163    
164   printf("\nData set: USAGE test:\n");
165
166   printf(" Traverse the empty set\n");
167   TRYFAIL(traverse(set));
168
169   fill(&set);
170   printf(" Free the data set\n");
171   gras_set_free(&set);
172   printf(" Free the data set again\n");
173   gras_set_free(&set);
174   
175   fill(&set);
176
177   printf(" - Change some values\n");
178   printf("   - Change 123 to 'Changed 123'\n");
179   debuged_add_with_data(set,"123","Changed 123");
180   printf("   - Change 123 back to '123'\n");
181   debuged_add_with_data(set,"123","123");
182   printf("   - Change 12a to 'Dummy 12a'\n");
183   debuged_add_with_data(set,"12a","Dummy 12a");
184   printf("   - Change 12a to '12a'\n");
185   debuged_add_with_data(set,"12a","12a");
186
187   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
188   printf(" - Traverse the resulting data set\n");
189   TRYFAIL(traverse(set));
190
191   printf(" - Retrive values\n");
192   gras_set_get_by_name(set,"123",(gras_set_elm_t*)&elm);
193   assert(elm);
194   TRYFAIL(strcmp("123",elm->data));
195
196   TRYEXPECT(gras_set_get_by_name(set,"Can't be found",(gras_set_elm_t*)&elm),
197             mismatch_error);
198   TRYEXPECT(gras_set_get_by_name(set,"123 Can't be found",(gras_set_elm_t*)&elm),
199             mismatch_error);
200   TRYEXPECT(gras_set_get_by_name(set,"12345678 NOT",(gras_set_elm_t*)&elm),
201             mismatch_error);
202
203   TRYFAIL(search_name(set,"12"));
204   TRYFAIL(search_name(set,"12a"));
205   TRYFAIL(search_name(set,"12b"));
206   TRYFAIL(search_name(set,"123"));
207   TRYFAIL(search_name(set,"123456"));
208   TRYFAIL(search_name(set,"1234"));
209   TRYFAIL(search_name(set,"123457"));
210
211   TRYFAIL(search_id(set,0,"12"));
212   TRYFAIL(search_id(set,1,"12a"));
213   TRYFAIL(search_id(set,2,"12b"));
214   TRYFAIL(search_id(set,3,"123"));
215   TRYFAIL(search_id(set,4,"123456"));
216   TRYFAIL(search_id(set,5,"1234"));
217   TRYFAIL(search_id(set,6,"123457"));
218
219   printf(" - Traverse the resulting data set\n");
220   TRYFAIL(traverse(set));
221
222   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
223
224   printf(" Free the data set (twice)\n");
225   gras_set_free(&set);
226   gras_set_free(&set);
227
228   printf(" - Traverse the resulting data set\n");
229   TRYFAIL(traverse(set));
230
231   gras_exit();
232   return 0;
233 }