File: verification.hpp

package info (click to toggle)
boost1.74 1.74.0-9
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 464,084 kB
  • sloc: cpp: 3,338,324; xml: 131,293; python: 33,088; ansic: 14,336; asm: 4,034; sh: 3,351; makefile: 1,193; perl: 1,036; yacc: 478; php: 212; ruby: 102; lisp: 24; sql: 13; csh: 6
file content (383 lines) | stat: -rw-r--r-- 11,995 bytes parent folder | download | duplicates (13)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// (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.


#ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED
#define BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED

#include <iostream>
#include <exception>
#include <string>
#include <string.h>
#include <fstream>
#ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
# include <istream>
# include <ostream>
#else
# include <iostream.h>
#endif

#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/iostreams/detail/char_traits.hpp>
#include <boost/iostreams/detail/config/wide_streams.hpp>
#include "./constants.hpp"

// Must come last.
#include <boost/iostreams/detail/config/disable_warnings.hpp>

// Code generation bugs cause tests to fail with global optimization.
#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
# pragma optimize("g", off)
#endif

// Included only by tests; no need to #undef.
#ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
# define BOOST_TEMPLATE_DECL template<typename Ch, typename Tr>
# define BOOST_CHAR Ch
# define BOOST_ISTREAM std::basic_istream<Ch, Tr>
# define BOOST_OSTREAM std::basic_ostream<Ch, Tr>
#else
# define BOOST_TEMPLATE_DECL
# define BOOST_CHAR char
# define BOOST_ISTREAM std::istream
# define BOOST_OSTREAM std::ostream
#endif

namespace boost { namespace iostreams { namespace test {

BOOST_TEMPLATE_DECL
bool compare_streams_in_chars(BOOST_ISTREAM& first, BOOST_ISTREAM& second)
{
    for (int z = 0; z < data_reps; ++z)
        for (int w = 0; w < data_length(); ++w)
            if (first.eof() != second.eof() || first.get() != second.get())
                return false;
    return true;
}

BOOST_TEMPLATE_DECL
bool compare_streams_in_chunks(BOOST_ISTREAM& first, BOOST_ISTREAM& second)
{
    int i = 0;
    do {
        BOOST_CHAR buf_one[chunk_size];
        BOOST_CHAR buf_two[chunk_size];
        first.read(buf_one, chunk_size);
        second.read(buf_two, chunk_size);
        std::streamsize amt = first.gcount();
        if ( amt != static_cast<std::streamsize>(second.gcount()) ||
             BOOST_IOSTREAMS_CHAR_TRAITS(BOOST_CHAR)::
                compare(buf_one, buf_two, static_cast<std::size_t>(amt)) != 0 )
            return false;
        ++i;
    } while (!first.eof());
    return true;
}

bool compare_files(const std::string& first, const std::string& second)
{
    using namespace std;
    ifstream one(first.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
    ifstream two(second.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
    return compare_streams_in_chunks(one, two);
}

#ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
    template<typename Container, typename Ch, typename Tr>
#else
    template<typename Container>
#endif
bool compare_container_and_stream(Container& cnt, BOOST_ISTREAM& is)
{
    typename Container::iterator first = cnt.begin();
    typename Container::iterator last = cnt.end();
    do  {
        if ((first == last) != is.eof()) return false;
        if (first != last && *first++ != is.get()) return false;
    } while (first != last);
    return true;
}

template<typename Container>
bool compare_container_and_file(Container& cnt, const std::string& file)
{
    std::ifstream fstrm(file.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
    return compare_container_and_stream(cnt, fstrm);
}

BOOST_TEMPLATE_DECL
void write_data_in_chars(BOOST_OSTREAM& os)
{
    for (int z = 0; z < data_reps; ++z) 
        for (int w = 0; w < data_length(); ++w) 
            os.put(detail::data((BOOST_CHAR*)0)[w]);
    os.flush();
}

BOOST_TEMPLATE_DECL
void write_data_in_chunks(BOOST_OSTREAM& os)
{
    const BOOST_CHAR* buf = detail::data((BOOST_CHAR*)0);
    for (int z = 0; z < data_reps; ++z)
        os.write(buf, data_length());
    os.flush();
}

bool test_seekable_in_chars(std::iostream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        int j;
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-chunk_size, BOOST_IOS::cur);
        for (j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-chunk_size, BOOST_IOS::cur);
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekp(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        int j;
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(off, BOOST_IOS::beg);
        for (j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(off, BOOST_IOS::beg);
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
    }

    // Test seeking with ios::end
    io.seekp(0, BOOST_IOS::end);
    off = io.tellp();
    io.seekp(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        int j;
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-off, BOOST_IOS::end);
        for (j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-off, BOOST_IOS::end);
        for (j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
    }
    return true;
}

bool test_seekable_in_chunks(std::iostream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        io.write(narrow_data(), chunk_size);
        io.seekp(-chunk_size, BOOST_IOS::cur);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
        io.seekg(-chunk_size, BOOST_IOS::cur);
        io.write(narrow_data(), chunk_size);
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekp(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        io.write(narrow_data(), chunk_size);
        io.seekp(off, BOOST_IOS::beg);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
        io.seekg(off, BOOST_IOS::beg);
        io.write(narrow_data(), chunk_size);
    }
    
    // Test seeking with ios::end
    io.seekp(0, BOOST_IOS::end);
    off = io.tellp();
    io.seekp(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        io.write(narrow_data(), chunk_size);
        io.seekp(-off, BOOST_IOS::end);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
        io.seekg(-off, BOOST_IOS::end);
        io.write(narrow_data(), chunk_size);
    }
    return true;
}

bool test_input_seekable(std::istream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-chunk_size, BOOST_IOS::cur);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekg(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(off, BOOST_IOS::beg);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }
    
    // Test seeking with ios::end
    io.seekg(0, BOOST_IOS::end);
    off = io.tellg();
    io.seekg(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-off, BOOST_IOS::end);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }
    return true;
}

bool test_output_seekable(std::ostream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-chunk_size, BOOST_IOS::cur);
        io.write(narrow_data(), chunk_size);
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekp(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(off, BOOST_IOS::beg);
        io.write(narrow_data(), chunk_size);
    }
    
    // Test seeking with ios::end
    io.seekp(0, BOOST_IOS::end);
    off = io.tellp();
    io.seekp(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-off, BOOST_IOS::end);
        io.write(narrow_data(), chunk_size);
    }
    return true;
}

bool test_dual_seekable(std::iostream& io)
{
    int i;  // old 'for' scope workaround.

    // Test seeking with ios::cur
    for (i = 0; i < data_reps; ++i) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-chunk_size, BOOST_IOS::cur);
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-chunk_size, BOOST_IOS::cur);
        io.write(narrow_data(), chunk_size);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }

    // Test seeking with ios::beg
    std::streamoff off = 0;
    io.seekp(0, BOOST_IOS::beg);
    io.seekg(0, BOOST_IOS::beg);
    for (i = 0; i < data_reps; ++i, off += chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(off, BOOST_IOS::beg);
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(off, BOOST_IOS::beg);
        io.write(narrow_data(), chunk_size);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }
    
    // Test seeking with ios::end
    io.seekp(0, BOOST_IOS::end);
    io.seekg(0, BOOST_IOS::end);
    off = io.tellp();
    io.seekp(-off, BOOST_IOS::end);
    io.seekg(-off, BOOST_IOS::end);
    for (i = 0; i < data_reps; ++i, off -= chunk_size) {
        for (int j = 0; j < chunk_size; ++j)
            io.put(narrow_data()[j]);
        io.seekp(-off, BOOST_IOS::end);
        for (int j = 0; j < chunk_size; ++j)
            if (io.get() != narrow_data()[j])
               return false;
        io.seekg(-off, BOOST_IOS::end);
        io.write(narrow_data(), chunk_size);
        char buf[chunk_size];
        io.read(buf, chunk_size);
        if (strncmp(buf, narrow_data(), chunk_size) != 0)
            return false;
    }
    return true;
}

} } } // End namespaces test, iostreams, boost.

#include <boost/iostreams/detail/config/enable_warnings.hpp>

#if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
# pragma optimize("", on)
#endif

#endif // #ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED