summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/iostreams/test/example_test.cpp
blob: 24384422a4754927d72fcdc8cc21c21c1f39ba0a (plain)
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
// (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.

#include <map>
#include <boost/iostreams/detail/ios.hpp>  // failure.
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/filter/test.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/test/test_tools.hpp>
#include <boost/test/unit_test.hpp>
#include "../example/container_device.hpp"
#include "../example/dictionary_filter.hpp"
#include "../example/line_wrapping_filter.hpp"
#include "../example/shell_comments_filter.hpp"
#include "../example/tab_expanding_filter.hpp"
#include "../example/unix2dos_filter.hpp"
#include "./detail/verification.hpp"
#include "./detail/sequence.hpp"
#include "./detail/temp_file.hpp"

using boost::unit_test::test_suite;
namespace io = boost::iostreams;
namespace ex = boost::iostreams::example;

//------------------container_device test-------------------------------------//

void container_device_test()
{
    using namespace std;
    using namespace boost::iostreams::test;

    typedef vector<char>                       vector_type;
    typedef ex::container_source<vector_type>  vector_source;
    typedef ex::container_sink<vector_type>    vector_sink;
    typedef ex::container_device<vector_type>  vector_device;

    {
        test_sequence<>              seq;
        test_file                    file;
        io::stream<vector_source>    first(seq);
        io::stream<io::file_source>  second(file.name(), in_mode);
        BOOST_CHECK(compare_streams_in_chunks(first, second));
    }

    {
        std::vector<char>        first;
        test_sequence<>          second;
        io::stream<vector_sink>  out(first);
        write_data_in_chunks(out);
        BOOST_CHECK(first == second);
    }

    {
        vector<char>              v;
        io::stream<vector_device> io(v);
        BOOST_CHECK(test_seekable_in_chunks(io));
    }
}

//------------------dictionary_filter test------------------------------------//

void dictionary_filter_test()
{
    using namespace std;

    io::example::dictionary d;

    // See http://english2american.com.
    d.add("answerphone",  "answering machine");
    d.add("bloke",        "guy");
    d.add("gearbox",      "transmission");
    d.add("ironmonger",   "hardware shop");
    d.add("loo",          "restroom");
    d.add("lorry",        "truck");
    d.add("rubber",       "eraser");
    d.add("spanner",      "monkey wrench");
    d.add("telly",        "TV");
    d.add("tyre",         "tire");
    d.add("waistcoat",    "vest");
    d.add("windscreen",   "windshield");

    const std::string input = // Note: last character is non-alphabetic.
        "I had a message on my answerphone from the bloke at the car "
        "dealership that the windscreen and tyre on my lorry were replaced. "
        "However, the gearbox would not be ready until tomorrow since the "
        "spanner that they needed was broken and they had to go to the "
        "ironmonger to buy a replacement. Since my lorry was not ready, "
        "I decided to take the bus downtown and buy a new waistcoat. I "
        "also stopped at the liquor store to buy some wine. I came home "
        "and watched the telly and drank my wine. I also worked on a "
        "crossword puzzle. Fortunately I had a pencil with a new rubber. "
        "During that evening I made frequent trips to the loo due to my "
        "excessive drinking";

    const std::string output = // Note: last character is non-alphabetic.
        "I had a message on my answering machine from the guy at the car "
        "dealership that the windshield and tire on my truck were replaced. "
        "However, the transmission would not be ready until tomorrow since "
        "the monkey wrench that they needed was broken and they had to go to "
        "the hardware shop to buy a replacement. Since my truck was not ready, "
        "I decided to take the bus downtown and buy a new vest. I also stopped "
        "at the liquor store to buy some wine. I came home and watched the TV "
        "and drank my wine. I also worked on a crossword puzzle. Fortunately I "
        "had a pencil with a new eraser. During that evening I made frequent "
        "trips to the restroom due to my excessive drinking";

    BOOST_CHECK(
        io::test_input_filter( io::example::dictionary_stdio_filter(d),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::dictionary_stdio_filter(d),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::dictionary_input_filter(d),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::dictionary_output_filter(d),
                                input, output )
    );
}

//------------------line_wrapping_filter test---------------------------------//

void line_wrapping_filter_test()
{
    using namespace std;

    const std::string input =
        "I had a message on my answerphone from the bloke at the car \n"
        "dealership that the windscreen and tyre on my lorry were replaced. \n"
        "However, the gearbox would not be ready until tomorrow since the \n"
        "spanner that they needed was broken and they had to go to the \n"
        "ironmonger to buy a replacement. Since my lorry was not ready, \n"
        "I decided to take the bus downtown and buy a new waistcoat. I \n"
        "also stopped at the liquor store to buy some wine. I came home \n"
        "and watched the telly and drank my wine. I also worked on a \n"
        "crossword puzzle. Fortunately I had a pencil with a new rubber. \n"
        "During that evening I made frequent trips to the loo due to my \n"
        "excessive drinking.";

    const std::string output =
        "I had a message on my answerphone from t\n"
        "he bloke at the car \n"
        "dealership that the windscreen and tyre \n"
        "on my lorry were replaced. \n"
        "However, the gearbox would not be ready \n"
        "until tomorrow since the \n"
        "spanner that they needed was broken and \n"
        "they had to go to the \n"
        "ironmonger to buy a replacement. Since m\n"
        "y lorry was not ready, \n"
        "I decided to take the bus downtown and b\n"
        "uy a new waistcoat. I \n"
        "also stopped at the liquor store to buy \n"
        "some wine. I came home \n"
        "and watched the telly and drank my wine.\n"
        " I also worked on a \n"
        "crossword puzzle. Fortunately I had a pe\n"
        "ncil with a new rubber. \n"
        "During that evening I made frequent trip\n"
        "s to the loo due to my \n"
        "excessive drinking.";

    BOOST_CHECK(
        io::test_input_filter( io::example::line_wrapping_stdio_filter(40),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::line_wrapping_stdio_filter(40),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::line_wrapping_input_filter(40),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::line_wrapping_output_filter(40),
                                input, output )
    );
}

//------------------shell_comments_filter test--------------------------------//

void shell_comments_filter_test()
{
    using namespace std;

    const std::string input = // From <libs/filesystem/build/Jamfile>.
        "lib boost_filesystem\n"
        "     : ../src/$(SOURCES).cpp\n"
        "     : # build requirements\n"
        "       [ common-names ] # magic for install and auto-link features\n"
        "       <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
        "       <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
        "     : debug release  # build variants\n"
        "     ;\n"
        "\n"
        "dll boost_filesystem\n"
        "     : ../src/$(SOURCES).cpp\n"
        "     : # build requirements\n"
        "       [ common-names ]  # magic for install and auto-link features\n"
        "       <define>BOOST_FILESYSTEM_DYN_LINK=1  # tell source we're building dll's\n"
        "       <runtime-link>dynamic  # build only for dynamic runtimes\n"
        "       <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
        "       <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
        "     : debug release  # build variants\n"
        "     ;";

    const std::string output =
        "lib boost_filesystem\n"
        "     : ../src/$(SOURCES).cpp\n"
        "     : \n"
        "       [ common-names ] \n"
        "       <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
        "       <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
        "     : debug release  \n"
        "     ;\n"
        "\n"
        "dll boost_filesystem\n"
        "     : ../src/$(SOURCES).cpp\n"
        "     : \n"
        "       [ common-names ]  \n"
        "       <define>BOOST_FILESYSTEM_DYN_LINK=1  \n"
        "       <runtime-link>dynamic  \n"
        "       <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
        "       <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
        "     : debug release  \n"
        "     ;";

    BOOST_CHECK(
        io::test_input_filter( io::example::shell_comments_stdio_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::shell_comments_stdio_filter(),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::shell_comments_input_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::shell_comments_output_filter(),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::shell_comments_dual_use_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::shell_comments_dual_use_filter(),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::shell_comments_multichar_input_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::shell_comments_multichar_output_filter(),
                                input, output )
    );
}

//------------------tab_expanding_filter test---------------------------------//

void tab_expanding_filter_test()
{
    using namespace std;

    const std::string input =
        "class tab_expanding_stdio_filter : public stdio_filter {\n"
        "public:\n"
        "\texplicit tab_expanding_stdio_filter(int\ttab_size = 8)\n"
        "\t\t: tab_size_(tab_size), col_no_(0)\n"
        "\t{\n"
        "\t\tassert(tab_size\t> 0);\n"
        "\t}\n"
        "private:\n"
        "\tvoid do_filter()\n"
        "\t{\n"
        "\t\tint\tc;\n"
        "\t\twhile ((c = std::cin.get()) != EOF) {\n"
        "\t\t\tif (c == '\\t') {\n"
        "\t\t\t\tint\tspaces = tab_size_ - (col_no_ %\ttab_size_);\n"
        "\t\t\t\tfor\t(; spaces >\t0; --spaces)\n"
        "\t\t\t\t\tput_char(' ');\n"
        "\t\t\t} else {\n"
        "\t\t\t\tput_char(c);\n"
        "\t\t\t}\n"
        "\t\t}\n"
        "\t}\n"
        "\tvoid do_close()\t{ col_no_ =\t0; }\n"
        "\tvoid put_char(int c)\n"
        "\t{\n"
        "\t\tstd::cout.put(c);\n"
        "\t\tif (c == '\\n') {\n"
        "\t\t\tcol_no_\t= 0;\n"
        "\t\t} else {\n"
        "\t\t\t++col_no_;\n"
        "\t\t}\n"
        "\t}\n"
        "\tint\t tab_size_;\n"
        "\tint\t col_no_;\n"
        "};";

    const std::string output =
        "class tab_expanding_stdio_filter : public stdio_filter {\n"
        "public:\n"
        "    explicit tab_expanding_stdio_filter(int tab_size = 8)\n"
        "        : tab_size_(tab_size), col_no_(0)\n"
        "    {\n"
        "        assert(tab_size > 0);\n"
        "    }\n"
        "private:\n"
        "    void do_filter()\n"
        "    {\n"
        "        int c;\n"
        "        while ((c = std::cin.get()) != EOF) {\n"
        "            if (c == '\\t') {\n"
        "                int spaces = tab_size_ - (col_no_ % tab_size_);\n"
        "                for (; spaces > 0; --spaces)\n"
        "                    put_char(' ');\n"
        "            } else {\n"
        "                put_char(c);\n"
        "            }\n"
        "        }\n"
        "    }\n"
        "    void do_close() { col_no_ = 0; }\n"
        "    void put_char(int c)\n"
        "    {\n"
        "        std::cout.put(c);\n"
        "        if (c == '\\n') {\n"
        "            col_no_ = 0;\n"
        "        } else {\n"
        "            ++col_no_;\n"
        "        }\n"
        "    }\n"
        "    int  tab_size_;\n"
        "    int  col_no_;\n"
        "};";

    BOOST_CHECK(
        io::test_input_filter( io::example::tab_expanding_stdio_filter(4),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::tab_expanding_stdio_filter(4),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::tab_expanding_input_filter(4),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::tab_expanding_output_filter(4),
                                input, output )
    );
}

//------------------unix2dos_filter test--------------------------------------//

void unix2dos_filter_test()
{
    using namespace std;

    const std::string input =
        "When I was one-and-twenty\n"
        "I heard a wise man say,\n"
        "'Give crowns and pounds and guineas\n"
        "But not your heart away;\n"
        "\n"
        "Give pearls away and rubies\n"
        "But keep your fancy free.'\n"
        "But I was one-and-twenty,\n"
        "No use to talk to me.\n"
        "\n"
        "When I was one-and-twenty\n"
        "I heard him say again,\n"
        "'The heart out of the bosom\n"
        "Was never given in vain;\n"
        "'Tis paid with sighs a plenty\n"
        "And sold for endless rue.'\n"
        "And I am two-and-twenty,\n"
        "And oh, 'tis true, 'tis true.";

    const std::string output =
        "When I was one-and-twenty\r\n"
        "I heard a wise man say,\r\n"
        "'Give crowns and pounds and guineas\r\n"
        "But not your heart away;\r\n"
        "\r\n"
        "Give pearls away and rubies\r\n"
        "But keep your fancy free.'\r\n"
        "But I was one-and-twenty,\r\n"
        "No use to talk to me.\r\n"
        "\r\n"
        "When I was one-and-twenty\r\n"
        "I heard him say again,\r\n"
        "'The heart out of the bosom\r\n"
        "Was never given in vain;\r\n"
        "'Tis paid with sighs a plenty\r\n"
        "And sold for endless rue.'\r\n"
        "And I am two-and-twenty,\r\n"
        "And oh, 'tis true, 'tis true.";

    BOOST_CHECK(
        io::test_input_filter( io::example::unix2dos_stdio_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::unix2dos_stdio_filter(),
                                input, output )
    );

    BOOST_CHECK(
        io::test_input_filter( io::example::unix2dos_input_filter(),
                               input, output )
    );

    BOOST_CHECK(
        io::test_output_filter( io::example::unix2dos_output_filter(),
                                input, output )
    );
}

test_suite* init_unit_test_suite(int, char* [])
{
    test_suite* test = BOOST_TEST_SUITE("example test");
    test->add(BOOST_TEST_CASE(&container_device_test));
    test->add(BOOST_TEST_CASE(&dictionary_filter_test));
    test->add(BOOST_TEST_CASE(&tab_expanding_filter_test));
    test->add(BOOST_TEST_CASE(&line_wrapping_filter_test));
    test->add(BOOST_TEST_CASE(&shell_comments_filter_test));
    test->add(BOOST_TEST_CASE(&unix2dos_filter_test));
    return test;
}