1 // Copyright Leandro Lucarella 2008 - 2010.
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file COPYING or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
8 #include <posixx/buffer.hpp> // buffer
9 #include <ostream> // std::ostream
10 #include <iomanip> // std::hex, setfill, setw
12 // declared here so boost.Test can see it
13 std::ostream& operator << (std::ostream& os, const posixx::buffer& b);
15 #include <boost/test/unit_test.hpp>
19 std::ostream& operator << (std::ostream& os, const posixx::buffer& b)
21 os << "buffer(size=" << b.size() << ", data=";
22 for (posixx::buffer::const_iterator i = b.begin(); i != b.end(); ++i)
23 os << std::hex << std::setfill('0') << std::setw(2) << *i;
27 // buffer iterators are char*, so they are printed as C-style string, leading
28 // to memory problems because they are not usually null-terminated.
29 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer ) // FIXME
30 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::iterator )
31 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_iterator )
32 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::reverse_iterator )
33 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_reverse_iterator )
35 // avoid compilation warning concerning unused variables
36 void dummy(buffer::const_iterator i) {}
38 BOOST_AUTO_TEST_SUITE( buffer_suite )
40 BOOST_AUTO_TEST_CASE( types_test )
42 buffer::value_type v = 1;
43 buffer::reference r = v;
44 BOOST_CHECK_EQUAL(r, 1);
46 BOOST_CHECK_EQUAL(v, 2);
47 buffer::const_reference cr = v;
48 BOOST_CHECK_EQUAL(cr, 2);
50 buffer::iterator i = b.end();
51 buffer::const_iterator ci = i;
53 buffer::size_type s = 5;
55 buffer::pointer p = &v;
56 BOOST_CHECK_EQUAL(*p, 2);
58 BOOST_CHECK_EQUAL(v, 3);
59 BOOST_CHECK_EQUAL(r, 3);
60 BOOST_CHECK_EQUAL(cr, 3);
61 buffer::const_pointer cp = p;
62 BOOST_CHECK_EQUAL(*cp, 3);
63 buffer::difference_type d = p - cp;
64 BOOST_CHECK_EQUAL(d, 0);
67 BOOST_AUTO_TEST_CASE( default_constructor_test )
70 BOOST_CHECK_EQUAL(b.size(), 0);
71 BOOST_CHECK_EQUAL(b.capacity(), b.size());
72 BOOST_CHECK(b.empty());
73 BOOST_CHECK_LT(b.size(), b.max_size());
74 BOOST_CHECK(b.begin() == b.end());
75 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
76 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
77 BOOST_CHECK(!b.c_array());
80 BOOST_AUTO_TEST_CASE( const_default_constructor_test )
83 BOOST_CHECK_EQUAL(b.size(), 0);
84 BOOST_CHECK_EQUAL(b.capacity(), b.size());
85 BOOST_CHECK(b.empty());
86 BOOST_CHECK_LT(b.size(), b.max_size());
87 BOOST_CHECK(b.begin() == b.end());
88 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
89 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
90 BOOST_CHECK(!b.c_array());
93 BOOST_AUTO_TEST_CASE( n_constructor_test )
96 BOOST_CHECK_EQUAL(b.size(), 100);
97 BOOST_CHECK_EQUAL(b.capacity(), b.size());
98 BOOST_CHECK(!b.empty());
99 BOOST_CHECK_LT(b.size(), b.max_size());
100 BOOST_CHECK(b.begin() != b.end());
101 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
102 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
103 b[0] = b[b.size()-1];
104 b.at(1) = b.at(b.size()-2);
105 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
106 BOOST_CHECK(b.c_array());
109 BOOST_AUTO_TEST_CASE( const_n_constructor_test )
112 BOOST_CHECK_EQUAL(b.size(), 100);
113 BOOST_CHECK_EQUAL(b.capacity(), b.size());
114 BOOST_CHECK(!b.empty());
115 BOOST_CHECK_LT(b.size(), b.max_size());
116 BOOST_CHECK(b.begin() != b.end());
117 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
118 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
119 buffer::value_type x = b[50];
121 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
122 BOOST_CHECK(b.c_array());
123 for (buffer::const_iterator i = b.begin(); i != b.end(); ++i);
126 BOOST_AUTO_TEST_CASE( default_n_copy_constructor_test )
129 BOOST_CHECK_EQUAL(b.size(), 100);
130 BOOST_CHECK_EQUAL(b.capacity(), b.size());
131 BOOST_CHECK(!b.empty());
132 BOOST_CHECK_LT(b.size(), b.max_size());
133 BOOST_CHECK(b.begin() != b.end());
134 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
135 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
136 buffer::value_type x = b[50];
138 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
139 BOOST_CHECK(b.c_array());
140 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
141 BOOST_CHECK_EQUAL(*i, 0x5f);
142 b[b.size()-1] = 0xf5;
143 b[0] = b[b.size()-1];
144 BOOST_CHECK_EQUAL(b[b.size()-1], 0xf5);
145 BOOST_CHECK_EQUAL(b.back(), 0xf5);
146 BOOST_CHECK_EQUAL(*(b.end()-1), 0xf5);
147 BOOST_CHECK_EQUAL(b[0], 0xf5);
148 BOOST_CHECK_EQUAL(b.front(), 0xf5);
149 BOOST_CHECK_EQUAL(*b.begin(), 0xf5);
150 b.at(b.size()-2) = 0xff;
151 b.at(1) = b.at(b.size()-2);
152 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
153 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
154 BOOST_CHECK_EQUAL(b[1], 0xff);
155 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
158 BOOST_AUTO_TEST_CASE( copy_constructor_test )
164 BOOST_CHECK_EQUAL(b.size(), bb.size());
165 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
166 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
167 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
168 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
171 BOOST_AUTO_TEST_CASE( iterator_constructor_test )
173 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
175 BOOST_CHECK_EQUAL(b.size(), 5);
176 BOOST_CHECK_EQUAL(b.capacity(), b.size());
177 BOOST_CHECK(!b.empty());
178 BOOST_CHECK_LT(b.size(), b.max_size());
179 BOOST_CHECK(b.begin() != b.end());
180 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
181 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
182 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
183 BOOST_CHECK(b.c_array());
184 for (int i = 0; i != 5; ++i)
185 BOOST_CHECK_EQUAL(b.at(i), i + 5);
188 BOOST_AUTO_TEST_CASE( const_iterator_constructor_test )
190 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
191 const buffer b(a, a + 5);
192 BOOST_CHECK_EQUAL(b.size(), 5);
193 BOOST_CHECK_EQUAL(b.capacity(), b.size());
194 BOOST_CHECK(!b.empty());
195 BOOST_CHECK_LT(b.size(), b.max_size());
196 BOOST_CHECK(b.begin() != b.end());
197 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
198 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
199 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
200 BOOST_CHECK(b.c_array());
201 for (int i = 0; i != 5; ++i)
202 BOOST_CHECK_EQUAL(b.at(i), i + 5);
205 BOOST_AUTO_TEST_CASE( operator_equals_test )
207 buffer::value_type a[] = { 56, 123 };
208 const buffer b(a, a + 2);
211 BOOST_CHECK_EQUAL(b.size(), bb.size());
212 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
213 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
214 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
215 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
216 buffer::pointer p = bb.c_array();
218 BOOST_CHECK_EQUAL(bb.c_array(), p);
219 BOOST_CHECK_EQUAL(b.size(), 2);
222 BOOST_AUTO_TEST_CASE( iterators_test )
224 buffer::value_type a[] = { 56, 123, 253 };
226 BOOST_CHECK_NE(b.begin(), b.end());
227 BOOST_CHECK_LT(b.begin(), b.end());
228 buffer::iterator i = b.begin();
229 BOOST_CHECK_EQUAL(*i, 56);
231 BOOST_CHECK_EQUAL(*i, 123);
233 BOOST_CHECK_EQUAL(*i, 253);
235 BOOST_CHECK_EQUAL(i, b.end());
237 BOOST_CHECK_EQUAL(*i, 253);
239 BOOST_CHECK_EQUAL(*i, 123);
241 BOOST_CHECK_EQUAL(*i, 56);
243 BOOST_CHECK_EQUAL(*(i + 1), 253);
244 BOOST_CHECK_EQUAL(*(i - 1), 56);
245 BOOST_CHECK_EQUAL(i - 1, b.begin());
246 BOOST_CHECK_EQUAL(i + 2, b.end());
247 BOOST_CHECK_EQUAL(i - b.begin(), 1);
249 BOOST_CHECK_EQUAL(*i, 77);
250 BOOST_CHECK_EQUAL(*(b.begin() + 1), 77);
253 BOOST_AUTO_TEST_CASE( reverse_iterators_test )
255 buffer::value_type a[] = { 56, 123, 253 };
257 BOOST_CHECK_NE(b.rbegin(), b.rend());
258 BOOST_CHECK_LT(b.rbegin(), b.rend());
259 buffer::reverse_iterator i = b.rbegin();
260 BOOST_CHECK_EQUAL(*i, 253);
262 BOOST_CHECK_EQUAL(*i, 123);
264 BOOST_CHECK_EQUAL(*i, 56);
266 BOOST_CHECK_EQUAL(i, b.rend());
268 BOOST_CHECK_EQUAL(*i, 56);
270 BOOST_CHECK_EQUAL(*i, 123);
272 BOOST_CHECK_EQUAL(*i, 253);
274 BOOST_CHECK_EQUAL(*(i + 1), 56);
275 BOOST_CHECK_EQUAL(*(i - 1), 253);
276 BOOST_CHECK_EQUAL(i - 1, b.rbegin());
277 BOOST_CHECK_EQUAL(i + 2, b.rend());
278 BOOST_CHECK_EQUAL(i - b.rbegin(), 1);
280 BOOST_CHECK_EQUAL(*i, 77);
281 BOOST_CHECK_EQUAL(*(b.rbegin() + 1), 77);
284 BOOST_AUTO_TEST_CASE( resize_test )
287 BOOST_CHECK(!b.c_array());
288 BOOST_CHECK(b.empty());
289 BOOST_CHECK_EQUAL(b.size(), 0);
290 BOOST_CHECK_EQUAL(b.capacity(), b.size());
292 BOOST_CHECK(b.c_array());
293 BOOST_CHECK(!b.empty());
294 BOOST_CHECK_EQUAL(b.size(), 10);
295 BOOST_CHECK_EQUAL(b.capacity(), b.size());
297 BOOST_CHECK_EQUAL(b[9], 1);
298 BOOST_CHECK_EQUAL(b.at(9), 1);
299 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
301 BOOST_CHECK(b.c_array());
302 BOOST_CHECK(!b.empty());
303 BOOST_CHECK_EQUAL(b.size(), 15);
304 BOOST_CHECK_EQUAL(b.capacity(), b.size());
305 BOOST_CHECK_EQUAL(b[9], 1);
306 BOOST_CHECK_EQUAL(b.at(9), 1);
307 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
308 for (buffer::const_iterator i = b.begin()+10; i != b.end(); ++i)
309 BOOST_CHECK_EQUAL(*i, 0x77);
311 BOOST_CHECK(!b.c_array());
312 BOOST_CHECK(b.empty());
313 BOOST_CHECK_EQUAL(b.size(), 0);
314 BOOST_CHECK_EQUAL(b.capacity(), b.size());
317 BOOST_AUTO_TEST_CASE( reserve_test )
321 BOOST_CHECK(b.c_array());
322 BOOST_CHECK(!b.empty());
323 BOOST_CHECK_EQUAL(b.size(), 10);
324 BOOST_CHECK_EQUAL(b.capacity(), b.size());
326 BOOST_CHECK_EQUAL(b[9], 1);
327 BOOST_CHECK_EQUAL(b.at(9), 1);
328 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
330 BOOST_CHECK(b.c_array());
331 BOOST_CHECK(!b.empty());
332 BOOST_CHECK_EQUAL(b.size(), 10);
333 BOOST_CHECK_EQUAL(b.capacity(), b.size());
334 BOOST_CHECK_EQUAL(b[9], 1);
336 BOOST_CHECK(b.c_array());
337 BOOST_CHECK(!b.empty());
338 BOOST_CHECK_EQUAL(b.size(), 20);
339 BOOST_CHECK_EQUAL(b.capacity(), b.size());
340 BOOST_CHECK_EQUAL(b[9], 1);
342 BOOST_CHECK(b.c_array());
343 BOOST_CHECK(!b.empty());
344 BOOST_CHECK_EQUAL(b.size(), 20);
345 BOOST_CHECK_EQUAL(b.capacity(), b.size());
346 BOOST_CHECK_EQUAL(b[9], 1);
349 BOOST_AUTO_TEST_CASE( assign_iterator_test )
351 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
353 BOOST_CHECK(!b.c_array());
354 BOOST_CHECK(b.empty());
355 BOOST_CHECK_EQUAL(b.size(), 0);
356 BOOST_CHECK_EQUAL(b.capacity(), b.size());
358 BOOST_CHECK_EQUAL(b.size(), 5);
359 BOOST_CHECK_EQUAL(b.capacity(), b.size());
360 BOOST_CHECK(!b.empty());
361 BOOST_CHECK_LT(b.size(), b.max_size());
362 BOOST_CHECK(b.begin() != b.end());
363 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
364 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
365 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
366 BOOST_CHECK(b.c_array());
367 for (int i = 0; i != 5; ++i)
368 BOOST_CHECK_EQUAL(b.at(i), i + 5);
371 BOOST_AUTO_TEST_CASE( assign_n_copy_test )
375 BOOST_CHECK_EQUAL(b.size(), 5);
376 BOOST_CHECK_EQUAL(b.capacity(), b.size());
377 BOOST_CHECK(!b.empty());
378 BOOST_CHECK_LT(b.size(), b.max_size());
379 BOOST_CHECK(b.begin() != b.end());
380 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
381 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
382 buffer::value_type x = b[2];
384 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
385 BOOST_CHECK(b.c_array());
386 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
387 BOOST_CHECK_EQUAL(*i, 0x33);
388 b[b.size()-1] = 0x66;
389 b[0] = b[b.size()-1];
390 BOOST_CHECK_EQUAL(b[b.size()-1], 0x66);
391 BOOST_CHECK_EQUAL(b.back(), 0x66);
392 BOOST_CHECK_EQUAL(*(b.end()-1), 0x66);
393 BOOST_CHECK_EQUAL(b[0], 0x66);
394 BOOST_CHECK_EQUAL(b.front(), 0x66);
395 BOOST_CHECK_EQUAL(*b.begin(), 0x66);
396 b.at(b.size()-2) = 0xff;
397 b.at(1) = b.at(b.size()-2);
398 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
399 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
400 BOOST_CHECK_EQUAL(b[1], 0xff);
401 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
404 BOOST_AUTO_TEST_CASE( clear_test )
408 BOOST_CHECK(!b.c_array());
409 BOOST_CHECK(b.empty());
410 BOOST_CHECK_EQUAL(b.size(), 0);
411 BOOST_CHECK_EQUAL(b.capacity(), b.size());
414 BOOST_AUTO_TEST_CASE( operators_test )
416 const buffer b1(5, 78);
417 const buffer b2(5, 78);
420 const buffer b3 = bb;
422 const buffer b4 = bb;
423 const buffer b5(4, 78);
426 const buffer b7 = bb;
427 BOOST_CHECK_EQUAL(b1, b1);
428 BOOST_CHECK_EQUAL(b1, b2);
429 BOOST_CHECK_LE(b1, b2);
430 BOOST_CHECK_GE(b1, b2);
431 BOOST_CHECK_NE(b1, b3);
432 BOOST_CHECK_NE(b1, b4);
433 BOOST_CHECK_NE(b1, b5);
434 BOOST_CHECK_NE(b1, b6);
435 BOOST_CHECK_GT(b1, b4);
436 BOOST_CHECK_GE(b1, b4);
437 BOOST_CHECK_LT(b1, b3);
438 BOOST_CHECK_LE(b1, b3);
439 BOOST_CHECK_GT(b1, b6);
440 BOOST_CHECK_LT(b6, b2);
441 BOOST_CHECK_GE(b3, b6);
442 BOOST_CHECK_LE(b6, b4);
443 BOOST_CHECK_EQUAL(b6, b6);
444 BOOST_CHECK_EQUAL(b6, b7);
445 BOOST_CHECK_GE(b6, b7);
446 BOOST_CHECK_LE(b6, b7);
449 BOOST_AUTO_TEST_CASE( swap_test )
451 buffer::value_type a1[5] = { 5, 6, 7, 8, 9 };
452 buffer::value_type a2[3] = { 55, 77, 99 };
453 const buffer b1c(a1, a1 + 5);
454 const buffer b2c(a2, a2 + 3);
458 BOOST_CHECK_EQUAL(b1, b2c);
459 BOOST_CHECK_EQUAL(b2, b1c);
460 posixx::swap(b2, b1);
461 BOOST_CHECK_EQUAL(b1, b1c);
462 BOOST_CHECK_EQUAL(b2, b2c);
465 BOOST_AUTO_TEST_SUITE_END()