Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / Sequence_Unit_Tests / value_sequence_tester.hpp
blobf2758fe5117d8805de6318ba48061851c9570edb
1 #ifndef guard_value_sequence_tester_hpp
2 #define guard_value_sequence_tester_hpp
3 /**
4 * @file
6 * @brief Helper class to implement tests for *_value_sequence
8 * @author Carlos O'Ryan
9 */
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);
29 tested_sequence x;
31 CHECK_EQUAL(
32 CORBA::ULong(tested_allocation_traits::default_maximum()),
33 x.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);
39 return 0;
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);
48 tested_sequence x;
49 FAIL_RETURN_IF_NOT(a.expect(0), a);
50 CHECK_EQUAL(
51 CORBA::ULong(tested_allocation_traits::default_maximum()),
52 x.maximum());
53 CHECK_EQUAL(CORBA::ULong(0), x.length());
55 tested_sequence y(x);
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);
65 return 0;
68 int test_index_accessor()
70 try
72 tested_sequence x;
73 x.length(8);
74 // Set x[4] to any value just for suppressing valgrind complains.
75 x[4] = 1;
77 tested_sequence const & y = x;
78 const_value_type & z = y[4];
79 CHECK_EQUAL(z, y[4]);
81 catch (const ::CORBA::BAD_PARAM &)
83 ACE_ERROR ((LM_ERROR, "Error: test_index_accessor: BAD_PARAM exception caught\n"));
84 return 1;
86 return 0;
89 int test_index_modifier()
91 tested_sequence x;
92 x.length(8);
94 tested_sequence const & y = x;
95 const_value_type & z = y[4];
96 x[4] = 4;
97 CHECK_EQUAL(4, x[4]);
98 CHECK_EQUAL(4, y[4]);
99 CHECK_EQUAL(4, z);
100 return 0;
103 int test_index_checking()
105 tested_sequence x;
106 x.length(8);
108 tested_sequence const & y = x;
109 int z = 0;
111 CHECK_THROW(z = y[32], std::range_error);
112 CHECK_THROW(x[32] = z, std::range_error);
113 return 0;
116 int test_copy_constructor_values()
118 tested_sequence a;
119 a.length(16);
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);
129 return 0;
132 int test_assignment_from_default()
134 expected_calls a(tested_allocation_traits::allocbuf_calls);
135 expected_calls f(tested_allocation_traits::freebuf_calls);
137 tested_sequence x;
138 FAIL_RETURN_IF_NOT(a.expect(0), a);
139 CHECK_EQUAL(
140 CORBA::ULong(tested_allocation_traits::default_maximum()),
141 x.maximum());
142 CHECK_EQUAL(CORBA::ULong(0), x.length());
144 tested_sequence y;
145 FAIL_RETURN_IF_NOT(a.expect(0), a);
147 y = x;
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);
158 return 0;
161 int test_assignment_values()
163 tested_sequence a;
164 a.length(16);
165 for(CORBA::ULong i = 0; i != 16; ++i) a[i] = i*i;
167 tested_sequence b;
168 b = a;
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);
177 return 0;
180 int test_exception_in_copy_constructor()
182 expected_calls f(tested_allocation_traits::freebuf_calls);
184 tested_sequence x; x.length(8);
185 f.reset();
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);
193 return 0;
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);
205 f.reset();
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);
215 return 0;
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]);
230 return 0;
233 int test_all()
235 int status = 0;
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();
248 return status;
252 TAO_END_VERSIONED_NAMESPACE_DECL
253 #endif // guard_value_sequence_tester_hpp