Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Map_Test.cpp
blob6de7224501eb07db5bb8e13bd969c755e099cf5f
2 //=============================================================================
3 /**
4 * @file Map_Test.cpp
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"
15 #include "Map_Test.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) \
23 ((X) \
24 ? static_cast<void>(0) \
25 : ACE_VERSIONED_NAMESPACE_NAME::__ace_assert(__FILE__, __LINE__, ACE_TEXT_CHAR_TO_TCHAR (#X)))
27 // Value type.
28 typedef size_t VALUE;
30 // Generic map type.
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;
42 static void
43 functionality_test (TEST_MAP &map,
44 size_t iterations)
46 size_t counter;
47 VALUE i;
48 KEY *original_keys = new KEY[iterations];
49 KEY *modified_keys = new KEY[iterations];
51 // Setup the keys to have some initial data.
52 for (i = 0;
53 i < iterations;
54 ++i)
56 original_keys[i].size (sizeof i / sizeof (KEY::TYPE));
57 ACE_OS::memcpy (&original_keys[i][0],
58 &i,
59 sizeof i);
62 // Make a copy of the keys so that we can compare with the original
63 // keys later.
64 for (i = 0; i < iterations; ++i)
66 modified_keys[i] = original_keys[i];
69 // Add to the map, allowing keys to be modified.
70 counter = 0;
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);
74 ++counter;
75 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
78 // Forward iteration...
80 counter = 0;
81 TEST_MAP::iterator end = map.end ();
83 for (TEST_MAP::iterator iter = map.begin ();
84 iter != end;
85 ++iter, ++counter)
87 TEST_MAP::value_type entry = *iter;
89 // Recover original key.
90 KEY original_key;
91 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.recover_key (entry.first (),
92 original_key) == 0);
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.
98 VALUE original_value;
99 ACE_OS::memcpy (&original_value,
100 &original_key[0],
101 sizeof original_value);
103 // Debugging info.
104 ACE_DEBUG ((LM_DEBUG,
105 ACE_TEXT ("(%d|%d|%d)"),
106 counter,
107 original_value,
108 entry.second ()));
111 ACE_DEBUG ((LM_DEBUG,
112 ACE_TEXT ("\n")));
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 ();
122 iter != end;
123 ++iter)
125 --counter;
126 TEST_MAP::value_type entry = *iter;
128 // Recover original key.
129 KEY original_key;
130 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.recover_key (entry.first (),
131 original_key) == 0);
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,
139 &original_key[0],
140 sizeof original_value);
142 // Debugging info.
143 ACE_DEBUG ((LM_DEBUG,
144 ACE_TEXT ("(%d|%d|%d)"),
145 counter,
146 original_value,
147 entry.second ()));
150 ACE_DEBUG ((LM_DEBUG,
151 ACE_TEXT ("\n")));
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)
158 VALUE j;
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);
168 --counter;
169 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
172 // Cleanup.
173 delete[] modified_keys;
174 delete[] original_keys;
177 static void
178 insert_test (TEST_MAP &map,
179 size_t iterations,
180 KEY *keys)
182 // Add to the map, allowing keys to be created by the map.
183 size_t counter = 0;
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);
187 ++counter;
188 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
192 static void
193 find_test (TEST_MAP &map,
194 size_t iterations,
195 KEY *keys)
197 // Find system generated keys.
198 for (VALUE i = 0; i < iterations; ++i)
200 VALUE j = 0;
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);
206 static void
207 unbind_test (TEST_MAP &map,
208 size_t iterations,
209 KEY *keys)
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);
216 --counter;
217 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
221 static void
222 performance_test (void (*ptf) (TEST_MAP &, size_t, KEY *),
223 TEST_MAP &map,
224 size_t iterations,
225 KEY *keys,
226 size_t table_size,
227 const ACE_TCHAR *test_name)
229 ACE_Profile_Timer timer;
230 timer.start ();
232 (*ptf) (map, iterations, keys);
234 timer.stop ();
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"),
242 test_name,
243 table_size,
244 iterations));
246 ACE_DEBUG ((LM_DEBUG,
247 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
248 et.real_time,
249 et.user_time,
250 et.system_time));
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;
267 if (argc > 1)
268 functionality_tests = ACE_OS::atoi (argv[1]);
270 if (argc > 2)
271 table_size = ACE_OS::atoi (argv[2]);
273 if (argc > 3)
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];
298 // Map Manager
299 performance_test (&insert_test,
300 map1,
301 iterations,
302 keys,
303 table_size,
304 ACE_TEXT ("Map Manager (insert test)"));
305 performance_test (&find_test,
306 map1,
307 iterations,
308 keys,
309 table_size,
310 ACE_TEXT ("Map Manager (find test)"));
311 performance_test (&unbind_test,
312 map1,
313 iterations,
314 keys,
315 table_size,
316 ACE_TEXT ("Map Manager (unbind test)"));
318 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
320 // Hash Map Manager
321 performance_test (&insert_test,
322 map2,
323 iterations,
324 keys,
325 table_size,
326 ACE_TEXT ("Hash Map Manager (insert test)"));
327 performance_test (&find_test,
328 map2,
329 iterations,
330 keys,
331 table_size,
332 ACE_TEXT ("Hash Map Manager (find test)"));
333 performance_test (&unbind_test,
334 map2,
335 iterations,
336 keys,
337 table_size,
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,
344 map3,
345 iterations,
346 keys,
347 table_size,
348 ACE_TEXT ("Active Map Manager (insert test)"));
349 performance_test (&find_test,
350 map3,
351 iterations,
352 keys,
353 table_size,
354 ACE_TEXT ("Active Map Manager (find test)"));
355 performance_test (&unbind_test,
356 map3,
357 iterations,
358 keys,
359 table_size,
360 ACE_TEXT ("Active Map Manager (unbind test)"));
362 delete[] keys;
364 ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
365 ACE_END_TEST;
367 return 0;
369 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL