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"
20 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
>;
22 using HASH_STRING_ENTRY
= HASH_STRING_MAP::ENTRY
;
23 using HASH_STRING_VALUE_SET
= HASH_STRING_ENTRY::VALUE_SET
;
24 using HASH_STRING_VALUE_SET_ITERATOR
= HASH_STRING_ENTRY::VALUE_SET_ITERATOR
;
25 using HASH_STRING_ITERATOR
= HASH_STRING_MAP::ITERATOR
;
26 using HASH_STRING_CONST_ITERATOR
= HASH_STRING_MAP::CONST_ITERATOR
;
27 using HASH_STRING_REVERSE_ITERATOR
= HASH_STRING_MAP::REVERSE_ITERATOR
;
29 static const ACE_TCHAR
*color_sorts
[] = {ACE_TEXT ("warm"), ACE_TEXT ("cold"), ACE_TEXT ("neutral"), 0};
30 static const ACE_TCHAR
*warm_colors
[] = {ACE_TEXT ("red"), ACE_TEXT ("yellow"), ACE_TEXT ("orange"), 0};
31 static const ACE_TCHAR
*cold_colors
[] = {ACE_TEXT ("blue"), ACE_TEXT ("cyan"), 0};
32 static const ACE_TCHAR
*neutral_colors
[] = {ACE_TEXT ("green"), ACE_TEXT ("purple"), 0};
37 HASH_STRING_MAP colors
;
41 // Check the <bind> operation.
42 for (i
= 0; warm_colors
[i
] != 0; i
++)
44 if (colors
.bind (color_sorts
[0],
45 warm_colors
[i
]) == -1)
46 ACE_ERROR_RETURN ((LM_ERROR
,
47 ACE_TEXT ("bind failed for %s in %s\n"),
53 HASH_STRING_ENTRY
*entry
= 0;
54 for (i
= 0; cold_colors
[i
] != 0; i
++)
56 if (colors
.bind (color_sorts
[1],
59 ACE_ERROR_RETURN ((LM_ERROR
,
60 ACE_TEXT ("bind failed for %s in %s\n"),
66 HASH_STRING_VALUE_SET value_set
;
68 for (i
= 0; neutral_colors
[i
] != 0; i
++)
69 value_set
.insert (neutral_colors
[i
]);
70 if (colors
.bind (color_sorts
[2],
73 ACE_ERROR_RETURN ((LM_ERROR
,
74 ACE_TEXT ("bind failed for %s\n"),
78 // Check the <trybind> operation.
80 if (colors
.trybind (color_sorts
[2],
82 ACE_ERROR_RETURN ((LM_ERROR
,
83 ACE_TEXT ("bind failed for %s\n"),
89 if (colors
.trybind (color_sorts
[2],
92 ACE_ERROR_RETURN ((LM_ERROR
,
93 ACE_TEXT ("bind failed for %s\n"),
99 if (colors
.trybind (color_sorts
[2],
102 ACE_ERROR_RETURN ((LM_ERROR
,
103 ACE_TEXT ("bind failed for %s\n"),
107 // Check the <rebind> operation.
109 value_set
.insert (neutral_colors
[0]);
110 if (colors
.rebind (color_sorts
[2],
112 ACE_ERROR_RETURN ((LM_ERROR
,
113 ACE_TEXT ("bind failed for %s\n"),
118 value_set
.insert (neutral_colors
[1]);
120 if (colors
.rebind (color_sorts
[2],
123 ACE_ERROR_RETURN ((LM_ERROR
,
124 ACE_TEXT ("bind failed for %s\n"),
129 value_set
.insert (neutral_colors
[0]);
130 HASH_STRING_VALUE_SET old_value_set
;
131 if (colors
.rebind (color_sorts
[2],
133 old_value_set
) == -1)
134 ACE_ERROR_RETURN ((LM_ERROR
,
135 ACE_TEXT ("bind failed for %s\n"),
140 value_set
.insert (neutral_colors
[1]);
141 old_value_set
.reset ();
143 if (colors
.rebind (color_sorts
[2],
147 ACE_ERROR_RETURN ((LM_ERROR
,
148 ACE_TEXT ("bind failed for %s\n"),
153 value_set
.insert (neutral_colors
[0]);
154 const ACE_TCHAR
*old_key
;
155 old_value_set
.reset ();
156 if (colors
.rebind (color_sorts
[2],
159 old_value_set
) == -1)
160 ACE_ERROR_RETURN ((LM_ERROR
,
161 ACE_TEXT ("bind failed for %s\n"),
166 for (i
= 0; neutral_colors
[i
] != 0; i
++)
167 value_set
.insert (neutral_colors
[i
]);
169 old_value_set
.reset ();
171 if (colors
.rebind (color_sorts
[2],
176 ACE_ERROR_RETURN ((LM_ERROR
,
177 ACE_TEXT ("bind failed for %s\n"),
181 // Check the <find> operation.
182 for (i
= 0; color_sorts
[i
] != 0; i
++)
183 if (colors
.find (color_sorts
[i
]) == 0)
184 ACE_DEBUG ((LM_DEBUG
,
185 ACE_TEXT ("%s found\n"),
188 ACE_ERROR_RETURN ((LM_ERROR
,
189 ACE_TEXT ("%s not found\n"),
194 for (i
= 0; color_sorts
[i
] != 0; i
++)
195 if (colors
.find (color_sorts
[i
],
197 ACE_DEBUG ((LM_DEBUG
,
198 ACE_TEXT ("%s found\n"),
201 ACE_ERROR_RETURN ((LM_ERROR
,
202 ACE_TEXT ("%s not found\n"),
207 for (i
= 0; color_sorts
[i
] != 0; i
++)
208 if (colors
.find (color_sorts
[i
],
210 ACE_DEBUG ((LM_DEBUG
,
211 ACE_TEXT ("%s found\n"),
214 ACE_ERROR_RETURN ((LM_ERROR
,
215 ACE_TEXT ("%s not found\n"),
219 for (i
= 0; warm_colors
[i
] != 0; i
++)
220 if (colors
.find (color_sorts
[0],
221 warm_colors
[i
]) == 0)
222 ACE_DEBUG ((LM_DEBUG
,
223 ACE_TEXT ("%s color %s found\n"),
227 ACE_ERROR_RETURN ((LM_ERROR
,
228 ACE_TEXT ("%s color %s not found\n"),
233 // Check the iterator.
236 for (HASH_STRING_ITERATOR
hash_iter (colors
);
237 hash_iter
.next (entry
) != 0;
238 hash_iter
.advance ())
240 ACE_DEBUG ((LM_DEBUG
,
241 ACE_TEXT ("iterating (%d): %s:\n"),
246 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
247 iter
!= entry
->item ().end ();
250 ACE_DEBUG ((LM_DEBUG
,
256 // Check the unbind operation.
257 if (colors
.unbind (color_sorts
[0]) == -1)
258 ACE_ERROR_RETURN ((LM_ERROR
,
259 ACE_TEXT ("unbind failed for %s\n"),
262 for (i
= 0; warm_colors
[i
] != 0; i
++)
264 if (colors
.bind (color_sorts
[0],
265 warm_colors
[i
]) == -1)
266 ACE_ERROR_RETURN ((LM_ERROR
,
267 ACE_TEXT ("bind failed for %s in %s\n"),
274 if (colors
.unbind (color_sorts
[0], value_set
) == -1)
275 ACE_ERROR_RETURN ((LM_ERROR
,
276 ACE_TEXT ("unbind failed for %s\n"),
279 for (i
= 0; warm_colors
[i
] != 0; i
++)
281 if (colors
.bind (color_sorts
[0],
282 warm_colors
[i
]) == -1)
283 ACE_ERROR_RETURN ((LM_ERROR
,
284 ACE_TEXT ("bind failed for %s in %s\n"),
290 for (i
= 0; warm_colors
[i
] != 0; i
++)
292 if (colors
.unbind (color_sorts
[0],
293 warm_colors
[i
]) == -1)
294 ACE_ERROR_RETURN ((LM_ERROR
,
295 ACE_TEXT ("unbind failed for %s in %s\n"),
301 // Check the reverse iterator.
304 for (HASH_STRING_REVERSE_ITERATOR
hash_iter (colors
);
305 hash_iter
.next (entry
) != 0;
306 hash_iter
.advance ())
308 ACE_DEBUG ((LM_DEBUG
,
309 ACE_TEXT ("reverse iterating (%d): %s\n"),
314 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
315 iter
!= entry
->item ().end ();
318 ACE_DEBUG ((LM_DEBUG
,
325 colors
.find (color_sorts
[1], entry
);
326 if (colors
.unbind (entry
) == -1)
327 ACE_ERROR_RETURN ((LM_ERROR
,
328 ACE_TEXT ("unbind failed for %s\n"),
332 // Check the const iterator.
335 for (HASH_STRING_CONST_ITERATOR
hash_iter (colors
);
336 hash_iter
.next (entry
) != 0;
337 hash_iter
.advance ())
339 ACE_DEBUG ((LM_DEBUG
,
340 ACE_TEXT ("const iterating (%d): %s\n"),
345 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
346 iter
!= entry
->item ().end ();
349 ACE_DEBUG ((LM_DEBUG
,
355 // Check the unbind_all operation.
356 if (colors
.unbind_all () != 0)
357 ACE_ERROR_RETURN ((LM_ERROR
,
358 ACE_TEXT ("unbind_all failed\n")),
365 run_main (int, ACE_TCHAR
*[])
367 ACE_START_TEST (ACE_TEXT ("Hash_Multi_Map_Manager_Test"));