Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Interface revolution: do not try to survive to malloc failure
[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 void fill(gras_dict_t **head);
20 static void 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 void fill(gras_dict_t **head) {
31   printf("\n Fill in the dictionnary\n");
32
33   gras_dict_new(head);
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(gras_dict_t *head,const char*key)
47 {
48   char *data=gras_strdup(key);
49
50   printf("   - Add %s\n",key);
51   gras_dict_set(head,key,data,&gras_free);
52   if (GRAS_LOG_ISENABLED(dict,gras_log_priority_debug)) {
53     gras_dict_dump(head,(void (*)(void*))&printf);
54     fflush(stdout);
55   }
56 }
57
58 static gras_error_t search(gras_dict_t *head,const char*key) {
59   void *data;
60   gras_error_t errcode;
61
62   
63   errcode=gras_dict_get(head,key,&data);
64   printf("   - Search %s. Found %s\n",key,data?(char*)data:"(null)");fflush(stdout);
65   if (strcmp((char*)data,key)) 
66     return mismatch_error;
67   return errcode;
68 }
69
70 static gras_error_t debuged_remove(gras_dict_t *head,const char*key)
71 {
72   gras_error_t errcode;
73
74   printf("   Remove '%s'\n",key);fflush(stdout);
75   errcode=gras_dict_remove(head,key);
76   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
77   return errcode;
78 }
79
80
81 static gras_error_t traverse(gras_dict_t *head) {
82   gras_dict_cursor_t *cursor=NULL;
83   char *key;
84   char *data;
85
86   gras_dict_foreach(head,cursor,key,data) {
87     printf("   - Seen:  %s->%s\n",key,data);
88     gras_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   gras_error_t errcode;
96   gras_dict_t *head=NULL;
97   char *data;
98
99   gras_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\n");
108   gras_dict_free(&head);
109   printf(" Free the dictionnary again\n");
110   gras_dict_free(&head);
111   
112   fill(&head);
113
114   printf(" - Change some values\n");
115   printf("   - Change 123 to 'Changed 123'\n");
116   gras_dict_set(head,"123",strdup("Changed 123"),&gras_free);
117   printf("   - Change 123 back to '123'\n");
118   gras_dict_set(head,"123",strdup("123"),&gras_free);
119   printf("   - Change 12a to 'Dummy 12a'\n");
120   gras_dict_set(head,"12a",strdup("Dummy 12a"),&gras_free);
121   printf("   - Change 12a to '12a'\n");
122   gras_dict_set(head,"12a",strdup("12a"),&gras_free);
123
124   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
125   printf(" - Traverse the resulting dictionnary\n");
126   TRYFAIL(traverse(head));
127
128   printf(" - Retrive values\n");
129   TRYFAIL(gras_dict_get(head,"123",(void**)&data));
130   assert(data);
131   TRYFAIL(strcmp("123",data));
132
133   TRYEXPECT(gras_dict_get(head,"Can't be found",(void**)&data),mismatch_error);
134   TRYEXPECT(gras_dict_get(head,"123 Can't be found",(void**)&data),mismatch_error);
135   TRYEXPECT(gras_dict_get(head,"12345678 NOT",(void**)&data),mismatch_error);
136
137   TRYFAIL(search(head,"12a"));
138   TRYFAIL(search(head,"12b"));
139   TRYFAIL(search(head,"12"));
140   TRYFAIL(search(head,"123456"));
141   TRYFAIL(search(head,"1234"));
142   TRYFAIL(search(head,"123457"));
143
144   printf(" - Traverse the resulting dictionnary\n");
145   TRYFAIL(traverse(head));
146
147   /*  gras_dict_dump(head,(void (*)(void*))&printf); */
148
149   printf(" Free the dictionnary (twice)\n");
150   gras_dict_free(&head);
151   gras_dict_free(&head);
152
153   printf(" - Traverse the resulting dictionnary\n");
154   TRYFAIL(traverse(head));
155
156   printf("\n");
157   fill(&head);
158   printf(" - Remove the data (traversing the resulting dictionnary each time)\n");
159   TRYEXPECT(debuged_remove(head,"Does not exist"),mismatch_error);
160   TRYFAIL(traverse(head));
161
162   gras_dict_free(&head);
163
164   printf(" - Remove data from the NULL dict (error message expected)\n");
165   TRYCATCH(debuged_remove(head,"12345"),mismatch_error);
166
167   printf(" - Remove each data manually (traversing the resulting dictionnary each time)\n");
168   fill(&head);
169   TRYFAIL(debuged_remove(head,"12a"));    TRYFAIL(traverse(head));
170   TRYFAIL(debuged_remove(head,"12b"));    TRYFAIL(traverse(head));
171   TRYFAIL(debuged_remove(head,"12"));     TRYFAIL(traverse(head));
172   TRYFAIL(debuged_remove(head,"123456")); TRYFAIL(traverse(head));
173   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
174   TRYFAIL(debuged_remove(head,"1234"));   TRYFAIL(traverse(head));
175   TRYFAIL(debuged_remove(head,"123457")); TRYFAIL(traverse(head));
176   TRYFAIL(debuged_remove(head,"123"));    TRYFAIL(traverse(head));
177   TRYEXPECT(debuged_remove(head,"12346"),mismatch_error);  TRYFAIL(traverse(head));
178   
179   printf(" - Free the dictionnary twice\n");
180   gras_dict_free(&head);
181   gras_dict_free(&head);
182   gras_exit();
183   return 0;
184 }