Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
regenerate after modification to exchange dynars
[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
15 XBT_LOG_EXTERNAL_CATEGORY(dict);
16 XBT_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
17
18 static void fill(xbt_dict_t *head);
19 static void debuged_add(xbt_dict_t head,const char*key);
20 static xbt_error_t search(xbt_dict_t head,const char*key);
21 static xbt_error_t debuged_remove(xbt_dict_t head,const char*key);
22 static xbt_error_t traverse(xbt_dict_t head);
23
24 static void print_str(void *str);
25 static void print_str(void *str) {
26   printf("%s",(char*)PRINTF_STR(str));
27 }
28
29 static void fill(xbt_dict_t *head) {
30   printf("\n Fill in the dictionnary\n");
31
32   *head = xbt_dict_new();
33   debuged_add(*head,"12");
34   debuged_add(*head,"12a");
35   debuged_add(*head,"12b");
36   debuged_add(*head,"123");
37   debuged_add(*head,"123456");
38   /* Child becomes child of what to add */
39   debuged_add(*head,"1234");
40   /* Need of common ancestor */
41   debuged_add(*head,"123457");
42
43 }
44
45 static void debuged_add(xbt_dict_t head,const char*key)
46 {
47   char *data=xbt_strdup(key);
48
49   printf("   - Add %s\n",PRINTF_STR(key));
50   xbt_dict_set(head,key,data,&free);
51   if (XBT_LOG_ISENABLED(dict,xbt_log_priority_debug)) {
52     xbt_dict_dump(head,(void (*)(void*))&printf);
53     fflush(stdout);
54   }
55 }
56
57 static xbt_error_t search(xbt_dict_t head,const char*key) {
58   void *data;
59   xbt_error_t errcode;
60
61   
62   errcode=xbt_dict_get(head,key,&data);
63   printf("   - Search %s. Found %s\n",PRINTF_STR(key),(char*) PRINTF_STR(data));fflush(stdout);
64   if (!data)
65      return errcode;
66   if (strcmp((char*)data,key)) 
67     return mismatch_error;
68   return errcode;
69 }
70
71 static xbt_error_t debuged_remove(xbt_dict_t head,const char*key)
72 {
73   xbt_error_t errcode;
74
75   printf("   Remove '%s'\n",PRINTF_STR(key));fflush(stdout);
76   errcode=xbt_dict_remove(head,key);
77   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
78   return errcode;
79 }
80
81
82 static xbt_error_t traverse(xbt_dict_t head) {
83   xbt_dict_cursor_t cursor=NULL;
84   char *key;
85   char *data;
86
87   xbt_dict_foreach(head,cursor,key,data) {
88     printf("   - Seen:  %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));
89     xbt_assert2(!data || !strcmp(key,data),
90                  "Key(%s) != value(%s). Abording\n",key,data);
91   }
92   return no_error;
93 }
94
95 int main(int argc,char **argv) {
96   xbt_error_t errcode;
97   xbt_dict_t head=NULL;
98   char *data;
99
100   xbt_init_defaultlog(&argc,argv,"dict.thresh=verbose");
101    
102   printf("\nGeneric dictionnary: USAGE test:\n");
103
104   printf(" Traverse the empty dictionnary\n");
105   TRYFAIL(traverse(head));
106
107   fill(&head);
108   printf(" Free the dictionnary (twice)\n");
109   xbt_dict_free(&head);
110   xbt_dict_free(&head);
111   
112   fill(&head);
113
114   /* xbt_dict_dump(head,(void (*)(void*))&printf);*/
115   printf(" - Test that it works with NULL data\n");
116   printf("   - Store NULL under 'null'\n");fflush(stdout);
117   xbt_dict_set(head,"null",NULL,NULL);
118   TRYFAIL(search(head,"null"));
119   /* xbt_dict_dump(head,(void (*)(void*))&printf); */
120   printf("   Check whether I see it while traversing\n");fflush(stdout);
121   {
122      xbt_dict_cursor_t cursor=NULL;
123      char *key;
124      int found=0;
125      
126      xbt_dict_foreach(head,cursor,key,data) {
127         printf("   - Seen:  %s->%s\n",PRINTF_STR(key),PRINTF_STR(data));fflush(stdout);
128         if (!strcmp(key,"null"))
129           found = 1;
130      }
131      xbt_assert0(found,"the key 'null', associated to NULL is not found");
132   }
133    
134   printf(" - Change some values\n");
135   printf("   - Change 123 to 'Changed 123'\n");
136   xbt_dict_set(head,"123",strdup("Changed 123"),&free);
137   printf("   - Change 123 back to '123'\n");
138   xbt_dict_set(head,"123",strdup("123"),&free);
139   printf("   - Change 12a to 'Dummy 12a'\n");
140   xbt_dict_set(head,"12a",strdup("Dummy 12a"),&free);
141   printf("   - Change 12a to '12a'\n");
142   xbt_dict_set(head,"12a",strdup("12a"),&free);
143
144   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
145   printf(" - Traverse the resulting dictionnary\n");
146   TRYFAIL(traverse(head));
147
148   printf(" - Retrive values\n");
149   TRYFAIL(xbt_dict_get(head,"123",(void**)&data));
150   xbt_assert(data);
151   TRYFAIL(strcmp("123",data));
152
153   TRYEXPECT(xbt_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
154   TRYEXPECT(xbt_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
155   TRYEXPECT(xbt_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
156
157   TRYFAIL(search(head,"12a"));
158   TRYFAIL(search(head,"12b"));
159   TRYFAIL(search(head,"12"));
160   TRYFAIL(search(head,"123456"));
161   TRYFAIL(search(head,"1234"));
162   TRYFAIL(search(head,"123457"));
163
164   printf(" - Traverse the resulting dictionnary\n");
165   TRYFAIL(traverse(head));
166
167   /*  xbt_dict_dump(head,(void (*)(void*))&printf); */
168
169   printf(" Free the dictionnary twice\n");
170   xbt_dict_free(&head);
171   xbt_dict_free(&head);
172
173   printf(" - Traverse the resulting dictionnary\n");
174   TRYFAIL(traverse(head));
175
176   printf("\n");
177   fill(&head);
178   printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
179   TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
180   TRYFAIL(traverse(head));
181
182   xbt_dict_free(&head);
183
184   printf(" - Remove data from the NULL dict (error message expected)\n");
185   TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
186
187   printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
188   fill(&head);
189   TRYFAIL(debuged_remove(head,"12a"));    TRYFAIL(traverse(head));
190   TRYFAIL(debuged_remove(head,"12b"));    TRYFAIL(traverse(head));
191   TRYFAIL(debuged_remove(head,"12"));     TRYFAIL(traverse(head));
192   TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
193   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
194   TRYFAIL(debuged_remove(head,"1234"));   TRYFAIL(traverse(head));
195   TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
196   TRYFAIL(debuged_remove(head,"123"));    TRYFAIL(traverse(head));
197   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
198   
199   printf(" - Free the dictionnary twice\n");
200   xbt_dict_free(&head);
201   xbt_dict_free(&head);
202   xbt_exit();
203   return 0;
204 }