2 //=============================================================================
6 * This is a simple test of the <ACE_Map> and illustrates how to
7 * use the forward and reverse iterators.
9 * @author Irfan Pyarali <irfan@cs.wustl.edu>
11 //=============================================================================
14 #include "test_config.h"
16 #include "ace/Map_T.h"
17 #include "ace/Profile_Timer.h"
20 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL
21 #define THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL(X) \
23 ? static_cast<void>(0) \
24 : ACE_VERSIONED_NAMESPACE_NAME::__ace_assert(__FILE__, __LINE__, ACE_TEXT_CHAR_TO_TCHAR (#X)))
30 using TEST_MAP
= ACE_Map
<KEY
, VALUE
>;
32 // Manager Manager adapter.
33 using MAP_MANAGER_ADAPTER
= ACE_Map_Manager_Adapter
<KEY
, VALUE
, Key_Generator
>;
35 // Hash Manager Manager adapter.
36 using HASH_MAP_MANAGER_ADAPTER
= ACE_Hash_Map_Manager_Ex_Adapter
<KEY
, VALUE
, Hash_Key
, ACE_Equal_To
<KEY
>, Key_Generator
>;
38 // Active Manager Manager adapter.
39 using ACTIVE_MAP_MANAGER_ADAPTER
= ACE_Active_Map_Manager_Adapter
<KEY
, VALUE
, Key_Adapter
>;
42 functionality_test (TEST_MAP
&map
,
47 KEY
*original_keys
= new KEY
[iterations
];
48 KEY
*modified_keys
= new KEY
[iterations
];
50 // Setup the keys to have some initial data.
55 original_keys
[i
].size (sizeof i
/ sizeof (KEY::TYPE
));
56 ACE_OS::memcpy (&original_keys
[i
][0],
61 // Make a copy of the keys so that we can compare with the original
63 for (i
= 0; i
< iterations
; ++i
)
65 modified_keys
[i
] = original_keys
[i
];
68 // Add to the map, allowing keys to be modified.
70 for (i
= 0; i
< iterations
; ++i
)
72 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.bind_modify_key (i
, modified_keys
[i
]) == 0);
74 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
77 // Forward iteration...
80 TEST_MAP::iterator end
= map
.end ();
82 for (TEST_MAP::iterator iter
= map
.begin ();
86 TEST_MAP::value_type entry
= *iter
;
88 // Recover original key.
90 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.recover_key (entry
.first (),
93 // Make sure recovering keys work.
94 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (original_keys
[entry
.second ()] == original_key
);
96 // Obtain value from key.
98 ACE_OS::memcpy (&original_value
,
100 sizeof original_value
);
103 ACE_DEBUG ((LM_DEBUG
,
104 ACE_TEXT ("(%d|%d|%d)"),
110 ACE_DEBUG ((LM_DEBUG
,
112 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (counter
== iterations
);
115 // Reverse iteration...
117 counter
= iterations
;
118 TEST_MAP::reverse_iterator end
= map
.rend ();
120 for (TEST_MAP::reverse_iterator iter
= map
.rbegin ();
125 TEST_MAP::value_type entry
= *iter
;
127 // Recover original key.
129 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.recover_key (entry
.first (),
132 // Make sure recovering keys work.
133 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (original_keys
[entry
.second ()] == original_key
);
135 // Obtain value from key.
136 VALUE original_value
;
137 ACE_OS::memcpy (&original_value
,
139 sizeof original_value
);
142 ACE_DEBUG ((LM_DEBUG
,
143 ACE_TEXT ("(%d|%d|%d)"),
149 ACE_DEBUG ((LM_DEBUG
,
151 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (counter
== 0);
154 // Search using the modified keys.
155 for (i
= 0; i
< iterations
; ++i
)
158 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.find (modified_keys
[i
], j
) != -1);
159 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (i
== j
);
162 // Rmoved keys from map.
163 counter
= iterations
;
164 for (i
= 0; i
< iterations
; ++i
)
166 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.unbind (modified_keys
[i
]) != -1);
168 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
172 delete[] modified_keys
;
173 delete[] original_keys
;
177 insert_test (TEST_MAP
&map
,
181 // Add to the map, allowing keys to be created by the map.
183 for (VALUE i
= 0; i
< iterations
; ++i
)
185 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.bind_create_key (i
, keys
[i
]) == 0);
187 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
192 find_test (TEST_MAP
&map
,
196 // Find system generated keys.
197 for (VALUE i
= 0; i
< iterations
; ++i
)
200 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.find (keys
[i
], j
) != -1);
201 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (i
== j
);
206 unbind_test (TEST_MAP
&map
,
210 // Remove system generated keys.
211 size_t counter
= iterations
;
212 for (VALUE i
= 0; i
< iterations
; ++i
)
214 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.unbind (keys
[i
]) != -1);
216 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
221 performance_test (void (*ptf
) (TEST_MAP
&, size_t, KEY
*),
226 const ACE_TCHAR
*test_name
)
228 ACE_Profile_Timer timer
;
231 (*ptf
) (map
, iterations
, keys
);
235 ACE_Profile_Timer::ACE_Elapsed_Time et
;
237 timer
.elapsed_time (et
);
239 ACE_DEBUG ((LM_DEBUG
,
240 ACE_TEXT ("time to run %s of size %d for %d iterations\n"),
245 ACE_DEBUG ((LM_DEBUG
,
246 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
251 ACE_DEBUG ((LM_DEBUG
,
252 ACE_TEXT ("time per call = %f usecs\n"),
253 (et
.real_time
/ ACE_timer_t (iterations
)) * 1000000));
257 run_main (int argc
, ACE_TCHAR
*argv
[])
259 ACE_START_TEST (ACE_TEXT ("Map_Test"));
260 ACE_LOG_MSG
->clr_flags (ACE_Log_Msg::VERBOSE_LITE
);
262 size_t table_size
= ACE_MAX_ITERATIONS
/ 2;
263 size_t iterations
= ACE_MAX_ITERATIONS
;
264 size_t functionality_tests
= 1;
267 functionality_tests
= ACE_OS::atoi (argv
[1]);
270 table_size
= ACE_OS::atoi (argv
[2]);
273 iterations
= ACE_OS::atoi (argv
[3]);
275 MAP_MANAGER_ADAPTER
map1 (table_size
);
276 HASH_MAP_MANAGER_ADAPTER
map2 (table_size
);
277 ACTIVE_MAP_MANAGER_ADAPTER
map3 (table_size
);
279 if (functionality_tests
)
281 // Functionality test of the maps.
282 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nMap Manager functionality test\n")));
283 functionality_test (map1
, iterations
);
285 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nHash Map Manager functionality test\n")));
286 functionality_test (map2
, iterations
);
288 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nActive Map Manager functionality test\n")));
289 functionality_test (map3
, iterations
);
291 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
294 // Performance test of the maps.
295 KEY
*keys
= new KEY
[iterations
];
298 performance_test (&insert_test
,
303 ACE_TEXT ("Map Manager (insert test)"));
304 performance_test (&find_test
,
309 ACE_TEXT ("Map Manager (find test)"));
310 performance_test (&unbind_test
,
315 ACE_TEXT ("Map Manager (unbind test)"));
317 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
320 performance_test (&insert_test
,
325 ACE_TEXT ("Hash Map Manager (insert test)"));
326 performance_test (&find_test
,
331 ACE_TEXT ("Hash Map Manager (find test)"));
332 performance_test (&unbind_test
,
337 ACE_TEXT ("Hash Map Manager (unbind test)"));
339 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
341 // Active Map Manager
342 performance_test (&insert_test
,
347 ACE_TEXT ("Active Map Manager (insert test)"));
348 performance_test (&find_test
,
353 ACE_TEXT ("Active Map Manager (find test)"));
354 performance_test (&unbind_test
,
359 ACE_TEXT ("Active Map Manager (unbind test)"));
363 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE_LITE
);
368 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL