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.)
6 // See http://www.boost.org/libs/iostreams for documentation.
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"
26 using boost::unit_test::test_suite
;
27 namespace io
= boost::iostreams
;
28 namespace ex
= boost::iostreams::example
;
30 //------------------container_device test-------------------------------------//
32 void container_device_test()
35 using namespace boost::iostreams::test
;
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
;
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
));
51 std::vector
<char> first
;
52 test_sequence
<> second
;
53 io::stream
<vector_sink
> out(first
);
54 write_data_in_chunks(out
);
55 BOOST_CHECK(first
== second
);
60 io::stream
<vector_device
> io(v
);
61 BOOST_CHECK(test_seekable_in_chunks(io
));
65 //------------------dictionary_filter test------------------------------------//
67 void dictionary_filter_test()
71 io::example::dictionary d
;
73 // See http://english2american.com.
74 d
.add("answerphone", "answering machine");
75 d
.add("bloke", "guy");
76 d
.add("gearbox", "transmission");
77 d
.add("ironmonger", "hardware shop");
78 d
.add("loo", "restroom");
79 d
.add("lorry", "truck");
80 d
.add("rubber", "eraser");
81 d
.add("spanner", "monkey wrench");
83 d
.add("tyre", "tire");
84 d
.add("waistcoat", "vest");
85 d
.add("windscreen", "windshield");
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 "
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";
113 io::test_input_filter( io::example::dictionary_stdio_filter(d
),
118 io::test_output_filter( io::example::dictionary_stdio_filter(d
),
123 io::test_input_filter( io::example::dictionary_input_filter(d
),
128 io::test_output_filter( io::example::dictionary_output_filter(d
),
133 //------------------line_wrapping_filter test---------------------------------//
135 void line_wrapping_filter_test()
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.";
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.";
176 io::test_input_filter( io::example::line_wrapping_stdio_filter(40),
181 io::test_output_filter( io::example::line_wrapping_stdio_filter(40),
186 io::test_input_filter( io::example::line_wrapping_input_filter(40),
191 io::test_output_filter( io::example::line_wrapping_output_filter(40),
196 //------------------shell_comments_filter test--------------------------------//
198 void shell_comments_filter_test()
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"
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"
223 const std::string output
=
224 "lib boost_filesystem\n"
225 " : ../src/$(SOURCES).cpp\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"
233 "dll boost_filesystem\n"
234 " : ../src/$(SOURCES).cpp\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"
245 io::test_input_filter( io::example::shell_comments_stdio_filter(),
250 io::test_output_filter( io::example::shell_comments_stdio_filter(),
255 io::test_input_filter( io::example::shell_comments_input_filter(),
260 io::test_output_filter( io::example::shell_comments_output_filter(),
265 io::test_input_filter( io::example::shell_comments_dual_use_filter(),
270 io::test_output_filter( io::example::shell_comments_dual_use_filter(),
275 io::test_input_filter( io::example::shell_comments_multichar_input_filter(),
280 io::test_output_filter( io::example::shell_comments_multichar_output_filter(),
285 //------------------tab_expanding_filter test---------------------------------//
287 void tab_expanding_filter_test()
291 const std::string input
=
292 "class tab_expanding_stdio_filter : public stdio_filter {\n"
294 "\texplicit tab_expanding_stdio_filter(int\ttab_size = 8)\n"
295 "\t\t: tab_size_(tab_size), col_no_(0)\n"
297 "\t\tassert(tab_size\t> 0);\n"
300 "\tvoid do_filter()\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"
309 "\t\t\t\tput_char(c);\n"
313 "\tvoid do_close()\t{ col_no_ =\t0; }\n"
314 "\tvoid put_char(int c)\n"
316 "\t\tstd::cout.put(c);\n"
317 "\t\tif (c == '\\n') {\n"
318 "\t\t\tcol_no_\t= 0;\n"
323 "\tint\t tab_size_;\n"
327 const std::string output
=
328 "class tab_expanding_stdio_filter : public stdio_filter {\n"
330 " explicit tab_expanding_stdio_filter(int tab_size = 8)\n"
331 " : tab_size_(tab_size), col_no_(0)\n"
333 " assert(tab_size > 0);\n"
336 " void do_filter()\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"
349 " void do_close() { col_no_ = 0; }\n"
350 " void put_char(int c)\n"
352 " std::cout.put(c);\n"
353 " if (c == '\\n') {\n"
364 io::test_input_filter( io::example::tab_expanding_stdio_filter(4),
369 io::test_output_filter( io::example::tab_expanding_stdio_filter(4),
374 io::test_input_filter( io::example::tab_expanding_input_filter(4),
379 io::test_output_filter( io::example::tab_expanding_output_filter(4),
384 //------------------unix2dos_filter test--------------------------------------//
386 void unix2dos_filter_test()
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"
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"
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.";
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"
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"
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.";
431 io::test_input_filter( io::example::unix2dos_stdio_filter(),
436 io::test_output_filter( io::example::unix2dos_stdio_filter(),
441 io::test_input_filter( io::example::unix2dos_input_filter(),
446 io::test_output_filter( io::example::unix2dos_output_filter(),
451 test_suite
* init_unit_test_suite(int, char* [])
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
));