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 typedef ACE_Hash_Multi_Map_Manager
<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};
41 HASH_STRING_MAP colors
;
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"),
57 HASH_STRING_ENTRY
*entry
= 0;
58 for (i
= 0; cold_colors
[i
] != 0; i
++)
60 if (colors
.bind (color_sorts
[1],
63 ACE_ERROR_RETURN ((LM_ERROR
,
64 ACE_TEXT ("bind failed for %s in %s\n"),
70 HASH_STRING_VALUE_SET value_set
;
72 for (i
= 0; neutral_colors
[i
] != 0; i
++)
73 value_set
.insert (neutral_colors
[i
]);
74 if (colors
.bind (color_sorts
[2],
77 ACE_ERROR_RETURN ((LM_ERROR
,
78 ACE_TEXT ("bind failed for %s\n"),
82 // Check the <trybind> operation.
84 if (colors
.trybind (color_sorts
[2],
86 ACE_ERROR_RETURN ((LM_ERROR
,
87 ACE_TEXT ("bind failed for %s\n"),
93 if (colors
.trybind (color_sorts
[2],
96 ACE_ERROR_RETURN ((LM_ERROR
,
97 ACE_TEXT ("bind failed for %s\n"),
103 if (colors
.trybind (color_sorts
[2],
106 ACE_ERROR_RETURN ((LM_ERROR
,
107 ACE_TEXT ("bind failed for %s\n"),
111 // Check the <rebind> operation.
113 value_set
.insert (neutral_colors
[0]);
114 if (colors
.rebind (color_sorts
[2],
116 ACE_ERROR_RETURN ((LM_ERROR
,
117 ACE_TEXT ("bind failed for %s\n"),
122 value_set
.insert (neutral_colors
[1]);
124 if (colors
.rebind (color_sorts
[2],
127 ACE_ERROR_RETURN ((LM_ERROR
,
128 ACE_TEXT ("bind failed for %s\n"),
133 value_set
.insert (neutral_colors
[0]);
134 HASH_STRING_VALUE_SET old_value_set
;
135 if (colors
.rebind (color_sorts
[2],
137 old_value_set
) == -1)
138 ACE_ERROR_RETURN ((LM_ERROR
,
139 ACE_TEXT ("bind failed for %s\n"),
144 value_set
.insert (neutral_colors
[1]);
145 old_value_set
.reset ();
147 if (colors
.rebind (color_sorts
[2],
151 ACE_ERROR_RETURN ((LM_ERROR
,
152 ACE_TEXT ("bind failed for %s\n"),
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],
163 old_value_set
) == -1)
164 ACE_ERROR_RETURN ((LM_ERROR
,
165 ACE_TEXT ("bind failed for %s\n"),
170 for (i
= 0; neutral_colors
[i
] != 0; i
++)
171 value_set
.insert (neutral_colors
[i
]);
173 old_value_set
.reset ();
175 if (colors
.rebind (color_sorts
[2],
180 ACE_ERROR_RETURN ((LM_ERROR
,
181 ACE_TEXT ("bind failed for %s\n"),
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"),
192 ACE_ERROR_RETURN ((LM_ERROR
,
193 ACE_TEXT ("%s not found\n"),
198 for (i
= 0; color_sorts
[i
] != 0; i
++)
199 if (colors
.find (color_sorts
[i
],
201 ACE_DEBUG ((LM_DEBUG
,
202 ACE_TEXT ("%s found\n"),
205 ACE_ERROR_RETURN ((LM_ERROR
,
206 ACE_TEXT ("%s not found\n"),
211 for (i
= 0; color_sorts
[i
] != 0; i
++)
212 if (colors
.find (color_sorts
[i
],
214 ACE_DEBUG ((LM_DEBUG
,
215 ACE_TEXT ("%s found\n"),
218 ACE_ERROR_RETURN ((LM_ERROR
,
219 ACE_TEXT ("%s not found\n"),
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"),
231 ACE_ERROR_RETURN ((LM_ERROR
,
232 ACE_TEXT ("%s color %s not found\n"),
237 // Check the iterator.
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"),
250 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
251 iter
!= entry
->item ().end ();
254 ACE_DEBUG ((LM_DEBUG
,
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"),
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"),
278 if (colors
.unbind (color_sorts
[0], value_set
) == -1)
279 ACE_ERROR_RETURN ((LM_ERROR
,
280 ACE_TEXT ("unbind failed for %s\n"),
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"),
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"),
305 // Check the reverse iterator.
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"),
318 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
319 iter
!= entry
->item ().end ();
322 ACE_DEBUG ((LM_DEBUG
,
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"),
336 // Check the const iterator.
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"),
349 for (HASH_STRING_VALUE_SET_ITERATOR
iter (entry
->item ());
350 iter
!= entry
->item ().end ();
353 ACE_DEBUG ((LM_DEBUG
,
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")),
369 run_main (int, ACE_TCHAR
*[])
371 ACE_START_TEST (ACE_TEXT ("Hash_Multi_Map_Manager_Test"));