1// (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2// (C) Copyright 2005-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 <map>
9#include <boost/iostreams/detail/ios.hpp> // failure.
10#include <boost/iostreams/device/file.hpp>
11#include <boost/iostreams/filter/test.hpp>
12#include <boost/iostreams/stream.hpp>
13#include <boost/mpl/vector.hpp>
14#include <boost/test/test_tools.hpp>
15#include <boost/test/unit_test.hpp>
16#include "../example/container_device.hpp"
17#include "../example/dictionary_filter.hpp"
18#include "../example/line_wrapping_filter.hpp"
19#include "../example/shell_comments_filter.hpp"
20#include "../example/tab_expanding_filter.hpp"
21#include "../example/unix2dos_filter.hpp"
22#include "./detail/verification.hpp"
23#include "./detail/sequence.hpp"
24#include "./detail/temp_file.hpp"
25
26using boost::unit_test::test_suite;
27namespace io = boost::iostreams;
28namespace ex = boost::iostreams::example;
29
30//------------------container_device test-------------------------------------//
31
32void container_device_test()
33{
34 using namespace std;
35 using namespace boost::iostreams::test;
36
37 typedef vector<char> vector_type;
38 typedef ex::container_source<vector_type> vector_source;
39 typedef ex::container_sink<vector_type> vector_sink;
40 typedef ex::container_device<vector_type> vector_device;
41
42 {
43 test_sequence<> seq;
44 test_file file;
45 io::stream<vector_source> first(seq);
46 io::stream<io::file_source> second(file.name(), in_mode);
47 BOOST_CHECK(compare_streams_in_chunks(first, second));
48 }
49
50 {
51 std::vector<char> first;
52 test_sequence<> second;
53 io::stream<vector_sink> out(first);
54 write_data_in_chunks(os&: out);
55 BOOST_CHECK(first == second);
56 }
57
58 {
59 vector<char> v;
60 io::stream<vector_device> io(v);
61 BOOST_CHECK(test_seekable_in_chunks(io));
62 }
63}
64
65//------------------dictionary_filter test------------------------------------//
66
67void dictionary_filter_test()
68{
69 using namespace std;
70
71 io::example::dictionary d;
72
73 // See http://english2american.com.
74 d.add(key: "answerphone", value: "answering machine");
75 d.add(key: "bloke", value: "guy");
76 d.add(key: "gearbox", value: "transmission");
77 d.add(key: "ironmonger", value: "hardware shop");
78 d.add(key: "loo", value: "restroom");
79 d.add(key: "lorry", value: "truck");
80 d.add(key: "rubber", value: "eraser");
81 d.add(key: "spanner", value: "monkey wrench");
82 d.add(key: "telly", value: "TV");
83 d.add(key: "tyre", value: "tire");
84 d.add(key: "waistcoat", value: "vest");
85 d.add(key: "windscreen", value: "windshield");
86
87 const std::string input = // Note: last character is non-alphabetic.
88 "I had a message on my answerphone from the bloke at the car "
89 "dealership that the windscreen and tyre on my lorry were replaced. "
90 "However, the gearbox would not be ready until tomorrow since the "
91 "spanner that they needed was broken and they had to go to the "
92 "ironmonger to buy a replacement. Since my lorry was not ready, "
93 "I decided to take the bus downtown and buy a new waistcoat. I "
94 "also stopped at the liquor store to buy some wine. I came home "
95 "and watched the telly and drank my wine. I also worked on a "
96 "crossword puzzle. Fortunately I had a pencil with a new rubber. "
97 "During that evening I made frequent trips to the loo due to my "
98 "excessive drinking";
99
100 const std::string output = // Note: last character is non-alphabetic.
101 "I had a message on my answering machine from the guy at the car "
102 "dealership that the windshield and tire on my truck were replaced. "
103 "However, the transmission would not be ready until tomorrow since "
104 "the monkey wrench that they needed was broken and they had to go to "
105 "the hardware shop to buy a replacement. Since my truck was not ready, "
106 "I decided to take the bus downtown and buy a new vest. I also stopped "
107 "at the liquor store to buy some wine. I came home and watched the TV "
108 "and drank my wine. I also worked on a crossword puzzle. Fortunately I "
109 "had a pencil with a new eraser. During that evening I made frequent "
110 "trips to the restroom due to my excessive drinking";
111
112 BOOST_CHECK(
113 io::test_input_filter( io::example::dictionary_stdio_filter(d),
114 input, output )
115 );
116
117 BOOST_CHECK(
118 io::test_output_filter( io::example::dictionary_stdio_filter(d),
119 input, output )
120 );
121
122 BOOST_CHECK(
123 io::test_input_filter( io::example::dictionary_input_filter(d),
124 input, output )
125 );
126
127 BOOST_CHECK(
128 io::test_output_filter( io::example::dictionary_output_filter(d),
129 input, output )
130 );
131}
132
133//------------------line_wrapping_filter test---------------------------------//
134
135void line_wrapping_filter_test()
136{
137 using namespace std;
138
139 const std::string input =
140 "I had a message on my answerphone from the bloke at the car \n"
141 "dealership that the windscreen and tyre on my lorry were replaced. \n"
142 "However, the gearbox would not be ready until tomorrow since the \n"
143 "spanner that they needed was broken and they had to go to the \n"
144 "ironmonger to buy a replacement. Since my lorry was not ready, \n"
145 "I decided to take the bus downtown and buy a new waistcoat. I \n"
146 "also stopped at the liquor store to buy some wine. I came home \n"
147 "and watched the telly and drank my wine. I also worked on a \n"
148 "crossword puzzle. Fortunately I had a pencil with a new rubber. \n"
149 "During that evening I made frequent trips to the loo due to my \n"
150 "excessive drinking.";
151
152 const std::string output =
153 "I had a message on my answerphone from t\n"
154 "he bloke at the car \n"
155 "dealership that the windscreen and tyre \n"
156 "on my lorry were replaced. \n"
157 "However, the gearbox would not be ready \n"
158 "until tomorrow since the \n"
159 "spanner that they needed was broken and \n"
160 "they had to go to the \n"
161 "ironmonger to buy a replacement. Since m\n"
162 "y lorry was not ready, \n"
163 "I decided to take the bus downtown and b\n"
164 "uy a new waistcoat. I \n"
165 "also stopped at the liquor store to buy \n"
166 "some wine. I came home \n"
167 "and watched the telly and drank my wine.\n"
168 " I also worked on a \n"
169 "crossword puzzle. Fortunately I had a pe\n"
170 "ncil with a new rubber. \n"
171 "During that evening I made frequent trip\n"
172 "s to the loo due to my \n"
173 "excessive drinking.";
174
175 BOOST_CHECK(
176 io::test_input_filter( io::example::line_wrapping_stdio_filter(40),
177 input, output )
178 );
179
180 BOOST_CHECK(
181 io::test_output_filter( io::example::line_wrapping_stdio_filter(40),
182 input, output )
183 );
184
185 BOOST_CHECK(
186 io::test_input_filter( io::example::line_wrapping_input_filter(40),
187 input, output )
188 );
189
190 BOOST_CHECK(
191 io::test_output_filter( io::example::line_wrapping_output_filter(40),
192 input, output )
193 );
194}
195
196//------------------shell_comments_filter test--------------------------------//
197
198void shell_comments_filter_test()
199{
200 using namespace std;
201
202 const std::string input = // From <libs/filesystem/build/Jamfile>.
203 "lib boost_filesystem\n"
204 " : ../src/$(SOURCES).cpp\n"
205 " : # build requirements\n"
206 " [ common-names ] # magic for install and auto-link features\n"
207 " <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
208 " <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
209 " : debug release # build variants\n"
210 " ;\n"
211 "\n"
212 "dll boost_filesystem\n"
213 " : ../src/$(SOURCES).cpp\n"
214 " : # build requirements\n"
215 " [ common-names ] # magic for install and auto-link features\n"
216 " <define>BOOST_FILESYSTEM_DYN_LINK=1 # tell source we're building dll's\n"
217 " <runtime-link>dynamic # build only for dynamic runtimes\n"
218 " <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
219 " <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
220 " : debug release # build variants\n"
221 " ;";
222
223 const std::string output =
224 "lib boost_filesystem\n"
225 " : ../src/$(SOURCES).cpp\n"
226 " : \n"
227 " [ common-names ] \n"
228 " <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
229 " <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
230 " : debug release \n"
231 " ;\n"
232 "\n"
233 "dll boost_filesystem\n"
234 " : ../src/$(SOURCES).cpp\n"
235 " : \n"
236 " [ common-names ] \n"
237 " <define>BOOST_FILESYSTEM_DYN_LINK=1 \n"
238 " <runtime-link>dynamic \n"
239 " <include>$(BOOST_ROOT) <sysinclude>$(BOOST_ROOT)\n"
240 " <no-warn>exception.cpp <no-warn>operations_posix_windows.cpp\n"
241 " : debug release \n"
242 " ;";
243
244 BOOST_CHECK(
245 io::test_input_filter( io::example::shell_comments_stdio_filter(),
246 input, output )
247 );
248
249 BOOST_CHECK(
250 io::test_output_filter( io::example::shell_comments_stdio_filter(),
251 input, output )
252 );
253
254 BOOST_CHECK(
255 io::test_input_filter( io::example::shell_comments_input_filter(),
256 input, output )
257 );
258
259 BOOST_CHECK(
260 io::test_output_filter( io::example::shell_comments_output_filter(),
261 input, output )
262 );
263
264 BOOST_CHECK(
265 io::test_input_filter( io::example::shell_comments_dual_use_filter(),
266 input, output )
267 );
268
269 BOOST_CHECK(
270 io::test_output_filter( io::example::shell_comments_dual_use_filter(),
271 input, output )
272 );
273
274 BOOST_CHECK(
275 io::test_input_filter( io::example::shell_comments_multichar_input_filter(),
276 input, output )
277 );
278
279 BOOST_CHECK(
280 io::test_output_filter( io::example::shell_comments_multichar_output_filter(),
281 input, output )
282 );
283}
284
285//------------------tab_expanding_filter test---------------------------------//
286
287void tab_expanding_filter_test()
288{
289 using namespace std;
290
291 const std::string input =
292 "class tab_expanding_stdio_filter : public stdio_filter {\n"
293 "public:\n"
294 "\texplicit tab_expanding_stdio_filter(int\ttab_size = 8)\n"
295 "\t\t: tab_size_(tab_size), col_no_(0)\n"
296 "\t{\n"
297 "\t\tassert(tab_size\t> 0);\n"
298 "\t}\n"
299 "private:\n"
300 "\tvoid do_filter()\n"
301 "\t{\n"
302 "\t\tint\tc;\n"
303 "\t\twhile ((c = std::cin.get()) != EOF) {\n"
304 "\t\t\tif (c == '\\t') {\n"
305 "\t\t\t\tint\tspaces = tab_size_ - (col_no_ %\ttab_size_);\n"
306 "\t\t\t\tfor\t(; spaces >\t0; --spaces)\n"
307 "\t\t\t\t\tput_char(' ');\n"
308 "\t\t\t} else {\n"
309 "\t\t\t\tput_char(c);\n"
310 "\t\t\t}\n"
311 "\t\t}\n"
312 "\t}\n"
313 "\tvoid do_close()\t{ col_no_ =\t0; }\n"
314 "\tvoid put_char(int c)\n"
315 "\t{\n"
316 "\t\tstd::cout.put(c);\n"
317 "\t\tif (c == '\\n') {\n"
318 "\t\t\tcol_no_\t= 0;\n"
319 "\t\t} else {\n"
320 "\t\t\t++col_no_;\n"
321 "\t\t}\n"
322 "\t}\n"
323 "\tint\t tab_size_;\n"
324 "\tint\t col_no_;\n"
325 "};";
326
327 const std::string output =
328 "class tab_expanding_stdio_filter : public stdio_filter {\n"
329 "public:\n"
330 " explicit tab_expanding_stdio_filter(int tab_size = 8)\n"
331 " : tab_size_(tab_size), col_no_(0)\n"
332 " {\n"
333 " assert(tab_size > 0);\n"
334 " }\n"
335 "private:\n"
336 " void do_filter()\n"
337 " {\n"
338 " int c;\n"
339 " while ((c = std::cin.get()) != EOF) {\n"
340 " if (c == '\\t') {\n"
341 " int spaces = tab_size_ - (col_no_ % tab_size_);\n"
342 " for (; spaces > 0; --spaces)\n"
343 " put_char(' ');\n"
344 " } else {\n"
345 " put_char(c);\n"
346 " }\n"
347 " }\n"
348 " }\n"
349 " void do_close() { col_no_ = 0; }\n"
350 " void put_char(int c)\n"
351 " {\n"
352 " std::cout.put(c);\n"
353 " if (c == '\\n') {\n"
354 " col_no_ = 0;\n"
355 " } else {\n"
356 " ++col_no_;\n"
357 " }\n"
358 " }\n"
359 " int tab_size_;\n"
360 " int col_no_;\n"
361 "};";
362
363 BOOST_CHECK(
364 io::test_input_filter( io::example::tab_expanding_stdio_filter(4),
365 input, output )
366 );
367
368 BOOST_CHECK(
369 io::test_output_filter( io::example::tab_expanding_stdio_filter(4),
370 input, output )
371 );
372
373 BOOST_CHECK(
374 io::test_input_filter( io::example::tab_expanding_input_filter(4),
375 input, output )
376 );
377
378 BOOST_CHECK(
379 io::test_output_filter( io::example::tab_expanding_output_filter(4),
380 input, output )
381 );
382}
383
384//------------------unix2dos_filter test--------------------------------------//
385
386void unix2dos_filter_test()
387{
388 using namespace std;
389
390 const std::string input =
391 "When I was one-and-twenty\n"
392 "I heard a wise man say,\n"
393 "'Give crowns and pounds and guineas\n"
394 "But not your heart away;\n"
395 "\n"
396 "Give pearls away and rubies\n"
397 "But keep your fancy free.'\n"
398 "But I was one-and-twenty,\n"
399 "No use to talk to me.\n"
400 "\n"
401 "When I was one-and-twenty\n"
402 "I heard him say again,\n"
403 "'The heart out of the bosom\n"
404 "Was never given in vain;\n"
405 "'Tis paid with sighs a plenty\n"
406 "And sold for endless rue.'\n"
407 "And I am two-and-twenty,\n"
408 "And oh, 'tis true, 'tis true.";
409
410 const std::string output =
411 "When I was one-and-twenty\r\n"
412 "I heard a wise man say,\r\n"
413 "'Give crowns and pounds and guineas\r\n"
414 "But not your heart away;\r\n"
415 "\r\n"
416 "Give pearls away and rubies\r\n"
417 "But keep your fancy free.'\r\n"
418 "But I was one-and-twenty,\r\n"
419 "No use to talk to me.\r\n"
420 "\r\n"
421 "When I was one-and-twenty\r\n"
422 "I heard him say again,\r\n"
423 "'The heart out of the bosom\r\n"
424 "Was never given in vain;\r\n"
425 "'Tis paid with sighs a plenty\r\n"
426 "And sold for endless rue.'\r\n"
427 "And I am two-and-twenty,\r\n"
428 "And oh, 'tis true, 'tis true.";
429
430 BOOST_CHECK(
431 io::test_input_filter( io::example::unix2dos_stdio_filter(),
432 input, output )
433 );
434
435 BOOST_CHECK(
436 io::test_output_filter( io::example::unix2dos_stdio_filter(),
437 input, output )
438 );
439
440 BOOST_CHECK(
441 io::test_input_filter( io::example::unix2dos_input_filter(),
442 input, output )
443 );
444
445 BOOST_CHECK(
446 io::test_output_filter( io::example::unix2dos_output_filter(),
447 input, output )
448 );
449}
450
451test_suite* init_unit_test_suite(int, char* [])
452{
453 test_suite* test = BOOST_TEST_SUITE("example test");
454 test->add(BOOST_TEST_CASE(&container_device_test));
455 test->add(BOOST_TEST_CASE(&dictionary_filter_test));
456 test->add(BOOST_TEST_CASE(&tab_expanding_filter_test));
457 test->add(BOOST_TEST_CASE(&line_wrapping_filter_test));
458 test->add(BOOST_TEST_CASE(&shell_comments_filter_test));
459 test->add(BOOST_TEST_CASE(&unix2dos_filter_test));
460 return test;
461}
462

source code of boost/libs/iostreams/test/example_test.cpp