Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
+= xbt_test_assert
[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 != not_found_error) 
95       RETHROW;
96     xbt_ex_free(e);
97   }
98 }
99
100 int main(int argc,char **argv) {
101   xbt_ex_t e;
102   xbt_dict_t head=NULL;
103   char *data;
104
105   xbt_init(&argc,argv);
106    
107   printf("\nGeneric dictionnary: USAGE test:\n");
108
109   printf(" Traverse the empty dictionnary\n");
110   traverse(head);
111
112   printf(" Traverse the full dictionnary\n");
113   fill(&head);
114   search(head,"12a");
115 xbt_dict_dump(head,(void (*)(void*))&printf);
116   traverse(head);
117
118   printf(" Free the dictionnary (twice)\n");
119   xbt_dict_free(&head);
120   xbt_dict_free(&head);
121   
122   fill(&head);
123
124   /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
125   printf(" - Test that it works with NULL data\n");
126   printf("   - Store NULL under 'null'\n");fflush(stdout);
127   xbt_dict_set(head,"null",NULL,NULL);
128   search(head,"null");
129   /* xbt_dict_dump(head,(void (*)(void*))&printf); */
130   printf("   Check whether I see it while traversing...\n");fflush(stdout);
131   {
132      xbt_dict_cursor_t cursor=NULL;
133      char *key;
134      int found=0;
135      
136      xbt_dict_foreach(head,cursor,key,data) {
137         printf("   - Seen:  %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));fflush(stdout);
138         if (!strcmp(key,"null"))
139           found = 1;
140      }
141      xbt_assert0(found,"the key 'null', associated to NULL is not found");
142   }
143   printf("   OK, I did found the searched NULL\n");
144
145   printf(" - Change some values\n");
146   printf("   - Change 123 to 'Changed 123'\n");
147   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
148   printf("   - Change 123 back to '123'\n");
149   xbt_dict_set(head,"123",strdup("123"),&free);
150   printf("   - Change 12a to 'Dummy 12a'\n");
151   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
152   printf("   - Change 12a to '12a'\n");
153   xbt_dict_set(head,"12a",strdup("12a"),&free);
154
155   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
156   printf(" - Traverse the resulting dictionnary\n");
157   traverse(head);
158
159   printf(" - Retrive values\n");
160   data = xbt_dict_get(head,"123");
161   xbt_assert(data);
162   strcmp("123",data);
163
164   search_not_found(head,"Can't be found");
165   search_not_found(head,"123 Can't be found");
166   search_not_found(head,"12345678 NOT");
167
168   search(head,"12a");
169   search(head,"12b");
170   search(head,"12");
171   search(head,"123456");
172   search(head,"1234");
173   search(head,"123457");
174
175   printf(" - Traverse the resulting dictionnary\n");
176   traverse(head);
177
178   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
179
180   printf(" Free the dictionnary twice\n");
181   xbt_dict_free(&head);
182   xbt_dict_free(&head);
183
184   printf(" - Traverse the resulting dictionnary\n");
185   traverse(head);
186
187   printf("\n");
188   fill(&head);
189   printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
190   TRY {
191     debuged_remove(head,"Does not exist");
192   } CATCH(e) {
193     if (e.category != not_found_error) 
194       RETHROW;
195     xbt_ex_free(e);
196   }
197   traverse(head);
198
199   xbt_dict_free(&head);
200
201   printf(" - Remove data from the NULL dict (error message expected)\n");
202   TRY {
203     debuged_remove(head,"12345");
204   } CATCH(e) {
205     if (e.category != arg_error) 
206       RETHROW;
207     xbt_ex_free(e);
208   } 
209
210   printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
211   fill(&head);
212   debuged_remove(head,"12a");    traverse(head);
213   debuged_remove(head,"12b");    traverse(head);
214   debuged_remove(head,"12");     traverse(head);
215   debuged_remove(head,"123456"); traverse(head);
216   TRY {
217     debuged_remove(head,"12346");
218   } CATCH(e) {
219     if (e.category != not_found_error) 
220       RETHROW;
221     xbt_ex_free(e);              traverse(head);
222   } 
223   debuged_remove(head,"1234");   traverse(head);
224   debuged_remove(head,"123457"); traverse(head);
225   debuged_remove(head,"123");    traverse(head);
226   TRY {
227     debuged_remove(head,"12346");
228   } CATCH(e) {
229     if (e.category != not_found_error) 
230       RETHROW;
231     xbt_ex_free(e);
232   }                              traverse(head);
233   
234   printf(" - Free the dictionnary twice\n");
235   xbt_dict_free(&head);
236   xbt_dict_free(&head);
237   xbt_exit();
238   return 0;
239 }