Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
3bbff8fdcd96d46f77dfe7740c03197cf569c956
[simgrid.git] / testsuite / xbt / dict_usage.c
1 /* $Id$ */
2
3 /* dict_usage - A test of normal usage of a dictionnary                     */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <assert.h>
13
14 #include <gras.h>
15
16 GRAS_LOG_EXTERNAL_CATEGORY(dict);
17
18 static gras_error_t fill(gras_dict_t **head);
19 static gras_error_t debuged_add(gras_dict_t *head,const char*key);
20 static gras_error_t search(gras_dict_t *head,const char*key);
21 static gras_error_t debuged_remove(gras_dict_t *head,const char*key);
22 static gras_error_t traverse(gras_dict_t *head);
23
24 static void print_str(void *str);
25 static void print_str(void *str) {
26   printf("%s",(char*)str);
27 }
28
29 static gras_error_t fill(gras_dict_t **head) {
30   gras_error_t errcode;
31   printf("\n Fill in the dictionnary\n");
32
33   TRY(gras_dict_new(head));
34   TRY(debuged_add(*head,"12"));
35   TRY(debuged_add(*head,"12a"));
36   TRY(debuged_add(*head,"12b"));
37   TRY(debuged_add(*head,"123"));
38   TRY(debuged_add(*head,"123456"));
39   // Child becomes child of what to add
40   TRY(debuged_add(*head,"1234"));
41   // Need of common ancestor
42   TRY(debuged_add(*head,"123457"));
43
44   return no_error;
45 }
46
47 static gras_error_t debuged_add(gras_dict_t *head,const char*key)
48 {
49   gras_error_t errcode;
50   char *data=strdup(key);
51
52   printf("   - Add %s\n",key);
53   errcode=gras_dict_set(head,key,data,&free);
54   if (GRAS_LOG_ISENABLED(dict,gras_log_priority_debug)) {
55     gras_dict_dump(head,(void (*)(void*))&printf);
56     fflush(stdout);
57   }
58   return errcode;
59 }
60
61 static gras_error_t search(gras_dict_t *head,const char*key) {
62   void *data;
63   gras_error_t errcode;
64
65   
66   errcode=gras_dict_get(head,key,&data);
67   printf("   - Search %s. Found %s\n",key,data?(char*)data:"(null)");fflush(stdout);
68   if (strcmp((char*)data,key)) 
69     return mismatch_error;
70   return errcode;
71 }
72
73 static gras_error_t debuged_remove(gras_dict_t *head,const char*key)
74 {
75   gras_error_t errcode;
76
77   printf("   Remove '%s'\n",key);fflush(stdout);
78   errcode=gras_dict_remove(head,key);
79   //  gras_dict_dump(head,(void (*)(void*))&printf);
80   return errcode;
81 }
82
83
84 static gras_error_t traverse(gras_dict_t *head) {
85   gras_dict_cursor_t *cursor=NULL;
86   char *key;
87   char *data;
88
89   gras_dict_foreach(head,cursor,key,data) {
90     printf("   - Seen:  %s->%s\n",key,data);
91     if (strcmp(key,data)) {
92       printf("Key(%s) != value(%s). Abording\n",key,data);
93       abort();
94     }
95   }
96   return no_error;
97 }
98
99 int main(int argc,char **argv) {
100   gras_error_t errcode;
101   gras_dict_t *head=NULL;
102   char *data;
103
104   gras_init_defaultlog(&argc,argv,"dict.thresh=verbose");
105    
106   printf("\nGeneric dictionnary: USAGE test:\n");
107
108   printf(" Traverse the empty dictionnary\n");
109   TRYFAIL(traverse(head));
110
111   TRYFAIL(fill(&head));
112   printf(" Free the dictionnary\n");
113   gras_dict_free(&head);
114   printf(" Free the dictionnary again\n");
115   gras_dict_free(&head);
116   
117   TRYFAIL(fill(&head));
118
119   printf(" - Change some values\n");
120   printf("   - Change 123 to 'Changed 123'\n");
121   TRYFAIL(gras_dict_set(head,"123",strdup("Changed 123"),&free));
122   printf("   - Change 123 back to '123'\n");
123   TRYFAIL(gras_dict_set(head,"123",strdup("123"),&free));
124   printf("   - Change 12a to 'Dummy 12a'\n");
125   TRYFAIL(gras_dict_set(head,"12a",strdup("Dummy 12a"),&free));
126   printf("   - Change 12a to '12a'\n");
127   TRYFAIL(gras_dict_set(head,"12a",strdup("12a"),&free));
128
129   //  gras_dict_dump(head,(void (*)(void*))&printf);
130   printf(" - Traverse the resulting dictionnary\n");
131   TRYFAIL(traverse(head));
132
133   printf(" - Retrive values\n");
134   TRYFAIL(gras_dict_get(head,"123",(void**)&data));
135   assert(data);
136   TRYFAIL(strcmp("123",data));
137
138   TRYEXPECT(gras_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
139   TRYEXPECT(gras_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
140   TRYEXPECT(gras_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
141
142   TRYFAIL(search(head,"12a"));
143   TRYFAIL(search(head,"12b"));
144   TRYFAIL(search(head,"12"));
145   TRYFAIL(search(head,"123456"));
146   TRYFAIL(search(head,"1234"));
147   TRYFAIL(search(head,"123457"));
148
149   printf(" - Traverse the resulting dictionnary\n");
150   TRYFAIL(traverse(head));
151
152   //  gras_dict_dump(head,(void (*)(void*))&printf);
153
154   printf(" Free the dictionnary (twice)\n");
155   gras_dict_free(&head);
156   gras_dict_free(&head);
157
158   printf(" - Traverse the resulting dictionnary\n");
159   TRYFAIL(traverse(head));
160
161   printf("\n");
162   TRYFAIL(fill(&head));
163   printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
164   TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
165   TRYFAIL(traverse(head));
166
167   gras_dict_free(&head);
168
169   printf(" - Remove data from the NULL dict (error message expected)\n");
170   TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
171
172   printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
173   TRYFAIL(fill(&head));
174   TRYFAIL(debuged_remove(head,"12a"));    TRYFAIL(traverse(head));
175   TRYFAIL(debuged_remove(head,"12b"));    TRYFAIL(traverse(head));
176   TRYFAIL(debuged_remove(head,"12"));     TRYFAIL(traverse(head));
177   TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
178   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
179   TRYFAIL(debuged_remove(head,"1234"));   TRYFAIL(traverse(head));
180   TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
181   TRYFAIL(debuged_remove(head,"123"));    TRYFAIL(traverse(head));
182   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
183   
184   printf(" - Free the dictionnary twice\n");
185   gras_dict_free(&head);
186   gras_dict_free(&head);
187   gras_exit();
188   return 0;
189 }