Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Hash_Multi_Map_Manager_Test.cpp
blob8117f5763b7e05fc16fddb9788d827498c3cf3b9
2 //=============================================================================
3 /**
4 * @file Hash_Multi_Map_Manager_Test.cpp
6 * This test illustrates the use of <ACE_Hash_Multi_Map_Manager> to
7 * maintain a hash table using strings.
9 * @author Shanshan Jiang <shanshan.jiang@vanderbilt.edu>
11 //=============================================================================
14 #include "test_config.h"
15 #include "ace/Hash_Multi_Map_Manager_T.h"
16 #include "ace/Null_Mutex.h"
20 typedef ACE_Hash_Multi_Map_Manager<const ACE_TCHAR *,
21 const ACE_TCHAR *,
22 ACE_Hash<const ACE_TCHAR *>,
23 ACE_Equal_To<const ACE_TCHAR *>,
24 ACE_Null_Mutex> HASH_STRING_MAP;
26 typedef HASH_STRING_MAP::ENTRY HASH_STRING_ENTRY;
27 typedef HASH_STRING_ENTRY::VALUE_SET HASH_STRING_VALUE_SET;
28 typedef HASH_STRING_ENTRY::VALUE_SET_ITERATOR HASH_STRING_VALUE_SET_ITERATOR;
29 typedef HASH_STRING_MAP::ITERATOR HASH_STRING_ITERATOR;
30 typedef HASH_STRING_MAP::CONST_ITERATOR HASH_STRING_CONST_ITERATOR;
31 typedef HASH_STRING_MAP::REVERSE_ITERATOR HASH_STRING_REVERSE_ITERATOR;
33 static const ACE_TCHAR *color_sorts[] = {ACE_TEXT ("warm"), ACE_TEXT ("cold"), ACE_TEXT ("neutral"), 0};
34 static const ACE_TCHAR *warm_colors[] = {ACE_TEXT ("red"), ACE_TEXT ("yellow"), ACE_TEXT ("orange"), 0};
35 static const ACE_TCHAR *cold_colors[] = {ACE_TEXT ("blue"), ACE_TEXT ("cyan"), 0};
36 static const ACE_TCHAR *neutral_colors[] = {ACE_TEXT ("green"), ACE_TEXT ("purple"), 0};
38 static int
39 run_test (void)
41 HASH_STRING_MAP colors;
43 size_t i;
45 // Check the <bind> operation.
46 for (i = 0; warm_colors[i] != 0; i++)
48 if (colors.bind (color_sorts[0],
49 warm_colors[i]) == -1)
50 ACE_ERROR_RETURN ((LM_ERROR,
51 ACE_TEXT ("bind failed for %s in %s\n"),
52 warm_colors[i],
53 color_sorts[0]),
54 -1);
57 HASH_STRING_ENTRY *entry = 0;
58 for (i = 0; cold_colors[i] != 0; i++)
60 if (colors.bind (color_sorts[1],
61 cold_colors[i],
62 entry) == -1)
63 ACE_ERROR_RETURN ((LM_ERROR,
64 ACE_TEXT ("bind failed for %s in %s\n"),
65 cold_colors[i],
66 color_sorts[1]),
67 -1);
70 HASH_STRING_VALUE_SET value_set;
71 entry = 0;
72 for (i = 0; neutral_colors[i] != 0; i++)
73 value_set.insert (neutral_colors[i]);
74 if (colors.bind (color_sorts[2],
75 value_set,
76 entry) == -1)
77 ACE_ERROR_RETURN ((LM_ERROR,
78 ACE_TEXT ("bind failed for %s\n"),
79 color_sorts[2]),
80 -1);
82 // Check the <trybind> operation.
83 value_set.reset ();
84 if (colors.trybind (color_sorts[2],
85 value_set) == -1)
86 ACE_ERROR_RETURN ((LM_ERROR,
87 ACE_TEXT ("bind failed for %s\n"),
88 color_sorts[2]),
89 -1);
91 value_set.reset ();
92 entry = 0;
93 if (colors.trybind (color_sorts[2],
94 value_set,
95 entry) == -1)
96 ACE_ERROR_RETURN ((LM_ERROR,
97 ACE_TEXT ("bind failed for %s\n"),
98 color_sorts[2]),
99 -1);
101 value_set.reset ();
102 entry = 0;
103 if (colors.trybind (color_sorts[2],
104 value_set,
105 entry) == -1)
106 ACE_ERROR_RETURN ((LM_ERROR,
107 ACE_TEXT ("bind failed for %s\n"),
108 color_sorts[2]),
109 -1);
111 // Check the <rebind> operation.
112 value_set.reset ();
113 value_set.insert (neutral_colors[0]);
114 if (colors.rebind (color_sorts[2],
115 value_set) == -1)
116 ACE_ERROR_RETURN ((LM_ERROR,
117 ACE_TEXT ("bind failed for %s\n"),
118 color_sorts[2]),
119 -1);
121 value_set.reset ();
122 value_set.insert (neutral_colors[1]);
123 entry = 0;
124 if (colors.rebind (color_sorts[2],
125 value_set,
126 entry) == -1)
127 ACE_ERROR_RETURN ((LM_ERROR,
128 ACE_TEXT ("bind failed for %s\n"),
129 color_sorts[2]),
130 -1);
132 value_set.reset ();
133 value_set.insert (neutral_colors[0]);
134 HASH_STRING_VALUE_SET old_value_set;
135 if (colors.rebind (color_sorts[2],
136 value_set,
137 old_value_set) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR,
139 ACE_TEXT ("bind failed for %s\n"),
140 color_sorts[2]),
141 -1);
143 value_set.reset ();
144 value_set.insert (neutral_colors[1]);
145 old_value_set.reset ();
146 entry = 0;
147 if (colors.rebind (color_sorts[2],
148 value_set,
149 old_value_set,
150 entry) == -1)
151 ACE_ERROR_RETURN ((LM_ERROR,
152 ACE_TEXT ("bind failed for %s\n"),
153 color_sorts[2]),
154 -1);
156 value_set.reset ();
157 value_set.insert (neutral_colors[0]);
158 const ACE_TCHAR *old_key;
159 old_value_set.reset ();
160 if (colors.rebind (color_sorts[2],
161 value_set,
162 old_key,
163 old_value_set) == -1)
164 ACE_ERROR_RETURN ((LM_ERROR,
165 ACE_TEXT ("bind failed for %s\n"),
166 color_sorts[2]),
167 -1);
169 value_set.reset ();
170 for (i = 0; neutral_colors[i] != 0; i++)
171 value_set.insert (neutral_colors[i]);
172 old_key = 0;
173 old_value_set.reset ();
174 entry = 0;
175 if (colors.rebind (color_sorts[2],
176 value_set,
177 old_key,
178 old_value_set,
179 entry) == -1)
180 ACE_ERROR_RETURN ((LM_ERROR,
181 ACE_TEXT ("bind failed for %s\n"),
182 color_sorts[2]),
183 -1);
185 // Check the <find> operation.
186 for (i = 0; color_sorts[i] != 0; i++)
187 if (colors.find (color_sorts[i]) == 0)
188 ACE_DEBUG ((LM_DEBUG,
189 ACE_TEXT ("%s found\n"),
190 color_sorts[i]));
191 else
192 ACE_ERROR_RETURN ((LM_ERROR,
193 ACE_TEXT ("%s not found\n"),
194 color_sorts[i]),
195 -1);
197 entry = 0;
198 for (i = 0; color_sorts[i] != 0; i++)
199 if (colors.find (color_sorts[i],
200 entry) == 0)
201 ACE_DEBUG ((LM_DEBUG,
202 ACE_TEXT ("%s found\n"),
203 color_sorts[i]));
204 else
205 ACE_ERROR_RETURN ((LM_ERROR,
206 ACE_TEXT ("%s not found\n"),
207 color_sorts[i]),
208 -1);
210 value_set.reset ();
211 for (i = 0; color_sorts[i] != 0; i++)
212 if (colors.find (color_sorts[i],
213 value_set) == 0)
214 ACE_DEBUG ((LM_DEBUG,
215 ACE_TEXT ("%s found\n"),
216 color_sorts[i]));
217 else
218 ACE_ERROR_RETURN ((LM_ERROR,
219 ACE_TEXT ("%s not found\n"),
220 color_sorts[i]),
221 -1);
223 for (i = 0; warm_colors[i] != 0; i++)
224 if (colors.find (color_sorts[0],
225 warm_colors[i]) == 0)
226 ACE_DEBUG ((LM_DEBUG,
227 ACE_TEXT ("%s color %s found\n"),
228 color_sorts[0],
229 warm_colors[i]));
230 else
231 ACE_ERROR_RETURN ((LM_ERROR,
232 ACE_TEXT ("%s color %s not found\n"),
233 color_sorts[0],
234 warm_colors[i]),
235 -1);
237 // Check the iterator.
238 i = 0;
239 entry = 0;
240 for (HASH_STRING_ITERATOR hash_iter (colors);
241 hash_iter.next (entry) != 0;
242 hash_iter.advance ())
244 ACE_DEBUG ((LM_DEBUG,
245 ACE_TEXT ("iterating (%d): %s:\n"),
247 entry->key ()));
248 i++;
250 for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->item ());
251 iter != entry->item ().end ();
252 iter++)
254 ACE_DEBUG ((LM_DEBUG,
255 ACE_TEXT ("%s \n"),
256 *iter));
260 // Check the unbind operation.
261 if (colors.unbind (color_sorts[0]) == -1)
262 ACE_ERROR_RETURN ((LM_ERROR,
263 ACE_TEXT ("unbind failed for %s\n"),
264 color_sorts[1]),
265 -1);
266 for (i = 0; warm_colors[i] != 0; i++)
268 if (colors.bind (color_sorts[0],
269 warm_colors[i]) == -1)
270 ACE_ERROR_RETURN ((LM_ERROR,
271 ACE_TEXT ("bind failed for %s in %s\n"),
272 warm_colors[i],
273 color_sorts[0]),
274 -1);
277 value_set.reset ();
278 if (colors.unbind (color_sorts[0], value_set) == -1)
279 ACE_ERROR_RETURN ((LM_ERROR,
280 ACE_TEXT ("unbind failed for %s\n"),
281 color_sorts[1]),
282 -1);
283 for (i = 0; warm_colors[i] != 0; i++)
285 if (colors.bind (color_sorts[0],
286 warm_colors[i]) == -1)
287 ACE_ERROR_RETURN ((LM_ERROR,
288 ACE_TEXT ("bind failed for %s in %s\n"),
289 warm_colors[i],
290 color_sorts[0]),
291 -1);
294 for (i = 0; warm_colors[i] != 0; i++)
296 if (colors.unbind (color_sorts[0],
297 warm_colors[i]) == -1)
298 ACE_ERROR_RETURN ((LM_ERROR,
299 ACE_TEXT ("unbind failed for %s in %s\n"),
300 warm_colors[i],
301 color_sorts[0]),
302 -1);
305 // Check the reverse iterator.
306 i = 0;
307 entry = 0;
308 for (HASH_STRING_REVERSE_ITERATOR hash_iter (colors);
309 hash_iter.next (entry) != 0;
310 hash_iter.advance ())
312 ACE_DEBUG ((LM_DEBUG,
313 ACE_TEXT ("reverse iterating (%d): %s\n"),
315 entry->key ()));
316 i++;
318 for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->item ());
319 iter != entry->item ().end ();
320 iter++)
322 ACE_DEBUG ((LM_DEBUG,
323 ACE_TEXT ("%s \n"),
324 *iter));
328 entry = 0;
329 colors.find (color_sorts[1], entry);
330 if (colors.unbind (entry) == -1)
331 ACE_ERROR_RETURN ((LM_ERROR,
332 ACE_TEXT ("unbind failed for %s\n"),
333 color_sorts[1]),
334 -1);
336 // Check the const iterator.
337 i = 0;
338 entry = 0;
339 for (HASH_STRING_CONST_ITERATOR hash_iter (colors);
340 hash_iter.next (entry) != 0;
341 hash_iter.advance ())
343 ACE_DEBUG ((LM_DEBUG,
344 ACE_TEXT ("const iterating (%d): %s\n"),
346 entry->key ()));
347 i++;
349 for (HASH_STRING_VALUE_SET_ITERATOR iter (entry->item ());
350 iter != entry->item ().end ();
351 iter++)
353 ACE_DEBUG ((LM_DEBUG,
354 ACE_TEXT ("%s \n"),
355 *iter));
359 // Check the unbind_all operation.
360 if (colors.unbind_all () != 0)
361 ACE_ERROR_RETURN ((LM_ERROR,
362 ACE_TEXT ("unbind_all failed\n")),
363 -1);
365 return 0;
369 run_main (int, ACE_TCHAR *[])
371 ACE_START_TEST (ACE_TEXT ("Hash_Multi_Map_Manager_Test"));
373 run_test ();
375 ACE_END_TEST;
377 return 0;