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"
21 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL
22 #define THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL(X) \
24 ? static_cast<void>(0) \
25 : ACE_VERSIONED_NAMESPACE_NAME::__ace_assert(__FILE__, __LINE__, ACE_TEXT_CHAR_TO_TCHAR (#X)))
31 typedef ACE_Map
<KEY
, VALUE
> TEST_MAP
;
33 // Manager Manager adapter.
34 typedef ACE_Map_Manager_Adapter
<KEY
, VALUE
, Key_Generator
> MAP_MANAGER_ADAPTER
;
36 // Hash Manager Manager adapter.
37 typedef ACE_Hash_Map_Manager_Ex_Adapter
<KEY
, VALUE
, Hash_Key
, ACE_Equal_To
<KEY
>, Key_Generator
> HASH_MAP_MANAGER_ADAPTER
;
39 // Active Manager Manager adapter.
40 typedef ACE_Active_Map_Manager_Adapter
<KEY
, VALUE
, Key_Adapter
> ACTIVE_MAP_MANAGER_ADAPTER
;
43 functionality_test (TEST_MAP
&map
,
48 KEY
*original_keys
= new KEY
[iterations
];
49 KEY
*modified_keys
= new KEY
[iterations
];
51 // Setup the keys to have some initial data.
56 original_keys
[i
].size (sizeof i
/ sizeof (KEY::TYPE
));
57 ACE_OS::memcpy (&original_keys
[i
][0],
62 // Make a copy of the keys so that we can compare with the original
64 for (i
= 0; i
< iterations
; ++i
)
66 modified_keys
[i
] = original_keys
[i
];
69 // Add to the map, allowing keys to be modified.
71 for (i
= 0; i
< iterations
; ++i
)
73 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.bind_modify_key (i
, modified_keys
[i
]) == 0);
75 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
78 // Forward iteration...
81 TEST_MAP::iterator end
= map
.end ();
83 for (TEST_MAP::iterator iter
= map
.begin ();
87 TEST_MAP::value_type entry
= *iter
;
89 // Recover original key.
91 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.recover_key (entry
.first (),
94 // Make sure recovering keys work.
95 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (original_keys
[entry
.second ()] == original_key
);
97 // Obtain value from key.
99 ACE_OS::memcpy (&original_value
,
101 sizeof original_value
);
104 ACE_DEBUG ((LM_DEBUG
,
105 ACE_TEXT ("(%d|%d|%d)"),
111 ACE_DEBUG ((LM_DEBUG
,
113 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (counter
== iterations
);
116 // Reverse iteration...
118 counter
= iterations
;
119 TEST_MAP::reverse_iterator end
= map
.rend ();
121 for (TEST_MAP::reverse_iterator iter
= map
.rbegin ();
126 TEST_MAP::value_type entry
= *iter
;
128 // Recover original key.
130 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.recover_key (entry
.first (),
133 // Make sure recovering keys work.
134 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (original_keys
[entry
.second ()] == original_key
);
136 // Obtain value from key.
137 VALUE original_value
;
138 ACE_OS::memcpy (&original_value
,
140 sizeof original_value
);
143 ACE_DEBUG ((LM_DEBUG
,
144 ACE_TEXT ("(%d|%d|%d)"),
150 ACE_DEBUG ((LM_DEBUG
,
152 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (counter
== 0);
155 // Search using the modified keys.
156 for (i
= 0; i
< iterations
; ++i
)
159 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.find (modified_keys
[i
], j
) != -1);
160 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (i
== j
);
163 // Rmoved keys from map.
164 counter
= iterations
;
165 for (i
= 0; i
< iterations
; ++i
)
167 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.unbind (modified_keys
[i
]) != -1);
169 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
173 delete[] modified_keys
;
174 delete[] original_keys
;
178 insert_test (TEST_MAP
&map
,
182 // Add to the map, allowing keys to be created by the map.
184 for (VALUE i
= 0; i
< iterations
; ++i
)
186 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.bind_create_key (i
, keys
[i
]) == 0);
188 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
193 find_test (TEST_MAP
&map
,
197 // Find system generated keys.
198 for (VALUE i
= 0; i
< iterations
; ++i
)
201 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.find (keys
[i
], j
) != -1);
202 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (i
== j
);
207 unbind_test (TEST_MAP
&map
,
211 // Remove system generated keys.
212 size_t counter
= iterations
;
213 for (VALUE i
= 0; i
< iterations
; ++i
)
215 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.unbind (keys
[i
]) != -1);
217 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map
.current_size () == counter
);
222 performance_test (void (*ptf
) (TEST_MAP
&, size_t, KEY
*),
227 const ACE_TCHAR
*test_name
)
229 ACE_Profile_Timer timer
;
232 (*ptf
) (map
, iterations
, keys
);
236 ACE_Profile_Timer::ACE_Elapsed_Time et
;
238 timer
.elapsed_time (et
);
240 ACE_DEBUG ((LM_DEBUG
,
241 ACE_TEXT ("time to run %s of size %d for %d iterations\n"),
246 ACE_DEBUG ((LM_DEBUG
,
247 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
252 ACE_DEBUG ((LM_DEBUG
,
253 ACE_TEXT ("time per call = %f usecs\n"),
254 (et
.real_time
/ ACE_timer_t (iterations
)) * 1000000));
258 run_main (int argc
, ACE_TCHAR
*argv
[])
260 ACE_START_TEST (ACE_TEXT ("Map_Test"));
261 ACE_LOG_MSG
->clr_flags (ACE_Log_Msg::VERBOSE_LITE
);
263 size_t table_size
= ACE_MAX_ITERATIONS
/ 2;
264 size_t iterations
= ACE_MAX_ITERATIONS
;
265 size_t functionality_tests
= 1;
268 functionality_tests
= ACE_OS::atoi (argv
[1]);
271 table_size
= ACE_OS::atoi (argv
[2]);
274 iterations
= ACE_OS::atoi (argv
[3]);
276 MAP_MANAGER_ADAPTER
map1 (table_size
);
277 HASH_MAP_MANAGER_ADAPTER
map2 (table_size
);
278 ACTIVE_MAP_MANAGER_ADAPTER
map3 (table_size
);
280 if (functionality_tests
)
282 // Functionality test of the maps.
283 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nMap Manager functionality test\n")));
284 functionality_test (map1
, iterations
);
286 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nHash Map Manager functionality test\n")));
287 functionality_test (map2
, iterations
);
289 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\nActive Map Manager functionality test\n")));
290 functionality_test (map3
, iterations
);
292 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
295 // Performance test of the maps.
296 KEY
*keys
= new KEY
[iterations
];
299 performance_test (&insert_test
,
304 ACE_TEXT ("Map Manager (insert test)"));
305 performance_test (&find_test
,
310 ACE_TEXT ("Map Manager (find test)"));
311 performance_test (&unbind_test
,
316 ACE_TEXT ("Map Manager (unbind test)"));
318 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
321 performance_test (&insert_test
,
326 ACE_TEXT ("Hash Map Manager (insert test)"));
327 performance_test (&find_test
,
332 ACE_TEXT ("Hash Map Manager (find test)"));
333 performance_test (&unbind_test
,
338 ACE_TEXT ("Hash Map Manager (unbind test)"));
340 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("\n")));
342 // Active Map Manager
343 performance_test (&insert_test
,
348 ACE_TEXT ("Active Map Manager (insert test)"));
349 performance_test (&find_test
,
354 ACE_TEXT ("Active Map Manager (find test)"));
355 performance_test (&unbind_test
,
360 ACE_TEXT ("Active Map Manager (unbind test)"));
364 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE_LITE
);
369 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL