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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
|
// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
// (C) Copyright 2005-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.
// Allow this file to be used by slice_test.hpp. It is important not to
// replace BOOST_IOSTREAMS_RESTRICT with BOOST_IOSTREAMS_RESTRICT here, since that
// would interfere with the oepration of the header
// <boost/iostreams/restrict.hpp>
#include <iostream>
#if defined(BOOST_IOSTREAMS_RESTRICT_USE_SLICE)
# include <boost/iostreams/slice.hpp>
# define BOOST_IOSTREAMS_RESTRICT slice
#else
# include <boost/iostreams/restrict.hpp>
# define BOOST_IOSTREAMS_RESTRICT restrict
#endif
#include <algorithm> // equal.
#include <cctype>
#include <iterator> // back_inserter.
#include <vector>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/null.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include "detail/closable.hpp"
#include "detail/constants.hpp"
#include "detail/filters.hpp"
#include "detail/operation_sequence.hpp"
#include "detail/sequence.hpp"
#include "detail/temp_file.hpp"
#include "detail/verification.hpp"
using namespace std;
using namespace boost::iostreams;
using namespace boost::iostreams::test;
using boost::unit_test::test_suite;
namespace io = boost::iostreams;
const char pad_char = '\n';
const int small_padding = 50;
const int large_padding = default_device_buffer_size + 50;
void write_padding(std::ofstream& out, int len)
{
for (int z = 0; z < len; ++z)
out.put(pad_char);
}
struct restricted_test_file : public temp_file {
restricted_test_file(int padding, bool half_open = false)
{
BOOST_IOS::openmode mode =
BOOST_IOS::out | BOOST_IOS::binary;
::std::ofstream f(name().c_str(), mode);
write_padding(f, padding);
const char* buf = narrow_data();
for (int z = 0; z < data_reps; ++z)
f.write(buf, data_length());
if (!half_open)
write_padding(f, padding);
}
};
struct restricted_test_sequence : public std::vector<char> {
restricted_test_sequence(int padding, bool half_open = false)
{
for (int z = 0; z < padding; ++z)
push_back(pad_char);
const char* buf = narrow_data();
for (int w = 0; w < data_reps; ++w)
insert(end(), buf, buf + data_length());
if (!half_open)
for (int x = 0; x < padding; ++x)
push_back(pad_char);
}
};
struct restricted_uppercase_file : public temp_file {
restricted_uppercase_file(int padding, bool half_open = false)
{
BOOST_IOS::openmode mode =
BOOST_IOS::out | BOOST_IOS::binary;
::std::ofstream f(name().c_str(), mode);
write_padding(f, padding);
const char* buf = narrow_data();
for (int z = 0; z < data_reps; ++z)
for (int w = 0; w < data_length(); ++w)
f.put((char) std::toupper(buf[w]));
if (!half_open)
write_padding(f, padding);
}
};
struct restricted_lowercase_file : public temp_file {
restricted_lowercase_file(int padding, bool half_open = false)
{
BOOST_IOS::openmode mode =
BOOST_IOS::out | BOOST_IOS::binary;
::std::ofstream f(name().c_str(), mode);
write_padding(f, padding);
const char* buf = narrow_data();
for (int z = 0; z < data_reps; ++z)
for (int w = 0; w < data_length(); ++w)
f.put((char) std::tolower(buf[w]));
if (!half_open)
write_padding(f, padding);
}
};
// Can't have a restricted view of a non-seekble output filter.
struct tolower_seekable_filter : public seekable_filter {
typedef char char_type;
struct category
: output_seekable,
filter_tag
{ };
template<typename Sink>
bool put(Sink& s, char c)
{ return boost::iostreams::put(s, (char) std::tolower(c)); }
template<typename Sink>
std::streampos seek(Sink& s, stream_offset off, BOOST_IOS::seekdir way)
{ return boost::iostreams::seek(s, off, way); }
};
void read_device()
{
{
restricted_test_file src1(small_padding);
test_file src2;
stream_offset off = small_padding,
len = data_reps * data_length();
filtering_istream first(
BOOST_IOSTREAMS_RESTRICT(file_source(src1.name(), in_mode), off, len));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from restriction<Device> with small padding"
);
}
{
restricted_test_file src1(large_padding);
test_file src2;
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_istream first(
BOOST_IOSTREAMS_RESTRICT(file_source(src1.name(), in_mode), off, len));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from restriction<Device> with large padding"
);
}
{
restricted_test_file src1(small_padding, true);
test_file src2;
stream_offset off = small_padding;
filtering_istream first(
BOOST_IOSTREAMS_RESTRICT(file_source(src1.name(), in_mode), off));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from half-open restriction<Device> "
"with small padding"
);
}
{
restricted_test_file src1(large_padding, true);
test_file src2;
stream_offset off = large_padding;
filtering_istream first(
BOOST_IOSTREAMS_RESTRICT(file_source(src1.name(), in_mode), off));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from half-open restriction<Device> "
"with large padding"
);
}
}
void read_direct_device()
{
{
test_sequence<char> first;
restricted_test_sequence src(small_padding);
array_source array_src(&src[0], &src[0] + src.size());
stream_offset off = small_padding,
len = data_reps * data_length();
filtering_istream second(BOOST_IOSTREAMS_RESTRICT(array_src, off, len));
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed reading from restriction<Direct>"
);
}
{
test_sequence<char> first;
restricted_test_sequence src(small_padding, true);
array_source array_src(&src[0], &src[0] + src.size());
stream_offset off = small_padding;
filtering_istream second(BOOST_IOSTREAMS_RESTRICT(array_src, off));
BOOST_CHECK_MESSAGE(
compare_container_and_stream(first, second),
"failed reading from half-open restriction<Direct>"
);
}
}
void read_filter()
{
{
restricted_test_file src1(small_padding);
uppercase_file src2;
stream_offset off = small_padding,
len = data_reps * data_length();
filtering_istream first;
first.push(BOOST_IOSTREAMS_RESTRICT(toupper_filter(), off, len));
first.push(file_source(src1.name(), in_mode));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from restriction<Filter> with small padding"
);
}
{
restricted_test_file src1(large_padding);
uppercase_file src2;
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_istream first;
first.push(BOOST_IOSTREAMS_RESTRICT(toupper_filter(), off, len));
first.push(file_source(src1.name(), in_mode));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from restriction<Filter> with large padding"
);
}
{
restricted_test_file src1(small_padding, true);
uppercase_file src2;
stream_offset off = small_padding;
filtering_istream first;
first.push(BOOST_IOSTREAMS_RESTRICT(toupper_filter(), off));
first.push(file_source(src1.name(), in_mode));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from half-open restriction<Filter> "
"with small padding"
);
}
{
restricted_test_file src1(large_padding, true);
uppercase_file src2;
stream_offset off = large_padding;
filtering_istream first;
first.push(BOOST_IOSTREAMS_RESTRICT(toupper_filter(), off));
first.push(file_source(src1.name(), in_mode));
ifstream second(src2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed reading from half-open restriction<Filter> "
"with large padding"
);
}
}
void write_device()
{
{
restricted_uppercase_file dest1(small_padding);
restricted_test_file dest2(small_padding);
stream_offset off = small_padding,
len = data_reps * data_length();
filtering_ostream out(
BOOST_IOSTREAMS_RESTRICT(file(dest1.name(), BOOST_IOS::binary), off, len));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Device> with small padding"
);
}
{
restricted_uppercase_file dest1(large_padding);
restricted_test_file dest2(large_padding);
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_ostream out
(BOOST_IOSTREAMS_RESTRICT(file(dest1.name(), BOOST_IOS::binary), off, len));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Device> with large padding"
);
}
{
restricted_uppercase_file dest1(small_padding, true);
restricted_test_file dest2(small_padding, true);
stream_offset off = small_padding;
filtering_ostream out
(BOOST_IOSTREAMS_RESTRICT(file(dest1.name(), BOOST_IOS::binary), off));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to half-open restriction<Device> "
"with small padding"
);
}
{
restricted_uppercase_file dest1(large_padding, true);
restricted_test_file dest2(large_padding, true);
stream_offset off = large_padding;
filtering_ostream out
(BOOST_IOSTREAMS_RESTRICT(file(dest1.name(), BOOST_IOS::binary), off));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to half-open restriction<Device> "
"with large padding"
);
}
}
void write_direct_device()
{
{
vector<char> dest1( data_reps * data_length() +
2 * small_padding,
'\n' );
restricted_test_sequence dest2(small_padding);
stream_offset off = small_padding,
len = data_reps * data_length();
array_sink array(&dest1[0], &dest1[0] + dest1.size());
filtering_ostream out(BOOST_IOSTREAMS_RESTRICT(array, off, len));
write_data_in_chunks(out);
out.reset();
BOOST_CHECK_MESSAGE(
std::equal(dest1.begin(), dest1.end(), dest2.begin()),
"failed writing to restriction<Direct>"
);
}
{
vector<char> dest1(
data_reps * data_length() + small_padding, '\n');
restricted_test_sequence dest2(small_padding, true);
stream_offset off = small_padding;
array_sink array(&dest1[0], &dest1[0] + dest1.size());
filtering_ostream out(BOOST_IOSTREAMS_RESTRICT(array, off));
write_data_in_chunks(out);
out.reset();
BOOST_CHECK_MESSAGE(
std::equal(dest1.begin(), dest1.end(), dest2.begin()),
"failed writing to half-open restriction<Direct>"
);
}
}
void write_filter()
{
{
restricted_test_file dest1(small_padding);
restricted_lowercase_file dest2(small_padding);
stream_offset off = small_padding,
len = data_reps * data_length();
filtering_ostream out;
out.push(BOOST_IOSTREAMS_RESTRICT(tolower_seekable_filter(), off, len));
out.push(file(dest1.name(), BOOST_IOS::binary));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Filter> with small padding"
);
}
{
restricted_test_file dest1(large_padding);
restricted_lowercase_file dest2(large_padding);
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_ostream out;
out.push(BOOST_IOSTREAMS_RESTRICT(tolower_seekable_filter(), off, len));
out.push(file(dest1.name(), BOOST_IOS::binary));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Filter> with large padding"
);
}
{
restricted_test_file dest1(small_padding, true);
restricted_lowercase_file dest2(small_padding, true);
stream_offset off = small_padding;
filtering_ostream out;
out.push(BOOST_IOSTREAMS_RESTRICT(tolower_seekable_filter(), off));
out.push(file(dest1.name(), BOOST_IOS::binary));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Filter> with small padding"
);
}
{
restricted_test_file dest1(large_padding, true);
restricted_lowercase_file dest2(large_padding, true);
stream_offset off = large_padding;
filtering_ostream out;
out.push(BOOST_IOSTREAMS_RESTRICT(tolower_seekable_filter(), off));
out.push(file(dest1.name(), BOOST_IOS::binary));
write_data_in_chunks(out);
out.reset();
ifstream first(dest1.name().c_str(), in_mode);
ifstream second(dest2.name().c_str(), in_mode);
BOOST_CHECK_MESSAGE(
compare_streams_in_chunks(first, second),
"failed writing to restriction<Filter> with large padding"
);
}
}
void seek_device()
{
{
restricted_test_file src(large_padding);
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_stream<seekable> io(
BOOST_IOSTREAMS_RESTRICT(file(src.name(), BOOST_IOS::binary), off, len));
BOOST_CHECK_MESSAGE(
test_seekable_in_chunks(io),
"failed seeking within restriction<Device>"
);
}
{
restricted_test_file src(large_padding, true);
stream_offset off = large_padding;
filtering_stream<seekable> io(
BOOST_IOSTREAMS_RESTRICT(file(src.name(), BOOST_IOS::binary), off));
BOOST_CHECK_MESSAGE(
test_seekable_in_chunks(io),
"failed seeking within half-open restriction<Device>"
);
}
}
void seek_direct_device()
{
{
vector<char> src(
data_reps * data_length() + 2 * small_padding, '\n');
stream_offset off = small_padding,
len = data_reps * data_length();
io::array ar(&src[0], &src[0] + src.size());
filtering_stream<seekable> io(BOOST_IOSTREAMS_RESTRICT(ar, off, len));
BOOST_CHECK_MESSAGE(
test_seekable_in_chars(io),
"failed seeking within restriction<Direct> with small padding"
);
}
{
vector<char> src(
data_reps * data_length() + small_padding, '\n');
stream_offset off = small_padding;
io::array ar(&src[0], &src[0] + src.size());
filtering_stream<seekable> io(BOOST_IOSTREAMS_RESTRICT(ar, off));
BOOST_CHECK_MESSAGE(
test_seekable_in_chars(io),
"failed seeking within half-open restriction<Direct> "
"with small padding"
);
}
}
void seek_filter()
{
{
restricted_test_file src(small_padding);
stream_offset off = large_padding,
len = data_reps * data_length();
filtering_stream<seekable> io;
io.push(BOOST_IOSTREAMS_RESTRICT(identity_seekable_filter(), off, len));
io.push(file(src.name(), BOOST_IOS::binary));
BOOST_CHECK_MESSAGE(
test_seekable_in_chars(io),
"failed seeking within restriction<Device>"
);
}
{
restricted_test_file src(small_padding, true);
stream_offset off = large_padding;
filtering_stream<seekable> io;
io.push(BOOST_IOSTREAMS_RESTRICT(identity_seekable_filter(), off));
io.push(file(src.name(), BOOST_IOS::binary));
BOOST_CHECK_MESSAGE(
test_seekable_in_chars(io),
"failed seeking within half-open restriction<Device>"
);
}
}
void close_device()
{
// Restrict a source
{
operation_sequence seq;
chain<input> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_device<input>(seq.new_operation(1)),
0
)
);
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a seekable device
{
operation_sequence seq;
chain<seekable> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_device<seekable>(seq.new_operation(1)),
0
)
);
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a direct source
{
operation_sequence seq;
chain<input> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_device<direct_input>(seq.new_operation(1)),
0
)
);
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a direct seekable device
{
operation_sequence seq;
chain<seekable> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_device<direct_seekable>(seq.new_operation(1)),
0
)
);
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
}
void close_filter()
{
// Restrict an input filter
{
operation_sequence seq;
chain<input> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_filter<input>(seq.new_operation(2)),
0
)
);
ch.push(closable_device<input>(seq.new_operation(1)));
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a seekable filter
{
operation_sequence seq;
chain<seekable> ch;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_filter<seekable>(seq.new_operation(1)),
0
)
);
ch.push(closable_device<seekable>(seq.new_operation(2)));
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a dual_use filter for input
{
operation_sequence seq;
chain<input> ch;
operation dummy;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_filter<dual_use>(
seq.new_operation(2),
dummy
),
0
)
);
ch.push(closable_device<input>(seq.new_operation(1)));
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
// Restrict a dual_use filter for output
{
operation_sequence seq;
chain<output> ch;
operation dummy;
ch.push(
io::BOOST_IOSTREAMS_RESTRICT(
closable_filter<dual_use>(
dummy,
seq.new_operation(1)
),
0
)
);
ch.push(closable_device<output>(seq.new_operation(2)));
BOOST_CHECK_NO_THROW(ch.reset());
BOOST_CHECK_OPERATION_SEQUENCE(seq);
}
}
test_suite* init_unit_test_suite(int, char* [])
{
test_suite* test =
BOOST_TEST_SUITE(BOOST_STRINGIZE(BOOST_IOSTREAMS_RESTRICT) " test");
test->add(BOOST_TEST_CASE(&read_device));
test->add(BOOST_TEST_CASE(&read_direct_device));
test->add(BOOST_TEST_CASE(&read_filter));
test->add(BOOST_TEST_CASE(&write_device));
test->add(BOOST_TEST_CASE(&write_direct_device));
test->add(BOOST_TEST_CASE(&write_filter));
test->add(BOOST_TEST_CASE(&seek_device));
test->add(BOOST_TEST_CASE(&seek_direct_device));
test->add(BOOST_TEST_CASE(&close_device));
test->add(BOOST_TEST_CASE(&close_filter));
return test;
}
|