2 //=============================================================================
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"
19 using HASH_STRING_MAP
= ACE_Hash_Multi_Map_Manager
<const ACE_TCHAR
*, const ACE_TCHAR
*, ACE_Hash
<const ACE_TCHAR
*>, ACE_Equal_To
<const ACE_TCHAR
*>, ACE_Null_Mutex
>;
21 using HASH_STRING_ENTRY
= HASH_STRING_MAP::ENTRY
;
22 using HASH_STRING_VALUE_SET
= HASH_STRING_ENTRY::VALUE_SET
;
23 using HASH_STRING_VALUE_SET_ITERATOR
= HASH_STRING_ENTRY::VALUE_SET_ITERATOR
;
24 using HASH_STRING_ITERATOR
= HASH_STRING_MAP::ITERATOR
;
25 using HASH_STRING_CONST_ITERATOR
= HASH_STRING_MAP::CONST_ITERATOR
;
26 using HASH_STRING_REVERSE_ITERATOR
= HASH_STRING_MAP::REVERSE_ITERATOR
;
28 static const ACE_TCHAR
*color_sorts
[] = {ACE_TEXT ("warm"), ACE_TEXT ("cold"), ACE_TEXT ("neutral"), 0};
29 static const ACE_TCHAR
*warm_colors
[] = {ACE_TEXT ("red"), ACE_TEXT ("yellow"), ACE_TEXT ("orange"), 0};
30 static const ACE_TCHAR
*cold_colors
[] = {ACE_TEXT ("blue"), ACE_TEXT ("cyan"), 0};
31 static const ACE_TCHAR
*neutral_colors
[] = {ACE_TEXT ("green"), ACE_TEXT ("purple"), 0};
36 HASH_STRING_MAP colors
;
40 // Check the <bind> operation.
41 for (i
= 0; warm_colors
[i
] != 0; i
++)
43 if (colors
.bind (color_sorts
[0],
44 warm_colors
[i
]) == -1)
45 ACE_ERROR_RETURN ((LM_ERROR
,
46 ACE_TEXT ("bind failed for %s in %s\n"),
52 HASH_STRING_ENTRY
*entry
= 0;
53 for (i
= 0; cold_colors
[i
] != 0; i
++)
55 if (colors
.bind (color_sorts
[1],
58 ACE_ERROR_RETURN ((LM_ERROR
,
59 ACE_TEXT ("bind failed for %s in %s\n"),
65 HASH_STRING_VALUE_SET value_set
;
67 for (i
= 0; neutral_colors
[i
] != 0; i
++)
68 value_set
.insert (neutral_colors
[i
]);
69 if (colors
.bind (color_sorts
[2],
72 ACE_ERROR_RETURN ((LM_ERROR
,
73 ACE_TEXT ("bind failed for %s\n"),
77 // Check the <trybind> operation.
79 if (colors
.trybind (color_sorts
[2],
81 ACE_ERROR_RETURN ((LM_ERROR
,
82 ACE_TEXT ("bind failed for %s\n"),
88 if (colors
.trybind (color_sorts
[2],
91 ACE_ERROR_RETURN ((LM_ERROR
,
92 ACE_TEXT ("bind failed for %s\n"),
98 if (colors
.trybind (color_sorts
[2],
101 ACE_ERROR_RETURN ((LM_ERROR
,
102 ACE_TEXT ("bind failed for %s\n"),
106 // Check the <rebind> operation.
108 value_set
.insert (neutral_colors
[0]);
109 if (colors
.rebind (color_sorts
[2],
111 ACE_ERROR_RETURN ((LM_ERROR
,
112 ACE_TEXT ("bind failed for %s\n"),
117 value_set
.insert (neutral_colors
[1]);
119 if (colors
.rebind (color_sorts
[2],
122 ACE_ERROR_RETURN ((LM_ERROR
,
123 ACE_TEXT ("bind failed for %s\n"),
128 value_set
.insert (neutral_colors
[0]);
129 HASH_STRING_VALUE_SET old_value_set
;
130 if (colors
.rebind (color_sorts
[2],
132 old_value_set
) == -1)
133 ACE_ERROR_RETURN ((LM_ERROR
,
134 ACE_TEXT ("bind failed for %s\n"),
139 value_set
.insert (neutral_colors
[1]);
140 old_value_set
.reset ();
142 if (colors
.rebind (color_sorts
[2],
146 ACE_ERROR_RETURN ((LM_ERROR
,
147 ACE_TEXT ("bind failed for %s\n"),
152 value_set
.insert (neutral_colors
[0]);
153 const ACE_TCHAR
*old_key
;
154 old_value_set
.reset ();
155 if (colors
.rebind (color_sorts
[2],
158 old_value_set
) == -1)
159 ACE_ERROR_RETURN ((LM_ERROR
,
160 ACE_TEXT ("bind failed for %s\n"),
165 for (i
= 0; neutral_colors
[i
] != 0; i
++)
166 value_set
.insert (neutral_colors
[i
]);
168 old_value_set
.reset ();
170 if (colors
.rebind (color_sorts
[2],
175 ACE_ERROR_RETURN ((LM_ERROR
,
176 ACE_TEXT ("bind failed for %s\n"),
180 // Check the <find> operation.
181 for (i
= 0; color_sorts
[i
] != 0; i
++)
182 if (colors
.find (color_sorts
[i
]) == 0)
183 ACE_DEBUG ((LM_DEBUG
,
184 ACE_TEXT ("%s found\n"),
187 ACE_ERROR_RETURN ((LM_ERROR
,
188 ACE_TEXT ("%s not found\n"),
193 for (i
= 0; color_sorts
[i
] != 0; i
++)
194 if (colors
.find (color_sorts
[i
],
196 ACE_DEBUG ((LM_DEBUG
,
197 ACE_TEXT ("%s found\n"),
200 ACE_ERROR_RETURN ((LM_ERROR
,
201 ACE_TEXT ("%s not found\n"),
206 for (i
= 0; color_sorts
[i
] != 0; i
++)
207 if (colors
.find (color_sorts
[i
],
209 ACE_DEBUG ((LM_DEBUG
,
210 ACE_TEXT ("%s found\n"),
213 ACE_ERROR_RETURN ((LM_ERROR
,
214 ACE_TEXT ("%s not found\n"),
218 for (i
= 0; warm_colors
[i
] != 0; i
++)
219 if (colors
.find (color_sorts
[0],
220 warm_colors
[i
]) == 0)
221 ACE_DEBUG ((LM_DEBUG
,
222 ACE_TEXT ("%s color %s found\n"),
226 ACE_ERROR_RETURN ((LM_ERROR
,
227 ACE_TEXT ("%s color %s not found\n"),
232 // Check the iterator.
235 for (HASH_STRING_ITERATOR
hash_iter (colors
);
236 hash_iter
.next (entry
) != 0;
237 hash_iter
.advance ())
239 ACE_DEBUG ((LM_DEBUG
,
240 ACE_TEXT ("iterating (%d): %s:\n"),
245 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
246 iter
!= entry
->item ().end ();
249 ACE_DEBUG ((LM_DEBUG
,
255 // Check the unbind operation.
256 if (colors
.unbind (color_sorts
[0]) == -1)
257 ACE_ERROR_RETURN ((LM_ERROR
,
258 ACE_TEXT ("unbind failed for %s\n"),
261 for (i
= 0; warm_colors
[i
] != 0; i
++)
263 if (colors
.bind (color_sorts
[0],
264 warm_colors
[i
]) == -1)
265 ACE_ERROR_RETURN ((LM_ERROR
,
266 ACE_TEXT ("bind failed for %s in %s\n"),
273 if (colors
.unbind (color_sorts
[0], value_set
) == -1)
274 ACE_ERROR_RETURN ((LM_ERROR
,
275 ACE_TEXT ("unbind failed for %s\n"),
278 for (i
= 0; warm_colors
[i
] != 0; i
++)
280 if (colors
.bind (color_sorts
[0],
281 warm_colors
[i
]) == -1)
282 ACE_ERROR_RETURN ((LM_ERROR
,
283 ACE_TEXT ("bind failed for %s in %s\n"),
289 for (i
= 0; warm_colors
[i
] != 0; i
++)
291 if (colors
.unbind (color_sorts
[0],
292 warm_colors
[i
]) == -1)
293 ACE_ERROR_RETURN ((LM_ERROR
,
294 ACE_TEXT ("unbind failed for %s in %s\n"),
300 // Check the reverse iterator.
303 for (HASH_STRING_REVERSE_ITERATOR
hash_iter (colors
);
304 hash_iter
.next (entry
) != 0;
305 hash_iter
.advance ())
307 ACE_DEBUG ((LM_DEBUG
,
308 ACE_TEXT ("reverse iterating (%d): %s\n"),
313 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
314 iter
!= entry
->item ().end ();
317 ACE_DEBUG ((LM_DEBUG
,
324 colors
.find (color_sorts
[1], entry
);
325 if (colors
.unbind (entry
) == -1)
326 ACE_ERROR_RETURN ((LM_ERROR
,
327 ACE_TEXT ("unbind failed for %s\n"),
331 // Check the const iterator.
334 for (HASH_STRING_CONST_ITERATOR
hash_iter (colors
);
335 hash_iter
.next (entry
) != 0;
336 hash_iter
.advance ())
338 ACE_DEBUG ((LM_DEBUG
,
339 ACE_TEXT ("const iterating (%d): %s\n"),
344 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
345 iter
!= entry
->item ().end ();
348 ACE_DEBUG ((LM_DEBUG
,
354 // Check the unbind_all operation.
355 if (colors
.unbind_all () != 0)
356 ACE_ERROR_RETURN ((LM_ERROR
,
357 ACE_TEXT ("unbind_all failed\n")),
364 run_main (int, ACE_TCHAR
*[])
366 ACE_START_TEST (ACE_TEXT ("Hash_Multi_Map_Manager_Test"));