1 #ifndef guard_value_sequence_tester_hpp
2 #define guard_value_sequence_tester_hpp
6 * @brief Helper class to implement tests for *_value_sequence
8 * @author Carlos O'Ryan
10 #include "tao/Basic_Types.h"
12 #include "test_macros.h"
13 #include "tao/SystemException.h"
15 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
17 template<class tested_sequence
,
18 class tested_allocation_traits
>
19 struct value_sequence_tester
21 typedef typename
tested_sequence::value_type value_type
;
22 typedef typename
tested_sequence::const_value_type const_value_type
;
24 int test_default_constructor()
26 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
27 expected_calls
f(tested_allocation_traits::freebuf_calls
);
32 CORBA::ULong(tested_allocation_traits::default_maximum()),
34 CHECK_EQUAL(CORBA::ULong(0), x
.length());
36 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
37 // Nothing was allocated then there is nothing to free.
38 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
43 int test_copy_constructor_from_default()
45 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
46 expected_calls
f(tested_allocation_traits::freebuf_calls
);
49 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
51 CORBA::ULong(tested_allocation_traits::default_maximum()),
53 CHECK_EQUAL(CORBA::ULong(0), x
.length());
56 // Default constructed sequence doesn't have elements,
57 // thus there is nothing to allocate/copy in copy constructor.
58 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
59 CHECK_EQUAL(x
.maximum(), y
.maximum());
60 CHECK_EQUAL(x
.length(), y
.length());
61 CHECK_EQUAL(x
.release(), y
.release());
63 // Nothing was allocated then there is nothing to free.
64 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
68 int test_index_accessor()
74 // Set x[4] to any value just for suppressing valgrind complains.
77 tested_sequence
const & y
= x
;
78 const_value_type
& z
= y
[4];
81 catch (const ::CORBA::BAD_PARAM
&)
83 ACE_ERROR ((LM_ERROR
, "Error: test_index_accessor: BAD_PARAM exception caught\n"));
89 int test_index_modifier()
94 tested_sequence
const & y
= x
;
95 const_value_type
& z
= y
[4];
103 int test_index_checking()
108 tested_sequence
const & y
= x
;
111 CHECK_THROW(z
= y
[32], std::range_error
);
112 CHECK_THROW(x
[32] = z
, std::range_error
);
116 int test_copy_constructor_values()
120 for(CORBA::ULong i
= 0; i
!= 16; ++i
) a
[i
] = i
*i
;
122 tested_sequence
b(a
);
123 CHECK_EQUAL(a
.length(), b
.length());
124 for(CORBA::ULong i
= 0; i
!= a
.length(); ++i
)
126 FAIL_RETURN_IF_NOT(a
[i
] == b
[i
],
127 "Mismatched elements at index " << i
);
132 int test_assignment_from_default()
134 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
135 expected_calls
f(tested_allocation_traits::freebuf_calls
);
138 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
140 CORBA::ULong(tested_allocation_traits::default_maximum()),
142 CHECK_EQUAL(CORBA::ULong(0), x
.length());
145 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
148 // Default constructed sequence doesn't have elements,
149 // thus there is nothing to allocate/copy in operator=.
150 FAIL_RETURN_IF_NOT(a
.expect(0), a
);
151 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
152 CHECK_EQUAL(x
.maximum(), y
.maximum());
153 CHECK_EQUAL(x
.length(), y
.length());
154 CHECK_EQUAL(x
.release(), y
.release());
156 // Nothing was allocated then there is nothing to free.
157 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
161 int test_assignment_values()
165 for(CORBA::ULong i
= 0; i
!= 16; ++i
) a
[i
] = i
*i
;
169 CHECK_EQUAL(a
.maximum(), b
.maximum());
170 CHECK_EQUAL(a
.length(), b
.length());
171 CHECK_EQUAL(a
.release(), b
.release());
172 for(CORBA::ULong i
= 0; i
!= a
.length(); ++i
)
174 FAIL_RETURN_IF_NOT(a
[i
] == b
[i
],
175 "Mismatched elements at index " << i
);
180 int test_exception_in_copy_constructor()
182 expected_calls
f(tested_allocation_traits::freebuf_calls
);
184 tested_sequence x
; x
.length(8);
187 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
188 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
189 CHECK_THROW(tested_sequence
y(x
), testing_exception
);
190 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
192 FAIL_RETURN_IF_NOT(f
.expect(1), f
);
196 int test_exception_in_assignment()
198 expected_calls
f(tested_allocation_traits::freebuf_calls
);
200 tested_sequence x
; x
.length(2);
202 tested_sequence y
; y
.length(3);
204 expected_calls
a(tested_allocation_traits::allocbuf_calls
);
206 tested_allocation_traits::allocbuf_calls
.failure_countdown(1);
207 CHECK_THROW(y
= x
, testing_exception
);
209 FAIL_RETURN_IF_NOT(a
.expect(1), a
);
210 FAIL_RETURN_IF_NOT(f
.expect(0), f
);
212 CHECK_EQUAL(CORBA::ULong(3), y
.length());
214 FAIL_RETURN_IF_NOT(f
.expect(2), f
);
218 int test_get_buffer_const()
220 tested_sequence a
; a
.length(4);
221 tested_sequence
const & b
= a
;
223 value_type
const * buffer
= b
.get_buffer();
224 a
[0] = 1; a
[1] = 4; a
[2] = 9; a
[3] = 16;
226 CHECK_EQUAL(1, buffer
[0]);
227 CHECK_EQUAL(4, buffer
[1]);
228 CHECK_EQUAL(9, buffer
[2]);
229 CHECK_EQUAL(16, buffer
[3]);
237 status
+=this->test_default_constructor();
238 status
+=this->test_copy_constructor_from_default();
239 status
+=this->test_index_accessor();
240 status
+=this->test_index_modifier();
241 status
+=this->test_index_checking();
242 status
+=this->test_copy_constructor_values();
243 status
+=this->test_assignment_from_default();
244 status
+=this->test_assignment_values();
245 status
+=this->test_exception_in_copy_constructor();
246 status
+=this->test_exception_in_assignment();
247 status
+=this->test_get_buffer_const();
252 TAO_END_VERSIONED_NAMESPACE_DECL
253 #endif // guard_value_sequence_tester_hpp