Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Missing header
[simgrid.git] / testsuite / xbt / dict_usage.c
1 /* $Id$ */
2
3 /* dict_usage - A test of normal usage of a dictionnary                     */
4
5 /* Copyright (c) 2003,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 "portable.h"
14 #include "xbt/ex.h"
15
16 XBT_LOG_EXTERNAL_CATEGORY(dict);
17 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
18
19 static void fill(xbt_dict_t *head);
20 static void debuged_add(xbt_dict_t head,const char*key);
21 static void search(xbt_dict_t head,const char*key);
22 static void debuged_remove(xbt_dict_t head,const char*key);
23 static void traverse(xbt_dict_t head);
24
25 static void print_str(void *str);
26 static void print_str(void *str) {
27   printf("%s",(char*)PRINTF_STR(str));
28 }
29
30 static void fill(xbt_dict_t *head) {
31   printf("\n Fill in the dictionnary\n");
32
33   *head = xbt_dict_new();
34   debuged_add(*head,"12");
35   debuged_add(*head,"12a");
36   debuged_add(*head,"12b");
37   debuged_add(*head,"123");
38   debuged_add(*head,"123456");
39   /* Child becomes child of what to add */
40   debuged_add(*head,"1234");
41   /* Need of common ancestor */
42   debuged_add(*head,"123457");
43
44 }
45
46 static void debuged_add(xbt_dict_t head,const char*key)
47 {
48   char *data=xbt_strdup(key);
49
50   printf("   - Add %s\n",PRINTF_STR(key));
51   xbt_dict_set(head,key,data,&free);
52   if (XBT_LOG_ISENABLED(dict,xbt_log_priority_debug)) {
53     xbt_dict_dump(head,(void (*)(void*))&printf);
54     fflush(stdout);
55   }
56 }
57
58 static void search(xbt_dict_t head,const char*key) {
59   void *data;
60   
61   data=xbt_dict_get(head,key);
62   printf("   - Search %s. Found %s\n",PRINTF_STR(key),(char*) PRINTF_STR(data));fflush(stdout);
63   if (data && strcmp((char*)data,key)) 
64     THROW2(mismatch_error,0,"Found %s while looking for %s",(char*)data,key);
65 }
66
67 static void debuged_remove(xbt_dict_t head,const char*key) {
68
69   printf("   Remove '%s'\n",PRINTF_STR(key));fflush(stdout);
70   xbt_dict_remove(head,key);
71   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
72 }
73
74
75 static void traverse(xbt_dict_t head) {
76   xbt_dict_cursor_t cursor=NULL;
77   char *key;
78   char *data;
79
80   xbt_dict_foreach(head,cursor,key,data) {
81     printf("   - Seen:  %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));
82     xbt_assert2(!data || !strcmp(key,data),
83                  "Key(%s) != value(%s). Abording\n",key,data);
84   }
85 }
86
87 static void search_not_found(xbt_dict_t head, const char *data) {
88   xbt_ex_t e;
89
90   TRY {    
91     data = xbt_dict_get(head,"Can't be found");
92     THROW1(unknown_error,0,"Found something which shouldn't be there (%s)",data);
93   } CATCH(e) {
94     if (e.category == mismatch_error) {
95       xbt_ex_free(e);
96     } else {
97       RETHROW;
98     }
99   }
100 }
101
102 int main(int argc,char **argv) {
103   xbt_ex_t e;
104   xbt_dict_t head=NULL;
105   char *data;
106
107   xbt_init(&argc,argv);
108    
109   printf("\nGeneric dictionnary: USAGE test:\n");
110
111   printf(" Traverse the empty dictionnary\n");
112   traverse(head);
113
114   printf(" Traverse the full dictionnary\n");
115   fill(&head);
116   search(head,"12a");
117 xbt_dict_dump(head,(void (*)(void*))&printf);
118   traverse(head);
119
120   printf(" Free the dictionnary (twice)\n");
121   xbt_dict_free(&head);
122   xbt_dict_free(&head);
123   
124   fill(&head);
125
126   /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
127   printf(" - Test that it works with NULL data\n");
128   printf("   - Store NULL under 'null'\n");fflush(stdout);
129   xbt_dict_set(head,"null",NULL,NULL);
130   search(head,"null");
131   /* xbt_dict_dump(head,(void (*)(void*))&printf); */
132   printf("   Check whether I see it while traversing...\n");fflush(stdout);
133   {
134      xbt_dict_cursor_t cursor=NULL;
135      char *key;
136      int found=0;
137      
138      xbt_dict_foreach(head,cursor,key,data) {
139         printf("   - Seen:  %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));fflush(stdout);
140         if (!strcmp(key,"null"))
141           found = 1;
142      }
143      xbt_assert0(found,"the key 'null', associated to NULL is not found");
144   }
145   printf("   OK, I did found the searched NULL\n");
146
147   printf(" - Change some values\n");
148   printf("   - Change 123 to 'Changed 123'\n");
149   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
150   printf("   - Change 123 back to '123'\n");
151   xbt_dict_set(head,"123",strdup("123"),&free);
152   printf("   - Change 12a to 'Dummy 12a'\n");
153   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
154   printf("   - Change 12a to '12a'\n");
155   xbt_dict_set(head,"12a",strdup("12a"),&free);
156
157   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
158   printf(" - Traverse the resulting dictionnary\n");
159   traverse(head);
160
161   printf(" - Retrive values\n");
162   data = xbt_dict_get(head,"123");
163   xbt_assert(data);
164   strcmp("123",data);
165
166   search_not_found(head,"Can't be found");
167   search_not_found(head,"123 Can't be found");
168   search_not_found(head,"12345678 NOT");
169
170   search(head,"12a");
171   search(head,"12b");
172   search(head,"12");
173   search(head,"123456");
174   search(head,"1234");
175   search(head,"123457");
176
177   printf(" - Traverse the resulting dictionnary\n");
178   traverse(head);
179
180   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
181
182   printf(" Free the dictionnary twice\n");
183   xbt_dict_free(&head);
184   xbt_dict_free(&head);
185
186   printf(" - Traverse the resulting dictionnary\n");
187   traverse(head);
188
189   printf("\n");
190   fill(&head);
191   printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
192   TRY {
193     debuged_remove(head,"Does not exist");
194   } CATCH(e) {
195     if (e.category != mismatch_error) 
196       RETHROW;
197     xbt_ex_free(e);
198   }
199   traverse(head);
200
201   xbt_dict_free(&head);
202
203   printf(" - Remove data from the NULL dict (error message expected)\n");
204   TRY {
205     debuged_remove(head,"12345");
206   } CATCH(e) {
207     if (e.category != arg_error) 
208       RETHROW;
209     xbt_ex_free(e);
210   } 
211
212   printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
213   fill(&head);
214   debuged_remove(head,"12a");    traverse(head);
215   debuged_remove(head,"12b");    traverse(head);
216   debuged_remove(head,"12");     traverse(head);
217   debuged_remove(head,"123456"); traverse(head);
218   TRY {
219     debuged_remove(head,"12346");
220   } CATCH(e) {
221     if (e.category != mismatch_error) 
222       RETHROW;
223     xbt_ex_free(e);              traverse(head);
224   } 
225   debuged_remove(head,"1234");   traverse(head);
226   debuged_remove(head,"123457"); traverse(head);
227   debuged_remove(head,"123");    traverse(head);
228   TRY {
229     debuged_remove(head,"12346");
230   } CATCH(e) {
231     if (e.category != mismatch_error) 
232       RETHROW;
233     xbt_ex_free(e);              traverse(head);
234   } 
235   
236   printf(" - Free the dictionnary twice\n");
237   xbt_dict_free(&head);
238   xbt_dict_free(&head);
239   xbt_exit();
240   return 0;
241 }