2 //=============================================================================
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
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"
21 test_duplicates (size_t count
)
24 ACE_TEXT ("Testing %B duplicates\n"), count
));
26 size_t duplicates
= 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
);
40 if (handle_set
.is_set ((ACE_HANDLE
) handle
))
43 handle_set
.set_bit ((ACE_HANDLE
) handle
);
48 if (handle_set
.is_set ((ACE_HANDLE
) handle
))
51 handle_set
.clr_bit ((ACE_HANDLE
) handle
);
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
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
[] =
93 (ACE_HANDLE
) (ACE_DEFAULT_SELECT_REACTOR_SIZE
- 1),
94 (ACE_HANDLE
) ACE_DEFAULT_SELECT_REACTOR_SIZE
,
97 #if defined (ACE_WIN64)
99 #endif /* ACE_WIN64 */
104 ACE_Handle_Set handle_set
;
105 ACE_Unbounded_Set
<ACE_HANDLE
> set
;
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 */
131 handle_vector
[i
] != ACE_INVALID_HANDLE
;
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 */
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"),
153 int const done
= set
.remove (handle
);
154 ACE_TEST_ASSERT (done
== 0);
158 ACE_TEST_ASSERT (count
== handle_set
.num_set ());
162 test_performance (size_t max_handles
,
163 size_t max_iterations
)
165 ACE_Handle_Set handle_set
;
168 for (i
= 0; i
< max_handles
; i
++)
169 handle_set
.set_bit ((ACE_HANDLE
) i
);
171 ACE_Profile_Timer timer
;
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
)
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"),
200 ACE_DEBUG ((LM_DEBUG
,
201 ACE_TEXT ("time per each of the %d calls = %f usecs\n"),
203 et
.real_time
/ double (count
) * 1000000));
207 run_main (int argc
, ACE_TCHAR
*argv
[])
209 ACE_START_TEST (ACE_TEXT ("Handle_Set_Test"));
212 ? ACE_OS::atoi (argv
[1])
213 : ACE_Handle_Set::MAXSIZE
;
216 ? ACE_OS::atoi (argv
[2])
217 : ACE_Handle_Set::MAXSIZE
;
218 size_t max_iterations
=
220 ? ACE_OS::atoi (argv
[3])
221 : ACE_MAX_ITERATIONS
;
223 test_duplicates (count
);
225 test_performance (max_handles
,