File: copy_test.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (172 lines) | stat: -rw-r--r-- 5,583 bytes parent folder | download | duplicates (21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2004-2007 Jonathan Turkanis
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)

// See http://www.boost.org/libs/iostreams for documentation.

#include <algorithm>         // Equal
#include <vector>
#include <boost/config.hpp>  // MSVC.
#include <boost/detail/workaround.hpp>
#include <boost/iostreams/concepts.hpp>  // sink
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include "../example/container_device.hpp"
#include "detail/sequence.hpp"

using namespace std;
using namespace boost;
using namespace boost::iostreams;
using namespace boost::iostreams::example;
using namespace boost::iostreams::test;
using boost::unit_test::test_suite;
                    
//------------------Definition of fixed_sink----------------------------------//

/*class fixed_sink : public sink {
public:
    fixed_sink(vector<char>& storage)
        : storage_(storage), pos_(0)
        { }
    std::streamsize write(const char_type* s, std::streamsize n)
    {
        streamsize capacity = static_cast<streamsize>(storage_.size() - pos_);
        streamsize result = (min)(n, capacity);
        std::copy(s, s + result, storage_.begin() + pos_);
        pos_ += result;
        return result;
    }
private:
    fixed_sink operator=(const fixed_sink&);
    typedef vector<char>::size_type size_type;
    vector<char>&  storage_;
    size_type      pos_;
};*/

//------------------Definition of stream types--------------------------------//

typedef container_source< vector<char> >  vector_source;
typedef container_sink< vector<char> >    vector_sink;
typedef stream<vector_source>             vector_istream;
typedef stream<vector_sink>               vector_ostream;
//typedef stream<fixed_sink>              fixed_ostream;

//------------------Definition of copy_test-----------------------------------//

void copy_test()
{
    // Stream to stream
    {
        test_sequence<>  src;
        vector<char>     dest;
        vector_istream   first;
        vector_ostream   second;
        first.open(vector_source(src));
        second.open(vector_sink(dest));
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(first, second) ==
                static_cast<streamsize>(src.size()) &&
                    src == dest,
            "failed copying from stream to stream"
        );
    }

    // Stream to indirect sink
    {
        test_sequence<>  src;
        vector<char>     dest;
        vector_istream   in;
        vector_sink      out(dest);
        in.open(vector_source(src));
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) &&
                    src == dest,
            "failed copying from stream to indirect sink"
        );
    }

    // Indirect source to stream
    {
        test_sequence<>  src;
        vector<char>     dest;
        vector_source    in(src);
        vector_ostream   out;
        out.open(vector_sink(dest));
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) &&
                    src == dest,
            "failed copying from indirect source to stream"
        );
    }

    // Indirect source to indirect sink
    {
        test_sequence<>  src;
        vector<char>     dest;
        vector_source    in(src);
        vector_sink      out(dest);
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) &&
                    src == dest,
            "failed copying from indirect source to indirect sink"
        );
    }

    // Direct source to direct sink
    {
        test_sequence<>  src;
        vector<char>     dest(src.size(), '?');
        array_source     in(&src[0], &src[0] + src.size());
        array_sink       out(&dest[0], &dest[0] + dest.size());
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) &&
                    src == dest,
            "failed copying from direct source to direct sink"
        );
    }

    // Direct source to indirect sink
    {
        test_sequence<>  src;
        vector<char>     dest;
        array_source     in(&src[0], &src[0] + src.size());
        vector_ostream   out(dest);
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) && 
                    src == dest,
            "failed copying from direct source to indirect sink"
        );
    }

    // Indirect source to direct sink
    {
        test_sequence<>  src;
        vector<char>     dest(src.size(), '?');
        vector_istream   in;
        array_sink       out(&dest[0], &dest[0] + dest.size());
        in.open(vector_source(src));
        BOOST_CHECK_MESSAGE(
            boost::iostreams::copy(in, out) ==
                static_cast<streamsize>(src.size()) && 
                    src == dest,
            "failed copying from indirect source to direct sink"
        );
    }
}

test_suite* init_unit_test_suite(int, char* []) 
{
    test_suite* test = BOOST_TEST_SUITE("copy test");
    test->add(BOOST_TEST_CASE(&copy_test));
    return test;
}