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 )
26 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::reverse_iterator )
27 BOOST_TEST_DONT_PRINT_LOG_VALUE( buffer::const_reverse_iterator )
29 // avoid compilation warning concerning unused variables
30 void dummy(buffer::const_iterator i) {}
32 BOOST_AUTO_TEST_SUITE( buffer_suite )
34 BOOST_AUTO_TEST_CASE( types_test )
36 buffer::value_type v = 1;
37 buffer::reference r = v;
38 BOOST_CHECK_EQUAL(r, 1);
40 BOOST_CHECK_EQUAL(v, 2);
41 buffer::const_reference cr = v;
42 BOOST_CHECK_EQUAL(cr, 2);
44 buffer::const_iterator ci = i;
46 buffer::size_type s = 5;
48 buffer::pointer p = &v;
49 BOOST_CHECK_EQUAL(*p, 2);
51 BOOST_CHECK_EQUAL(v, 3);
52 BOOST_CHECK_EQUAL(r, 3);
53 BOOST_CHECK_EQUAL(cr, 3);
54 buffer::const_pointer cp = p;
55 BOOST_CHECK_EQUAL(*cp, 3);
56 buffer::difference_type d = p - cp;
57 BOOST_CHECK_EQUAL(d, 0);
60 BOOST_AUTO_TEST_CASE( default_constructor_test )
63 BOOST_CHECK_EQUAL(b.size(), 0);
64 BOOST_CHECK_EQUAL(b.capacity(), b.size());
65 BOOST_CHECK(b.empty());
66 BOOST_CHECK_LT(b.size(), b.max_size());
67 BOOST_CHECK(b.begin() == b.end());
68 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
69 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
70 BOOST_CHECK(!b.c_array());
73 BOOST_AUTO_TEST_CASE( const_default_constructor_test )
76 BOOST_CHECK_EQUAL(b.size(), 0);
77 BOOST_CHECK_EQUAL(b.capacity(), b.size());
78 BOOST_CHECK(b.empty());
79 BOOST_CHECK_LT(b.size(), b.max_size());
80 BOOST_CHECK(b.begin() == b.end());
81 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
82 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
83 BOOST_CHECK(!b.c_array());
86 BOOST_AUTO_TEST_CASE( n_constructor_test )
89 BOOST_CHECK_EQUAL(b.size(), 100);
90 BOOST_CHECK_EQUAL(b.capacity(), b.size());
91 BOOST_CHECK(!b.empty());
92 BOOST_CHECK_LT(b.size(), b.max_size());
93 BOOST_CHECK(b.begin() != b.end());
94 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
95 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
97 b.at(1) = b.at(b.size()-2);
98 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
99 BOOST_CHECK(b.c_array());
102 BOOST_AUTO_TEST_CASE( const_n_constructor_test )
105 BOOST_CHECK_EQUAL(b.size(), 100);
106 BOOST_CHECK_EQUAL(b.capacity(), b.size());
107 BOOST_CHECK(!b.empty());
108 BOOST_CHECK_LT(b.size(), b.max_size());
109 BOOST_CHECK(b.begin() != b.end());
110 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
111 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
112 buffer::value_type x = b[50];
114 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
115 BOOST_CHECK(b.c_array());
116 for (buffer::const_iterator i = b.begin(); i != b.end(); ++i);
119 BOOST_AUTO_TEST_CASE( default_n_copy_constructor_test )
122 BOOST_CHECK_EQUAL(b.size(), 100);
123 BOOST_CHECK_EQUAL(b.capacity(), b.size());
124 BOOST_CHECK(!b.empty());
125 BOOST_CHECK_LT(b.size(), b.max_size());
126 BOOST_CHECK(b.begin() != b.end());
127 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
128 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
129 buffer::value_type x = b[50];
131 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
132 BOOST_CHECK(b.c_array());
133 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
134 BOOST_CHECK_EQUAL(*i, 0x5f);
135 b[b.size()-1] = 0xf5;
136 b[0] = b[b.size()-1];
137 BOOST_CHECK_EQUAL(b[b.size()-1], 0xf5);
138 BOOST_CHECK_EQUAL(b.back(), 0xf5);
139 BOOST_CHECK_EQUAL(*(b.end()-1), 0xf5);
140 BOOST_CHECK_EQUAL(b[0], 0xf5);
141 BOOST_CHECK_EQUAL(b.front(), 0xf5);
142 BOOST_CHECK_EQUAL(*b.begin(), 0xf5);
143 b.at(b.size()-2) = 0xff;
144 b.at(1) = b.at(b.size()-2);
145 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
146 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
147 BOOST_CHECK_EQUAL(b[1], 0xff);
148 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
151 BOOST_AUTO_TEST_CASE( copy_constructor_test )
157 BOOST_CHECK_EQUAL(b.size(), bb.size());
158 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
159 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
160 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
161 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
164 BOOST_AUTO_TEST_CASE( iterator_constructor_test )
166 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
168 BOOST_CHECK_EQUAL(b.size(), 5);
169 BOOST_CHECK_EQUAL(b.capacity(), b.size());
170 BOOST_CHECK(!b.empty());
171 BOOST_CHECK_LT(b.size(), b.max_size());
172 BOOST_CHECK(b.begin() != b.end());
173 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
174 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
175 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
176 BOOST_CHECK(b.c_array());
177 for (int i = 0; i != 5; ++i)
178 BOOST_CHECK_EQUAL(b.at(i), i + 5);
181 BOOST_AUTO_TEST_CASE( const_iterator_constructor_test )
183 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
184 const buffer b(a, a + 5);
185 BOOST_CHECK_EQUAL(b.size(), 5);
186 BOOST_CHECK_EQUAL(b.capacity(), b.size());
187 BOOST_CHECK(!b.empty());
188 BOOST_CHECK_LT(b.size(), b.max_size());
189 BOOST_CHECK(b.begin() != b.end());
190 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
191 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
192 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
193 BOOST_CHECK(b.c_array());
194 for (int i = 0; i != 5; ++i)
195 BOOST_CHECK_EQUAL(b.at(i), i + 5);
198 BOOST_AUTO_TEST_CASE( operator_equals_test )
200 buffer::value_type a[] = { 56, 123 };
201 const buffer b(a, a + 2);
204 BOOST_CHECK_EQUAL(b.size(), bb.size());
205 BOOST_CHECK_EQUAL(b.capacity(), bb.capacity());
206 BOOST_CHECK_EQUAL(b.empty(), bb.empty());
207 BOOST_CHECK_EQUAL(b.at(0), bb.at(0));
208 BOOST_CHECK_EQUAL(b.at(1), bb.at(1));
209 buffer::pointer p = bb.c_array();
211 BOOST_CHECK_EQUAL(bb.c_array(), p);
212 BOOST_CHECK_EQUAL(b.size(), 2);
215 BOOST_AUTO_TEST_CASE( iterators_test )
217 buffer::value_type a[] = { 56, 123, 253 };
219 BOOST_CHECK_NE(b.begin(), b.end());
220 BOOST_CHECK_LT(b.begin(), b.end());
221 buffer::iterator i = b.begin();
222 BOOST_CHECK_EQUAL(*i, 56);
224 BOOST_CHECK_EQUAL(*i, 123);
226 BOOST_CHECK_EQUAL(*i, 253);
228 BOOST_CHECK_EQUAL(i, b.end());
230 BOOST_CHECK_EQUAL(*i, 253);
232 BOOST_CHECK_EQUAL(*i, 123);
234 BOOST_CHECK_EQUAL(*i, 56);
236 BOOST_CHECK_EQUAL(*(i + 1), 253);
237 BOOST_CHECK_EQUAL(*(i - 1), 56);
238 BOOST_CHECK_EQUAL(i - 1, b.begin());
239 BOOST_CHECK_EQUAL(i + 2, b.end());
240 BOOST_CHECK_EQUAL(i - b.begin(), 1);
242 BOOST_CHECK_EQUAL(*i, 77);
243 BOOST_CHECK_EQUAL(*(b.begin() + 1), 77);
246 BOOST_AUTO_TEST_CASE( reverse_iterators_test )
248 buffer::value_type a[] = { 56, 123, 253 };
250 BOOST_CHECK_NE(b.rbegin(), b.rend());
251 BOOST_CHECK_LT(b.rbegin(), b.rend());
252 buffer::reverse_iterator i = b.rbegin();
253 BOOST_CHECK_EQUAL(*i, 253);
255 BOOST_CHECK_EQUAL(*i, 123);
257 BOOST_CHECK_EQUAL(*i, 56);
259 BOOST_CHECK_EQUAL(i, b.rend());
261 BOOST_CHECK_EQUAL(*i, 56);
263 BOOST_CHECK_EQUAL(*i, 123);
265 BOOST_CHECK_EQUAL(*i, 253);
267 BOOST_CHECK_EQUAL(*(i + 1), 56);
268 BOOST_CHECK_EQUAL(*(i - 1), 253);
269 BOOST_CHECK_EQUAL(i - 1, b.rbegin());
270 BOOST_CHECK_EQUAL(i + 2, b.rend());
271 BOOST_CHECK_EQUAL(i - b.rbegin(), 1);
273 BOOST_CHECK_EQUAL(*i, 77);
274 BOOST_CHECK_EQUAL(*(b.rbegin() + 1), 77);
277 BOOST_AUTO_TEST_CASE( resize_test )
280 BOOST_CHECK(!b.c_array());
281 BOOST_CHECK(b.empty());
282 BOOST_CHECK_EQUAL(b.size(), 0);
283 BOOST_CHECK_EQUAL(b.capacity(), b.size());
285 BOOST_CHECK(b.c_array());
286 BOOST_CHECK(!b.empty());
287 BOOST_CHECK_EQUAL(b.size(), 10);
288 BOOST_CHECK_EQUAL(b.capacity(), b.size());
290 BOOST_CHECK_EQUAL(b[9], 1);
291 BOOST_CHECK_EQUAL(b.at(9), 1);
292 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
294 BOOST_CHECK(b.c_array());
295 BOOST_CHECK(!b.empty());
296 BOOST_CHECK_EQUAL(b.size(), 15);
297 BOOST_CHECK_EQUAL(b.capacity(), b.size());
298 BOOST_CHECK_EQUAL(b[9], 1);
299 BOOST_CHECK_EQUAL(b.at(9), 1);
300 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
301 for (buffer::const_iterator i = b.begin()+10; i != b.end(); ++i)
302 BOOST_CHECK_EQUAL(*i, 0x77);
304 BOOST_CHECK(!b.c_array());
305 BOOST_CHECK(b.empty());
306 BOOST_CHECK_EQUAL(b.size(), 0);
307 BOOST_CHECK_EQUAL(b.capacity(), b.size());
310 BOOST_AUTO_TEST_CASE( reserve_test )
314 BOOST_CHECK(b.c_array());
315 BOOST_CHECK(!b.empty());
316 BOOST_CHECK_EQUAL(b.size(), 10);
317 BOOST_CHECK_EQUAL(b.capacity(), b.size());
319 BOOST_CHECK_EQUAL(b[9], 1);
320 BOOST_CHECK_EQUAL(b.at(9), 1);
321 BOOST_CHECK_EQUAL(b.c_array()[9], 1);
323 BOOST_CHECK(b.c_array());
324 BOOST_CHECK(!b.empty());
325 BOOST_CHECK_EQUAL(b.size(), 10);
326 BOOST_CHECK_EQUAL(b.capacity(), b.size());
327 BOOST_CHECK_EQUAL(b[9], 1);
329 BOOST_CHECK(b.c_array());
330 BOOST_CHECK(!b.empty());
331 BOOST_CHECK_EQUAL(b.size(), 20);
332 BOOST_CHECK_EQUAL(b.capacity(), b.size());
333 BOOST_CHECK_EQUAL(b[9], 1);
335 BOOST_CHECK(b.c_array());
336 BOOST_CHECK(!b.empty());
337 BOOST_CHECK_EQUAL(b.size(), 20);
338 BOOST_CHECK_EQUAL(b.capacity(), b.size());
339 BOOST_CHECK_EQUAL(b[9], 1);
342 BOOST_AUTO_TEST_CASE( assign_iterator_test )
344 buffer::value_type a[5] = { 5, 6, 7, 8, 9 };
346 BOOST_CHECK(!b.c_array());
347 BOOST_CHECK(b.empty());
348 BOOST_CHECK_EQUAL(b.size(), 0);
349 BOOST_CHECK_EQUAL(b.capacity(), b.size());
351 BOOST_CHECK_EQUAL(b.size(), 5);
352 BOOST_CHECK_EQUAL(b.capacity(), b.size());
353 BOOST_CHECK(!b.empty());
354 BOOST_CHECK_LT(b.size(), b.max_size());
355 BOOST_CHECK(b.begin() != b.end());
356 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
357 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
358 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
359 BOOST_CHECK(b.c_array());
360 for (int i = 0; i != 5; ++i)
361 BOOST_CHECK_EQUAL(b.at(i), i + 5);
364 BOOST_AUTO_TEST_CASE( assign_n_copy_test )
368 BOOST_CHECK_EQUAL(b.size(), 5);
369 BOOST_CHECK_EQUAL(b.capacity(), b.size());
370 BOOST_CHECK(!b.empty());
371 BOOST_CHECK_LT(b.size(), b.max_size());
372 BOOST_CHECK(b.begin() != b.end());
373 BOOST_CHECK_EQUAL(b.end() - b.begin(), b.size());
374 //BOOST_CHECK_EQUAL(b.rbegin(), b.rend());
375 buffer::value_type x = b[2];
377 BOOST_CHECK_THROW(b.at(b.size()), std::out_of_range);
378 BOOST_CHECK(b.c_array());
379 for (buffer::iterator i = b.begin(); i != b.end(); ++i)
380 BOOST_CHECK_EQUAL(*i, 0x33);
381 b[b.size()-1] = 0x66;
382 b[0] = b[b.size()-1];
383 BOOST_CHECK_EQUAL(b[b.size()-1], 0x66);
384 BOOST_CHECK_EQUAL(b.back(), 0x66);
385 BOOST_CHECK_EQUAL(*(b.end()-1), 0x66);
386 BOOST_CHECK_EQUAL(b[0], 0x66);
387 BOOST_CHECK_EQUAL(b.front(), 0x66);
388 BOOST_CHECK_EQUAL(*b.begin(), 0x66);
389 b.at(b.size()-2) = 0xff;
390 b.at(1) = b.at(b.size()-2);
391 BOOST_CHECK_EQUAL(b[b.size()-2], 0xff);
392 BOOST_CHECK_EQUAL(*(b.end()-2), 0xff);
393 BOOST_CHECK_EQUAL(b[1], 0xff);
394 BOOST_CHECK_EQUAL(*(b.begin()+1), 0xff);
397 BOOST_AUTO_TEST_CASE( clear_test )
401 BOOST_CHECK(!b.c_array());
402 BOOST_CHECK(b.empty());
403 BOOST_CHECK_EQUAL(b.size(), 0);
404 BOOST_CHECK_EQUAL(b.capacity(), b.size());
407 BOOST_AUTO_TEST_CASE( operators_test )
409 const buffer b1(5, 78);
410 const buffer b2(5, 78);
413 const buffer b3 = bb;
415 const buffer b4 = bb;
416 const buffer b5(4, 78);
419 const buffer b7 = bb;
420 BOOST_CHECK_EQUAL(b1, b1);
421 BOOST_CHECK_EQUAL(b1, b2);
422 BOOST_CHECK_LE(b1, b2);
423 BOOST_CHECK_GE(b1, b2);
424 BOOST_CHECK_NE(b1, b3);
425 BOOST_CHECK_NE(b1, b4);
426 BOOST_CHECK_NE(b1, b5);
427 BOOST_CHECK_NE(b1, b6);
428 BOOST_CHECK_GT(b1, b4);
429 BOOST_CHECK_GE(b1, b4);
430 BOOST_CHECK_LT(b1, b3);
431 BOOST_CHECK_LE(b1, b3);
432 BOOST_CHECK_GT(b1, b6);
433 BOOST_CHECK_LT(b6, b2);
434 BOOST_CHECK_GE(b3, b6);
435 BOOST_CHECK_LE(b6, b4);
436 BOOST_CHECK_EQUAL(b6, b6);
437 BOOST_CHECK_EQUAL(b6, b7);
438 BOOST_CHECK_GE(b6, b7);
439 BOOST_CHECK_LE(b6, b7);
442 BOOST_AUTO_TEST_CASE( swap_test )
444 buffer::value_type a1[5] = { 5, 6, 7, 8, 9 };
445 buffer::value_type a2[3] = { 55, 77, 99 };
446 const buffer b1c(a1, a1 + 5);
447 const buffer b2c(a2, a2 + 3);
451 BOOST_CHECK_EQUAL(b1, b2c);
452 BOOST_CHECK_EQUAL(b2, b1c);
453 posixx::swap(b2, b1);
454 BOOST_CHECK_EQUAL(b1, b1c);
455 BOOST_CHECK_EQUAL(b2, b2c);
458 BOOST_AUTO_TEST_SUITE_END()