Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
23d60a31b38c7e5c22636456e4798a53dfd70e0a
[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 #include "xbt/ex.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 void search_name(xbt_set_t set,const char*key);
34 static void search_id(xbt_set_t head,int id,
35                       const char*expected_key);
36 static void 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 void search_name(xbt_set_t head,const char*key) {
90   my_elem_t elm = (my_elem_t)xbt_set_get_by_name(head,key);
91
92   printf("   - Search by name %s. Found %s (under ID %d)\n",
93          key, 
94          elm? elm->data:"(null)",
95          elm? elm->ID:-1);
96   if (strcmp(key,elm->name))
97     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
98            key,elm->name);
99   if (strcmp(elm->name,elm->data))
100     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
101            key,elm->name);
102   fflush(stdout);
103 }
104
105 static void search_id(xbt_set_t head,int id,const char*key) {
106   my_elem_t elm = (my_elem_t) xbt_set_get_by_id(head,id);
107
108   printf("   - Search by id %d. Found %s (data %s)\n",
109          id, 
110          elm? elm->name:"(null)",
111          elm? elm->data:"(null)");
112   if (id != elm->ID)
113     THROW2(mismatch_error,0,"The found ID (%d) is not the one expected (%d)",
114            elm->ID,id);
115   if (strcmp(key,elm->name))
116     THROW2(mismatch_error,0,"The key (%s) is not the one expected (%s)",
117            elm->name,key);
118   if (strcmp(elm->name,elm->data))
119     THROW2(mismatch_error,0,"The name (%s) != data (%s)",
120            elm->name,elm->data);
121   fflush(stdout);
122 }
123
124
125 static void traverse(xbt_set_t set) {
126   xbt_set_cursor_t cursor=NULL;
127   my_elem_t         elm=NULL;
128
129   xbt_set_foreach(set,cursor,elm) {
130     xbt_assert0(elm,"Dude ! Got a null elm during traversal!");
131     printf("   - Id(%d):  %s->%s\n",elm->ID,elm->name,elm->data);
132     xbt_assert2(!strcmp(elm->name,elm->data),
133                  "Key(%s) != value(%s). Abording",
134                  elm->name,elm->data);
135   }
136 }
137
138 static void search_not_found(xbt_set_t set, const char *data) {
139   xbt_ex_t e;
140   
141   TRY {
142     xbt_set_get_by_name(set,data);
143     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
144   } CATCH(e) {
145     if (e.category == mismatch_error) {
146       xbt_ex_free(e);
147     } else {
148       RETHROW;
149     }
150   }
151 }
152
153 int main(int argc,char **argv) {
154   xbt_set_t set=NULL;
155   my_elem_t  elm;
156
157   xbt_init(&argc,argv);
158    
159   printf("\nData set: USAGE test:\n");
160
161   printf(" Traverse the empty set\n");
162   traverse(set);
163
164   fill(&set);
165   printf(" Free the data set\n");
166   xbt_set_free(&set);
167   printf(" Free the data set again\n");
168   xbt_set_free(&set);
169   
170   fill(&set);
171
172   printf(" - Change some values\n");
173   printf("   - Change 123 to 'Changed 123'\n");
174   debuged_add_with_data(set,"123","Changed 123");
175   printf("   - Change 123 back to '123'\n");
176   debuged_add_with_data(set,"123","123");
177   printf("   - Change 12a to 'Dummy 12a'\n");
178   debuged_add_with_data(set,"12a","Dummy 12a");
179   printf("   - Change 12a to '12a'\n");
180   debuged_add_with_data(set,"12a","12a");
181
182   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
183   printf(" - Traverse the resulting data set\n");
184   traverse(set);
185
186   printf(" - Retrive values\n");
187   elm = (my_elem_t) xbt_set_get_by_name(set,"123");
188   xbt_assert(elm);
189   strcmp("123",elm->data);
190
191   search_not_found(set,"Can't be found");
192   search_not_found(set,"123 Can't be found");
193   search_not_found(set,"12345678 NOT");
194
195   search_name(set,"12");
196   search_name(set,"12a");
197   search_name(set,"12b");
198   search_name(set,"123");
199   search_name(set,"123456");
200   search_name(set,"1234");
201   search_name(set,"123457");
202
203   search_id(set,0,"12");
204   search_id(set,1,"12a");
205   search_id(set,2,"12b");
206   search_id(set,3,"123");
207   search_id(set,4,"123456");
208   search_id(set,5,"1234");
209   search_id(set,6,"123457");
210
211   printf(" - Traverse the resulting data set\n");
212   traverse(set);
213
214   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
215
216   printf(" Free the data set (twice)\n");
217   xbt_set_free(&set);
218   xbt_set_free(&set);
219
220   printf(" - Traverse the resulting data set\n");
221   traverse(set);
222
223   xbt_exit();
224   return 0;
225 }