Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / bounded_object_reference_sequence_ut.cpp
blobcaf9c661f1adc0d3c696043c1d92855d66f185d9
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/Bounded_Object_Reference_Sequence_T.h"
17 #include "test_macros.h"
20 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO;
22 CORBA::ULong const TMAX = 64;
24 struct Tester
26 typedef bounded_object_reference_sequence<mock_reference,mock_reference_var,TMAX> tested_sequence;
27 typedef tested_sequence::value_type value_type;
28 typedef tested_sequence::const_value_type const_value_type;
30 typedef tested_sequence::element_traits tested_element_traits;
31 typedef tested_sequence::allocation_traits tested_allocation_traits;
32 typedef TAO::details::range_checking<value_type,true> range;
34 int test_default_constructor()
36 expected_calls a(tested_allocation_traits::allocbuf_calls);
37 expected_calls f(tested_allocation_traits::freebuf_calls);
38 expected_calls i(tested_element_traits::default_initializer_calls);
40 tested_sequence x;
42 CHECK_EQUAL(TMAX, x.maximum());
43 CHECK_EQUAL(CORBA::ULong(0), x.length());
44 CHECK_EQUAL(false, x.release());
46 FAIL_RETURN_IF_NOT(a.expect(0), a);
47 FAIL_RETURN_IF_NOT(f.expect(0), f);
48 FAIL_RETURN_IF_NOT(i.expect(0), i);
49 return 0;
52 int test_buffer_constructor_release_true()
54 expected_calls a(tested_allocation_traits::allocbuf_calls);
55 expected_calls f(tested_allocation_traits::freebuf_calls);
56 expected_calls i(tested_element_traits::default_initializer_calls);
58 CORBA::ULong maximum = 32;
59 tested_sequence::value_type * data = tested_sequence::allocbuf(maximum);
60 a.reset();
61 tested_sequence x(maximum / 2, data, true);
63 CHECK_EQUAL(TMAX, x.maximum());
64 CHECK_EQUAL(x.length(), maximum / 2);
65 CHECK_EQUAL(x.get_buffer(), data);
66 CHECK_EQUAL(x.release(), true);
68 FAIL_RETURN_IF_NOT(a.expect(0), a);
69 FAIL_RETURN_IF_NOT(f.expect(1), f);
70 // 64 is here because this is a bounded sequence and spec requires
71 // that maximum() elements are allocated for them by allocbuf.
72 FAIL_RETURN_IF_NOT(i.expect(64), i);
73 return 0;
76 int test_buffer_constructor_release_false()
78 expected_calls a(tested_allocation_traits::allocbuf_calls);
79 expected_calls f(tested_allocation_traits::freebuf_calls);
80 expected_calls i(tested_element_traits::default_initializer_calls);
82 CORBA::ULong maximum = 64;
83 tested_sequence::value_type * data = tested_sequence::allocbuf(maximum);
84 a.reset();
86 tested_sequence x(maximum / 2, data, false);
88 CHECK_EQUAL(TMAX, x.maximum());
89 CHECK_EQUAL(x.length(), maximum / 2);
90 CHECK_EQUAL(x.get_buffer(), data);
91 CHECK_EQUAL(x.release(), false);
93 FAIL_RETURN_IF_NOT(a.expect(0), a);
94 FAIL_RETURN_IF_NOT(f.expect(0), f);
95 // Same as above. allocbuf default initializes maximum() elements.
96 FAIL_RETURN_IF_NOT(i.expect(64), i);
98 tested_sequence::freebuf(data);
100 FAIL_RETURN_IF_NOT(a.expect(0), a);
101 FAIL_RETURN_IF_NOT(f.expect(1), f);
102 FAIL_RETURN_IF_NOT(i.expect(0), i);
103 return 0;
106 int test_copy_constructor_from_default()
108 expected_calls a(tested_allocation_traits::allocbuf_calls);
109 expected_calls f(tested_allocation_traits::freebuf_calls);
110 expected_calls i(tested_element_traits::default_initializer_calls);
112 tested_sequence x;
114 tested_sequence y(x);
116 FAIL_RETURN_IF_NOT(a.expect(0), a);
117 FAIL_RETURN_IF_NOT(f.expect(0), f);
118 FAIL_RETURN_IF_NOT(i.expect(0), i);
119 return 0;
122 int test_copy_constructor()
124 expected_calls a(tested_allocation_traits::allocbuf_calls);
125 expected_calls da(tested_allocation_traits::default_buffer_allocation_calls);
126 expected_calls f(tested_allocation_traits::freebuf_calls);
127 expected_calls di(tested_element_traits::default_initializer_calls);
128 expected_calls d(mock_reference::duplicate_calls);
129 expected_calls r(mock_reference::release_calls);
130 CORBA::ULong const l = 16;
132 tested_sequence x;
133 FAIL_RETURN_IF_NOT(da.expect(1), da);
134 FAIL_RETURN_IF_NOT(a.expect(0), a);
136 x.length(l);
137 // length() allocates a buffer of size maximum() and
138 // default initializes it.
139 FAIL_RETURN_IF_NOT(di.expect(TMAX), i);
140 FAIL_RETURN_IF_NOT(a.expect(1), a);
141 FAIL_RETURN_IF_NOT(f.expect(0), f);
142 CHECK_EQUAL(l, x.length());
143 for(CORBA::ULong i = 0; i != l; ++i)
145 x[i] = mock_reference::allocate(i);
148 d.reset(); r.reset();
150 tested_sequence y(x);
151 FAIL_RETURN_IF_NOT(a.expect(1), a);
152 FAIL_RETURN_IF_NOT(f.expect(0), f);
153 CHECK_EQUAL(l, y.length());
154 FAIL_RETURN_IF_NOT(d.expect(l), d);
155 FAIL_RETURN_IF_NOT(di.expect(TMAX - l), d);
156 for(CORBA::ULong i = 0; i != l; ++i)
158 CHECK_EQUAL(int(i), y[i]->id());
161 FAIL_RETURN_IF_NOT(d.expect(0), d);
162 FAIL_RETURN_IF_NOT(r.expect(2*TMAX), r);
163 FAIL_RETURN_IF_NOT(a.expect(0), a);
164 FAIL_RETURN_IF_NOT(f.expect(2), f);
165 return 0;
168 int test_copy_constructor_throw_duplicate()
170 expected_calls a(tested_allocation_traits::allocbuf_calls);
171 expected_calls da(tested_allocation_traits::default_buffer_allocation_calls);
172 expected_calls f(tested_allocation_traits::freebuf_calls);
173 expected_calls i(tested_element_traits::default_initializer_calls);
174 expected_calls d(mock_reference::duplicate_calls);
175 expected_calls r(mock_reference::release_calls);
176 CORBA::ULong const l = 16;
178 tested_sequence x;
179 FAIL_RETURN_IF_NOT(da.expect(1), da);
180 FAIL_RETURN_IF_NOT(a.expect(0), a);
182 x.length(l);
183 // length() allocates a buffer of size maximum() and
184 // default initializes it.
185 FAIL_RETURN_IF_NOT(i.expect(TMAX), i);
186 FAIL_RETURN_IF_NOT(a.expect(1), i);
188 FAIL_RETURN_IF_NOT(f.expect(0), f);
189 CHECK_EQUAL(l, x.length());
190 for(CORBA::ULong inc = 0; inc != l; ++inc)
192 x[inc] = mock_reference::allocate(inc);
195 d.reset(); r.reset();
197 mock_reference::duplicate_calls.failure_countdown(8);
198 CHECK_THROW(tested_sequence y(x), testing_exception);
199 FAIL_RETURN_IF_NOT(a.expect(1), a);
200 FAIL_RETURN_IF_NOT(da.expect(0), da);
201 FAIL_RETURN_IF_NOT(f.expect(1), f);
202 FAIL_RETURN_IF_NOT(d.expect(8), d);
203 FAIL_RETURN_IF_NOT(r.expect(TMAX), r);
205 FAIL_RETURN_IF_NOT(d.expect(0), d);
206 FAIL_RETURN_IF_NOT(r.expect(TMAX), r);
207 FAIL_RETURN_IF_NOT(a.expect(0), a);
208 FAIL_RETURN_IF_NOT(f.expect(1), f);
209 // There are TMAX-16 default initializer calls.
210 FAIL_RETURN_IF_NOT(i.expect(48), i);
211 return 0;
214 int test_set_length_less_than_maximum()
216 expected_calls a(tested_allocation_traits::allocbuf_calls);
217 expected_calls da(tested_allocation_traits::default_buffer_allocation_calls);
218 expected_calls f(tested_allocation_traits::freebuf_calls);
219 expected_calls i(tested_element_traits::default_initializer_calls);
221 tested_sequence x;
223 x.length(8);
224 CHECK_EQUAL(TMAX, x.maximum());
225 CHECK_EQUAL(CORBA::ULong(8), x.length());
226 CHECK_EQUAL(true, x.release());
228 // length() allocates a buffer of size maximum() and
229 // default initializes it.
230 FAIL_RETURN_IF_NOT(i.expect(TMAX), i);
232 FAIL_RETURN_IF_NOT(a.expect(1), a);
233 FAIL_RETURN_IF_NOT(da.expect(1), da);
234 FAIL_RETURN_IF_NOT(f.expect(1), f);
235 return 0;
238 int test_set_length_more_than_maximum()
240 expected_calls a(tested_allocation_traits::allocbuf_calls);
241 expected_calls da(tested_allocation_traits::default_buffer_allocation_calls);
242 expected_calls f(tested_allocation_traits::freebuf_calls);
243 expected_calls i(tested_element_traits::default_initializer_calls);
245 tested_sequence x;
246 FAIL_RETURN_IF_NOT(da.expect(1), da);
247 FAIL_RETURN_IF_NOT(a.expect(0), a);
248 x.length(16);
249 // length() allocates a buffer of size maximum() and
250 // default initializes it.
251 FAIL_RETURN_IF_NOT(i.expect(TMAX), i);
253 CHECK_THROW(x.length(2 * TMAX), std::runtime_error);
254 FAIL_RETURN_IF_NOT(a.expect(1), a);
255 FAIL_RETURN_IF_NOT(f.expect(0), f);
256 FAIL_RETURN_IF_NOT(i.expect(0), i);
258 CHECK_EQUAL(TMAX, x.maximum());
259 CHECK_EQUAL(CORBA::ULong(16), x.length());
260 CHECK_EQUAL(true, x.release());
262 FAIL_RETURN_IF_NOT(f.expect(1), f);
263 return 0;
266 value_type * alloc_and_init_buffer()
268 value_type * buf = tested_sequence::allocbuf();
269 buf[0] = mock_reference::allocate(1);
270 buf[1] = mock_reference::allocate(4);
271 buf[2] = mock_reference::allocate(9);
272 buf[3] = mock_reference::allocate(16);
274 return buf;
277 int check_values(tested_sequence const & a)
279 CHECK_EQUAL( 1, a[0]->id());
280 CHECK_EQUAL( 4, a[1]->id());
281 CHECK_EQUAL( 9, a[2]->id());
282 CHECK_EQUAL(16, a[3]->id());
283 return 0;
286 int test_replace_release_true()
288 value_type * buffer = alloc_and_init_buffer();
290 expected_calls c(tested_allocation_traits::allocbuf_calls);
291 expected_calls f(tested_allocation_traits::freebuf_calls);
292 expected_calls r(tested_element_traits::release_calls);
294 tested_sequence a;
295 a.replace(4, buffer, true);
296 FAIL_RETURN_IF_NOT(c.expect(0), c);
297 FAIL_RETURN_IF_NOT(f.expect(0), f);
298 FAIL_RETURN_IF_NOT(r.expect(0), r);
300 CHECK_EQUAL(TMAX, a.maximum());
301 CHECK_EQUAL(CORBA::ULong(4), a.length());
302 CHECK_EQUAL(buffer, a.get_buffer());
303 CHECK_EQUAL(true, a.release());
304 check_values(a);
306 FAIL_RETURN_IF_NOT(c.expect(0), c);
307 // Since we've given away the ownership the buffer is deallocated by
308 // the sequence.
309 FAIL_RETURN_IF_NOT(f.expect(1), f);
310 FAIL_RETURN_IF_NOT(r.expect(TMAX), r);
311 return 0;
314 int test_replace_release_false()
316 value_type * buffer = alloc_and_init_buffer();
318 expected_calls c(tested_allocation_traits::allocbuf_calls);
319 expected_calls f(tested_allocation_traits::freebuf_calls);
320 expected_calls r(tested_element_traits::release_calls);
322 tested_sequence a;
323 a.replace(4, buffer, false);
324 FAIL_RETURN_IF_NOT(c.expect(0), c);
325 FAIL_RETURN_IF_NOT(f.expect(0), f);
326 FAIL_RETURN_IF_NOT(r.expect(0), r);
328 CHECK_EQUAL(TMAX, a.maximum());
329 CHECK_EQUAL(CORBA::ULong(4), a.length());
330 CHECK_EQUAL(buffer, a.get_buffer());
331 CHECK_EQUAL(false, a.release());
332 check_values(a);
334 FAIL_RETURN_IF_NOT(c.expect(0), c);
335 FAIL_RETURN_IF_NOT(f.expect(0), f);
336 tested_sequence::freebuf(buffer);
337 FAIL_RETURN_IF_NOT(r.expect(TMAX), r);
338 return 0;
341 int test_replace_release_default()
343 value_type * buffer = alloc_and_init_buffer();
345 expected_calls c(tested_allocation_traits::allocbuf_calls);
346 expected_calls f(tested_allocation_traits::freebuf_calls);
347 expected_calls r(tested_element_traits::release_calls);
349 tested_sequence a;
350 a.replace(4, buffer);
351 FAIL_RETURN_IF_NOT(c.expect(0), c);
352 FAIL_RETURN_IF_NOT(f.expect(0), f);
353 FAIL_RETURN_IF_NOT(r.expect(0), r);
355 CHECK_EQUAL(TMAX, a.maximum());
356 CHECK_EQUAL(CORBA::ULong(4), a.length());
357 CHECK_EQUAL(buffer, a.get_buffer());
358 CHECK_EQUAL(false, a.release());
359 check_values(a);
361 FAIL_RETURN_IF_NOT(c.expect(0), c);
362 FAIL_RETURN_IF_NOT(f.expect(0), f);
363 tested_sequence::freebuf(buffer);
364 FAIL_RETURN_IF_NOT(r.expect(TMAX), r);
365 return 0;
369 int ACE_TMAIN(int,ACE_TCHAR*[])
371 int status = 0;
372 Tester mytester;
376 status += mytester.test_default_constructor();
377 status += mytester.test_buffer_constructor_release_true();
378 status += mytester.test_buffer_constructor_release_false();
379 status += mytester.test_copy_constructor_from_default();
380 status += mytester.test_copy_constructor();
381 status += mytester.test_copy_constructor_throw_duplicate();
382 status += mytester.test_set_length_less_than_maximum();
383 status += mytester.test_set_length_more_than_maximum();
384 status += mytester.test_replace_release_true();
385 status += mytester.test_replace_release_false();
386 status += mytester.test_replace_release_default();
388 catch (const ::CORBA::Exception &ex)
390 ex._tao_print_exception("ERROR : unexpected CORBA exception caugth :");
391 ++status;
394 return status;