• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // buffer.cpp
3 // ~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2021 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 // Disable autolinking for unit tests.
12 #if !defined(BOOST_ALL_NO_LIB)
13 #define BOOST_ALL_NO_LIB 1
14 #endif // !defined(BOOST_ALL_NO_LIB)
15 
16 // Test that header file is self-contained.
17 #include <boost/asio/buffer.hpp>
18 
19 #include "unit_test.hpp"
20 
21 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
22 # include <boost/array.hpp>
23 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
24 
25 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
26 # include <array>
27 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
28 
29 //------------------------------------------------------------------------------
30 
31 // buffer_compile test
32 // ~~~~~~~~~~~~~~~~~~~
33 // The following test checks that all overloads of the buffer function compile
34 // and link correctly. Runtime failures are ignored.
35 
36 namespace buffer_compile {
37 
38 using namespace boost::asio;
39 
test()40 void test()
41 {
42   try
43   {
44     char raw_data[1024];
45     const char const_raw_data[1024] = "";
46     void* void_ptr_data = raw_data;
47     const void* const_void_ptr_data = const_raw_data;
48 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
49     boost::array<char, 1024> array_data;
50     const boost::array<char, 1024>& const_array_data_1 = array_data;
51     boost::array<const char, 1024> const_array_data_2 = { { 0 } };
52 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
53 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
54     std::array<char, 1024> std_array_data;
55     const std::array<char, 1024>& const_std_array_data_1 = std_array_data;
56     std::array<const char, 1024> const_std_array_data_2 = { { 0 } };
57 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
58     std::vector<char> vector_data(1024);
59     const std::vector<char>& const_vector_data = vector_data;
60     std::string string_data(1024, ' ');
61     const std::string const_string_data(1024, ' ');
62     std::vector<mutable_buffer> mutable_buffer_sequence;
63     std::vector<const_buffer> const_buffer_sequence;
64 #if defined(BOOST_ASIO_HAS_STD_STRING_VIEW)
65     std::string_view string_view_data(string_data);
66 #elif defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
67     std::experimental::string_view string_view_data(string_data);
68 #endif // defined(BOOST_ASIO_HAS_STD_EXPERIMENTAL_STRING_VIEW)
69 
70     // mutable_buffer constructors.
71 
72     mutable_buffer mb1;
73     mutable_buffer mb2(void_ptr_data, 1024);
74     mutable_buffer mb3(mb1);
75     (void)mb3;
76 
77     // mutable_buffer functions.
78 
79     void* ptr1 = mb1.data();
80     (void)ptr1;
81 
82     std::size_t n1 = mb1.size();
83     (void)n1;
84 
85     // mutable_buffer operators.
86 
87     mb1 += 128;
88     mb1 = mb2 + 128;
89     mb1 = 128 + mb2;
90 
91 #if !defined(BOOST_ASIO_NO_DEPRECATED)
92 
93     // mutable_buffers_1 constructors.
94 
95     mutable_buffers_1 mbc1(mb1);
96     mutable_buffers_1 mbc2(mbc1);
97 
98     // mutable_buffers_1 functions.
99 
100     mutable_buffers_1::const_iterator iter1 = mbc1.begin();
101     (void)iter1;
102     mutable_buffers_1::const_iterator iter2 = mbc1.end();
103     (void)iter2;
104 
105 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
106 
107     // const_buffer constructors.
108 
109     const_buffer cb1;
110     const_buffer cb2(const_void_ptr_data, 1024);
111     const_buffer cb3(cb1);
112     (void)cb3;
113     const_buffer cb4(mb1);
114     (void)cb4;
115 
116     // const_buffer functions.
117 
118     const void* ptr2 = cb1.data();
119     (void)ptr2;
120 
121     std::size_t n2 = cb1.size();
122     (void)n2;
123 
124     // const_buffer operators.
125 
126     cb1 += 128;
127     cb1 = cb2 + 128;
128     cb1 = 128 + cb2;
129 
130 #if !defined(BOOST_ASIO_NO_DEPRECATED)
131 
132     // const_buffers_1 constructors.
133 
134     const_buffers_1 cbc1(cb1);
135     const_buffers_1 cbc2(cbc1);
136 
137     // const_buffers_1 functions.
138 
139     const_buffers_1::const_iterator iter3 = cbc1.begin();
140     (void)iter3;
141     const_buffers_1::const_iterator iter4 = cbc1.end();
142     (void)iter4;
143 
144 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
145 
146     // buffer_size function overloads.
147 
148     std::size_t size1 = buffer_size(mb1);
149     (void)size1;
150     std::size_t size2 = buffer_size(cb1);
151     (void)size2;
152 #if !defined(BOOST_ASIO_NO_DEPRECATED)
153     std::size_t size3 = buffer_size(mbc1);
154     (void)size3;
155     std::size_t size4 = buffer_size(cbc1);
156     (void)size4;
157 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
158     std::size_t size5 = buffer_size(mutable_buffer_sequence);
159     (void)size5;
160     std::size_t size6 = buffer_size(const_buffer_sequence);
161     (void)size6;
162 
163     // buffer_cast function overloads.
164 
165 #if !defined(BOOST_ASIO_NO_DEPRECATED)
166     void* ptr3 = buffer_cast<void*>(mb1);
167     (void)ptr3;
168     const void* ptr4 = buffer_cast<const void*>(cb1);
169     (void)ptr4;
170 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
171 
172     // buffer function overloads.
173 
174     mb1 = buffer(mb2);
175     mb1 = buffer(mb2, 128);
176     cb1 = buffer(cb2);
177     cb1 = buffer(cb2, 128);
178     mb1 = buffer(void_ptr_data, 1024);
179     cb1 = buffer(const_void_ptr_data, 1024);
180     mb1 = buffer(raw_data);
181     mb1 = buffer(raw_data, 1024);
182     cb1 = buffer(const_raw_data);
183     cb1 = buffer(const_raw_data, 1024);
184 #if defined(BOOST_ASIO_HAS_BOOST_ARRAY)
185     mb1 = buffer(array_data);
186     mb1 = buffer(array_data, 1024);
187     cb1 = buffer(const_array_data_1);
188     cb1 = buffer(const_array_data_1, 1024);
189     cb1 = buffer(const_array_data_2);
190     cb1 = buffer(const_array_data_2, 1024);
191 #endif // defined(BOOST_ASIO_HAS_BOOST_ARRAY)
192 #if defined(BOOST_ASIO_HAS_STD_ARRAY)
193     mb1 = buffer(std_array_data);
194     mb1 = buffer(std_array_data, 1024);
195     cb1 = buffer(const_std_array_data_1);
196     cb1 = buffer(const_std_array_data_1, 1024);
197     cb1 = buffer(const_std_array_data_2);
198     cb1 = buffer(const_std_array_data_2, 1024);
199 #endif // defined(BOOST_ASIO_HAS_STD_ARRAY)
200     mb1 = buffer(vector_data);
201     mb1 = buffer(vector_data, 1024);
202     cb1 = buffer(const_vector_data);
203     cb1 = buffer(const_vector_data, 1024);
204     mb1 = buffer(string_data);
205     mb1 = buffer(string_data, 1024);
206     cb1 = buffer(const_string_data);
207     cb1 = buffer(const_string_data, 1024);
208 #if defined(BOOST_ASIO_HAS_STRING_VIEW)
209     cb1 = buffer(string_view_data);
210     cb1 = buffer(string_view_data, 1024);
211 #endif // defined(BOOST_ASIO_HAS_STRING_VIEW)
212 
213     // buffer_copy function overloads.
214 
215     std::size_t size7 = buffer_copy(mb1, cb2);
216     (void)size7;
217 #if !defined(BOOST_ASIO_NO_DEPRECATED)
218     std::size_t size8 = buffer_copy(mb1, cbc2);
219     (void)size8;
220 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
221     std::size_t size9 = buffer_copy(mb1, mb2);
222     (void)size9;
223 #if !defined(BOOST_ASIO_NO_DEPRECATED)
224     std::size_t size10 = buffer_copy(mb1, mbc2);
225     (void)size10;
226 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
227     std::size_t size11 = buffer_copy(mb1, const_buffer_sequence);
228     (void)size11;
229 #if !defined(BOOST_ASIO_NO_DEPRECATED)
230     std::size_t size12 = buffer_copy(mbc1, cb2);
231     (void)size12;
232     std::size_t size13 = buffer_copy(mbc1, cbc2);
233     (void)size13;
234     std::size_t size14 = buffer_copy(mbc1, mb2);
235     (void)size14;
236     std::size_t size15 = buffer_copy(mbc1, mbc2);
237     (void)size15;
238     std::size_t size16 = buffer_copy(mbc1, const_buffer_sequence);
239     (void)size16;
240 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
241     std::size_t size17 = buffer_copy(mutable_buffer_sequence, cb2);
242     (void)size17;
243 #if !defined(BOOST_ASIO_NO_DEPRECATED)
244     std::size_t size18 = buffer_copy(mutable_buffer_sequence, cbc2);
245     (void)size18;
246 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
247     std::size_t size19 = buffer_copy(mutable_buffer_sequence, mb2);
248     (void)size19;
249 #if !defined(BOOST_ASIO_NO_DEPRECATED)
250     std::size_t size20 = buffer_copy(mutable_buffer_sequence, mbc2);
251     (void)size20;
252 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
253     std::size_t size21 = buffer_copy(
254         mutable_buffer_sequence, const_buffer_sequence);
255     (void)size21;
256     std::size_t size22 = buffer_copy(mb1, cb2, 128);
257     (void)size22;
258 #if !defined(BOOST_ASIO_NO_DEPRECATED)
259     std::size_t size23 = buffer_copy(mb1, cbc2, 128);
260     (void)size23;
261 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
262     std::size_t size24 = buffer_copy(mb1, mb2, 128);
263     (void)size24;
264 #if !defined(BOOST_ASIO_NO_DEPRECATED)
265     std::size_t size25 = buffer_copy(mb1, mbc2, 128);
266     (void)size25;
267 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
268     std::size_t size26 = buffer_copy(mb1, const_buffer_sequence, 128);
269     (void)size26;
270 #if !defined(BOOST_ASIO_NO_DEPRECATED)
271     std::size_t size27 = buffer_copy(mbc1, cb2, 128);
272     (void)size27;
273     std::size_t size28 = buffer_copy(mbc1, cbc2, 128);
274     (void)size28;
275     std::size_t size29 = buffer_copy(mbc1, mb2, 128);
276     (void)size29;
277     std::size_t size30 = buffer_copy(mbc1, mbc2, 128);
278     (void)size30;
279     std::size_t size31 = buffer_copy(mbc1, const_buffer_sequence, 128);
280     (void)size31;
281 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
282     std::size_t size32 = buffer_copy(mutable_buffer_sequence, cb2, 128);
283     (void)size32;
284 #if !defined(BOOST_ASIO_NO_DEPRECATED)
285     std::size_t size33 = buffer_copy(mutable_buffer_sequence, cbc2, 128);
286     (void)size33;
287 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
288     std::size_t size34 = buffer_copy(mutable_buffer_sequence, mb2, 128);
289     (void)size34;
290 #if !defined(BOOST_ASIO_NO_DEPRECATED)
291     std::size_t size35 = buffer_copy(mutable_buffer_sequence, mbc2, 128);
292     (void)size35;
293 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
294     std::size_t size36 = buffer_copy(
295         mutable_buffer_sequence, const_buffer_sequence, 128);
296     (void)size36;
297 
298     // dynamic_buffer function overloads.
299 
300     dynamic_string_buffer<char, std::string::traits_type,
301       std::string::allocator_type> db1 = dynamic_buffer(string_data);
302     (void)db1;
303     dynamic_string_buffer<char, std::string::traits_type,
304       std::string::allocator_type> db2 = dynamic_buffer(string_data, 1024);
305     (void)db2;
306     dynamic_vector_buffer<char, std::allocator<char> >
307       db3 = dynamic_buffer(vector_data);
308     (void)db3;
309     dynamic_vector_buffer<char, std::allocator<char> >
310       db4 = dynamic_buffer(vector_data, 1024);
311     (void)db4;
312 
313     // dynamic_buffer member functions.
314 
315     std::size_t size37 = db1.size();
316     (void)size37;
317     std::size_t size38 = db3.size();
318     (void)size38;
319 
320     std::size_t size39 = db1.max_size();
321     (void)size39;
322     std::size_t size40 = db3.max_size();
323     (void)size40;
324 
325 #if !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
326     dynamic_string_buffer<char, std::string::traits_type,
327       std::string::allocator_type>::const_buffers_type
328         cb5 = db1.data();
329     (void)cb5;
330     dynamic_vector_buffer<char, std::allocator<char> >::const_buffers_type
331       cb6 = db3.data();
332     (void)cb6;
333 
334     dynamic_string_buffer<char, std::string::traits_type,
335       std::string::allocator_type>::mutable_buffers_type mb5
336         = db1.prepare(1024);
337     (void)mb5;
338     dynamic_vector_buffer<char, std::allocator<char> >::mutable_buffers_type
339       mb6 = db3.prepare(1024);
340     (void)mb6;
341 
342     db1.commit(1024);
343     db3.commit(1024);
344 #endif // !defined(BOOST_ASIO_NO_DYNAMIC_BUFFER_V1)
345 
346     dynamic_string_buffer<char, std::string::traits_type,
347       std::string::allocator_type>::mutable_buffers_type
348         mb7 = db1.data(0, 1);
349     (void)mb7;
350     dynamic_vector_buffer<char, std::allocator<char> >::mutable_buffers_type
351       mb8 = db3.data(0, 1);
352     (void)mb8;
353 
354     dynamic_string_buffer<char, std::string::traits_type,
355       std::string::allocator_type>::const_buffers_type
356         cb7 = static_cast<const dynamic_string_buffer<char,
357           std::string::traits_type,
358             std::string::allocator_type>&>(db1).data(0, 1);
359     (void)cb7;
360     dynamic_vector_buffer<char, std::allocator<char> >::const_buffers_type
361       cb8 = static_cast<const dynamic_vector_buffer<char,
362         std::allocator<char> >&>(db3).data(0, 1);
363     (void)cb8;
364 
365     db1.grow(1024);
366     db3.grow(1024);
367 
368     db1.shrink(1024);
369     db3.shrink(1024);
370 
371     db1.consume(0);
372     db3.consume(0);
373   }
374   catch (std::exception&)
375   {
376   }
377 }
378 
379 } // namespace buffer_compile
380 
381 //------------------------------------------------------------------------------
382 
383 namespace buffer_copy_runtime {
384 
385 using namespace boost::asio;
386 using namespace std;
387 
test()388 void test()
389 {
390   char dest_data[256];
391   char source_data[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
392 
393   memset(dest_data, 0, sizeof(dest_data));
394   mutable_buffer mb1 = boost::asio::buffer(dest_data);
395   mutable_buffer mb2 = boost::asio::buffer(source_data);
396   std::size_t n = buffer_copy(mb1, mb2);
397   BOOST_ASIO_CHECK(n == sizeof(source_data));
398   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
399 
400   memset(dest_data, 0, sizeof(dest_data));
401   mb1 = boost::asio::buffer(dest_data);
402   const_buffer cb1 = boost::asio::buffer(source_data);
403   n = buffer_copy(mb1, cb1);
404   BOOST_ASIO_CHECK(n == sizeof(source_data));
405   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
406 
407 #if !defined(BOOST_ASIO_NO_DEPRECATED)
408   memset(dest_data, 0, sizeof(dest_data));
409   mb1 = boost::asio::buffer(dest_data);
410   mutable_buffers_1 mbc1 = boost::asio::buffer(source_data);
411   n = buffer_copy(mb1, mbc1);
412   BOOST_ASIO_CHECK(n == sizeof(source_data));
413   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
414 
415   memset(dest_data, 0, sizeof(dest_data));
416   mb1 = boost::asio::buffer(dest_data);
417   const_buffers_1 cbc1 = const_buffers_1(boost::asio::buffer(source_data));
418   n = buffer_copy(mb1, cbc1);
419   BOOST_ASIO_CHECK(n == sizeof(source_data));
420   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
421 
422   memset(dest_data, 0, sizeof(dest_data));
423   mbc1 = boost::asio::buffer(dest_data);
424   mb1 = boost::asio::buffer(source_data);
425   n = buffer_copy(mbc1, mb1);
426   BOOST_ASIO_CHECK(n == sizeof(source_data));
427   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
428 
429   memset(dest_data, 0, sizeof(dest_data));
430   mbc1 = boost::asio::buffer(dest_data);
431   cb1 = boost::asio::buffer(source_data);
432   n = buffer_copy(mbc1, cb1);
433   BOOST_ASIO_CHECK(n == sizeof(source_data));
434   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
435 
436   memset(dest_data, 0, sizeof(dest_data));
437   mbc1 = boost::asio::buffer(dest_data);
438   mutable_buffers_1 mbc2 = boost::asio::buffer(source_data);
439   n = buffer_copy(mbc1, mbc2);
440   BOOST_ASIO_CHECK(n == sizeof(source_data));
441   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
442 
443   memset(dest_data, 0, sizeof(dest_data));
444   mbc1 = boost::asio::buffer(dest_data);
445   cbc1 = const_buffers_1(boost::asio::buffer(source_data));
446   n = buffer_copy(mbc1, cbc1);
447   BOOST_ASIO_CHECK(n == sizeof(source_data));
448   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
449 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
450 
451   memset(dest_data, 0, sizeof(dest_data));
452   mb1 = boost::asio::buffer(dest_data);
453   std::vector<mutable_buffer> mv1;
454   mv1.push_back(boost::asio::buffer(source_data, 5));
455   mv1.push_back(boost::asio::buffer(source_data) + 5);
456   n = buffer_copy(mb1, mv1);
457   BOOST_ASIO_CHECK(n == sizeof(source_data));
458   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
459 
460   memset(dest_data, 0, sizeof(dest_data));
461   mb1 = boost::asio::buffer(dest_data);
462   std::vector<const_buffer> cv1;
463   cv1.push_back(boost::asio::buffer(source_data, 6));
464   cv1.push_back(boost::asio::buffer(source_data) + 6);
465   n = buffer_copy(mb1, cv1);
466   BOOST_ASIO_CHECK(n == sizeof(source_data));
467   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
468 
469   memset(dest_data, 0, sizeof(dest_data));
470   mv1.clear();
471   mv1.push_back(boost::asio::buffer(dest_data, 7));
472   mv1.push_back(boost::asio::buffer(dest_data) + 7);
473   cb1 = boost::asio::buffer(source_data);
474   n = buffer_copy(mv1, cb1);
475   BOOST_ASIO_CHECK(n == sizeof(source_data));
476   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
477 
478   memset(dest_data, 0, sizeof(dest_data));
479   mv1.clear();
480   mv1.push_back(boost::asio::buffer(dest_data, 7));
481   mv1.push_back(boost::asio::buffer(dest_data) + 7);
482   cv1.clear();
483   cv1.push_back(boost::asio::buffer(source_data, 8));
484   cv1.push_back(boost::asio::buffer(source_data) + 8);
485   n = buffer_copy(mv1, cv1);
486   BOOST_ASIO_CHECK(n == sizeof(source_data));
487   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
488 
489   memset(dest_data, 0, sizeof(dest_data));
490   mb1 = boost::asio::buffer(dest_data);
491   mb2 = boost::asio::buffer(source_data);
492   n = buffer_copy(mb1, mb2, 10);
493   BOOST_ASIO_CHECK(n == 10);
494   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
495 
496   memset(dest_data, 0, sizeof(dest_data));
497   mb1 = boost::asio::buffer(dest_data);
498   cb1 = boost::asio::buffer(source_data);
499   n = buffer_copy(mb1, cb1, 10);
500   BOOST_ASIO_CHECK(n == 10);
501   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
502 
503 #if !defined(BOOST_ASIO_NO_DEPRECATED)
504   memset(dest_data, 0, sizeof(dest_data));
505   mb1 = boost::asio::buffer(dest_data);
506   mbc1 = boost::asio::buffer(source_data);
507   n = buffer_copy(mb1, mbc1, 10);
508   BOOST_ASIO_CHECK(n == 10);
509   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
510 
511   memset(dest_data, 0, sizeof(dest_data));
512   mb1 = boost::asio::buffer(dest_data);
513   cbc1 = const_buffers_1(boost::asio::buffer(source_data));
514   n = buffer_copy(mb1, cbc1, 10);
515   BOOST_ASIO_CHECK(n == 10);
516   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
517 
518   memset(dest_data, 0, sizeof(dest_data));
519   mbc1 = boost::asio::buffer(dest_data);
520   mb1 = boost::asio::buffer(source_data);
521   n = buffer_copy(mbc1, mb1, 10);
522   BOOST_ASIO_CHECK(n == 10);
523   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
524 
525   memset(dest_data, 0, sizeof(dest_data));
526   mbc1 = boost::asio::buffer(dest_data);
527   cb1 = boost::asio::buffer(source_data);
528   n = buffer_copy(mbc1, cb1, 10);
529   BOOST_ASIO_CHECK(n == 10);
530   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
531 
532   memset(dest_data, 0, sizeof(dest_data));
533   mbc1 = boost::asio::buffer(dest_data);
534   mbc2 = boost::asio::buffer(source_data);
535   n = buffer_copy(mbc1, mbc2, 10);
536   BOOST_ASIO_CHECK(n == 10);
537   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
538 
539   memset(dest_data, 0, sizeof(dest_data));
540   mbc1 = boost::asio::buffer(dest_data);
541   cbc1 = const_buffers_1(boost::asio::buffer(source_data));
542   n = buffer_copy(mbc1, cbc1, 10);
543   BOOST_ASIO_CHECK(n == 10);
544   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
545 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
546 
547   memset(dest_data, 0, sizeof(dest_data));
548   mb1 = boost::asio::buffer(dest_data);
549   mv1.clear();
550   mv1.push_back(boost::asio::buffer(source_data, 5));
551   mv1.push_back(boost::asio::buffer(source_data) + 5);
552   n = buffer_copy(mb1, mv1, 10);
553   BOOST_ASIO_CHECK(n == 10);
554   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
555 
556   memset(dest_data, 0, sizeof(dest_data));
557   mb1 = boost::asio::buffer(dest_data);
558   cv1.clear();
559   cv1.push_back(boost::asio::buffer(source_data, 6));
560   cv1.push_back(boost::asio::buffer(source_data) + 6);
561   n = buffer_copy(mb1, cv1, 10);
562   BOOST_ASIO_CHECK(n == 10);
563   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
564 
565   memset(dest_data, 0, sizeof(dest_data));
566   mv1.clear();
567   mv1.push_back(boost::asio::buffer(dest_data, 7));
568   mv1.push_back(boost::asio::buffer(dest_data) + 7);
569   cb1 = boost::asio::buffer(source_data);
570   n = buffer_copy(mv1, cb1, 10);
571   BOOST_ASIO_CHECK(n == 10);
572   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
573 
574   memset(dest_data, 0, sizeof(dest_data));
575   mv1.clear();
576   mv1.push_back(boost::asio::buffer(dest_data, 7));
577   mv1.push_back(boost::asio::buffer(dest_data) + 7);
578   cv1.clear();
579   cv1.push_back(boost::asio::buffer(source_data, 8));
580   cv1.push_back(boost::asio::buffer(source_data) + 8);
581   n = buffer_copy(mv1, cv1, 10);
582   BOOST_ASIO_CHECK(n == 10);
583   BOOST_ASIO_CHECK(memcmp(dest_data, source_data, n) == 0);
584 }
585 
586 } // namespace buffer_copy_runtime
587 
588 //------------------------------------------------------------------------------
589 
590 namespace buffer_sequence {
591 
592 using namespace boost::asio;
593 using namespace std;
594 
595 struct valid_const_a
596 {
597   typedef const_buffer* const_iterator;
598   typedef const_buffer value_type;
beginbuffer_sequence::valid_const_a599   const_buffer* begin() const { return 0; }
endbuffer_sequence::valid_const_a600   const_buffer* end() const { return 0; }
601 };
602 
603 #if defined(BOOST_ASIO_HAS_DECLTYPE)
604 struct valid_const_b
605 {
beginbuffer_sequence::valid_const_b606   const_buffer* begin() const { return 0; }
endbuffer_sequence::valid_const_b607   const_buffer* end() const { return 0; }
608 };
609 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
610 
611 struct valid_mutable_a
612 {
613   typedef mutable_buffer* const_iterator;
614   typedef mutable_buffer value_type;
beginbuffer_sequence::valid_mutable_a615   mutable_buffer* begin() const { return 0; }
endbuffer_sequence::valid_mutable_a616   mutable_buffer* end() const { return 0; }
617 };
618 
619 #if defined(BOOST_ASIO_HAS_DECLTYPE)
620 struct valid_mutable_b
621 {
beginbuffer_sequence::valid_mutable_b622   mutable_buffer* begin() const { return 0; }
endbuffer_sequence::valid_mutable_b623   mutable_buffer* end() const { return 0; }
624 };
625 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
626 
627 struct invalid_const_a
628 {
629   typedef int value_type;
beginbuffer_sequence::invalid_const_a630   int* begin() const { return 0; }
endbuffer_sequence::invalid_const_a631   const_buffer* end() const { return 0; }
632 };
633 
634 struct invalid_const_b
635 {
636   typedef const_buffer value_type;
beginbuffer_sequence::invalid_const_b637   const_buffer* begin() const { return 0; }
638 };
639 
640 struct invalid_const_c
641 {
642   typedef const_buffer value_type;
endbuffer_sequence::invalid_const_c643   const_buffer* end() const { return 0; }
644 };
645 
646 #if defined(BOOST_ASIO_HAS_DECLTYPE)
647 struct invalid_const_d
648 {
beginbuffer_sequence::invalid_const_d649   int* begin() const { return 0; }
endbuffer_sequence::invalid_const_d650   const_buffer* end() const { return 0; }
651 };
652 
653 struct invalid_const_e
654 {
beginbuffer_sequence::invalid_const_e655   const_buffer* begin() const { return 0; }
656 };
657 
658 struct invalid_const_f
659 {
endbuffer_sequence::invalid_const_f660   const_buffer* end() const { return 0; }
661 };
662 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
663 
664 struct invalid_mutable_a
665 {
666   typedef int value_type;
beginbuffer_sequence::invalid_mutable_a667   int* begin() const { return 0; }
endbuffer_sequence::invalid_mutable_a668   mutable_buffer* end() const { return 0; }
669 };
670 
671 struct invalid_mutable_b
672 {
673   typedef mutable_buffer value_type;
beginbuffer_sequence::invalid_mutable_b674   mutable_buffer* begin() const { return 0; }
675 };
676 
677 struct invalid_mutable_c
678 {
679   typedef mutable_buffer value_type;
endbuffer_sequence::invalid_mutable_c680   mutable_buffer* end() const { return 0; }
681 };
682 
683 #if defined(BOOST_ASIO_HAS_DECLTYPE)
684 struct invalid_mutable_d
685 {
beginbuffer_sequence::invalid_mutable_d686   int* begin() const { return 0; }
endbuffer_sequence::invalid_mutable_d687   mutable_buffer* end() const { return 0; }
688 };
689 
690 struct invalid_mutable_e
691 {
beginbuffer_sequence::invalid_mutable_e692   mutable_buffer* begin() const { return 0; }
693 };
694 
695 struct invalid_mutable_f
696 {
endbuffer_sequence::invalid_mutable_f697   mutable_buffer* end() const { return 0; }
698 };
699 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
700 
test()701 void test()
702 {
703   BOOST_ASIO_CHECK(is_const_buffer_sequence<const_buffer>::value);
704   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<const_buffer>::value);
705 
706   const_buffer b1;
707   BOOST_ASIO_CHECK(buffer_sequence_begin(b1) == &b1);
708   BOOST_ASIO_CHECK(buffer_sequence_end(b1) == &b1 + 1);
709 
710   BOOST_ASIO_CHECK(is_const_buffer_sequence<mutable_buffer>::value);
711   BOOST_ASIO_CHECK(is_mutable_buffer_sequence<mutable_buffer>::value);
712 
713   mutable_buffer b2;
714   BOOST_ASIO_CHECK(buffer_sequence_begin(b2) == &b2);
715   BOOST_ASIO_CHECK(buffer_sequence_end(b2) == &b2 + 1);
716 
717 #if !defined(BOOST_ASIO_NO_DEPRECATED)
718   BOOST_ASIO_CHECK(is_const_buffer_sequence<const_buffers_1>::value);
719   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<const_buffers_1>::value);
720 
721   const_buffers_1 b3(0, 0);
722   BOOST_ASIO_CHECK(buffer_sequence_begin(b3) == &b3);
723   BOOST_ASIO_CHECK(buffer_sequence_end(b3) == &b3 + 1);
724 
725   BOOST_ASIO_CHECK(is_const_buffer_sequence<mutable_buffers_1>::value);
726   BOOST_ASIO_CHECK(is_mutable_buffer_sequence<mutable_buffers_1>::value);
727 
728   mutable_buffers_1 b4(0, 0);
729   BOOST_ASIO_CHECK(buffer_sequence_begin(b4) == &b4);
730   BOOST_ASIO_CHECK(buffer_sequence_end(b4) == &b4 + 1);
731 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
732 
733   BOOST_ASIO_CHECK(is_const_buffer_sequence<vector<const_buffer> >::value);
734   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<vector<const_buffer> >::value);
735 
736   vector<const_buffer> b5;
737   BOOST_ASIO_CHECK(buffer_sequence_begin(b5) == b5.begin());
738   BOOST_ASIO_CHECK(buffer_sequence_end(b5) == b5.end());
739 
740   BOOST_ASIO_CHECK(is_const_buffer_sequence<vector<mutable_buffer> >::value);
741   BOOST_ASIO_CHECK(is_mutable_buffer_sequence<vector<mutable_buffer> >::value);
742 
743   vector<mutable_buffer> b6;
744   BOOST_ASIO_CHECK(buffer_sequence_begin(b6) == b6.begin());
745   BOOST_ASIO_CHECK(buffer_sequence_end(b6) == b6.end());
746 
747   BOOST_ASIO_CHECK(is_const_buffer_sequence<valid_const_a>::value);
748   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<valid_const_a>::value);
749 
750   valid_const_a b7;
751   BOOST_ASIO_CHECK(buffer_sequence_begin(b7) == b7.begin());
752   BOOST_ASIO_CHECK(buffer_sequence_end(b7) == b7.end());
753 
754 #if defined(BOOST_ASIO_HAS_DECLTYPE)
755   BOOST_ASIO_CHECK(is_const_buffer_sequence<valid_const_b>::value);
756   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<valid_const_b>::value);
757 
758   valid_const_b b8;
759   BOOST_ASIO_CHECK(buffer_sequence_begin(b8) == b8.begin());
760   BOOST_ASIO_CHECK(buffer_sequence_end(b8) == b8.end());
761 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
762 
763   BOOST_ASIO_CHECK(is_const_buffer_sequence<valid_mutable_a>::value);
764   BOOST_ASIO_CHECK(is_mutable_buffer_sequence<valid_mutable_a>::value);
765 
766   valid_mutable_a b9;
767   BOOST_ASIO_CHECK(buffer_sequence_begin(b9) == b9.begin());
768   BOOST_ASIO_CHECK(buffer_sequence_end(b9) == b9.end());
769 
770 #if defined(BOOST_ASIO_HAS_DECLTYPE)
771   BOOST_ASIO_CHECK(is_const_buffer_sequence<valid_mutable_b>::value);
772   BOOST_ASIO_CHECK(is_mutable_buffer_sequence<valid_mutable_b>::value);
773 
774   valid_mutable_b b10;
775   BOOST_ASIO_CHECK(buffer_sequence_begin(b10) == b10.begin());
776   BOOST_ASIO_CHECK(buffer_sequence_end(b10) == b10.end());
777 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
778 
779   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_a>::value);
780   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_a>::value);
781 
782   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_b>::value);
783   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_b>::value);
784 
785   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_c>::value);
786   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_c>::value);
787 
788 #if defined(BOOST_ASIO_HAS_DECLTYPE)
789   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_d>::value);
790   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_d>::value);
791 
792   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_e>::value);
793   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_e>::value);
794 
795   BOOST_ASIO_CHECK(!is_const_buffer_sequence<invalid_const_f>::value);
796   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_const_f>::value);
797 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
798 
799   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_a>::value);
800   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_a>::value);
801 
802   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_b>::value);
803   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_b>::value);
804 
805   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_c>::value);
806   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_c>::value);
807 
808 #if defined(BOOST_ASIO_HAS_DECLTYPE)
809   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_d>::value);
810   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_d>::value);
811 
812   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_e>::value);
813   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_e>::value);
814 
815   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_f>::value);
816   BOOST_ASIO_CHECK(!is_mutable_buffer_sequence<invalid_mutable_f>::value);
817 #endif // defined(BOOST_ASIO_HAS_DECLTYPE)
818 }
819 
820 } // namespace buffer_sequence
821 
822 //------------------------------------------------------------------------------
823 
824 BOOST_ASIO_TEST_SUITE
825 (
826   "buffer",
827   BOOST_ASIO_COMPILE_TEST_CASE(buffer_compile::test)
828   BOOST_ASIO_TEST_CASE(buffer_copy_runtime::test)
829   BOOST_ASIO_TEST_CASE(buffer_sequence::test)
830 )
831