copy_test.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2004-2007 Jonathan Turkanis
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  5. // See http://www.boost.org/libs/iostreams for documentation.
  6. #include <algorithm> // Equal
  7. #include <vector>
  8. #include <boost/config.hpp> // MSVC.
  9. #include <boost/detail/workaround.hpp>
  10. #include <boost/iostreams/concepts.hpp> // sink
  11. #include <boost/iostreams/copy.hpp>
  12. #include <boost/iostreams/device/array.hpp>
  13. #include <boost/iostreams/device/file.hpp>
  14. #include <boost/iostreams/stream.hpp>
  15. #include <boost/test/test_tools.hpp>
  16. #include <boost/test/unit_test.hpp>
  17. #include "../example/container_device.hpp"
  18. #include "detail/sequence.hpp"
  19. using namespace std;
  20. using namespace boost;
  21. using namespace boost::iostreams;
  22. using namespace boost::iostreams::example;
  23. using namespace boost::iostreams::test;
  24. using boost::unit_test::test_suite;
  25. //------------------Definition of fixed_sink----------------------------------//
  26. /*class fixed_sink : public sink {
  27. public:
  28. fixed_sink(vector<char>& storage)
  29. : storage_(storage), pos_(0)
  30. { }
  31. std::streamsize write(const char_type* s, std::streamsize n)
  32. {
  33. streamsize capacity = static_cast<streamsize>(storage_.size() - pos_);
  34. streamsize result = (min)(n, capacity);
  35. std::copy(s, s + result, storage_.begin() + pos_);
  36. pos_ += result;
  37. return result;
  38. }
  39. private:
  40. fixed_sink operator=(const fixed_sink&);
  41. typedef vector<char>::size_type size_type;
  42. vector<char>& storage_;
  43. size_type pos_;
  44. };*/
  45. //------------------Definition of stream types--------------------------------//
  46. typedef container_source< vector<char> > vector_source;
  47. typedef container_sink< vector<char> > vector_sink;
  48. typedef stream<vector_source> vector_istream;
  49. typedef stream<vector_sink> vector_ostream;
  50. //typedef stream<fixed_sink> fixed_ostream;
  51. //------------------Definition of copy_test-----------------------------------//
  52. void copy_test()
  53. {
  54. // Stream to stream
  55. {
  56. test_sequence<> src;
  57. vector<char> dest;
  58. vector_istream first;
  59. vector_ostream second;
  60. first.open(vector_source(src));
  61. second.open(vector_sink(dest));
  62. BOOST_CHECK_MESSAGE(
  63. boost::iostreams::copy(first, second) ==
  64. static_cast<streamsize>(src.size()) &&
  65. src == dest,
  66. "failed copying from stream to stream"
  67. );
  68. }
  69. // Stream to indirect sink
  70. {
  71. test_sequence<> src;
  72. vector<char> dest;
  73. vector_istream in;
  74. vector_sink out(dest);
  75. in.open(vector_source(src));
  76. BOOST_CHECK_MESSAGE(
  77. boost::iostreams::copy(in, out) ==
  78. static_cast<streamsize>(src.size()) &&
  79. src == dest,
  80. "failed copying from stream to indirect sink"
  81. );
  82. }
  83. // Indirect source to stream
  84. {
  85. test_sequence<> src;
  86. vector<char> dest;
  87. vector_source in(src);
  88. vector_ostream out;
  89. out.open(vector_sink(dest));
  90. BOOST_CHECK_MESSAGE(
  91. boost::iostreams::copy(in, out) ==
  92. static_cast<streamsize>(src.size()) &&
  93. src == dest,
  94. "failed copying from indirect source to stream"
  95. );
  96. }
  97. // Indirect source to indirect sink
  98. {
  99. test_sequence<> src;
  100. vector<char> dest;
  101. vector_source in(src);
  102. vector_sink out(dest);
  103. BOOST_CHECK_MESSAGE(
  104. boost::iostreams::copy(in, out) ==
  105. static_cast<streamsize>(src.size()) &&
  106. src == dest,
  107. "failed copying from indirect source to indirect sink"
  108. );
  109. }
  110. // Direct source to direct sink
  111. {
  112. test_sequence<> src;
  113. vector<char> dest(src.size(), '?');
  114. array_source in(&src[0], &src[0] + src.size());
  115. array_sink out(&dest[0], &dest[0] + dest.size());
  116. BOOST_CHECK_MESSAGE(
  117. boost::iostreams::copy(in, out) ==
  118. static_cast<streamsize>(src.size()) &&
  119. src == dest,
  120. "failed copying from direct source to direct sink"
  121. );
  122. }
  123. // Direct source to indirect sink
  124. {
  125. test_sequence<> src;
  126. vector<char> dest;
  127. array_source in(&src[0], &src[0] + src.size());
  128. vector_ostream out(dest);
  129. BOOST_CHECK_MESSAGE(
  130. boost::iostreams::copy(in, out) ==
  131. static_cast<streamsize>(src.size()) &&
  132. src == dest,
  133. "failed copying from direct source to indirect sink"
  134. );
  135. }
  136. // Indirect source to direct sink
  137. {
  138. test_sequence<> src;
  139. vector<char> dest(src.size(), '?');
  140. vector_istream in;
  141. array_sink out(&dest[0], &dest[0] + dest.size());
  142. in.open(vector_source(src));
  143. BOOST_CHECK_MESSAGE(
  144. boost::iostreams::copy(in, out) ==
  145. static_cast<streamsize>(src.size()) &&
  146. src == dest,
  147. "failed copying from indirect source to direct sink"
  148. );
  149. }
  150. }
  151. test_suite* init_unit_test_suite(int, char* [])
  152. {
  153. test_suite* test = BOOST_TEST_SUITE("copy test");
  154. test->add(BOOST_TEST_CASE(&copy_test));
  155. return test;
  156. }