Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
58acd2bac44d5e213fe29411883e1f4dd1c6129c
[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 GRAS_LOG_NEW_DEFAULT_CATEGORY(test,"Logging specific to this test");
18
19 static gras_error_t fill(gras_dict_t **head);
20 static gras_error_t debuged_add(gras_dict_t *head,const char*key);
21 static gras_error_t search(gras_dict_t *head,const char*key);
22 static gras_error_t debuged_remove(gras_dict_t *head,const char*key);
23 static gras_error_t traverse(gras_dict_t *head);
24
25 static void print_str(void *str);
26 static void print_str(void *str) {
27   printf("%s",(char*)str);
28 }
29
30 static gras_error_t fill(gras_dict_t **head) {
31   gras_error_t errcode;
32   printf("\n Fill in the dictionnary\n");
33
34   TRY(gras_dict_new(head));
35   TRY(debuged_add(*head,"12"));
36   TRY(debuged_add(*head,"12a"));
37   TRY(debuged_add(*head,"12b"));
38   TRY(debuged_add(*head,"123"));
39   TRY(debuged_add(*head,"123456"));
40   // Child becomes child of what to add
41   TRY(debuged_add(*head,"1234"));
42   // Need of common ancestor
43   TRY(debuged_add(*head,"123457"));
44
45   return no_error;
46 }
47
48 static gras_error_t debuged_add(gras_dict_t *head,const char*key)
49 {
50   gras_error_t errcode;
51   char *data=strdup(key);
52
53   printf("   - Add %s\n",key);
54   errcode=gras_dict_set(head,key,data,&gras_free);
55   if (GRAS_LOG_ISENABLED(dict,gras_log_priority_debug)) {
56     gras_dict_dump(head,(void (*)(void*))&printf);
57     fflush(stdout);
58   }
59   return errcode;
60 }
61
62 static gras_error_t search(gras_dict_t *head,const char*key) {
63   void *data;
64   gras_error_t errcode;
65
66   
67   errcode=gras_dict_get(head,key,&data);
68   printf("   - Search %s. Found %s\n",key,data?(char*)data:"(null)");fflush(stdout);
69   if (strcmp((char*)data,key)) 
70     return mismatch_error;
71   return errcode;
72 }
73
74 static gras_error_t debuged_remove(gras_dict_t *head,const char*key)
75 {
76   gras_error_t errcode;
77
78   printf("   Remove '%s'\n",key);fflush(stdout);
79   errcode=gras_dict_remove(head,key);
80   //  gras_dict_dump(head,(void (*)(void*))&printf);
81   return errcode;
82 }
83
84
85 static gras_error_t traverse(gras_dict_t *head) {
86   gras_dict_cursor_t *cursor=NULL;
87   char *key;
88   char *data;
89
90   gras_dict_foreach(head,cursor,key,data) {
91     printf("   - Seen:  %s->%s\n",key,data);
92     gras_assert2(!strcmp(key,data),
93                  "Key(%s) != value(%s). Abording\n",key,data);
94   }
95   return no_error;
96 }
97
98 int main(int argc,char **argv) {
99   gras_error_t errcode;
100   gras_dict_t *head=NULL;
101   char *data;
102
103   gras_init_defaultlog(&argc,argv,"dict.thresh=verbose");
104    
105   printf("\nGeneric dictionnary: USAGE test:\n");
106
107   printf(" Traverse the empty dictionnary\n");
108   TRYFAIL(traverse(head));
109
110   TRYFAIL(fill(&head));
111   printf(" Free the dictionnary\n");
112   gras_dict_free(&head);
113   printf(" Free the dictionnary again\n");
114   gras_dict_free(&head);
115   
116   TRYFAIL(fill(&head));
117
118   printf(" - Change some values\n");
119   printf("   - Change 123 to 'Changed 123'\n");
120   TRYFAIL(gras_dict_set(head,"123",strdup("Changed 123"),&gras_free));
121   printf("   - Change 123 back to '123'\n");
122   TRYFAIL(gras_dict_set(head,"123",strdup("123"),&gras_free));
123   printf("   - Change 12a to 'Dummy 12a'\n");
124   TRYFAIL(gras_dict_set(head,"12a",strdup("Dummy 12a"),&gras_free));
125   printf("   - Change 12a to '12a'\n");
126   TRYFAIL(gras_dict_set(head,"12a",strdup("12a"),&gras_free));
127
128   //  gras_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(gras_dict_get(head,"123",(void**)&data));
134   assert(data);
135   TRYFAIL(strcmp("123",data));
136
137   TRYEXPECT(gras_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
138   TRYEXPECT(gras_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
139   TRYEXPECT(gras_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   //  gras_dict_dump(head,(void (*)(void*))&printf);
152
153   printf(" Free the dictionnary (twice)\n");
154   gras_dict_free(&head);
155   gras_dict_free(&head);
156
157   printf(" - Traverse the resulting dictionnary\n");
158   TRYFAIL(traverse(head));
159
160   printf("\n");
161   TRYFAIL(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   gras_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   TRYFAIL(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   gras_dict_free(&head);
185   gras_dict_free(&head);
186   gras_exit();
187   return 0;
188 }