Merge pull request #2309 from mitza-oci/warnings
[ACE_TAO.git] / ACE / tests / Map_Test.cpp
blob706ca1bb7886039b41d4fd239a4e961764492b77
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"
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) \
22 ((X) \
23 ? static_cast<void>(0) \
24 : ACE_VERSIONED_NAMESPACE_NAME::__ace_assert(__FILE__, __LINE__, ACE_TEXT_CHAR_TO_TCHAR (#X)))
26 // Value type.
27 using VALUE = size_t;
29 // Generic map type.
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>;
41 static void
42 functionality_test (TEST_MAP &map,
43 size_t iterations)
45 size_t counter;
46 VALUE i;
47 KEY *original_keys = new KEY[iterations];
48 KEY *modified_keys = new KEY[iterations];
50 // Setup the keys to have some initial data.
51 for (i = 0;
52 i < iterations;
53 ++i)
55 original_keys[i].size (sizeof i / sizeof (KEY::TYPE));
56 ACE_OS::memcpy (&original_keys[i][0],
57 &i,
58 sizeof i);
61 // Make a copy of the keys so that we can compare with the original
62 // keys later.
63 for (i = 0; i < iterations; ++i)
65 modified_keys[i] = original_keys[i];
68 // Add to the map, allowing keys to be modified.
69 counter = 0;
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);
73 ++counter;
74 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
77 // Forward iteration...
79 counter = 0;
80 TEST_MAP::iterator end = map.end ();
82 for (TEST_MAP::iterator iter = map.begin ();
83 iter != end;
84 ++iter, ++counter)
86 TEST_MAP::value_type entry = *iter;
88 // Recover original key.
89 KEY original_key;
90 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.recover_key (entry.first (),
91 original_key) == 0);
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.
97 VALUE original_value;
98 ACE_OS::memcpy (&original_value,
99 &original_key[0],
100 sizeof original_value);
102 // Debugging info.
103 ACE_DEBUG ((LM_DEBUG,
104 ACE_TEXT ("(%d|%d|%d)"),
105 counter,
106 original_value,
107 entry.second ()));
110 ACE_DEBUG ((LM_DEBUG,
111 ACE_TEXT ("\n")));
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 ();
121 iter != end;
122 ++iter)
124 --counter;
125 TEST_MAP::value_type entry = *iter;
127 // Recover original key.
128 KEY original_key;
129 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.recover_key (entry.first (),
130 original_key) == 0);
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,
138 &original_key[0],
139 sizeof original_value);
141 // Debugging info.
142 ACE_DEBUG ((LM_DEBUG,
143 ACE_TEXT ("(%d|%d|%d)"),
144 counter,
145 original_value,
146 entry.second ()));
149 ACE_DEBUG ((LM_DEBUG,
150 ACE_TEXT ("\n")));
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)
157 VALUE j;
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);
167 --counter;
168 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
171 // Cleanup.
172 delete[] modified_keys;
173 delete[] original_keys;
176 static void
177 insert_test (TEST_MAP &map,
178 size_t iterations,
179 KEY *keys)
181 // Add to the map, allowing keys to be created by the map.
182 size_t counter = 0;
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);
186 ++counter;
187 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
191 static void
192 find_test (TEST_MAP &map,
193 size_t iterations,
194 KEY *keys)
196 // Find system generated keys.
197 for (VALUE i = 0; i < iterations; ++i)
199 VALUE j = 0;
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);
205 static void
206 unbind_test (TEST_MAP &map,
207 size_t iterations,
208 KEY *keys)
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);
215 --counter;
216 THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL (map.current_size () == counter);
220 static void
221 performance_test (void (*ptf) (TEST_MAP &, size_t, KEY *),
222 TEST_MAP &map,
223 size_t iterations,
224 KEY *keys,
225 size_t table_size,
226 const ACE_TCHAR *test_name)
228 ACE_Profile_Timer timer;
229 timer.start ();
231 (*ptf) (map, iterations, keys);
233 timer.stop ();
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"),
241 test_name,
242 table_size,
243 iterations));
245 ACE_DEBUG ((LM_DEBUG,
246 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
247 et.real_time,
248 et.user_time,
249 et.system_time));
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;
266 if (argc > 1)
267 functionality_tests = ACE_OS::atoi (argv[1]);
269 if (argc > 2)
270 table_size = ACE_OS::atoi (argv[2]);
272 if (argc > 3)
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];
297 // Map Manager
298 performance_test (&insert_test,
299 map1,
300 iterations,
301 keys,
302 table_size,
303 ACE_TEXT ("Map Manager (insert test)"));
304 performance_test (&find_test,
305 map1,
306 iterations,
307 keys,
308 table_size,
309 ACE_TEXT ("Map Manager (find test)"));
310 performance_test (&unbind_test,
311 map1,
312 iterations,
313 keys,
314 table_size,
315 ACE_TEXT ("Map Manager (unbind test)"));
317 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
319 // Hash Map Manager
320 performance_test (&insert_test,
321 map2,
322 iterations,
323 keys,
324 table_size,
325 ACE_TEXT ("Hash Map Manager (insert test)"));
326 performance_test (&find_test,
327 map2,
328 iterations,
329 keys,
330 table_size,
331 ACE_TEXT ("Hash Map Manager (find test)"));
332 performance_test (&unbind_test,
333 map2,
334 iterations,
335 keys,
336 table_size,
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,
343 map3,
344 iterations,
345 keys,
346 table_size,
347 ACE_TEXT ("Active Map Manager (insert test)"));
348 performance_test (&find_test,
349 map3,
350 iterations,
351 keys,
352 table_size,
353 ACE_TEXT ("Active Map Manager (find test)"));
354 performance_test (&unbind_test,
355 map3,
356 iterations,
357 keys,
358 table_size,
359 ACE_TEXT ("Active Map Manager (unbind test)"));
361 delete[] keys;
363 ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
364 ACE_END_TEST;
366 return 0;
368 #undef THIS_IS_NOT_AN_ASSERT_IT_IS_A_NON_DEBUG_TEST_AS_WELL