Use =default for skeleton copy constructor
[ACE_TAO.git] / ACE / tests / Handle_Set_Test.cpp
blob8fdb69ac7f42f70b685e203a957492cbcf29b456
2 //=============================================================================
3 /**
4 * @file Handle_Set_Test.cpp
6 * This test illustrates the use of ACE_Handle_Set to maintain a
7 * set of handles. No command line arguments are needed to run
8 * the test.
10 * @author Prashant Jain <pjain@cs.wustl.edu>
12 //=============================================================================
14 #include "test_config.h"
15 #include "ace/OS_NS_stdlib.h"
16 #include "ace/Profile_Timer.h"
17 #include "ace/Handle_Set.h"
18 #include "ace/Containers.h"
20 static void
21 test_duplicates (size_t count)
23 ACE_DEBUG ((LM_DEBUG,
24 ACE_TEXT ("Testing %B duplicates\n"), count));
26 size_t duplicates = 0;
27 size_t sets = 0;
28 size_t clears = 0;
30 ACE_Handle_Set handle_set;
32 u_int seed = (u_int) ACE_OS::time (0);
34 for (size_t i = 0; i < count; i++)
36 size_t const handle = static_cast<size_t> (ACE_OS::rand_r (&seed) % ACE_Handle_Set::MAXSIZE);
38 if (ACE_ODD (handle))
40 if (handle_set.is_set ((ACE_HANDLE) handle))
41 ++duplicates;
43 handle_set.set_bit ((ACE_HANDLE) handle);
44 ++sets;
46 else
48 if (handle_set.is_set ((ACE_HANDLE) handle))
49 --duplicates;
51 handle_set.clr_bit ((ACE_HANDLE) handle);
52 ++clears;
56 ACE_TEST_ASSERT (count == sets + clears);
57 ACE_TEST_ASSERT (handle_set.num_set () + duplicates == sets);
59 ACE_Handle_Set copy_set (handle_set);
60 ACE_TEST_ASSERT (copy_set.num_set () + duplicates == sets);
62 ACE_Handle_Set move_set (std::move(handle_set));
63 ACE_TEST_ASSERT (move_set.num_set () + duplicates == sets);
65 ACE_Handle_Set move_assign = std::move(copy_set);
66 ACE_TEST_ASSERT (move_assign.num_set () + duplicates == sets);
69 // This is the vector of handles to test. These numbers are chosen to
70 // test for boundaries conditions. Note that if
71 // <ACE_DEFAULT_SELECT_REACTOR_SIZE> is less than any of these
72 // <HANDLE> values, the logic in <test_boundaries> will simply ignore
73 // these values.
74 #if defined (ACE_WIN64)
75 // The casts below are legit...
76 # pragma warning(push)
77 # pragma warning(disable : 4312)
78 #endif /* ACE_WIN64 */
79 static ACE_HANDLE handle_vector[] =
81 (ACE_HANDLE) 0,
82 (ACE_HANDLE) 1,
83 (ACE_HANDLE) 31,
84 (ACE_HANDLE) 32,
85 (ACE_HANDLE) 63,
86 (ACE_HANDLE) 64,
87 (ACE_HANDLE) 65,
88 (ACE_HANDLE) 127,
89 (ACE_HANDLE) 128,
90 (ACE_HANDLE) 129,
91 (ACE_HANDLE) 254,
92 (ACE_HANDLE) 255,
93 (ACE_HANDLE) (ACE_DEFAULT_SELECT_REACTOR_SIZE - 1),
94 (ACE_HANDLE) ACE_DEFAULT_SELECT_REACTOR_SIZE,
95 ACE_INVALID_HANDLE
97 #if defined (ACE_WIN64)
98 # pragma warning(pop)
99 #endif /* ACE_WIN64 */
101 static void
102 test_boundaries ()
104 ACE_Handle_Set handle_set;
105 ACE_Unbounded_Set<ACE_HANDLE> set;
106 ACE_HANDLE handle;
108 // First test an empty set.
110 for (ACE_Handle_Set_Iterator i1 (handle_set);
111 (handle = i1 ()) != ACE_INVALID_HANDLE;
114 ACE_TEST_ASSERT (0 ==
115 ACE_TEXT ("this shouldn't get called since ")
116 ACE_TEXT ("the set is empty!\n"));
119 ACE_DEBUG ((LM_DEBUG,
120 ACE_TEXT ("ACE_DEFAULT_SELECT_REACTOR_SIZE %d\n"),
121 ACE_DEFAULT_SELECT_REACTOR_SIZE));
123 // Insert the vector of <ACE_HANDLE>s into the set.
125 #if defined (ACE_WIN64)
126 // The casts below are legit...
127 # pragma warning(push)
128 # pragma warning(disable : 4312)
129 #endif /* ACE_WIN64 */
130 for (int i = 0;
131 handle_vector[i] != ACE_INVALID_HANDLE;
132 i++)
134 if (handle_vector[i] < (ACE_HANDLE) ACE_DEFAULT_SELECT_REACTOR_SIZE)
136 handle_set.set_bit (handle_vector[i]);
137 set.insert (handle_vector[i]);
140 #if defined (ACE_WIN64)
141 # pragma warning(pop)
142 #endif /* ACE_WIN64 */
144 int count = 0;
146 for (ACE_Handle_Set_Iterator i2 (handle_set);
147 (handle = i2 ()) != ACE_INVALID_HANDLE;
150 ACE_DEBUG ((LM_DEBUG,
151 ACE_TEXT ("obtained handle %d\n"),
152 handle));
153 int const done = set.remove (handle);
154 ACE_TEST_ASSERT (done == 0);
155 count++;
158 ACE_TEST_ASSERT (count == handle_set.num_set ());
161 static void
162 test_performance (size_t max_handles,
163 size_t max_iterations)
165 ACE_Handle_Set handle_set;
166 size_t i;
168 for (i = 0; i < max_handles; i++)
169 handle_set.set_bit ((ACE_HANDLE) i);
171 ACE_Profile_Timer timer;
172 size_t count = 0;
174 timer.start ();
176 for (i = 0; i < max_iterations; i++)
178 ACE_Handle_Set_Iterator iter (handle_set);
180 // Only iterate up to <handle_set.max_set ()>.
181 while (iter () != ACE_INVALID_HANDLE)
182 count++;
185 timer.stop ();
187 ACE_TEST_ASSERT (count == max_handles * max_iterations);
189 ACE_Profile_Timer::ACE_Elapsed_Time et;
191 timer.elapsed_time (et);
193 ACE_DEBUG ((LM_DEBUG,
194 ACE_TEXT ("real time = %f secs, user time = %f secs, ")
195 ACE_TEXT ("system time = %f secs\n"),
196 et.real_time,
197 et.user_time,
198 et.system_time));
200 ACE_DEBUG ((LM_DEBUG,
201 ACE_TEXT ("time per each of the %d calls = %f usecs\n"),
202 count,
203 et.real_time / double (count) * 1000000));
207 run_main (int argc, ACE_TCHAR *argv[])
209 ACE_START_TEST (ACE_TEXT ("Handle_Set_Test"));
211 int count = argc > 1
212 ? ACE_OS::atoi (argv[1])
213 : ACE_Handle_Set::MAXSIZE;
214 size_t max_handles =
215 argc > 2
216 ? ACE_OS::atoi (argv[2])
217 : ACE_Handle_Set::MAXSIZE;
218 size_t max_iterations =
219 argc > 3
220 ? ACE_OS::atoi (argv[3])
221 : ACE_MAX_ITERATIONS;
223 test_duplicates (count);
224 test_boundaries ();
225 test_performance (max_handles,
226 max_iterations);
227 ACE_END_TEST;
228 return 0;