4 * @brief Unit test for unbounded string sequences.
6 * @author Carlos O'Ryan
8 #include "testing_string_traits.hpp"
9 #include "tao/String_Traits_T.h"
10 #include "testing_allocation_traits.hpp"
11 #include "testing_range_checking.hpp"
13 #include "tao/Unbounded_Basic_String_Sequence_T.h"
14 #include "tao/CORBA_String.h"
16 #include "string_sequence_tester.hpp"
18 #include "test_macros.h"
21 using namespace TAO_VERSIONED_NAMESPACE_NAME::TAO
;
23 template<class tested_sequence
>
26 typedef typename
tested_sequence::character_type char_type
;
27 typedef string_sequence_test_helpers
<char_type
> helper
;
28 typedef typename
tested_sequence::value_type value_type
;
29 typedef typename
tested_sequence::const_value_type const_value_type
;
31 typedef typename
tested_sequence::element_traits tested_element_traits
;
32 typedef typename
tested_sequence::allocation_traits tested_allocation_traits
;
33 typedef TAO::details::range_checking
<value_type
,true> range
;
35 int test_ulong_constructor()
37 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
38 expected_calls
f(tested_allocation_traits::freebuf_calls
);
39 expected_calls
i(tested_element_traits::default_initializer_calls
);
41 tested_sequence
x(16);
43 CHECK_EQUAL(CORBA::ULong(16), x
.maximum());
44 CHECK_EQUAL(CORBA::ULong(0), x
.length());
45 CHECK_EQUAL(true, x
.release());
47 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
48 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
49 // ulong constructor calls allocbuf and thus there must be
50 // maximum() default initilized elements.
51 FAIL_RETURN_IF_NOT(i
.expect(16), i
);
55 int test_copy_constructor_from_ulong()
57 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
58 expected_calls
f(tested_allocation_traits::freebuf_calls
);
59 expected_calls
i(tested_element_traits::default_initializer_calls
);
60 expected_calls
r(tested_element_traits::release_calls
);
61 expected_calls
d(tested_element_traits::duplicate_calls
);
63 tested_sequence
x(16);
64 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
65 // ulong constructor calls allocbuf and thus there must be
66 // maximum() default initilized elements.
67 FAIL_RETURN_IF_NOT(i
.expect(16), i
);
71 CHECK_EQUAL(CORBA::ULong(16), x
.maximum());
72 CHECK_EQUAL(CORBA::ULong(8), x
.length());
73 CHECK_EQUAL(true, x
.release());
75 // length() doesn't default initialize anything since initialization
76 // was done in ulong constructor.
77 FAIL_RETURN_IF_NOT(i
.expect(0), i
);
80 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
81 CHECK_EQUAL(CORBA::ULong(16), y
.maximum());
82 CHECK_EQUAL(CORBA::ULong(8), y
.length());
83 CHECK_EQUAL(true, y
.release());
85 FAIL_RETURN_IF_NOT(d
.expect(8), d
);
86 // Copy constructor must duplicate length() elements and default
87 // initilize rest maximum()-length().
88 FAIL_RETURN_IF_NOT(i
.expect(8), d
);
90 FAIL_RETURN_IF_NOT(f
.expect(2), f
);
91 // There must be 32 elements released since maximum() is 16.
92 FAIL_RETURN_IF_NOT(r
.expect(32), i
);
96 int test_set_length_less_than_maximum()
98 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
99 expected_calls
f(tested_allocation_traits::freebuf_calls
);
100 expected_calls
i(tested_element_traits::default_initializer_calls
);
102 tested_sequence
x(16);
105 CHECK_EQUAL(CORBA::ULong(16), x
.maximum());
106 CHECK_EQUAL(CORBA::ULong(8), x
.length());
107 CHECK_EQUAL(true, x
.release());
109 // ulong constructor calls allocbuf and thus there must be
110 // maximum() default initilized elements.
111 FAIL_RETURN_IF_NOT(i
.expect(16), i
);
113 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
114 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
118 int test_set_length_more_than_maximum()
120 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
121 expected_calls
f(tested_allocation_traits::freebuf_calls
);
122 expected_calls
i(tested_element_traits::default_initializer_calls
);
124 tested_sequence
x(16);
125 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
128 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
129 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
131 // ulong constructor calls allocbuf and thus there must be
132 // maximum() default initilized elements + length() leads to buffer
133 // reallocation maximum() gets set to a new value.
134 FAIL_RETURN_IF_NOT(i
.expect(48), i
);
136 CHECK_EQUAL(CORBA::ULong(32), x
.maximum());
137 CHECK_EQUAL(CORBA::ULong(32), x
.length());
138 CHECK_EQUAL(true, x
.release());
140 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
144 int test_exception_in_ulong_constructor()
146 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
147 expected_calls
f(tested_allocation_traits::freebuf_calls
);
149 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
150 CHECK_THROW(tested_sequence
x(16), testing_exception
);
151 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
153 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
157 int test_exception_in_length()
159 expected_calls
f(tested_allocation_traits::freebuf_calls
);
160 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
161 expected_calls
d(tested_element_traits::duplicate_calls
);
162 expected_calls
r(tested_element_traits::release_calls
);
164 tested_sequence y
; y
.length(4);
165 for(CORBA::ULong i
= 0; i
!= 4; ++i
)
167 y
[i
] = helper::allocate_test_string();
174 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
175 CHECK_THROW(y
.length(8), testing_exception
);
176 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
177 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
178 FAIL_RETURN_IF_NOT(d
.expect(0), d
);
179 FAIL_RETURN_IF_NOT(r
.expect(0), r
);
181 CHECK_EQUAL(CORBA::ULong(4), y
.length());
182 for(CORBA::ULong i
= 0; i
!= 4; ++i
)
185 helper::compare_test_string(y
[i
]),
186 "Mismatch in element " << i
187 << ", got=" << y
[i
]);
190 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
194 int test_default_initializer_exception_in_length()
196 expected_calls
f(tested_allocation_traits::freebuf_calls
);
197 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
198 expected_calls
d(tested_element_traits::default_initializer_calls
);
199 expected_calls
r(tested_element_traits::release_calls
);
201 tested_sequence y
; y
.length(4);
202 for(CORBA::ULong i
= 0; i
!= 4; ++i
)
204 y
[i
] = helper::allocate_test_string();
211 tested_element_traits::default_initializer_calls
.failure_countdown(3);
212 CHECK_THROW(y
.length(8), testing_exception
);
213 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
214 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
215 FAIL_RETURN_IF_NOT(d
.expect(3), d
);
216 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
218 CHECK_EQUAL(CORBA::ULong(4), y
.length());
219 for(CORBA::ULong i
= 0; i
!= 4; ++i
)
222 helper::compare_test_string(y
[i
]),
223 "Mismatch in element " << i
224 << ", got=" << y
[i
]);
227 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
231 value_type
* alloc_and_init_buffer()
233 value_type
* buf
= tested_sequence::allocbuf(8);
234 tested_element_traits::release (buf
[0]);
235 buf
[0] = helper::to_string(1);
236 tested_element_traits::release (buf
[1]);
237 buf
[1] = helper::to_string(4);
238 tested_element_traits::release (buf
[2]);
239 buf
[2] = helper::to_string(9);
240 tested_element_traits::release (buf
[3]);
241 buf
[3] = helper::to_string(16);
246 int check_values(tested_sequence
const & a
)
248 CHECK(helper::compare(1, a
[0]));
249 CHECK(helper::compare(4, a
[1]));
250 CHECK(helper::compare(9, a
[2]));
251 CHECK(helper::compare(16, a
[3]));
255 int test_buffer_constructor_default()
257 value_type
* buffer
= alloc_and_init_buffer();
258 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
259 expected_calls
f(tested_allocation_traits::freebuf_calls
);
260 expected_calls
r(tested_element_traits::release_calls
);
262 tested_sequence
a(8, 4, buffer
, false);
263 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
264 CHECK_EQUAL(CORBA::ULong(4), a
.length());
265 CHECK_EQUAL(buffer
, a
.get_buffer());
266 CHECK_EQUAL(false, a
.release());
269 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
270 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
271 tested_sequence::freebuf(buffer
);
272 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
276 int test_buffer_constructor_false()
278 value_type
* buffer
= alloc_and_init_buffer();
279 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
280 expected_calls
f(tested_allocation_traits::freebuf_calls
);
281 expected_calls
r(tested_element_traits::release_calls
);
283 tested_sequence
a(8, 4, buffer
, false);
284 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
285 CHECK_EQUAL(CORBA::ULong(4), a
.length());
286 CHECK_EQUAL(buffer
, a
.get_buffer());
287 CHECK_EQUAL(false, a
.release());
290 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
291 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
292 tested_sequence::freebuf(buffer
);
293 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
297 int test_buffer_constructor_true()
299 value_type
* buffer
= alloc_and_init_buffer();
300 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
301 expected_calls
f(tested_allocation_traits::freebuf_calls
);
302 expected_calls
r(tested_element_traits::release_calls
);
304 tested_sequence
a(8, 4, buffer
, true);
305 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
306 CHECK_EQUAL(CORBA::ULong(4), a
.length());
307 CHECK_EQUAL(buffer
, a
.get_buffer());
308 CHECK_EQUAL(true, a
.release());
311 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
312 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
313 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
317 int test_replace_default()
319 value_type
* buffer
= alloc_and_init_buffer();
321 expected_calls
c(tested_allocation_traits::allocbuf_calls
);
322 expected_calls
f(tested_allocation_traits::freebuf_calls
);
323 expected_calls
r(tested_element_traits::release_calls
);
326 a
.replace(8, 4, buffer
);
327 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
328 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
329 FAIL_RETURN_IF_NOT(r
.expect(0), 0);
331 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
332 CHECK_EQUAL(CORBA::ULong(4), a
.length());
333 CHECK_EQUAL(buffer
, a
.get_buffer());
334 CHECK_EQUAL(false, a
.release());
337 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
338 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
339 tested_sequence::freebuf(buffer
);
340 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
344 int test_replace_false()
346 value_type
* buffer
= alloc_and_init_buffer();
348 expected_calls
c(tested_allocation_traits::allocbuf_calls
);
349 expected_calls
f(tested_allocation_traits::freebuf_calls
);
350 expected_calls
r(tested_element_traits::release_calls
);
353 a
.replace(8, 4, buffer
, false);
354 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
355 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
356 FAIL_RETURN_IF_NOT(r
.expect(0), 0);
358 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
359 CHECK_EQUAL(CORBA::ULong(4), a
.length());
360 CHECK_EQUAL(buffer
, a
.get_buffer());
361 CHECK_EQUAL(false, a
.release());
364 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
365 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
366 tested_sequence::freebuf(buffer
);
367 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
371 int test_replace_true()
373 value_type
* buffer
= alloc_and_init_buffer();
375 expected_calls
c(tested_allocation_traits::allocbuf_calls
);
376 expected_calls
f(tested_allocation_traits::freebuf_calls
);
377 expected_calls
r(tested_element_traits::release_calls
);
380 a
.replace(8, 4, buffer
, true);
381 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
382 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
383 FAIL_RETURN_IF_NOT(r
.expect(0), r
);
385 CHECK_EQUAL(CORBA::ULong(8), a
.maximum());
386 CHECK_EQUAL(CORBA::ULong(4), a
.length());
387 CHECK_EQUAL(buffer
, a
.get_buffer());
388 CHECK_EQUAL(true, a
.release());
391 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
392 // Since we've given away the ownership the buffer is deallocated by
394 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
395 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
399 int test_get_buffer_default()
401 value_type
* buffer
= alloc_and_init_buffer();
402 tested_sequence
a(8, 4, buffer
, true);
403 CHECK_EQUAL(a
.get_buffer(), buffer
);
407 int test_get_buffer_false()
409 value_type
* buffer
= alloc_and_init_buffer();
410 tested_sequence
a(8, 4, buffer
, true);
411 CHECK_EQUAL(a
.get_buffer(), buffer
);
415 int test_get_buffer_true_with_release_false()
417 value_type
* buffer
= alloc_and_init_buffer();
418 tested_sequence
a(8, 4, buffer
, false);
419 CHECK(0 == a
.get_buffer(true));
420 tested_sequence::freebuf(buffer
);
424 int test_get_buffer_true_with_release_true()
426 value_type
* buffer
= alloc_and_init_buffer();
427 expected_calls
c(tested_allocation_traits::allocbuf_calls
);
428 expected_calls
f(tested_allocation_traits::freebuf_calls
);
429 expected_calls
r(tested_element_traits::release_calls
);
431 tested_sequence
a(8, 4, buffer
, true);
432 CHECK_EQUAL(buffer
, a
.get_buffer(true));
434 tested_sequence
const & b
= a
;
435 CHECK_EQUAL(0UL, b
.maximum());
436 CHECK_EQUAL(0UL, b
.length());
437 CHECK(0 != b
.get_buffer());
438 CHECK_EQUAL(true, b
.release());
440 FAIL_RETURN_IF_NOT(c
.expect(1), c
);
442 CHECK(buffer
!= b
.get_buffer());
444 FAIL_RETURN_IF_NOT(c
.expect(0), c
);
445 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
446 tested_sequence::freebuf(buffer
);
447 FAIL_RETURN_IF_NOT(r
.expect(8), r
);
451 int test_regression_2201()
453 value_type
* buffer
= alloc_and_init_buffer();
454 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
455 expected_calls
f(tested_allocation_traits::freebuf_calls
);
456 expected_calls
r(tested_element_traits::release_calls
);
458 tested_sequence
x(8, 4, buffer
, true);
459 CHECK_EQUAL(CORBA::ULong(8), x
.maximum());
460 CHECK_EQUAL(CORBA::ULong(4), x
.length());
461 CHECK_EQUAL(buffer
, x
.get_buffer());
462 CHECK_EQUAL(true, x
.release());
465 CHECK_EQUAL(CORBA::ULong(3), x
.length());
467 CHECK_EQUAL(CORBA::ULong(4), x
.length());
468 CHECK(helper::compare_empty(x
[3]));
470 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
471 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
472 // 1 additional release call happens when we shrink
473 // the sequence to length 3.
474 FAIL_RETURN_IF_NOT(r
.expect(9), r
);
482 typedef string_sequence_tester
<tested_sequence
> common
;
484 status
+= tester
.test_all ();
486 status
+= this->test_ulong_constructor();
487 status
+= this->test_copy_constructor_from_ulong();
488 status
+= this->test_set_length_less_than_maximum();
489 status
+= this->test_set_length_more_than_maximum();
490 status
+= this->test_exception_in_ulong_constructor();
491 status
+= this->test_exception_in_length();
492 status
+= this->test_default_initializer_exception_in_length();
493 status
+= this->test_buffer_constructor_default();
494 status
+= this->test_buffer_constructor_false();
495 status
+= this->test_buffer_constructor_true();
496 status
+= this->test_replace_default();
497 status
+= this->test_replace_false();
498 status
+= this->test_replace_true();
499 status
+= this->test_get_buffer_default();
500 status
+= this->test_get_buffer_false();
501 status
+= this->test_get_buffer_true_with_release_false();
502 status
+= this->test_get_buffer_true_with_release_true();
503 status
+= this->test_regression_2201();
509 int ACE_TMAIN(int,ACE_TCHAR
*[])
515 typedef Tester
<unbounded_basic_string_sequence
<char> > nTester
;
517 status
+= ntester
.test_all ();
520 #if defined(ACE_HAS_WCHAR) && !defined(TAO_LACKS_WCHAR_CXX_STDLIB)
522 typedef Tester
<unbounded_basic_string_sequence
<CORBA::WChar
> > wTester
;
524 status
+= wtester
.test_all ();
528 catch (const ::CORBA::Exception
&ex
)
530 ex
._tao_print_exception("ERROR : unexpected CORBA exception caugth :");