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