1 #ifndef guard_string_sequence_tester_hpp
2 #define guard_string_sequence_tester_hpp
6 * @brief Helper class to implement tests for *_string_sequence
8 * @author Carlos O'Ryan
10 #include "tao/String_Traits_T.h"
12 #include "ace/OS_NS_string.h"
14 #include "test_macros.h"
18 template<typename charT
>
19 struct string_sequence_test_helpers
24 struct string_sequence_test_helpers
<char>
26 inline static char const * test_string()
28 return "In a hole in the ground there lived a Hobbit";
31 inline static char * allocate_test_string()
33 return TAO::details::string_traits
<char,true>::duplicate(
37 static bool compare_test_string(char const * value
)
39 return ACE_OS::strcmp(test_string(), value
) == 0;
42 inline static char * to_string(CORBA::ULong i
)
44 std::ostringstream os
;
46 return TAO::details::string_traits
<char,true>::duplicate(
50 inline static bool compare(int i
, char const * value
)
52 std::ostringstream os
;
54 return ACE_OS::strcmp(os
.str().c_str(), value
) == 0;
57 inline static bool compare_empty(char const * value
)
59 return ACE_OS::strcmp(value
, "") == 0;
63 #if defined(_GLIBCPP_VERSION) && !defined(_GLIBCPP_USE_WCHAR_T) && !defined(TAO_LACKS_WCHAR_CXX_STDLIB)
64 # define TAO_LACKS_WCHAR_CXX_STDLIB
67 #if defined(ACE_HAS_WCHAR) && !defined(TAO_LACKS_WCHAR_CXX_STDLIB)
69 struct string_sequence_test_helpers
<CORBA::WChar
>
71 inline static CORBA::WChar
const * test_string()
73 return L
"In a hole in the ground there lived a Hobbit";
76 inline static CORBA::WChar
* allocate_test_string()
78 return TAO::details::string_traits
<CORBA::WChar
,true>::duplicate(
82 static bool compare_test_string(CORBA::WChar
const * value
)
84 return ACE_OS::strcmp(test_string(), value
) == 0;
87 inline static CORBA::WChar
* to_string(CORBA::ULong i
)
89 std::wostringstream os
;
91 return TAO::details::string_traits
<CORBA::WChar
,true>::duplicate(
95 inline static bool compare(int i
, CORBA::WChar
const * value
)
97 std::wostringstream os
;
99 return ACE_OS::strcmp(os
.str().c_str(), value
) == 0;
102 inline static bool compare_empty(CORBA::WChar
const * value
)
104 return ACE_OS::strcmp(value
, L
"") == 0;
107 #endif /* ACE_HAS_WCHAR */
109 template<class tested_sequence
>
110 struct string_sequence_tester
112 typedef typename
tested_sequence::character_type character_type
;
113 typedef string_sequence_test_helpers
<character_type
> helper
;
114 typedef typename
tested_sequence::value_type value_type
;
115 typedef typename
tested_sequence::const_value_type const_value_type
;
116 typedef typename
tested_sequence::element_traits tested_element_traits
;
117 typedef typename
tested_sequence::allocation_traits tested_allocation_traits
;
119 int test_default_constructor()
121 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
122 expected_calls
f(tested_allocation_traits::freebuf_calls
);
127 CORBA::ULong(tested_allocation_traits::default_maximum()),
129 CHECK_EQUAL(CORBA::ULong(0), x
.length());
131 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
132 // Nothing was allocated then there is nothing to free.
133 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
137 int test_copy_constructor_from_default()
139 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
140 expected_calls
f(tested_allocation_traits::freebuf_calls
);
141 expected_calls
i(tested_element_traits::default_initializer_calls
);
142 expected_calls
d(tested_element_traits::duplicate_calls
);
146 a
.reset(); f
.reset(); i
.reset(); d
.reset();
148 tested_sequence
y(x
);
149 // Default constructed sequence doesn't have elements,
150 // thus there is nothing to allocate/copy in copy constructor.
151 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
152 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
153 FAIL_RETURN_IF_NOT(i
.expect(0), i
);
154 FAIL_RETURN_IF_NOT(d
.expect(0), d
);
156 CHECK_EQUAL(x
.maximum(), y
.maximum());
157 CHECK_EQUAL(x
.length(), y
.length());
158 CHECK_EQUAL(x
.release(), y
.release());
160 // Nothing was allocated then there is nothing to free.
161 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
165 int test_index_accessor()
170 tested_sequence
const & y
= x
;
171 character_type
const * t
= y
[4];
172 FAIL_RETURN_IF_NOT(helper::compare_empty(t
),
173 "Unexpected string value " << t
);
177 int test_index_modifier()
182 tested_sequence
const & y
= x
;
184 character_type
const * text
= helper::test_string();
187 character_type
const * t
= y
[4];
189 FAIL_RETURN_IF_NOT(ACE_OS::strcmp(text
, x
[4]) == 0,
190 "Mismatched values expected=" << text
191 << ", got=" << x
[4]);
192 FAIL_RETURN_IF_NOT(ACE_OS::strcmp(text
, y
[4]) == 0,
193 "Mismatched values expected=" << text
194 << ", got=" << y
[4]);
199 int test_index_checking()
204 tested_sequence
const & y
= x
;
205 character_type
const * lhs
;
206 character_type
const * rhs
= 0;
207 CHECK_THROW(lhs
= y
[32], std::range_error
);
208 CHECK_THROW(x
[32] = rhs
, std::range_error
);
209 ACE_UNUSED_ARG (lhs
);
213 int test_copy_constructor_values()
217 for(CORBA::ULong i
= 0; i
!= 16; ++i
)
219 a
[i
] = helper::to_string(i
);
222 expected_calls
d(tested_element_traits::duplicate_calls
);
223 expected_calls
r(tested_element_traits::release_calls
);
225 CORBA::ULong max
= 0;
227 tested_sequence
b(a
);
228 FAIL_RETURN_IF_NOT(d
.expect(16), d
);
231 CHECK_EQUAL(a
.length(), b
.length());
232 for(CORBA::ULong i
= 0; i
!= a
.length(); ++i
)
234 FAIL_RETURN_IF_NOT(ACE_OS::strcmp(a
[i
], b
[i
]) == 0,
235 "Mismatched elements at index=" << i
240 FAIL_RETURN_IF_NOT(r
.expect(max
), r
);
244 int test_freebuf_releases_elements()
246 value_type
* buffer
= tested_sequence::allocbuf(32);
248 expected_calls
r(tested_element_traits::release_calls
);
249 expected_calls
f(tested_allocation_traits::freebuf_calls
);
251 tested_sequence::freebuf(buffer
);
253 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
254 FAIL_RETURN_IF_NOT(r
.expect(32), r
);
258 int test_assignment_from_default()
260 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
261 expected_calls
f(tested_allocation_traits::freebuf_calls
);
265 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
267 CORBA::ULong(tested_allocation_traits::default_maximum()),
269 CHECK_EQUAL(CORBA::ULong(0), x
.length());
272 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
275 // Default constructed sequence doesn't have elements,
276 // thus there is nothing to allocate/copy in operator=.
277 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
278 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
279 CHECK_EQUAL(x
.maximum(), y
.maximum());
280 CHECK_EQUAL(x
.length(), y
.length());
281 CHECK_EQUAL(x
.release(), y
.release());
283 // Nothing was allocated then there is nothing to free.
284 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
288 int test_assignment_values()
292 for(CORBA::ULong i
= 0; i
!= 16; ++i
)
294 a
[i
] = helper::to_string(i
);
297 expected_calls
d(tested_element_traits::duplicate_calls
);
298 expected_calls
r(tested_element_traits::release_calls
);
299 CORBA::ULong max
= 0;
303 FAIL_RETURN_IF_NOT(d
.expect(16), d
);
307 CHECK_EQUAL(a
.maximum(), b
.maximum());
308 CHECK_EQUAL(a
.length(), b
.length());
309 CHECK_EQUAL(a
.release(), b
.release());
310 for(CORBA::ULong i
= 0; i
!= a
.length(); ++i
)
312 FAIL_RETURN_IF_NOT(ACE_OS::strcmp(a
[i
], b
[i
]) == 0,
313 "Mismatched elements at index " << i
);
317 FAIL_RETURN_IF_NOT(r
.expect(max
), r
);
321 int test_exception_in_copy_constructor()
323 expected_calls
f(tested_allocation_traits::freebuf_calls
);
325 tested_sequence x
; x
.length(8);
328 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
329 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
330 CHECK_THROW(tested_sequence
y(x
), testing_exception
);
331 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
333 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
337 int test_exception_in_assignment()
339 expected_calls
f(tested_allocation_traits::freebuf_calls
);
341 tested_sequence x
; x
.length(2);
343 tested_sequence y
; y
.length(3);
345 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
347 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
348 CHECK_THROW(y
= x
, testing_exception
);
350 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
351 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
353 CHECK_EQUAL(CORBA::ULong(3), y
.length());
355 FAIL_RETURN_IF_NOT(f
.expect(2), f
);
359 int test_duplicate_exception_in_copy_constructor()
361 expected_calls
f(tested_allocation_traits::freebuf_calls
);
363 tested_sequence x
; x
.length(8);
366 for(CORBA::ULong i
= 0; i
!= 8; ++i
)
368 x
[i
] = helper::allocate_test_string();
371 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
372 expected_calls
d(tested_element_traits::duplicate_calls
);
373 expected_calls
r(tested_element_traits::release_calls
);
375 tested_element_traits::duplicate_calls
.failure_countdown(4);
377 CHECK_THROW(tested_sequence
y(x
), testing_exception
);
378 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
379 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
380 FAIL_RETURN_IF_NOT(d
.expect(4), d
);
381 FAIL_RETURN_IF_NOT(r
.expect(x
.maximum()), r
);
383 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
387 int test_duplicate_exception_in_assignment()
389 expected_calls
f(tested_allocation_traits::freebuf_calls
);
391 tested_sequence x
; x
.length(4);
394 for(CORBA::ULong i
= 0; i
!= 4; ++i
)
396 x
[i
] = helper::allocate_test_string();
399 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
400 expected_calls
d(tested_element_traits::duplicate_calls
);
401 expected_calls
r(tested_element_traits::release_calls
);
405 tested_sequence y
; y
.length(8);
406 for(CORBA::ULong i
= 0; i
!= 8; ++i
)
408 y
[i
] = helper::allocate_test_string();
415 tested_element_traits::duplicate_calls
.failure_countdown(4);
416 CHECK_THROW(x
= y
, testing_exception
);
417 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
418 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
419 FAIL_RETURN_IF_NOT(d
.expect(4), d
);
420 FAIL_RETURN_IF_NOT(r
.expect(y
.maximum()), r
);
422 CHECK_EQUAL(CORBA::ULong(8), y
.length());
423 for(CORBA::ULong i
= 0; i
!= 8; ++i
)
426 helper::compare_test_string(y
[i
]),
427 "Mismatch in element " << i
428 << ", got=" << y
[i
]);
431 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
433 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
437 int test_get_buffer_const()
439 tested_sequence a
; a
.length(8);
440 tested_sequence
const & b
= a
;
442 const_value_type
const * buffer
= b
.get_buffer();
443 a
[0] = helper::test_string();
445 CHECK_EQUAL(buffer
, b
.get_buffer());
446 FAIL_RETURN_IF_NOT(ACE_OS::strcmp(a
[0], buffer
[0]) == 0,
447 "Mismatched elements a[0]=" << a
[0]
448 << ", buffer[0]=" << buffer
[0]);
455 status
+= this->test_default_constructor();
456 status
+= this->test_copy_constructor_from_default();
457 status
+= this->test_index_accessor();
458 status
+= this->test_index_modifier();
459 status
+= this->test_index_checking();
460 status
+= this->test_copy_constructor_values();
461 status
+= this->test_freebuf_releases_elements();
462 status
+= this->test_assignment_from_default();
463 status
+= this->test_assignment_values();
464 status
+= this->test_exception_in_copy_constructor();
465 status
+= this->test_exception_in_assignment();
466 status
+= this->test_duplicate_exception_in_copy_constructor();
467 status
+= this->test_duplicate_exception_in_assignment();
468 status
+= this->test_get_buffer_const();
474 #endif // guard_string_sequence_tester_hpp