Logo AND Algorithmique Numérique Distribuée

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