Logo AND Algorithmique Numérique Distribuée

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