summaryrefslogtreecommitdiff
path: root/ACE/tests/Notification_Queue_Unit_Test.cpp
blob: cc138e3fc037c8b5740ba74adf5c7316729400f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
 * @file Notification_Queue_Unit_Test.cpp
 *
 * $Id$
 *
 * A unit test for the ACE_Notification_Queue class.
 *
 * @author Carlos O'Ryan <coryan@atdesk.com>
 *
 */

#include "test_config.h"
#include "ace/Notification_Queue.h"

ACE_RCSID(tests,
          Notification_Queue_Unit_Test, "$Id$")

#define TEST_LIST \
  ACTION(null_test) \
  ACTION(pop_returns_element_pushed) \
  ACTION(purge_empty_queue) \
  ACTION(purge_with_no_matches) \
  ACTION(purge_with_single_match) \
  ACTION(purge_with_multiple_matches) \

// Declare all the tests
#define ACTION(TEST_NAME) void TEST_NAME (char const * test_name);
TEST_LIST
#undef ACTION

int
run_main (int, ACE_TCHAR *[])
{
  ACE_START_TEST (ACE_TEXT ("Notification_Queue_Unit_Test"));

  // Call all the tests
#define ACTION(TEST_NAME) TEST_NAME (#TEST_NAME);
TEST_LIST
#undef ACTION

  ACE_END_TEST;

  return 0;
}

// There are far more elegant ways to do this.  Ideally one would use
// an existing framework (Boost.Test, TUT, CppTest).  But this will
// do for our purposes
#define TEST_INTEGER_EQUAL(X, Y, MSG) \
  do { \
    if ((X) == (Y)) break; \
    ACE_ERROR ((LM_ERROR, \
		ACE_TEXT("%C in (%C %N:%l) %C (%d) != %C (%d)\n"), \
		ACE_TEXT(MSG), test_name, \
		ACE_TEXT(#X), (X), ACE_TEXT(#Y), (Y) )); \
  } while(0)
#define TEST_INTEGER_NOT_EQUAL(X, Y, MSG) \
  do { \
    if ((X) != (Y)) break; \
    ACE_ERROR ((LM_ERROR, \
		ACE_TEXT("%C in (%C %N:%l) %C (%d) == %C (%d)\n"), \
		ACE_TEXT(MSG), test_name, \
		ACE_TEXT(#X), (X), ACE_TEXT(#Y), (Y) )); \
  } while(0)
#define TEST_ASSERT(PREDICATE, MESSAGE) \
  do { \
    if ((PREDICATE)) break; \
    ACE_ERROR ((LM_ERROR, \
		ACE_TEXT("Assertion failure in (%C %N:%l) %C %C\n"), \
		test_name, ACE_TEXT(#PREDICATE), MESSAGE )); \
  } while(0)


void null_test(char const * test_name)
{
  ACE_Notification_Queue queue;

  TEST_INTEGER_EQUAL(0, 0, "Test framework failure");
  TEST_INTEGER_NOT_EQUAL(1, 0, "Test framework failure");
  TEST_ASSERT(true, ACE_TEXT("True is still true"));
}

class Event_Handler : public ACE_Event_Handler
{
public:
  Event_Handler(int event_handler_id)
    : ACE_Event_Handler()
    , id (event_handler_id)
  {
  }

  int id;
};

void pop_returns_element_pushed(char const * test_name)
{
  ACE_Notification_Queue queue;

  Event_Handler eh1(1);
  Event_Handler eh2(2);
  Event_Handler eh3(2);

  int result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::READ_MASK));
  TEST_ASSERT(result == 1, "push[1] should return 1");

  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));
  TEST_ASSERT(result == 0, "push[2] should return 0");

  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh3,
			      ACE_Event_Handler::READ_MASK
			      |ACE_Event_Handler::WRITE_MASK));
  TEST_ASSERT(result == 0, "push[3] should return 0");

  ACE_Notification_Buffer current;
  bool more_messages_queued;
  ACE_Notification_Buffer next;

  result = queue.pop_next_notification(current, more_messages_queued, next);
  TEST_ASSERT(result == 1, "pop[0] should return 1");
  TEST_ASSERT(more_messages_queued, "pop[0] should have more messages");

  TEST_INTEGER_EQUAL(current.eh_, &eh1, "Wrong handler extracted");
  TEST_INTEGER_EQUAL(current.mask_, ACE_Event_Handler::READ_MASK,
		     "Wrong mask extracted");

  result = queue.pop_next_notification(current, more_messages_queued, next);
  TEST_ASSERT(result == 1, "pop[1] should return 1");
  TEST_ASSERT(more_messages_queued, "pop[1] should have more messages");

  TEST_INTEGER_EQUAL(current.eh_, &eh2, "Wrong handler extracted");
  TEST_INTEGER_EQUAL(current.mask_, ACE_Event_Handler::WRITE_MASK,
		     "Wrong mask extracted");

  result = queue.pop_next_notification(current, more_messages_queued, next);
  TEST_ASSERT(result == 1, "pop[2] should return 1");
  TEST_ASSERT(!more_messages_queued, "pop[2] should not have more messages");

  TEST_INTEGER_EQUAL(current.eh_, &eh3, "Wrong handler extracted");
  TEST_INTEGER_EQUAL(current.mask_, ACE_Event_Handler::READ_MASK
		     |ACE_Event_Handler::WRITE_MASK,
		     "Wrong mask extracted");

  more_messages_queued = true;
  result = queue.pop_next_notification(current, more_messages_queued, next);
  TEST_ASSERT(result == 0, "pop[3] should return 0");
  TEST_ASSERT(!more_messages_queued, "pop[3] should not have more messages");
}

void purge_empty_queue(char const * test_name)
{
  ACE_Notification_Queue queue;

  Event_Handler eh1(1);

  int result = queue.purge_pending_notifications(&eh1,
						 ACE_Event_Handler::READ_MASK);
  TEST_ASSERT(result == 0, "purge of empty queue should return 0");
}

void purge_with_no_matches(char const * test_name)
{
  ACE_Notification_Queue queue;

  Event_Handler eh1(1);
  Event_Handler eh2(2);

  int result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::READ_MASK));

  result = queue.purge_pending_notifications(&eh2,
					     ACE_Event_Handler::READ_MASK);
  TEST_ASSERT(result == 0, "purge of eh2 should return 0");

  result = queue.purge_pending_notifications(&eh1,
					     ACE_Event_Handler::WRITE_MASK);
  TEST_ASSERT(result == 0, "purge of eh1/WRITE should return 0");
}

void purge_with_single_match(char const * test_name)
{
  ACE_Notification_Queue queue;

  Event_Handler eh1(1);
  Event_Handler eh2(2);

  int result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::READ_MASK
			      |ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::READ_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));

  result = queue.purge_pending_notifications(&eh2,
					     ACE_Event_Handler::READ_MASK);
  TEST_INTEGER_EQUAL(result, 1, "purge of eh2/READ should return 1");

  result = queue.purge_pending_notifications(&eh1,
					     ACE_Event_Handler::READ_MASK);
  TEST_INTEGER_EQUAL(result, 0, "purge of eh1/READ should return 0");
}

void purge_with_multiple_matches(char const * test_name)
{
  ACE_Notification_Queue queue;

  Event_Handler eh1(1);
  Event_Handler eh2(2);

  int result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::READ_MASK
			      |ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh1,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::READ_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));
  result = queue.push_new_notification(
      ACE_Notification_Buffer(&eh2,
			      ACE_Event_Handler::WRITE_MASK));

  result = queue.purge_pending_notifications(&eh2,
					     ACE_Event_Handler::WRITE_MASK);
  TEST_INTEGER_EQUAL(result, 3, "purge of eh2/WRITE should return 3");

  result = queue.purge_pending_notifications(&eh1,
					     ACE_Event_Handler::WRITE_MASK);
  TEST_INTEGER_EQUAL(result, 1, "purge of eh1/WRITE should return 1");
}