2 //=============================================================================
4 * @file Map_Manager_Test.cpp
6 * This is a simple test of the <ACE_Map_Manager> and
7 * <ACE_Hash_Map_Manager> that illustrates how to use the forward
8 * and reverse iterators.
10 * @author Irfan Pyarali <irfan@cs.wustl.edu>
11 * @author Douglas C. Schmidt <d.schmidt@vanderbilt.edu>
12 * @author and Kirthika Parameswaran <kirthika@cs.wustl.edu>
14 //=============================================================================
17 #include "test_config.h"
18 #include "ace/Map_Manager.h"
19 #include "ace/Hash_Map_Manager.h"
20 #include "ace/Active_Map_Manager.h"
21 #include "ace/Profile_Timer.h"
22 #include "ace/Hash_Cache_Map_Manager_T.h"
23 #include "ace/Caching_Strategies_T.h"
25 using MUTEX
= ACE_Null_Mutex
;
26 using TYPE
= ACE_UINT32
;
27 using ACTIVE_KEY
= ACE_Active_Map_Manager_Key
;
28 using HASH_KEY
= ACE_Hash
<TYPE
>;
29 using COMPARE_KEYS
= ACE_Equal_To
<TYPE
>;
31 using MAP_MANAGER
= ACE_Map_Manager
<TYPE
, TYPE
, MUTEX
>;
32 using ITERATOR
= ACE_Map_Iterator
<TYPE
, TYPE
, MUTEX
>;
33 using REVERSE_ITERATOR
= ACE_Map_Reverse_Iterator
<TYPE
, TYPE
, MUTEX
>;
34 using MAP_ENTRY
= ACE_Map_Entry
<TYPE
, TYPE
>;
35 using HASH_MAP_MANAGER
= ACE_Hash_Map_Manager_Ex
<TYPE
, TYPE
, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
36 using HASH_ITERATOR
= ACE_Hash_Map_Iterator_Ex
<TYPE
, TYPE
, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
37 using HASH_REVERSE_ITERATOR
= ACE_Hash_Map_Reverse_Iterator_Ex
<TYPE
, TYPE
, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
38 using HASH_ENTRY
= ACE_Hash_Map_Entry
<TYPE
, TYPE
>;
39 using ACTIVE_MAP_MANAGER
= ACE_Active_Map_Manager
<TYPE
>;
41 using CACHE_MAP_IMPL
= ACE_Hash_Map_Manager_Ex
<TYPE
, std::pair
<TYPE
, int>, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
42 using CACHE_ITER_IMPL
= ACE_Hash_Map_Iterator_Ex
<TYPE
, std::pair
<TYPE
, int>, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
43 using CACHE_REV_ITER_IMPL
= ACE_Hash_Map_Reverse_Iterator_Ex
<TYPE
, std::pair
<TYPE
, int>, HASH_KEY
, COMPARE_KEYS
, MUTEX
>;
45 using NULL_CLEANUP
= ANCLE
<TYPE
, TYPE
, CACHE_MAP_IMPL
>;
46 using NULL_UTILITY
= ACE_Null_Caching_Utility
<TYPE
, TYPE
, CACHE_MAP_IMPL
, CACHE_ITER_IMPL
, ATTR
>;
47 using NULL_CACHING_STRATEGY
= ANULL
<ATTR
, NULL_UTILITY
>;
48 using CACHE_MAP_MANAGER
= ACMM
<TYPE
, TYPE
, CACHE_MAP_IMPL
, CACHE_ITER_IMPL
, CACHE_REV_ITER_IMPL
, NULL_CACHING_STRATEGY
, ATTR
>;
49 using HASH_CACHE_MAP_MANAGER
= ACE_Hash_Cache_Map_Manager
<TYPE
, TYPE
, HASH_KEY
, COMPARE_KEYS
, NULL_CACHING_STRATEGY
, ATTR
>;
52 test_cache_map_manager (size_t table_size
,
56 NULL_CACHING_STRATEGY null_caching_strategy
;
57 CACHE_MAP_MANAGER
map (null_caching_strategy
,
63 for (i
= 0; i
< iterations
; i
++)
64 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
71 CACHE_MAP_MANAGER::ITERATOR end
= map
.end ();
73 for (CACHE_MAP_MANAGER::ITERATOR iter
= map
.begin ();
78 ACE_TEXT ("(%d|%d|%d)"),
87 ACE_TEST_ASSERT (i
== iterations
);
94 CACHE_MAP_MANAGER::REVERSE_ITERATOR rend
= map
.rend ();
96 for (CACHE_MAP_MANAGER::REVERSE_ITERATOR iter
= map
.rbegin ();
100 ACE_DEBUG ((LM_DEBUG
,
101 ACE_TEXT ("(%d|%d|%d)"),
108 ACE_DEBUG ((LM_DEBUG
,
110 ACE_TEST_ASSERT (k
== -1);
117 CACHE_MAP_MANAGER::iterator end
= map
.end ();
119 for (CACHE_MAP_MANAGER::iterator iter
= map
.begin ();
123 ACE_DEBUG ((LM_DEBUG
,
124 ACE_TEXT ("(%d|%d|%d)"),
131 ACE_DEBUG ((LM_DEBUG
,
133 ACE_TEST_ASSERT (i
== iterations
);
140 CACHE_MAP_MANAGER::reverse_iterator rend
= map
.rend ();
142 for (CACHE_MAP_MANAGER::reverse_iterator iter
= map
.rbegin ();
146 ACE_DEBUG ((LM_DEBUG
,
147 ACE_TEXT ("(%d|%d|%d)"),
154 ACE_DEBUG ((LM_DEBUG
,
156 ACE_TEST_ASSERT (k
== -1);
162 for (i
= 0; i
< iterations
; ++i
)
164 ACE_TEST_ASSERT (map
.find (i
, j
) != -1);
165 ACE_TEST_ASSERT (i
== j
);
168 size_t remaining_entries
= iterations
;
169 for (i
= 0; i
< iterations
; ++i
)
171 ACE_TEST_ASSERT (map
.unbind (i
) != -1);
173 ACE_TEST_ASSERT (map
.current_size () == remaining_entries
);
180 test_hash_cache_map_manager (size_t table_size
,
184 NULL_CACHING_STRATEGY null_caching_strategy
;
185 HASH_CACHE_MAP_MANAGER
map (null_caching_strategy
,
191 for (i
= 0; i
< iterations
; i
++)
192 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
199 HASH_CACHE_MAP_MANAGER::ITERATOR end
= map
.end ();
201 for (HASH_CACHE_MAP_MANAGER::ITERATOR iter
= map
.begin ();
205 ACE_DEBUG ((LM_DEBUG
,
206 ACE_TEXT ("(%d|%d|%d)"),
213 ACE_DEBUG ((LM_DEBUG
,
215 ACE_TEST_ASSERT (i
== iterations
);
222 HASH_CACHE_MAP_MANAGER::REVERSE_ITERATOR rend
= map
.rend ();
224 for (HASH_CACHE_MAP_MANAGER::REVERSE_ITERATOR iter
= map
.rbegin ();
228 ACE_DEBUG ((LM_DEBUG
,
229 ACE_TEXT ("(%d|%d|%d)"),
236 ACE_DEBUG ((LM_DEBUG
,
238 ACE_TEST_ASSERT (k
== -1);
245 HASH_CACHE_MAP_MANAGER::iterator end
= map
.end ();
247 for (HASH_CACHE_MAP_MANAGER::iterator iter
= map
.begin ();
251 ACE_DEBUG ((LM_DEBUG
,
252 ACE_TEXT ("(%d|%d|%d)"),
259 ACE_DEBUG ((LM_DEBUG
,
261 ACE_TEST_ASSERT (i
== iterations
);
268 HASH_CACHE_MAP_MANAGER::reverse_iterator rend
= map
.rend ();
270 for (HASH_CACHE_MAP_MANAGER::reverse_iterator iter
= map
.rbegin ();
274 ACE_DEBUG ((LM_DEBUG
,
275 ACE_TEXT ("(%d|%d|%d)"),
282 ACE_DEBUG ((LM_DEBUG
,
284 ACE_TEST_ASSERT (k
== -1);
290 for (i
= 0; i
< iterations
; ++i
)
292 ACE_TEST_ASSERT (map
.find (i
, j
) != -1);
293 ACE_TEST_ASSERT (i
== j
);
296 size_t remaining_entries
= iterations
;
297 for (i
= 0; i
< iterations
; ++i
)
299 ACE_TEST_ASSERT (map
.unbind (i
) != -1);
301 ACE_TEST_ASSERT (map
.current_size () == remaining_entries
);
308 test_active_map_manager (size_t table_size
,
312 ACTIVE_MAP_MANAGER
map (table_size
);
317 ACTIVE_MAP_MANAGER::key_type
*active_keys
;
319 ACE_NEW_RETURN (active_keys
,
320 ACTIVE_MAP_MANAGER::key_type
[iterations
],
326 ACE_TEST_ASSERT (map
.bind (i
, active_keys
[i
]) != -1);
333 ACTIVE_MAP_MANAGER::iterator end
= map
.end ();
335 for (ACTIVE_MAP_MANAGER::iterator iter
= map
.begin ();
339 ACTIVE_MAP_MANAGER::ENTRY
&entry
= *iter
;
340 ACE_DEBUG ((LM_DEBUG
,
341 ACE_TEXT ("(%d|%d-%d|%d)"),
343 entry
.ext_id_
.slot_index (),
344 entry
.ext_id_
.slot_generation (),
349 ACE_DEBUG ((LM_DEBUG
,
351 ACE_TEST_ASSERT (i
== iterations
);
357 ACTIVE_MAP_MANAGER::reverse_iterator rend
= map
.rend ();
359 for (ACTIVE_MAP_MANAGER::reverse_iterator iter
= map
.rbegin ();
363 ACTIVE_MAP_MANAGER::ENTRY
&entry
= *iter
;
364 ACE_UNUSED_ARG (entry
);
365 ACE_DEBUG ((LM_DEBUG
,
366 ACE_TEXT ("(%d|%d-%d|%d)"),
368 entry
.ext_id_
.slot_index (),
369 entry
.ext_id_
.slot_generation (),
374 ACE_DEBUG ((LM_DEBUG
,
376 ACE_TEST_ASSERT (k
== -1);
380 for (i
= 0; i
< iterations
; ++i
)
382 ACE_TEST_ASSERT (map
.find (active_keys
[i
], j
) != -1);
383 ACE_TEST_ASSERT (i
== j
);
386 size_t remaining_entries
= iterations
;
387 for (i
= 0; i
< iterations
; ++i
)
389 ACE_TEST_ASSERT (map
.unbind (active_keys
[i
]) != -1);
391 ACE_TEST_ASSERT (map
.current_size () == remaining_entries
);
394 delete [] active_keys
;
400 test_hash_map_manager (size_t table_size
,
404 HASH_MAP_MANAGER
map (table_size
);
409 for (i
= 0; i
< iterations
; i
++)
410 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
417 HASH_ITERATOR end
= map
.end ();
418 for (HASH_ITERATOR iter
= map
.begin ();
422 HASH_ENTRY
&entry
= *iter
;
423 ACE_DEBUG ((LM_DEBUG
,
424 ACE_TEXT ("(%d|%d|%d)"),
431 ACE_DEBUG ((LM_DEBUG
,
433 ACE_TEST_ASSERT (i
== iterations
);
438 HASH_ENTRY
*entry
= 0;
440 for (HASH_ITERATOR
iterator (map
);
441 iterator
.next (entry
) != 0;
444 ACE_DEBUG ((LM_DEBUG
,
445 ACE_TEXT ("(%d|%d|%d)"),
452 ACE_DEBUG ((LM_DEBUG
,
454 ACE_TEST_ASSERT (i
== iterations
);
459 HASH_REVERSE_ITERATOR rend
= map
.rend ();
461 for (HASH_REVERSE_ITERATOR iter
= map
.rbegin ();
465 HASH_ENTRY
&entry
= *iter
;
466 ACE_DEBUG ((LM_DEBUG
,
467 ACE_TEXT ("(%d|%d|%d)"),
474 ACE_DEBUG ((LM_DEBUG
,
476 ACE_TEST_ASSERT (k
== -1);
481 HASH_ENTRY
*entry
= 0;
483 for (HASH_REVERSE_ITERATOR
iterator (map
);
484 iterator
.next (entry
) != 0;
487 ACE_DEBUG ((LM_DEBUG
,
488 ACE_TEXT ("(%d|%d|%d)"),
495 ACE_DEBUG ((LM_DEBUG
,
497 ACE_TEST_ASSERT (k
== -1);
503 HASH_MAP_MANAGER::iterator end
= map
.end ();
504 for (HASH_MAP_MANAGER::iterator iter
= map
.begin ();
508 HASH_MAP_MANAGER::ENTRY
&entry
= *iter
;
509 ACE_DEBUG ((LM_DEBUG
,
510 ACE_TEXT ("(%d|%d|%d)"),
517 ACE_DEBUG ((LM_DEBUG
,
519 ACE_TEST_ASSERT (i
== iterations
);
524 HASH_MAP_MANAGER::ENTRY
*entry
= 0;
526 for (HASH_MAP_MANAGER::ITERATOR
iterator (map
);
527 iterator
.next (entry
) != 0;
530 ACE_DEBUG ((LM_DEBUG
,
531 ACE_TEXT ("(%d|%d|%d)"),
538 ACE_DEBUG ((LM_DEBUG
,
540 ACE_TEST_ASSERT (i
== iterations
);
545 HASH_MAP_MANAGER::reverse_iterator rend
= map
.rend ();
547 for (HASH_MAP_MANAGER::reverse_iterator iter
= map
.rbegin ();
551 HASH_MAP_MANAGER::ENTRY
&entry
= *iter
;
552 ACE_DEBUG ((LM_DEBUG
,
553 ACE_TEXT ("(%d|%d|%d)"),
560 ACE_DEBUG ((LM_DEBUG
,
562 ACE_TEST_ASSERT (k
== -1);
567 HASH_MAP_MANAGER::ENTRY
*entry
= 0;
569 for (HASH_MAP_MANAGER::REVERSE_ITERATOR
iterator (map
);
570 iterator
.next (entry
) != 0;
573 ACE_DEBUG ((LM_DEBUG
,
574 ACE_TEXT ("(%d|%d|%d)"),
581 ACE_DEBUG ((LM_DEBUG
,
583 ACE_TEST_ASSERT (k
== -1);
587 for (i
= 0; i
< iterations
; ++i
)
589 ACE_TEST_ASSERT (map
.find (i
, j
) != -1);
590 ACE_TEST_ASSERT (i
== j
);
593 size_t remaining_entries
= iterations
;
594 for (i
= 0; i
< iterations
; ++i
)
596 ACE_TEST_ASSERT (map
.unbind (i
) != -1);
598 ACE_TEST_ASSERT (map
.current_size () == remaining_entries
);
605 test_map_manager (size_t table_size
,
609 MAP_MANAGER
map (table_size
);
614 for (i
= 0; i
< iterations
; ++i
)
615 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
622 ITERATOR end
= map
.end ();
623 for (ITERATOR iter
= map
.begin ();
627 MAP_ENTRY
&entry
= *iter
;
628 ACE_DEBUG ((LM_DEBUG
,
629 ACE_TEXT ("(%d|%d|%d)"),
636 ACE_DEBUG ((LM_DEBUG
,
638 ACE_TEST_ASSERT (i
== iterations
);
643 MAP_ENTRY
*entry
= 0;
645 for (ITERATOR
iterator (map
);
646 iterator
.next (entry
) != 0;
649 ACE_DEBUG ((LM_DEBUG
,
650 ACE_TEXT ("(%d|%d|%d)"),
657 ACE_DEBUG ((LM_DEBUG
,
659 ACE_TEST_ASSERT (i
== iterations
);
664 REVERSE_ITERATOR rend
= map
.rend ();
666 for (REVERSE_ITERATOR iter
= map
.rbegin ();
670 MAP_ENTRY
&entry
= *iter
;
671 ACE_DEBUG ((LM_DEBUG
,
672 ACE_TEXT ("(%d|%d|%d)"),
679 ACE_DEBUG ((LM_DEBUG
,
681 ACE_TEST_ASSERT (k
== -1);
686 MAP_ENTRY
*entry
= 0;
688 for (REVERSE_ITERATOR
iterator (map
);
689 iterator
.next (entry
) != 0;
692 ACE_DEBUG ((LM_DEBUG
,
693 ACE_TEXT ("(%d|%d|%d)"),
700 ACE_DEBUG ((LM_DEBUG
,
702 ACE_TEST_ASSERT (k
== -1);
708 MAP_MANAGER::iterator end
= map
.end ();
709 for (MAP_MANAGER::iterator iter
= map
.begin ();
713 MAP_MANAGER::ENTRY
&entry
= *iter
;
714 ACE_DEBUG ((LM_DEBUG
,
715 ACE_TEXT ("(%d|%d|%d)"),
722 ACE_DEBUG ((LM_DEBUG
,
724 ACE_TEST_ASSERT (i
== iterations
);
729 MAP_MANAGER::ENTRY
*entry
= 0;
731 for (MAP_MANAGER::ITERATOR
iterator (map
);
732 iterator
.next (entry
) != 0;
735 ACE_DEBUG ((LM_DEBUG
,
736 ACE_TEXT ("(%d|%d|%d)"),
743 ACE_DEBUG ((LM_DEBUG
,
745 ACE_TEST_ASSERT (i
== iterations
);
750 MAP_MANAGER::reverse_iterator rend
= map
.rend ();
752 for (MAP_MANAGER::reverse_iterator iter
= map
.rbegin ();
756 MAP_ENTRY
&entry
= *iter
;
757 ACE_DEBUG ((LM_DEBUG
,
758 ACE_TEXT ("(%d|%d|%d)"),
765 ACE_DEBUG ((LM_DEBUG
,
767 ACE_TEST_ASSERT (k
== -1);
772 MAP_MANAGER::ENTRY
*entry
= 0;
774 for (MAP_MANAGER::REVERSE_ITERATOR
iterator (map
);
775 iterator
.next (entry
) != 0;
778 ACE_DEBUG ((LM_DEBUG
,
779 ACE_TEXT ("(%d|%d|%d)"),
786 ACE_DEBUG ((LM_DEBUG
,
788 ACE_TEST_ASSERT (k
== -1);
792 for (i
= 0; i
< iterations
; ++i
)
794 ACE_TEST_ASSERT (map
.find (i
, j
) != -1);
795 ACE_TEST_ASSERT (i
== j
);
798 size_t remaining_entries
= iterations
;
799 for (i
= 0; i
< iterations
; ++i
)
801 ACE_TEST_ASSERT (map
.unbind (i
) != -1);
803 ACE_TEST_ASSERT (map
.current_size () == remaining_entries
);
807 // This is extra for the map manager.
809 for (i
= 0; i
< iterations
; ++i
)
810 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
812 // Unbind in one swoop.
814 ACE_TEST_ASSERT (map
.current_size () == 0);
816 for (i
= 0; i
< iterations
; ++i
)
817 ACE_TEST_ASSERT (map
.bind (i
, i
) != -1);
819 // Unbind one at a time.
820 MAP_MANAGER::iterator end
= map
.end ();
823 MAP_MANAGER::iterator iter
= map
.begin ();
826 ACE_TEST_ASSERT (map
.unbind ((*iter
).ext_id_
) != -1);
829 ACE_TEST_ASSERT (map
.current_size () == 0);
835 run_test (int (*ptf
) (size_t, size_t, int),
839 const ACE_TCHAR
*test_name
)
841 ACE_Profile_Timer timer
;
844 (*ptf
) (table_size
, iterations
, test_iterators
);
848 ACE_Profile_Timer::ACE_Elapsed_Time et
;
850 timer
.elapsed_time (et
);
852 ACE_TCHAR
*test_iterators_string
= 0;
855 test_iterators_string
=
856 const_cast<ACE_TCHAR
*> (ACE_TEXT ( "includes executing iterators"));
858 test_iterators_string
=
859 const_cast<ACE_TCHAR
*> (ACE_TEXT ("doesn't include executing iterators"));
861 ACE_DEBUG ((LM_DEBUG
,
862 ACE_TEXT ("time to test a map of size %d for %d iterations using %s (%s)\n"),
866 test_iterators_string
));
868 ACE_DEBUG ((LM_DEBUG
,
869 ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
874 ACE_timer_t time_result
= 0.0;
876 time_result
= (et
.real_time
/ ACE_timer_t (iterations
)) * 1000000;
878 ACE_DEBUG ((LM_DEBUG
,
879 ACE_TEXT ("time per call = %f usecs\n"),
886 run_main (int argc
, ACE_TCHAR
*argv
[])
888 ACE_START_TEST (ACE_TEXT ("Map_Manager_Test"));
889 ACE_LOG_MSG
->clr_flags (ACE_Log_Msg::VERBOSE_LITE
);
891 size_t table_size
= ACE_MAX_ITERATIONS
/ 2;
892 size_t iterations
= ACE_MAX_ITERATIONS
;
893 int test_iterators
= 1;
896 table_size
= ACE_OS::atoi (argv
[1]);
899 iterations
= ACE_OS::atoi (argv
[2]);
902 test_iterators
= ACE_OS::atoi (argv
[3]);
906 ACE_ERROR ((LM_ERROR
, "LM_ERROR@Table size is 0\n"));
911 // Test the <ACE_Map_Manager>.
912 result
+= run_test (&test_map_manager
,
916 ACE_TEXT ("Map_Manager"));
918 // Test the <ACE_Hash_Map_Manager>.
919 result
+= run_test (&test_hash_map_manager
,
923 ACE_TEXT ("Hash_Map_Manager"));
925 // Test the <ACE_Hash_Map_Manager>.
926 result
+= run_test (&test_active_map_manager
,
930 ACE_TEXT ("Active_Map_Manager"));
932 // Test the <ACE_Cache_Map_Manager>.
933 result
+= run_test (&test_cache_map_manager
,
937 ACE_TEXT ("Cache_Map_Manager"));
939 // Test the <ACE_Hash_Cache_Map_Manager>.
940 result
+= run_test (&test_hash_cache_map_manager
,
944 ACE_TEXT ("Hash_Cache_Map_Manager"));
946 ACE_LOG_MSG
->set_flags (ACE_Log_Msg::VERBOSE_LITE
);