Logo AND Algorithmique Numérique Distribuée

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