2 * @file Notification_Queue_Unit_Test.cpp
4 * A unit test for the ACE_Notification_Queue class.
6 * @author Carlos O'Ryan <coryan@atdesk.com>
9 #include "test_config.h"
10 #include "ace/Notification_Queue.h"
14 ACTION(pop_returns_element_pushed) \
15 ACTION(purge_empty_queue) \
16 ACTION(purge_with_no_matches) \
17 ACTION(purge_with_single_match) \
18 ACTION(purge_with_multiple_matches) \
19 ACTION(reset_empty_queue) \
20 ACTION(reset_non_empty_queue) \
22 // Declare all the tests
23 #define ACTION(TEST_NAME) void TEST_NAME (char const * test_name);
28 run_main (int, ACE_TCHAR
*[])
30 ACE_START_TEST (ACE_TEXT ("Notification_Queue_Unit_Test"));
33 #define ACTION(TEST_NAME) TEST_NAME (#TEST_NAME);
42 // There are far more elegant ways to do this. Ideally one would use
43 // an existing framework (Boost.Test, TUT, CppTest). But this will
44 // do for our purposes
45 void test_equal(int x
, int y
, char const * x_msg
, char const * y_msg
,
46 char const * error_message
,
47 char const * test_name
, char const * filename
, int lineno
);
48 void test_equal(void * x
, void * y
, char const * x_msg
, char const * y_msg
,
49 char const * error_message
,
50 char const * test_name
, char const * filename
, int lineno
);
51 void test_not_equal(int x
, int y
, char const * x_msg
, char const * y_msg
,
52 char const * error_message
,
53 char const * test_name
, char const * filename
, int lineno
);
54 void test_assert(bool predicate
, char const * predicate_msg
,
55 char const * error_message
,
56 char const * test_name
, char const * filename
, int lineno
);
58 #define TEST_EQUAL(X, Y, MSG) \
59 test_equal((X), (Y), #X, #Y, MSG, test_name, __FILE__, __LINE__)
60 #define TEST_NOT_EQUAL(X, Y, MSG) \
61 test_not_equal((X), (Y), #X, #Y, MSG, test_name, __FILE__, __LINE__)
62 #define TEST_ASSERT(PREDICATE, MESSAGE) \
63 test_assert((PREDICATE), #PREDICATE, MESSAGE, test_name, __FILE__, __LINE__)
65 void null_test(char const * test_name
)
67 ACE_Notification_Queue queue
;
69 TEST_EQUAL(0, 0, "Test framework failure");
70 TEST_NOT_EQUAL(1, 0, "Test framework failure");
71 TEST_ASSERT(true, "True is still true");
74 class Event_Handler
: public ACE_Event_Handler
77 Event_Handler(int event_handler_id
)
79 , id (event_handler_id
)
86 void pop_returns_element_pushed(char const * test_name
)
88 ACE_Notification_Queue queue
;
94 int result
= queue
.push_new_notification(
95 ACE_Notification_Buffer(&eh1
,
96 ACE_Event_Handler::READ_MASK
));
97 TEST_ASSERT(result
== 1, "push[1] should return 1");
99 result
= queue
.push_new_notification(
100 ACE_Notification_Buffer(&eh2
,
101 ACE_Event_Handler::WRITE_MASK
));
102 TEST_ASSERT(result
== 0, "push[2] should return 0");
104 result
= queue
.push_new_notification(
105 ACE_Notification_Buffer(&eh3
,
106 ACE_Event_Handler::READ_MASK
|
107 ACE_Event_Handler::WRITE_MASK
));
108 TEST_ASSERT(result
== 0, "push[3] should return 0");
110 ACE_Notification_Buffer current
;
111 bool more_messages_queued
;
112 ACE_Notification_Buffer next
;
114 result
= queue
.pop_next_notification(current
, more_messages_queued
, next
);
115 TEST_ASSERT(result
== 1, "pop[0] should return 1");
116 TEST_ASSERT(more_messages_queued
, "pop[0] should have more messages");
118 TEST_EQUAL(current
.eh_
, &eh1
, "Wrong handler extracted");
119 TEST_EQUAL(current
.mask_
, ACE_Event_Handler::READ_MASK
,
120 "Wrong mask extracted");
122 result
= queue
.pop_next_notification(current
, more_messages_queued
, next
);
123 TEST_ASSERT(result
== 1, "pop[1] should return 1");
124 TEST_ASSERT(more_messages_queued
, "pop[1] should have more messages");
126 TEST_EQUAL(current
.eh_
, &eh2
, "Wrong handler extracted");
127 TEST_EQUAL(current
.mask_
, ACE_Event_Handler::WRITE_MASK
,
128 "Wrong mask extracted");
130 result
= queue
.pop_next_notification(current
, more_messages_queued
, next
);
131 TEST_ASSERT(result
== 1, "pop[2] should return 1");
132 TEST_ASSERT(!more_messages_queued
, "pop[2] should not have more messages");
134 TEST_EQUAL(current
.eh_
, &eh3
, "Wrong handler extracted");
135 TEST_EQUAL(current
.mask_
, ACE_Event_Handler::READ_MASK
|
136 ACE_Event_Handler::WRITE_MASK
,
137 "Wrong mask extracted");
139 more_messages_queued
= true;
140 result
= queue
.pop_next_notification(current
, more_messages_queued
, next
);
141 TEST_ASSERT(result
== 0, "pop[3] should return 0");
142 TEST_ASSERT(!more_messages_queued
, "pop[3] should not have more messages");
145 void purge_empty_queue(char const * test_name
)
147 ACE_Notification_Queue queue
;
149 Event_Handler
eh1(1);
151 int result
= queue
.purge_pending_notifications(&eh1
,
152 ACE_Event_Handler::READ_MASK
);
153 TEST_ASSERT(result
== 0, "purge of empty queue should return 0");
156 void purge_with_no_matches(char const * test_name
)
158 ACE_Notification_Queue queue
;
160 Event_Handler
eh1(1);
161 Event_Handler
eh2(2);
163 int result
= queue
.push_new_notification(
164 ACE_Notification_Buffer(&eh1
,
165 ACE_Event_Handler::READ_MASK
));
167 result
= queue
.purge_pending_notifications(&eh2
,
168 ACE_Event_Handler::READ_MASK
);
169 TEST_ASSERT(result
== 0, "purge of eh2 should return 0");
171 result
= queue
.purge_pending_notifications(&eh1
,
172 ACE_Event_Handler::WRITE_MASK
);
173 TEST_ASSERT(result
== 0, "purge of eh1/WRITE should return 0");
176 void purge_with_single_match(char const * test_name
)
178 ACE_Notification_Queue queue
;
180 Event_Handler
eh1(1);
181 Event_Handler
eh2(2);
183 int result
= queue
.push_new_notification(
184 ACE_Notification_Buffer(&eh1
,
185 ACE_Event_Handler::READ_MASK
|
186 ACE_Event_Handler::WRITE_MASK
));
187 result
= queue
.push_new_notification(
188 ACE_Notification_Buffer(&eh1
,
189 ACE_Event_Handler::WRITE_MASK
));
190 result
= queue
.push_new_notification(
191 ACE_Notification_Buffer(&eh2
,
192 ACE_Event_Handler::READ_MASK
));
193 result
= queue
.push_new_notification(
194 ACE_Notification_Buffer(&eh2
,
195 ACE_Event_Handler::WRITE_MASK
));
196 result
= queue
.push_new_notification(
197 ACE_Notification_Buffer(&eh2
,
198 ACE_Event_Handler::WRITE_MASK
));
199 result
= queue
.push_new_notification(
200 ACE_Notification_Buffer(&eh2
,
201 ACE_Event_Handler::WRITE_MASK
));
203 result
= queue
.purge_pending_notifications(&eh2
,
204 ACE_Event_Handler::READ_MASK
);
205 TEST_EQUAL(result
, 1, "purge of eh2/READ should return 1");
207 result
= queue
.purge_pending_notifications(&eh1
,
208 ACE_Event_Handler::READ_MASK
);
209 TEST_EQUAL(result
, 0, "purge of eh1/READ should return 0");
212 void purge_with_multiple_matches(char const * test_name
)
214 ACE_Notification_Queue queue
;
216 Event_Handler
eh1(1);
217 Event_Handler
eh2(2);
219 int result
= queue
.push_new_notification(
220 ACE_Notification_Buffer(&eh1
,
221 ACE_Event_Handler::READ_MASK
|
222 ACE_Event_Handler::WRITE_MASK
));
223 result
= queue
.push_new_notification(
224 ACE_Notification_Buffer(&eh1
,
225 ACE_Event_Handler::WRITE_MASK
));
226 result
= queue
.push_new_notification(
227 ACE_Notification_Buffer(&eh2
,
228 ACE_Event_Handler::READ_MASK
));
229 result
= queue
.push_new_notification(
230 ACE_Notification_Buffer(&eh2
,
231 ACE_Event_Handler::WRITE_MASK
));
232 result
= queue
.push_new_notification(
233 ACE_Notification_Buffer(&eh2
,
234 ACE_Event_Handler::WRITE_MASK
));
235 result
= queue
.push_new_notification(
236 ACE_Notification_Buffer(&eh2
,
237 ACE_Event_Handler::WRITE_MASK
));
239 result
= queue
.purge_pending_notifications(&eh2
,
240 ACE_Event_Handler::WRITE_MASK
);
241 TEST_EQUAL(result
, 3, "purge of eh2/WRITE should return 3");
243 result
= queue
.purge_pending_notifications(&eh1
,
244 ACE_Event_Handler::WRITE_MASK
);
245 TEST_EQUAL(result
, 1, "purge of eh1/WRITE should return 1");
248 void reset_empty_queue(char const * /* test_name */)
250 ACE_Notification_Queue queue
;
255 void reset_non_empty_queue(char const * /* test_name */)
257 ACE_Notification_Queue queue
;
259 Event_Handler
eh1(1);
260 Event_Handler
eh2(2);
262 queue
.push_new_notification(
263 ACE_Notification_Buffer(0,
264 ACE_Event_Handler::READ_MASK
));
265 queue
.push_new_notification(
266 ACE_Notification_Buffer(&eh1
,
267 ACE_Event_Handler::READ_MASK
));
268 queue
.push_new_notification(
269 ACE_Notification_Buffer(&eh2
,
270 ACE_Event_Handler::WRITE_MASK
));
271 queue
.push_new_notification(
272 ACE_Notification_Buffer(0,
273 ACE_Event_Handler::WRITE_MASK
));
278 void test_equal(int x
, int y
, char const * x_msg
, char const * y_msg
,
279 char const * error_message
,
280 char const * test_name
, char const * filename
, int lineno
)
283 ACE_ERROR ((LM_ERROR
,
284 ACE_TEXT("%C in (%C %C:%d) %C (%d) != %C (%d)\n"),
286 test_name
, filename
, lineno
,
287 x_msg
, x
, y_msg
, y
));
290 void test_equal(void * x
, void * y
, char const * x_msg
, char const * y_msg
,
291 char const * error_message
,
292 char const * test_name
, char const * filename
, int lineno
)
295 ACE_ERROR ((LM_ERROR
,
296 ACE_TEXT("%C in (%C %C:%d) %C (%@) != %C (%@)\n"),
298 test_name
, filename
, lineno
,
299 x_msg
, x
, y_msg
, y
));
302 void test_not_equal(int x
, int y
, char const * x_msg
, char const * y_msg
,
303 char const * error_message
,
304 char const * test_name
, char const * filename
, int lineno
)
307 ACE_ERROR ((LM_ERROR
,
308 ACE_TEXT("%C in (%C %C:%d) %C (%d) != %C (%d)\n"),
310 test_name
, filename
, lineno
,
311 x_msg
, x
, y_msg
, y
));
314 void test_assert(bool predicate
, char const * predicate_msg
,
315 char const * error_message
,
316 char const * test_name
, char const * filename
, int lineno
)
318 if (predicate
) return;
319 ACE_ERROR ((LM_ERROR
,
320 ACE_TEXT("Assertion in (%C %C:%d) %C %C\n"),
321 test_name
, filename
, lineno
,
322 predicate_msg
, error_message
));