Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / unbounded_object_reference_sequence_ut.cpp
blobbb3f96f6737904f0270f4999890275c92fc69b69
1 /**
2 * @file
4 * @brief Unit test for unbounded sequences of object references.
6 * @author Carlos O'Ryan
7 */
8 #include "testing_object_reference_traits.hpp"
9 #include "tao/Object_Reference_Traits_T.h"
10 #include "testing_allocation_traits.hpp"
11 #include "testing_range_checking.hpp"
13 #include "mock_reference.hpp"
15 #include "tao/Unbounded_Object_Reference_Sequence_T.h"
17 #include "test_macros.h"
20 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
22 struct Tester
24 typedef unbounded_object_reference_sequence<mock_reference, mock_reference_var> tested_sequence;
25 typedef tested_sequence::value_type value_type;
26 typedef tested_sequence::const_value_type const_value_type;
28 typedef tested_sequence::element_traits tested_element_traits;
29 typedef tested_sequence::allocation_traits tested_allocation_traits;
30 typedef TAO::details::range_checking<value_type,true> range;
32 int test_default_constructor()
34 expected_calls a(tested_allocation_traits::allocbuf_calls);
35 expected_calls f(tested_allocation_traits::freebuf_calls);
36 expected_calls i(tested_element_traits::default_initializer_calls);
38 tested_sequence x;
40 CHECK_EQUAL(CORBA::ULong(0), x.maximum());
41 CHECK_EQUAL(CORBA::ULong(0), x.length());
42 CHECK_EQUAL(false, x.release());
44 FAIL_RETURN_IF_NOT(a.expect(0), a);
45 FAIL_RETURN_IF_NOT(f.expect(0), f);
46 FAIL_RETURN_IF_NOT(i.expect(0), i);
47 return 0;
50 int test_ulong_constructor()
52 expected_calls a(tested_allocation_traits::allocbuf_calls);
53 expected_calls f(tested_allocation_traits::freebuf_calls);
54 expected_calls i(tested_element_traits::default_initializer_calls);
56 tested_sequence x(16);
58 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
59 CHECK_EQUAL(CORBA::ULong(0), x.length());
60 CHECK_EQUAL(true, x.release());
62 FAIL_RETURN_IF_NOT(a.expect(1), a);
63 FAIL_RETURN_IF_NOT(f.expect(1), f);
64 // ulong constructor calls allocbuf and thus there must be
65 // maximum() default initilized elements.
66 FAIL_RETURN_IF_NOT(i.expect(16), i);
67 return 0;
70 int test_ulong_constructor_throw()
72 expected_calls a(tested_allocation_traits::allocbuf_calls);
73 expected_calls f(tested_allocation_traits::freebuf_calls);
74 expected_calls i(tested_element_traits::default_initializer_calls);
76 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
77 CHECK_THROW(tested_sequence x(16), testing_exception);
79 FAIL_RETURN_IF_NOT(a.expect(1), a);
80 FAIL_RETURN_IF_NOT(f.expect(0), f);
81 FAIL_RETURN_IF_NOT(i.expect(0), i);
82 return 0;
85 int test_buffer_constructor_release_true()
87 expected_calls a(tested_allocation_traits::allocbuf_calls);
88 expected_calls f(tested_allocation_traits::freebuf_calls);
89 expected_calls i(tested_element_traits::default_initializer_calls);
91 CORBA::ULong maximum = 32;
92 tested_sequence::value_type * data = tested_sequence::allocbuf(maximum);
93 a.reset();
94 tested_sequence x(maximum, maximum / 2, data, true);
96 CHECK_EQUAL(x.maximum(), maximum);
97 CHECK_EQUAL(x.length(), maximum / 2);
98 CHECK_EQUAL(x.get_buffer(), data);
99 CHECK_EQUAL(x.release(), true);
101 FAIL_RETURN_IF_NOT(a.expect(0), a);
102 FAIL_RETURN_IF_NOT(f.expect(1), f);
103 // allocbuf default initializes maximum elements.
104 FAIL_RETURN_IF_NOT(i.expect(32), i);
105 return 0;
108 int test_buffer_constructor_release_false()
110 expected_calls a(tested_allocation_traits::allocbuf_calls);
111 expected_calls f(tested_allocation_traits::freebuf_calls);
112 expected_calls i(tested_element_traits::default_initializer_calls);
114 CORBA::ULong maximum = 64;
115 tested_sequence::value_type * data = tested_sequence::allocbuf(maximum);
116 a.reset();
118 tested_sequence x(maximum, maximum / 2, data, false);
120 CHECK_EQUAL(x.maximum(), maximum);
121 CHECK_EQUAL(x.length(), maximum / 2);
122 CHECK_EQUAL(x.get_buffer(), data);
123 CHECK_EQUAL(x.release(), false);
125 FAIL_RETURN_IF_NOT(a.expect(0), a);
126 FAIL_RETURN_IF_NOT(f.expect(0), f);
127 // allocbuf default initializes maximum elements.
128 FAIL_RETURN_IF_NOT(i.expect(64), i);
130 tested_sequence::freebuf(data);
132 FAIL_RETURN_IF_NOT(a.expect(0), a);
133 FAIL_RETURN_IF_NOT(f.expect(1), f);
134 FAIL_RETURN_IF_NOT(i.expect(0), i);
135 return 0;
138 int test_copy_constructor_from_default()
140 expected_calls a(tested_allocation_traits::allocbuf_calls);
141 expected_calls f(tested_allocation_traits::freebuf_calls);
142 expected_calls i(tested_element_traits::default_initializer_calls);
144 tested_sequence x;
146 tested_sequence y(x);
148 FAIL_RETURN_IF_NOT(a.expect(0), a);
149 FAIL_RETURN_IF_NOT(f.expect(0), f);
150 FAIL_RETURN_IF_NOT(i.expect(0), i);
151 return 0;
154 int test_copy_constructor()
156 expected_calls a(tested_allocation_traits::allocbuf_calls);
157 expected_calls f(tested_allocation_traits::freebuf_calls);
158 expected_calls i(tested_element_traits::default_initializer_calls);
159 expected_calls d(mock_reference::duplicate_calls);
160 expected_calls r(mock_reference::release_calls);
161 CORBA::ULong const l = 16;
163 tested_sequence x(l);
164 x.length(l);
165 FAIL_RETURN_IF_NOT(i.expect(l), i);
167 FAIL_RETURN_IF_NOT(a.expect(1), a);
168 FAIL_RETURN_IF_NOT(f.expect(0), f);
169 CHECK_EQUAL(l, x.length());
170 for(CORBA::ULong i = 0; i != l; ++i)
172 x[i] = mock_reference::allocate(i);
175 d.reset(); r.reset();
177 tested_sequence y(x);
178 FAIL_RETURN_IF_NOT(a.expect(1), a);
179 FAIL_RETURN_IF_NOT(f.expect(0), f);
180 CHECK_EQUAL(l, x.length());
181 FAIL_RETURN_IF_NOT(d.expect(l), d);
182 for(CORBA::ULong i = 0; i != l; ++i)
184 CHECK_EQUAL(int(i), y[i]->id());
187 FAIL_RETURN_IF_NOT(d.expect(0), d);
188 FAIL_RETURN_IF_NOT(r.expect(2*l), r);
189 FAIL_RETURN_IF_NOT(a.expect(0), a);
190 FAIL_RETURN_IF_NOT(f.expect(2), f);
191 FAIL_RETURN_IF_NOT(i.expect(0), i);
192 return 0;
195 int test_copy_constructor_throw_duplicate()
197 expected_calls a(tested_allocation_traits::allocbuf_calls);
198 expected_calls f(tested_allocation_traits::freebuf_calls);
199 expected_calls i(tested_element_traits::default_initializer_calls);
200 expected_calls d(mock_reference::duplicate_calls);
201 expected_calls r(mock_reference::release_calls);
202 CORBA::ULong const l = 16;
204 tested_sequence x(l);
205 x.length(l);
207 FAIL_RETURN_IF_NOT(i.expect(l), i);
209 FAIL_RETURN_IF_NOT(a.expect(1), a);
210 FAIL_RETURN_IF_NOT(f.expect(0), f);
211 CHECK_EQUAL(l, x.length());
212 for(CORBA::ULong i = 0; i != l; ++i)
214 x[i] = mock_reference::allocate(i);
217 d.reset(); r.reset();
219 mock_reference::duplicate_calls.failure_countdown(8);
220 CHECK_THROW(tested_sequence y(x), testing_exception);
221 FAIL_RETURN_IF_NOT(a.expect(1), a);
222 FAIL_RETURN_IF_NOT(f.expect(1), f);
223 FAIL_RETURN_IF_NOT(d.expect(8), d);
224 FAIL_RETURN_IF_NOT(r.expect(l), r);
226 FAIL_RETURN_IF_NOT(d.expect(0), d);
227 FAIL_RETURN_IF_NOT(r.expect(l), r);
228 FAIL_RETURN_IF_NOT(a.expect(0), a);
229 FAIL_RETURN_IF_NOT(f.expect(1), f);
230 FAIL_RETURN_IF_NOT(i.expect(0), i);
231 return 0;
234 int test_set_length_less_than_maximum()
236 expected_calls a(tested_allocation_traits::allocbuf_calls);
237 expected_calls f(tested_allocation_traits::freebuf_calls);
238 expected_calls i(tested_element_traits::default_initializer_calls);
240 tested_sequence x(16);
242 x.length(8);
243 CHECK_EQUAL(CORBA::ULong(16), x.maximum());
244 CHECK_EQUAL(CORBA::ULong(8), x.length());
245 CHECK_EQUAL(true, x.release());
247 // allocbuf default initializes 16 elements.
248 FAIL_RETURN_IF_NOT(i.expect(16), i);
250 FAIL_RETURN_IF_NOT(a.expect(1), a);
251 FAIL_RETURN_IF_NOT(f.expect(1), f);
252 return 0;
255 int test_set_length_more_than_maximum()
257 expected_calls a(tested_allocation_traits::allocbuf_calls);
258 expected_calls f(tested_allocation_traits::freebuf_calls);
259 expected_calls i(tested_element_traits::default_initializer_calls);
261 tested_sequence x(16);
262 FAIL_RETURN_IF_NOT(a.expect(1), a);
264 x.length(32);
265 FAIL_RETURN_IF_NOT(a.expect(1), a);
266 FAIL_RETURN_IF_NOT(f.expect(1), f);
268 // ulong constructor calls allocbuf and thus there must be
269 // maximum() default initilized elements + length() leads to buffer
270 // reallocation maximum() gets set to a new value.
271 FAIL_RETURN_IF_NOT(i.expect(48), i);
273 CHECK_EQUAL(CORBA::ULong(32), x.maximum());
274 CHECK_EQUAL(CORBA::ULong(32), x.length());
275 CHECK_EQUAL(true, x.release());
277 FAIL_RETURN_IF_NOT(f.expect(1), f);
278 return 0;
281 int test_set_length_copy_elements()
283 expected_calls a(tested_allocation_traits::allocbuf_calls);
284 expected_calls f(tested_allocation_traits::freebuf_calls);
285 expected_calls i(tested_element_traits::default_initializer_calls);
287 tested_sequence x(16);
288 FAIL_RETURN_IF_NOT(a.expect(1), a);
290 x.length(16);
291 CHECK_EQUAL(CORBA::ULong(16), x.length());
292 FAIL_RETURN_IF_NOT(a.expect(0), a);
293 FAIL_RETURN_IF_NOT(f.expect(0), a);
294 FAIL_RETURN_IF_NOT(i.expect(16), i);
296 for(int j = 0; j != 16; ++j)
298 CHECK_EQUAL(mock_reference::_nil(), x[j]);
299 x[j] = mock_reference::allocate(j);
302 x.length(32);
303 CHECK_EQUAL(CORBA::ULong(32), x.length());
304 FAIL_RETURN_IF_NOT(a.expect(1), a);
305 FAIL_RETURN_IF_NOT(f.expect(1), f);
306 FAIL_RETURN_IF_NOT(i.expect(16), i);
308 tested_sequence const & y = x;
310 for(CORBA::ULong i = 0; i != 16UL; ++i)
312 CHECK(mock_reference::_nil() != y[i]);
313 CHECK_EQUAL(int(i), y[i]->id());
315 for(CORBA::ULong i = 16; i != 32UL; ++i)
317 CHECK_EQUAL(mock_reference::_nil(), y[i]);
320 FAIL_RETURN_IF_NOT(f.expect(1), f);
321 FAIL_RETURN_IF_NOT(i.expect(0), i);
322 return 0;
325 int test_set_length_throw()
327 expected_calls a(tested_allocation_traits::allocbuf_calls);
328 expected_calls f(tested_allocation_traits::freebuf_calls);
329 expected_calls i(tested_element_traits::default_initializer_calls);
331 tested_sequence x(16);
332 FAIL_RETURN_IF_NOT(a.expect(1), a);
333 x.length(16);
334 FAIL_RETURN_IF_NOT(i.expect(16), i);
336 for(int j = 0; j != 16; ++j)
338 CHECK_EQUAL(mock_reference::_nil(), x[j]);
339 x[j] = mock_reference::allocate(j);
342 tested_allocation_traits::allocbuf_calls.failure_countdown(1);
343 CHECK_THROW(x.length(32), testing_exception);
344 FAIL_RETURN_IF_NOT(a.expect(1), a);
345 FAIL_RETURN_IF_NOT(f.expect(0), f);
346 CHECK_EQUAL(CORBA::ULong(16), x.length());
347 FAIL_RETURN_IF_NOT(i.expect(0), i);
349 tested_sequence const & y = x;
351 for(int j = 0; j != 16; ++j)
353 CHECK(mock_reference::_nil() != y[j]);
354 CHECK_EQUAL(j, y[j]->id());
357 FAIL_RETURN_IF_NOT(a.expect(0), a);
358 FAIL_RETURN_IF_NOT(f.expect(1), f);
359 FAIL_RETURN_IF_NOT(i.expect(0), i);
360 return 0;
363 value_type * alloc_and_init_buffer()
365 value_type * buf = tested_sequence::allocbuf(8);
366 buf[0] = mock_reference::allocate(1);
367 buf[1] = mock_reference::allocate(4);
368 buf[2] = mock_reference::allocate(9);
369 buf[3] = mock_reference::allocate(16);
371 return buf;
374 int check_values(tested_sequence const & a)
376 CHECK_EQUAL( 1, a[0]->id());
377 CHECK_EQUAL( 4, a[1]->id());
378 CHECK_EQUAL( 9, a[2]->id());
379 CHECK_EQUAL(16, a[3]->id());
380 return 0;
383 int test_replace_release_true()
385 value_type * buffer = alloc_and_init_buffer();
387 expected_calls c(tested_allocation_traits::allocbuf_calls);
388 expected_calls f(tested_allocation_traits::freebuf_calls);
389 expected_calls r(tested_element_traits::release_calls);
391 tested_sequence a;
392 a.replace(8, 4, buffer, true);
393 FAIL_RETURN_IF_NOT(c.expect(0), c);
394 FAIL_RETURN_IF_NOT(f.expect(0), f);
395 FAIL_RETURN_IF_NOT(r.expect(0), r);
397 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
398 CHECK_EQUAL(CORBA::ULong(4), a.length());
399 CHECK_EQUAL(buffer, a.get_buffer());
400 CHECK_EQUAL(true, a.release());
401 check_values(a);
403 FAIL_RETURN_IF_NOT(c.expect(0), c);
404 // Since we've given away the ownership the buffer is deallocated by
405 // the sequence.
406 FAIL_RETURN_IF_NOT(f.expect(1), f);
407 FAIL_RETURN_IF_NOT(r.expect(8), r);
408 return 0;
411 int test_replace_release_false()
413 value_type * buffer = alloc_and_init_buffer();
415 expected_calls c(tested_allocation_traits::allocbuf_calls);
416 expected_calls f(tested_allocation_traits::freebuf_calls);
417 expected_calls r(tested_element_traits::release_calls);
419 tested_sequence a;
420 a.replace(8, 4, buffer, false);
421 FAIL_RETURN_IF_NOT(c.expect(0), c);
422 FAIL_RETURN_IF_NOT(f.expect(0), f);
423 FAIL_RETURN_IF_NOT(r.expect(0), 0);
425 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
426 CHECK_EQUAL(CORBA::ULong(4), a.length());
427 CHECK_EQUAL(buffer, a.get_buffer());
428 CHECK_EQUAL(false, a.release());
429 check_values(a);
431 FAIL_RETURN_IF_NOT(c.expect(0), c);
432 FAIL_RETURN_IF_NOT(f.expect(0), f);
433 tested_sequence::freebuf(buffer);
434 FAIL_RETURN_IF_NOT(r.expect(8), r);
435 return 0;
438 int test_replace_release_default()
440 value_type * buffer = alloc_and_init_buffer();
442 expected_calls c(tested_allocation_traits::allocbuf_calls);
443 expected_calls f(tested_allocation_traits::freebuf_calls);
444 expected_calls r(tested_element_traits::release_calls);
446 tested_sequence a;
447 a.replace(8, 4, buffer);
448 FAIL_RETURN_IF_NOT(c.expect(0), c);
449 FAIL_RETURN_IF_NOT(f.expect(0), f);
450 FAIL_RETURN_IF_NOT(r.expect(0), 0);
452 CHECK_EQUAL(CORBA::ULong(8), a.maximum());
453 CHECK_EQUAL(CORBA::ULong(4), a.length());
454 CHECK_EQUAL(buffer, a.get_buffer());
455 CHECK_EQUAL(false, a.release());
456 check_values(a);
458 FAIL_RETURN_IF_NOT(c.expect(0), c);
459 FAIL_RETURN_IF_NOT(f.expect(0), f);
460 tested_sequence::freebuf(buffer);
461 FAIL_RETURN_IF_NOT(r.expect(8), r);
462 return 0;
465 int test_all()
467 int status = 0;
468 status += this->test_default_constructor();
469 status += this->test_ulong_constructor();
470 status += this->test_ulong_constructor_throw();
471 status += this->test_buffer_constructor_release_true();
472 status += this->test_buffer_constructor_release_false();
473 status += this->test_copy_constructor_from_default();
474 status += this->test_copy_constructor();
475 status += this->test_copy_constructor_throw_duplicate();
476 status += this->test_set_length_less_than_maximum();
477 status += this->test_set_length_more_than_maximum();
478 status += this->test_set_length_copy_elements();
479 status += this->test_set_length_throw();
480 status += this->test_replace_release_true();
481 status += this->test_replace_release_false();
482 status += this->test_replace_release_default();
483 return status;
486 Tester() {}
489 int ACE_TMAIN(int,ACE_TCHAR*[])
491 int status = 0;
495 Tester tester;
496 status += tester.test_all();
499 catch (const ::CORBA::Exception &ex)
501 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
502 ++status;
505 return status;