2 #include <posixx/buffer.hpp> // buffer
3 #include <ostream> // std::ostream
4 #include <iomanip> // std::hex, setfill, setw
6 // declared here so boost.Test can see it
7 std::ostream& operator << (std::ostream& os, const posixx::buffer& b);
9 #include <boost/test/unit_test.hpp>
13 std::ostream& operator << (std::ostream& os, const posixx::buffer& b)
15 os << "buffer(size=" << b.size() << ", data=";
16 for (posixx::buffer::const_iterator i = b.begin(); i != b.end(); ++i)
17 os << std::hex << std::setfill('0') << std::setw(2) << *i;
21 // buffer iterators are char*, so they are printed as C-style string, leading
22 // to memory problems because they are not usually null-terminated.
23 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer ) // FIXME
24 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::iterator )
25 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_iterator )
27 // avoid compilation warning concerning unused variables
28 void dummy(buffer::const_iterator i) {}
30 BOOST_AUTO_TEST_SUITE( buffer_suite )
32 BOOST_AUTO_TEST_CASE( types_test )
34 buffer::value_type v = 1;
35 buffer::reference r = v;
36 BOOST_CHECK_EQUAL(r, 1);
38 BOOST_CHECK_EQUAL(v, 2);
39 buffer::const_reference cr = v;
40 BOOST_CHECK_EQUAL(cr, 2);
42 buffer::const_iterator ci = i;
44 buffer::size_type s = 5;
46 buffer::pointer p = &v;
47 BOOST_CHECK_EQUAL(*p, 2);
49 BOOST_CHECK_EQUAL(v, 3);
50 BOOST_CHECK_EQUAL(r, 3);
51 BOOST_CHECK_EQUAL(cr, 3);
52 buffer::const_pointer cp = p;
53 BOOST_CHECK_EQUAL(*cp, 3);
54 buffer::difference_type d = p - cp;
55 BOOST_CHECK_EQUAL(d, 0);
58 BOOST_AUTO_TEST_CASE( default_constructor_test )
61 BOOST_CHECK_EQUAL(b.size(), 0);
62 BOOST_CHECK_EQUAL(b.capacity(), b.size());
63 BOOST_CHECK(b.empty());
64 BOOST_CHECK_LT(b.size(), b.max_size());
65 BOOST_CHECK(b.begin() == b.end());
66 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
67 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
68 BOOST_CHECK(!b.c_array());
71 BOOST_AUTO_TEST_CASE( const_default_constructor_test )
74 BOOST_CHECK_EQUAL(b.size(), 0);
75 BOOST_CHECK_EQUAL(b.capacity(), b.size());
76 BOOST_CHECK(b.empty());
77 BOOST_CHECK_LT(b.size(), b.max_size());
78 BOOST_CHECK(b.begin() == b.end());
79 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
80 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
81 BOOST_CHECK(!b.c_array());
84 BOOST_AUTO_TEST_CASE( n_constructor_test )
87 BOOST_CHECK_EQUAL(b.size(), 100);
88 BOOST_CHECK_EQUAL(b.capacity(), b.size());
89 BOOST_CHECK(!b.empty());
90 BOOST_CHECK_LT(b.size(), b.max_size());
91 BOOST_CHECK(b.begin() != b.end());
92 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
93 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
95 b.at(1) = b.at(b.size()-2);
96 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
97 BOOST_CHECK(b.c_array());
100 BOOST_AUTO_TEST_CASE( const_n_constructor_test )
103 BOOST_CHECK_EQUAL(b.size(), 100);
104 BOOST_CHECK_EQUAL(b.capacity(), b.size());
105 BOOST_CHECK(!b.empty());
106 BOOST_CHECK_LT(b.size(), b.max_size());
107 BOOST_CHECK(b.begin() != b.end());
108 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
109 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
110 buffer::value_type x = b[50];
112 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
113 BOOST_CHECK(b.c_array());
114 for (buffer::const_iterator i = b.begin(); i != b.end(); ++i);
117 BOOST_AUTO_TEST_CASE( default_n_copy_constructor_test )
120 BOOST_CHECK_EQUAL(b.size(), 100);
121 BOOST_CHECK_EQUAL(b.capacity(), b.size());
122 BOOST_CHECK(!b.empty());
123 BOOST_CHECK_LT(b.size(), b.max_size());
124 BOOST_CHECK(b.begin() != b.end());
125 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
126 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
127 buffer::value_type x = b[50];
129 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
130 BOOST_CHECK(b.c_array());
131 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
132 BOOST_CHECK_EQUAL(*i, 0x5f);
133 b[b.size()-1] = 0xf5;
134 b[0] = b[b.size()-1];
135 BOOST_CHECK_EQUAL(b[b.size()-1], 0xf5);
136 BOOST_CHECK_EQUAL(b.back(), 0xf5);
137 BOOST_CHECK_EQUAL(*(b.end()-1), 0xf5);
138 BOOST_CHECK_EQUAL(b[0], 0xf5);
139 BOOST_CHECK_EQUAL(b.front(), 0xf5);
140 BOOST_CHECK_EQUAL(*b.begin(), 0xf5);
141 b.at(b.size()-2) = 0xff;
142 b.at(1) = b.at(b.size()-2);
143 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
144 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
145 BOOST_CHECK_EQUAL(b[1], 0xff);
146 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
149 BOOST_AUTO_TEST_CASE( copy_constructor_test )
155 BOOST_CHECK_EQUAL(b.size(), bb.size());
156 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
157 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
158 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
159 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
162 BOOST_AUTO_TEST_CASE( iterator_constructor_test )
164 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
166 BOOST_CHECK_EQUAL(b.size(), 5);
167 BOOST_CHECK_EQUAL(b.capacity(), b.size());
168 BOOST_CHECK(!b.empty());
169 BOOST_CHECK_LT(b.size(), b.max_size());
170 BOOST_CHECK(b.begin() != b.end());
171 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
172 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
173 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
174 BOOST_CHECK(b.c_array());
175 for (int i = 0; i != 5; ++i)
176 BOOST_CHECK_EQUAL(b.at(i), i + 5);
179 BOOST_AUTO_TEST_CASE( const_iterator_constructor_test )
181 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
182 const buffer b(a, a + 5);
183 BOOST_CHECK_EQUAL(b.size(), 5);
184 BOOST_CHECK_EQUAL(b.capacity(), b.size());
185 BOOST_CHECK(!b.empty());
186 BOOST_CHECK_LT(b.size(), b.max_size());
187 BOOST_CHECK(b.begin() != b.end());
188 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
189 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
190 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
191 BOOST_CHECK(b.c_array());
192 for (int i = 0; i != 5; ++i)
193 BOOST_CHECK_EQUAL(b.at(i), i + 5);
196 BOOST_AUTO_TEST_CASE( operator_equals_test )
198 buffer::value_type a[] = { 56, 123 };
199 const buffer b(a, a + 2);
202 BOOST_CHECK_EQUAL(b.size(), bb.size());
203 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
204 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
205 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
206 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
207 buffer::pointer p = bb.c_array();
209 BOOST_CHECK_EQUAL(bb.c_array(), p);
210 BOOST_CHECK_EQUAL(b.size(), 2);
213 BOOST_AUTO_TEST_CASE( iterators_test )
215 buffer::value_type a[] = { 56, 123, 253 };
217 BOOST_CHECK_NE(b.begin(), b.end());
218 BOOST_CHECK_LT(b.begin(), b.end());
219 buffer::iterator i = b.begin();
220 BOOST_CHECK_EQUAL(*i, 56);
222 BOOST_CHECK_EQUAL(*i, 123);
224 BOOST_CHECK_EQUAL(*i, 253);
226 BOOST_CHECK_EQUAL(i, b.end());
228 BOOST_CHECK_EQUAL(*i, 253);
230 BOOST_CHECK_EQUAL(*i, 123);
232 BOOST_CHECK_EQUAL(*i, 56);
234 BOOST_CHECK_EQUAL(*(i + 1), 253);
235 BOOST_CHECK_EQUAL(*(i - 1), 56);
236 BOOST_CHECK_EQUAL(i - 1, b.begin());
237 BOOST_CHECK_EQUAL(i + 2, b.end());
238 BOOST_CHECK_EQUAL(i - b.begin(), 1);
240 BOOST_CHECK_EQUAL(*i, 77);
241 BOOST_CHECK_EQUAL(*(b.begin() + 1), 77);
244 BOOST_AUTO_TEST_CASE( resize_test )
247 BOOST_CHECK(!b.c_array());
248 BOOST_CHECK(b.empty());
249 BOOST_CHECK_EQUAL(b.size(), 0);
250 BOOST_CHECK_EQUAL(b.capacity(), b.size());
252 BOOST_CHECK(b.c_array());
253 BOOST_CHECK(!b.empty());
254 BOOST_CHECK_EQUAL(b.size(), 10);
255 BOOST_CHECK_EQUAL(b.capacity(), b.size());
257 BOOST_CHECK_EQUAL(b[9], 1);
258 BOOST_CHECK_EQUAL(b.at(9), 1);
259 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
261 BOOST_CHECK(b.c_array());
262 BOOST_CHECK(!b.empty());
263 BOOST_CHECK_EQUAL(b.size(), 15);
264 BOOST_CHECK_EQUAL(b.capacity(), b.size());
265 BOOST_CHECK_EQUAL(b[9], 1);
266 BOOST_CHECK_EQUAL(b.at(9), 1);
267 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
268 for (buffer::const_iterator i = b.begin()+10; i != b.end(); ++i)
269 BOOST_CHECK_EQUAL(*i, 0x77);
271 BOOST_CHECK(!b.c_array());
272 BOOST_CHECK(b.empty());
273 BOOST_CHECK_EQUAL(b.size(), 0);
274 BOOST_CHECK_EQUAL(b.capacity(), b.size());
277 BOOST_AUTO_TEST_CASE( reserve_test )
281 BOOST_CHECK(b.c_array());
282 BOOST_CHECK(!b.empty());
283 BOOST_CHECK_EQUAL(b.size(), 10);
284 BOOST_CHECK_EQUAL(b.capacity(), b.size());
286 BOOST_CHECK_EQUAL(b[9], 1);
287 BOOST_CHECK_EQUAL(b.at(9), 1);
288 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
290 BOOST_CHECK(b.c_array());
291 BOOST_CHECK(!b.empty());
292 BOOST_CHECK_EQUAL(b.size(), 10);
293 BOOST_CHECK_EQUAL(b.capacity(), b.size());
294 BOOST_CHECK_EQUAL(b[9], 1);
296 BOOST_CHECK(b.c_array());
297 BOOST_CHECK(!b.empty());
298 BOOST_CHECK_EQUAL(b.size(), 20);
299 BOOST_CHECK_EQUAL(b.capacity(), b.size());
300 BOOST_CHECK_EQUAL(b[9], 1);
302 BOOST_CHECK(b.c_array());
303 BOOST_CHECK(!b.empty());
304 BOOST_CHECK_EQUAL(b.size(), 20);
305 BOOST_CHECK_EQUAL(b.capacity(), b.size());
306 BOOST_CHECK_EQUAL(b[9], 1);
309 BOOST_AUTO_TEST_CASE( assign_iterator_test )
311 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
313 BOOST_CHECK(!b.c_array());
314 BOOST_CHECK(b.empty());
315 BOOST_CHECK_EQUAL(b.size(), 0);
316 BOOST_CHECK_EQUAL(b.capacity(), b.size());
318 BOOST_CHECK_EQUAL(b.size(), 5);
319 BOOST_CHECK_EQUAL(b.capacity(), b.size());
320 BOOST_CHECK(!b.empty());
321 BOOST_CHECK_LT(b.size(), b.max_size());
322 BOOST_CHECK(b.begin() != b.end());
323 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
324 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
325 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
326 BOOST_CHECK(b.c_array());
327 for (int i = 0; i != 5; ++i)
328 BOOST_CHECK_EQUAL(b.at(i), i + 5);
331 BOOST_AUTO_TEST_CASE( assign_n_copy_test )
335 BOOST_CHECK_EQUAL(b.size(), 5);
336 BOOST_CHECK_EQUAL(b.capacity(), b.size());
337 BOOST_CHECK(!b.empty());
338 BOOST_CHECK_LT(b.size(), b.max_size());
339 BOOST_CHECK(b.begin() != b.end());
340 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
341 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
342 buffer::value_type x = b[2];
344 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
345 BOOST_CHECK(b.c_array());
346 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
347 BOOST_CHECK_EQUAL(*i, 0x33);
348 b[b.size()-1] = 0x66;
349 b[0] = b[b.size()-1];
350 BOOST_CHECK_EQUAL(b[b.size()-1], 0x66);
351 BOOST_CHECK_EQUAL(b.back(), 0x66);
352 BOOST_CHECK_EQUAL(*(b.end()-1), 0x66);
353 BOOST_CHECK_EQUAL(b[0], 0x66);
354 BOOST_CHECK_EQUAL(b.front(), 0x66);
355 BOOST_CHECK_EQUAL(*b.begin(), 0x66);
356 b.at(b.size()-2) = 0xff;
357 b.at(1) = b.at(b.size()-2);
358 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
359 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
360 BOOST_CHECK_EQUAL(b[1], 0xff);
361 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
364 BOOST_AUTO_TEST_CASE( clear_test )
368 BOOST_CHECK(!b.c_array());
369 BOOST_CHECK(b.empty());
370 BOOST_CHECK_EQUAL(b.size(), 0);
371 BOOST_CHECK_EQUAL(b.capacity(), b.size());
374 BOOST_AUTO_TEST_CASE( operators_test )
376 const buffer b1(5, 78);
377 const buffer b2(5, 78);
380 const buffer b3 = bb;
382 const buffer b4 = bb;
383 const buffer b5(4, 78);
386 const buffer b7 = bb;
387 BOOST_CHECK_EQUAL(b1, b1);
388 BOOST_CHECK_EQUAL(b1, b2);
389 BOOST_CHECK_LE(b1, b2);
390 BOOST_CHECK_GE(b1, b2);
391 BOOST_CHECK_NE(b1, b3);
392 BOOST_CHECK_NE(b1, b4);
393 BOOST_CHECK_NE(b1, b5);
394 BOOST_CHECK_NE(b1, b6);
395 BOOST_CHECK_GT(b1, b4);
396 BOOST_CHECK_GE(b1, b4);
397 BOOST_CHECK_LT(b1, b3);
398 BOOST_CHECK_LE(b1, b3);
399 BOOST_CHECK_GT(b1, b6);
400 BOOST_CHECK_LT(b6, b2);
401 BOOST_CHECK_GE(b3, b6);
402 BOOST_CHECK_LE(b6, b4);
403 BOOST_CHECK_EQUAL(b6, b6);
404 BOOST_CHECK_EQUAL(b6, b7);
405 BOOST_CHECK_GE(b6, b7);
406 BOOST_CHECK_LE(b6, b7);
409 BOOST_AUTO_TEST_CASE( swap_test )
411 buffer::value_type a1[5] = { 5, 6, 7, 8, 9 };
412 buffer::value_type a2[3] = { 55, 77, 99 };
413 const buffer b1c(a1, a1 + 5);
414 const buffer b2c(a2, a2 + 3);
418 BOOST_CHECK_EQUAL(b1, b2c);
419 BOOST_CHECK_EQUAL(b2, b1c);
420 posixx::swap(b2, b1);
421 BOOST_CHECK_EQUAL(b1, b1c);
422 BOOST_CHECK_EQUAL(b2, b2c);
425 BOOST_AUTO_TEST_SUITE_END()