Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / unbounded_octet_sequence_nocopy_ut.cpp
bloba3521bf16aa2ba57b1aeb813e287419090e83cef
1 /**
2 * @file
4 * @brief Unit test for unbounded sequences of octet types using the TAO
5 * specific no copy specialization
7 * @author Johnny Willemsen
8 */
9 #include "testing_allocation_traits.hpp"
10 #include "testing_range_checking.hpp"
12 #include "tao/Unbounded_Octet_Sequence_T.h"
14 #include "value_sequence_tester.hpp"
16 #include "test_macros.h"
18 #include "tao/Basic_Types.h"
19 #include "tao/CDR.h"
21 #include "ace/OS_NS_stdio.h"
24 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
26 typedef unbounded_value_sequence<CORBA::Octet> tested_sequence;
27 typedef tested_sequence::element_traits tested_element_traits;
28 typedef tested_sequence::allocation_traits tested_allocation_traits;
29 typedef details::range_checking<CORBA::Octet,true> range;
31 struct Tester
33 typedef tested_sequence::value_type value_type;
35 int test_copy_constructor_from_ulong()
37 expected_calls a(tested_allocation_traits::allocbuf_calls);
38 expected_calls f(tested_allocation_traits::freebuf_calls);
40 tested_sequence x(16);
41 FAIL_RETURN_IF_NOT(a.expect(1), a);
42 x.length(8);
44 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
45 CHECK_EQUAL(CORBA::ULong(8), x.length());
46 CHECK_EQUAL(true, x.release());
48 tested_sequence y(x);
49 FAIL_RETURN_IF_NOT(a.expect(1), a);
50 CHECK_EQUAL(CORBA::ULong(16), y.maximum());
51 CHECK_EQUAL(CORBA::ULong(8), y.length());
52 CHECK_EQUAL(true, y.release());
54 FAIL_RETURN_IF_NOT(f.expect(2), f);
55 return 0;
58 int test_assignment_from_ulong()
60 expected_calls a(tested_allocation_traits::allocbuf_calls);
61 expected_calls f(tested_allocation_traits::freebuf_calls);
63 tested_sequence x(16);
64 x.length(8);
65 FAIL_RETURN_IF_NOT(a.expect(1), a);
66 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
67 CHECK_EQUAL(CORBA::ULong(8), x.length());
68 CHECK_EQUAL(true, x.release());
70 tested_sequence y;
71 FAIL_RETURN_IF_NOT(a.expect(0), a);
73 y = x;
74 FAIL_RETURN_IF_NOT(a.expect(1), a);
75 // Since above no allocation for y was done then
76 // no deallocation needed during assignment.
77 FAIL_RETURN_IF_NOT(f.expect(0), f);
78 CHECK_EQUAL(CORBA::ULong(16), y.maximum());
79 CHECK_EQUAL(CORBA::ULong(8), y.length());
80 CHECK_EQUAL(true, y.release());
82 FAIL_RETURN_IF_NOT(f.expect(2), f);
83 return 0;
86 int test_ulong_constructor()
88 expected_calls a(tested_allocation_traits::allocbuf_calls);
89 expected_calls f(tested_allocation_traits::freebuf_calls);
91 tested_sequence x(16);
93 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
94 CHECK_EQUAL(CORBA::ULong(0), x.length());
95 CHECK_EQUAL(true, x.release());
97 FAIL_RETURN_IF_NOT(a.expect(1), a);
98 FAIL_RETURN_IF_NOT(f.expect(1), f);
99 return 0;
102 int test_exception_in_ulong_constructor()
104 expected_calls a(tested_allocation_traits::allocbuf_calls);
105 expected_calls f(tested_allocation_traits::freebuf_calls);
107 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
108 CHECK_THROW(tested_sequence x(16), testing_exception);
109 FAIL_RETURN_IF_NOT(a.expect(1), a);
111 FAIL_RETURN_IF_NOT(f.expect(0), f);
112 return 0;
115 int test_set_length_less_than_maximum()
117 expected_calls a(tested_allocation_traits::allocbuf_calls);
118 expected_calls f(tested_allocation_traits::freebuf_calls);
120 tested_sequence x(16);
122 x.length(8);
123 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
124 CHECK_EQUAL(CORBA::ULong(8), x.length());
125 CHECK_EQUAL(true, x.release());
127 FAIL_RETURN_IF_NOT(a.expect(1), a);
128 FAIL_RETURN_IF_NOT(f.expect(1), f);
129 return 0;
132 int test_set_length_more_than_maximum()
134 expected_calls a(tested_allocation_traits::allocbuf_calls);
135 expected_calls f(tested_allocation_traits::freebuf_calls);
137 tested_sequence x(16);
138 FAIL_RETURN_IF_NOT(a.expect(1), a);
140 x.length(32);
141 FAIL_RETURN_IF_NOT(a.expect(1), a);
142 FAIL_RETURN_IF_NOT(f.expect(1), f);
144 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
145 CHECK_EQUAL(CORBA::ULong(32), x.length());
146 CHECK_EQUAL(true, x.release());
148 FAIL_RETURN_IF_NOT(f.expect(1), f);
149 return 0;
152 int test_exception_in_set_length()
154 expected_calls f(tested_allocation_traits::freebuf_calls);
156 tested_sequence x;
158 expected_calls a(tested_allocation_traits::allocbuf_calls);
159 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
160 CHECK_THROW(x.length(8), testing_exception);
161 FAIL_RETURN_IF_NOT(a.expect(1), a);
163 // length() above tried to allocate a buffer but it didn't reach
164 // new[] and sequence was not changed, thus no need to deallocate.
165 FAIL_RETURN_IF_NOT(f.expect(0), f);
166 return 0;
169 value_type * alloc_and_init_buffer()
171 value_type * buf = tested_sequence::allocbuf(8);
172 buf[0] = 1; buf[1] = 4; buf[2] = 9; buf[3] = 16;
174 return buf;
177 ACE_Message_Block * alloc_and_init_mb()
179 char buf[9];
180 ACE_OS::sprintf (buf, "%s", "testing ");
181 size_t n = (ACE_OS::strlen (buf) + 1) * sizeof (char);
182 ACE_Message_Block * mb = new ACE_Message_Block (n);
183 mb->copy ((char *) buf, n);
185 return mb;
188 int test_buffer_constructor_default()
190 value_type * buffer = alloc_and_init_buffer();
192 expected_calls a(tested_allocation_traits::allocbuf_calls);
193 expected_calls f(tested_allocation_traits::freebuf_calls);
195 tested_sequence a(8, 4, buffer);
196 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
197 CHECK_EQUAL(CORBA::ULong(4), a.length());
198 CHECK_EQUAL(buffer, a.get_buffer());
199 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
200 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
201 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
202 CHECK_EQUAL(CORBA::Octet(16), a[3]);
203 CHECK_EQUAL(false, a.release());
205 FAIL_RETURN_IF_NOT(a.expect(0), a);
206 FAIL_RETURN_IF_NOT(f.expect(0), f);
207 tested_sequence::freebuf(buffer);
208 return 0;
211 int test_buffer_constructor_false()
213 value_type * buffer = alloc_and_init_buffer();
214 expected_calls a(tested_allocation_traits::allocbuf_calls);
215 expected_calls f(tested_allocation_traits::freebuf_calls);
217 tested_sequence a(8, 4, buffer, false);
218 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
219 CHECK_EQUAL(CORBA::ULong(4), a.length());
220 CHECK_EQUAL(buffer, a.get_buffer());
221 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
222 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
223 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
224 CHECK_EQUAL(CORBA::Octet(16), a[3]);
225 CHECK_EQUAL(false, a.release());
227 FAIL_RETURN_IF_NOT(a.expect(0), a);
228 FAIL_RETURN_IF_NOT(f.expect(0), f);
229 tested_sequence::freebuf(buffer);
230 return 0;
233 int test_buffer_constructor_true()
235 value_type * buffer = alloc_and_init_buffer();
236 expected_calls a(tested_allocation_traits::allocbuf_calls);
237 expected_calls f(tested_allocation_traits::freebuf_calls);
239 tested_sequence a(8, 4, buffer, true);
240 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
241 CHECK_EQUAL(CORBA::ULong(4), a.length());
242 CHECK_EQUAL(buffer, a.get_buffer());
243 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
244 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
245 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
246 CHECK_EQUAL(CORBA::Octet(16), a[3]);
247 CHECK_EQUAL(true, a.release());
249 FAIL_RETURN_IF_NOT(a.expect(0), a);
250 FAIL_RETURN_IF_NOT(f.expect(1), f);
251 return 0;
254 int test_replace_default()
256 value_type * buffer = alloc_and_init_buffer();
258 expected_calls c(tested_allocation_traits::allocbuf_calls);
259 expected_calls f(tested_allocation_traits::freebuf_calls);
261 tested_sequence a;
262 a.replace(8, 4, buffer);
263 FAIL_RETURN_IF_NOT(c.expect(0), c);
264 // Default constructed sequence doesn't allocate a buffer.
265 FAIL_RETURN_IF_NOT(f.expect(0), f);
267 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
268 CHECK_EQUAL(CORBA::ULong(4), a.length());
269 CHECK_EQUAL(buffer, a.get_buffer());
270 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
271 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
272 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
273 CHECK_EQUAL(CORBA::Octet(16), a[3]);
274 CHECK_EQUAL(false, a.release());
276 FAIL_RETURN_IF_NOT(c.expect(0), c);
277 FAIL_RETURN_IF_NOT(f.expect(0), f);
278 tested_sequence::freebuf(buffer);
279 return 0;
282 int test_replace_false()
284 value_type * buffer = alloc_and_init_buffer();
285 expected_calls c(tested_allocation_traits::allocbuf_calls);
286 expected_calls f(tested_allocation_traits::freebuf_calls);
289 tested_sequence a;
290 a.replace(8, 4, buffer, false);
291 FAIL_RETURN_IF_NOT(c.expect(0), c);
292 // Default constructed sequence doesn't allocate a buffer.
293 FAIL_RETURN_IF_NOT(f.expect(0), f);
295 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
296 CHECK_EQUAL(CORBA::ULong(4), a.length());
297 CHECK_EQUAL(buffer, a.get_buffer());
298 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
299 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
300 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
301 CHECK_EQUAL(CORBA::Octet(16), a[3]);
302 CHECK_EQUAL(false, a.release());
304 FAIL_RETURN_IF_NOT(c.expect(0), c);
305 FAIL_RETURN_IF_NOT(f.expect(0), f);
306 tested_sequence::freebuf(buffer);
307 return 0;
310 int test_replace_true()
312 value_type * buffer = alloc_and_init_buffer();
313 expected_calls c(tested_allocation_traits::allocbuf_calls);
314 expected_calls f(tested_allocation_traits::freebuf_calls);
317 tested_sequence a;
318 a.replace(8, 4, buffer, true);
319 FAIL_RETURN_IF_NOT(c.expect(0), c);
320 // Default constructed sequence doesn't allocate a buffer.
321 FAIL_RETURN_IF_NOT(f.expect(0), f);
323 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
324 CHECK_EQUAL(CORBA::ULong(4), a.length());
325 CHECK_EQUAL(buffer, a.get_buffer());
326 CHECK_EQUAL(CORBA::Octet( 1), a[0]);
327 CHECK_EQUAL(CORBA::Octet( 4), a[1]);
328 CHECK_EQUAL(CORBA::Octet( 9), a[2]);
329 CHECK_EQUAL(CORBA::Octet(16), a[3]);
330 CHECK_EQUAL(true, a.release());
332 FAIL_RETURN_IF_NOT(c.expect(0), c);
333 FAIL_RETURN_IF_NOT(f.expect(1), f);
334 return 0;
337 int test_get_buffer_default()
339 value_type * buffer = alloc_and_init_buffer();
340 tested_sequence a(8, 4, buffer, true);
341 CHECK_EQUAL(a.get_buffer(), buffer);
342 return 0;
345 int test_get_buffer_false()
347 value_type * buffer = alloc_and_init_buffer();
348 tested_sequence a(8, 4, buffer, true);
349 CHECK_EQUAL(a.get_buffer(), buffer);
350 return 0;
353 int test_get_buffer_true_with_release_false()
355 value_type * buffer = alloc_and_init_buffer();
356 tested_sequence a(8, 4, buffer, false);
357 CHECK(0 == a.get_buffer(true));
358 tested_sequence::freebuf(buffer);
359 return 0;
362 int test_get_buffer_true_with_release_true()
364 value_type * buffer = alloc_and_init_buffer();
365 expected_calls c(tested_allocation_traits::allocbuf_calls);
366 expected_calls f(tested_allocation_traits::freebuf_calls);
368 tested_sequence a(8, 4, buffer, true);
369 CHECK_EQUAL(buffer, a.get_buffer(true));
371 tested_sequence const & b = a;
372 CHECK_EQUAL(0UL, b.maximum());
373 CHECK_EQUAL(0UL, b.length());
374 CHECK(0 != b.get_buffer());
375 CHECK_EQUAL(true, b.release());
377 FAIL_RETURN_IF_NOT(c.expect(1), c);
379 CHECK(buffer != b.get_buffer());
381 FAIL_RETURN_IF_NOT(c.expect(0), c);
382 FAIL_RETURN_IF_NOT(f.expect(1), f);
383 tested_sequence::freebuf(buffer);
384 return 0;
387 int test_no_copy_octet()
389 #if (TAO_NO_COPY_OCTET_SEQUENCES == 1)
390 ACE_Message_Block * mb = alloc_and_init_mb();
391 tested_sequence a (8, mb);
392 CHECK_EQUAL(CORBA::Octet( 't'), a[0]);
393 CHECK_EQUAL(CORBA::Octet( 'g'), a[6]);
395 char upperbuf[256];
396 ACE_OS::sprintf (upperbuf, "%s", "THIS IS A TEST");
397 size_t n = (ACE_OS::strlen (upperbuf) + 1) * sizeof (char);
398 ACE_Message_Block * upper_mb = 0;
399 ACE_NEW_RETURN (upper_mb,
400 ACE_Message_Block (n), 1);
401 // Copy buf into the Message_Block and update the wr_ptr ().
402 upper_mb->copy ((char *) upperbuf, n);
403 a.replace (n, upper_mb);
404 CHECK_EQUAL(CORBA::Octet( 'T'), a[0]);
405 CHECK_EQUAL(CORBA::Octet( 'S'), a[6]);
406 delete upper_mb;
407 delete mb;
408 #endif
409 return 0;
412 int test_all()
414 int status = 0;
415 status += this->test_no_copy_octet();
416 status += this->test_ulong_constructor();
417 status += this->test_copy_constructor_from_ulong();
418 status += this->test_assignment_from_ulong();
419 status += this->test_exception_in_ulong_constructor();
420 status += this->test_set_length_less_than_maximum();
421 status += this->test_set_length_more_than_maximum();
422 status += this->test_exception_in_set_length();
423 status += this->test_buffer_constructor_default();
424 status += this->test_buffer_constructor_false();
425 status += this->test_buffer_constructor_true();
426 status += this->test_replace_default();
427 status += this->test_replace_false();
428 status += this->test_replace_true();
429 status += this->test_get_buffer_false();
430 status += this->test_get_buffer_true_with_release_false();
431 status += this->test_get_buffer_true_with_release_true();
432 return status;
435 Tester() {}
438 int ACE_TMAIN(int,ACE_TCHAR*[])
440 int status = 0;
444 Tester tester;
445 status += tester.test_all();
449 typedef value_sequence_tester<tested_sequence,tested_allocation_traits> common;
450 common tester;
451 status += tester.test_all ();
454 catch (const ::CORBA::Exception &ex)
456 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
457 ++status;
460 return status;