Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / string_sequence_tester.hpp
blob5ffdf62d68b5e6a658170514aa7d9817bf3b0d3b
1 #ifndef guard_string_sequence_tester_hpp
2 #define guard_string_sequence_tester_hpp
3 /**
4 * @file
6 * @brief Helper class to implement tests for *_string_sequence
8 * @author Carlos O'Ryan
9 */
10 #include "tao/String_Traits_T.h"
12 #include "ace/OS_NS_string.h"
14 #include "test_macros.h"
16 #include <sstream>
18 template<typename charT>
19 struct string_sequence_test_helpers
23 template<>
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(
34 test_string());
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;
45 os << i;
46 return TAO::details::string_traits<char,true>::duplicate(
47 os.str().c_str());
50 inline static bool compare(int i, char const * value)
52 std::ostringstream os;
53 os << i;
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
65 #endif
67 #if defined(ACE_HAS_WCHAR) && !defined(TAO_LACKS_WCHAR_CXX_STDLIB)
68 template<>
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(
79 test_string());
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;
90 os << i;
91 return TAO::details::string_traits<CORBA::WChar,true>::duplicate(
92 os.str().c_str());
95 inline static bool compare(int i, CORBA::WChar const * value)
97 std::wostringstream os;
98 os << i;
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);
124 tested_sequence x;
126 CHECK_EQUAL(
127 CORBA::ULong(tested_allocation_traits::default_maximum()),
128 x.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);
134 return 0;
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);
144 tested_sequence x;
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);
162 return 0;
165 int test_index_accessor()
167 tested_sequence x;
168 x.length(8);
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);
174 return 0;
177 int test_index_modifier()
179 tested_sequence x;
180 x.length(8);
182 tested_sequence const & y = x;
184 character_type const * text = helper::test_string();
185 x[4] = text;
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]);
195 CHECK(text != t);
196 return 0;
199 int test_index_checking()
201 tested_sequence x;
202 x.length(8);
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);
210 return 0;
213 int test_copy_constructor_values()
215 tested_sequence a;
216 a.length(16);
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);
229 max = b.maximum();
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
236 << ", a=" << a[i]
237 << ", b=" << b[i]);
240 FAIL_RETURN_IF_NOT(r.expect(max), r);
241 return 0;
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);
255 return 0;
258 int test_assignment_from_default()
260 expected_calls a(tested_allocation_traits::allocbuf_calls);
261 expected_calls f(tested_allocation_traits::freebuf_calls);
264 tested_sequence x;
265 FAIL_RETURN_IF_NOT(a.expect(0), a);
266 CHECK_EQUAL(
267 CORBA::ULong(tested_allocation_traits::default_maximum()),
268 x.maximum());
269 CHECK_EQUAL(CORBA::ULong(0), x.length());
271 tested_sequence y;
272 FAIL_RETURN_IF_NOT(a.expect(0), a);
274 y = x;
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);
285 return 0;
288 int test_assignment_values()
290 tested_sequence a;
291 a.length(16);
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;
301 tested_sequence b;
302 b = a;
303 FAIL_RETURN_IF_NOT(d.expect(16), d);
305 max = b.maximum();
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);
315 r.reset();
317 FAIL_RETURN_IF_NOT(r.expect(max), r);
318 return 0;
321 int test_exception_in_copy_constructor()
323 expected_calls f(tested_allocation_traits::freebuf_calls);
325 tested_sequence x; x.length(8);
326 f.reset();
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);
334 return 0;
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);
346 f.reset();
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);
356 return 0;
359 int test_duplicate_exception_in_copy_constructor()
361 expected_calls f(tested_allocation_traits::freebuf_calls);
363 tested_sequence x; x.length(8);
364 f.reset();
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);
384 return 0;
387 int test_duplicate_exception_in_assignment()
389 expected_calls f(tested_allocation_traits::freebuf_calls);
391 tested_sequence x; x.length(4);
392 f.reset();
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();
411 a.reset();
412 d.reset();
413 r.reset();
414 f.reset();
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)
425 FAIL_RETURN_IF_NOT(
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);
434 return 0;
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]);
449 return 0;
452 int test_all ()
454 int status=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();
470 return status;
474 #endif // guard_string_sequence_tester_hpp