Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / ACE / tests / Lazy_Map_Manager_Test.cpp
blob7af3f0b067576d9c61198b43d6bc408de5351706
2 //=============================================================================
3 /**
4 * @file Lazy_Map_Manager_Test.cpp
6 * This is a simple test of the <ACE_Map_Manager> and
7 * <ACE_Active_Map_Manager> that illustrates how lazy map managers
8 * allow the deletion of entries while iterating over the map.
10 * @author Irfan Pyarali <irfan@cs.wustl.edu>
12 //=============================================================================
15 #include "test_config.h"
16 #include "ace/Map_Manager.h"
17 #include "ace/Active_Map_Manager.h"
21 // Simple map manager.
22 typedef ACE_Map_Manager<int, int, ACE_Null_Mutex> LAZY_MAP;
24 // Displaying the contents of a map manager.
26 void
27 display_map (LAZY_MAP &map)
30 // Simple iteration printing the entries.
31 for (LAZY_MAP::iterator iter = map.begin ();
32 iter != map.end ();
33 ++iter)
35 LAZY_MAP::ENTRY &entry = *iter;
36 ACE_DEBUG ((LM_DEBUG,
37 ACE_TEXT ("%d "),
38 entry.int_id_));
41 ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("\n")));
45 // Simple reverse iteration printing the entries.
46 for (LAZY_MAP::reverse_iterator iter = map.rbegin ();
47 iter != map.rend ();
48 ++iter)
50 LAZY_MAP::ENTRY &entry = *iter;
51 ACE_DEBUG ((LM_DEBUG,
52 ACE_TEXT ("%d "),
53 entry.int_id_));
56 ACE_DEBUG ((LM_DEBUG,
57 ACE_TEXT ("\n")));
60 ACE_DEBUG ((LM_DEBUG,
61 ACE_TEXT ("\n")));
64 // Test for map manager.
66 void
67 map_test (void)
69 // Map of size 3.
70 LAZY_MAP map (3);
71 int i = 0;
73 // Insert a few entries.
74 for (i = 0; i < 3; ++i)
75 map.bind (i, i);
77 display_map (map);
79 // Remove middle one.
80 map.unbind (1);
82 display_map (map);
84 // Remove the entry on one end.
85 map.unbind (0);
87 display_map (map);
89 // Remove the entry on the other end.
90 map.unbind (2);
92 display_map (map);
94 // If we have lazy map managers, we can delete entries while
95 // iterating over the map.
97 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
99 // Insert a few entries.
100 for (i = 0; i < 3; ++i)
101 map.bind (i, i);
103 display_map (map);
105 // Remove middle one.
107 // Deletion while iterating.
108 for (LAZY_MAP::iterator iter = map.begin ();
109 iter != map.end ();
110 ++iter)
112 LAZY_MAP::ENTRY &entry = *iter;
113 if (entry.int_id_ == 1)
114 map.unbind (1);
117 display_map (map);
120 // Remove the entry on one end.
122 // Deletion while iterating.
123 for (LAZY_MAP::iterator iter = map.begin ();
124 iter != map.end ();
125 ++iter)
127 LAZY_MAP::ENTRY &entry = *iter;
128 if (entry.int_id_ == 0)
129 map.unbind (0);
132 display_map (map);
135 // Remove the entry on the other end.
137 // Deletion while iterating.
138 for (LAZY_MAP::iterator iter = map.begin ();
139 iter != map.end ();
140 ++iter)
142 LAZY_MAP::ENTRY &entry = *iter;
143 if (entry.int_id_ == 2)
144 map.unbind (2);
147 display_map (map);
150 #endif /* ACE_HAS_LAZY_MAP_MANAGER */
152 // Insert a few entries. This will force an increase in map size.
153 for (i = 0; i < 4; ++i)
154 map.bind (i, i);
156 display_map (map);
158 // Remove a few entries (in reverse order).
159 for (i = 3; i >= 0; --i)
160 map.unbind (i);
162 display_map (map);
165 // Simple active map manager.
166 typedef ACE_Active_Map_Manager<int> ACTIVE_MAP;
168 // Displaying the contents of an active map manager.
170 void
171 display_map (ACTIVE_MAP &map)
174 // Simple iteration printing the entries.
175 for (ACTIVE_MAP::iterator iter = map.begin ();
176 iter != map.end ();
177 ++iter)
179 ACTIVE_MAP::ENTRY &entry = *iter;
180 ACE_DEBUG ((LM_DEBUG,
181 ACE_TEXT ("%d "),
182 entry.int_id_));
185 ACE_DEBUG ((LM_DEBUG,
186 ACE_TEXT ("\n")));
190 // Simple reverse iteration printing the entries.
191 for (ACTIVE_MAP::reverse_iterator iter = map.rbegin ();
192 iter != map.rend ();
193 ++iter)
195 ACTIVE_MAP::ENTRY &entry = *iter;
196 ACE_DEBUG ((LM_DEBUG,
197 ACE_TEXT ("%d "),
198 entry.int_id_));
201 ACE_DEBUG ((LM_DEBUG,
202 ACE_TEXT ("\n")));
205 ACE_DEBUG ((LM_DEBUG,
206 ACE_TEXT ("\n")));
209 // Test for active map manager.
211 void
212 active_map_test (void)
214 // Map of size 3.
215 ACTIVE_MAP map (3);
216 ACE_Active_Map_Manager_Key keys[4];
217 int i = 0;
219 // Insert a few entries.
220 for (i = 0; i < 3; ++i)
221 map.bind (i, keys[i]);
223 display_map (map);
225 // Remove middle one.
226 map.unbind (keys[1]);
228 display_map (map);
230 // Remove the entry on one end.
231 map.unbind (keys[0]);
233 display_map (map);
235 // Remove the entry on the other end.
236 map.unbind (keys[2]);
238 display_map (map);
240 // If we have lazy map managers, we can delete entries while
241 // iterating over the map.
243 #if defined (ACE_HAS_LAZY_MAP_MANAGER)
245 // Insert a few entries.
246 for (i = 0; i < 3; ++i)
247 map.bind (i, keys[i]);
249 display_map (map);
251 // Remove middle one.
253 // Deletion while iterating.
254 for (ACTIVE_MAP::iterator iter = map.begin ();
255 iter != map.end ();
256 ++iter)
258 ACTIVE_MAP::ENTRY &entry = *iter;
259 if (entry.int_id_ == 1)
260 map.unbind (keys[1]);
263 display_map (map);
266 // Remove the entry on one end.
268 // Deletion while iterating.
269 for (ACTIVE_MAP::iterator iter = map.begin ();
270 iter != map.end ();
271 ++iter)
273 ACTIVE_MAP::ENTRY &entry = *iter;
274 if (entry.int_id_ == 0)
275 map.unbind (keys[0]);
278 display_map (map);
281 // Remove the entry on the other end.
283 // Deletion while iterating.
284 for (ACTIVE_MAP::iterator iter = map.begin ();
285 iter != map.end ();
286 ++iter)
288 ACTIVE_MAP::ENTRY &entry = *iter;
289 if (entry.int_id_ == 2)
290 map.unbind (keys[2]);
293 display_map (map);
296 #endif /* ACE_HAS_LAZY_MAP_MANAGER */
298 // Insert a few entries. This will force an increase in map size.
299 for (i = 0; i < 4; ++i)
300 map.bind (i, keys[i]);
302 display_map (map);
304 // Remove a few entries (in reverse order).
305 for (i = 3; i >= 0; --i)
306 map.unbind (keys[i]);
308 display_map (map);
312 run_main (int, ACE_TCHAR *[])
314 ACE_START_TEST (ACE_TEXT ("Lazy_Map_Manager_Test"));
315 ACE_LOG_MSG->clr_flags (ACE_Log_Msg::VERBOSE_LITE);
317 ACE_DEBUG ((LM_DEBUG,
318 ACE_TEXT ("\nMap Manager...\n\n")));
319 map_test ();
321 ACE_DEBUG ((LM_DEBUG,
322 ACE_TEXT ("\nActive Map Manager...\n\n")));
323 active_map_test ();
325 ACE_LOG_MSG->set_flags (ACE_Log_Msg::VERBOSE_LITE);
326 ACE_END_TEST;
327 return 0;