1 | // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com) |
2 | // (C) Copyright 2004-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 <string> |
9 | #include <boost/iostreams/filter/test.hpp> |
10 | #include <boost/test/test_tools.hpp> |
11 | #include <boost/test/unit_test.hpp> |
12 | #include "detail/filters.hpp" |
13 | |
14 | using namespace boost::iostreams; |
15 | using namespace boost::iostreams::test; |
16 | using boost::unit_test::test_suite; |
17 | |
18 | const std::string lower = |
19 | "in addition to providing an abstract framework the " |
20 | "library provides a number of concrete filters, sources " |
21 | "and sinks which serve as example applications of the " |
22 | "library but are also useful in their own right. these " |
23 | "include components for accessing memory-mapped files, " |
24 | "for file access via operating system file descriptors, " |
25 | "for code conversion, for text filtering with regular " |
26 | "expressions, for line-ending conversion and for " |
27 | "compression and decompression in the zlib, gzip and " |
28 | "bzip2 formats." ; |
29 | |
30 | const std::string upper = |
31 | "IN ADDITION TO PROVIDING AN ABSTRACT FRAMEWORK THE " |
32 | "LIBRARY PROVIDES A NUMBER OF CONCRETE FILTERS, SOURCES " |
33 | "AND SINKS WHICH SERVE AS EXAMPLE APPLICATIONS OF THE " |
34 | "LIBRARY BUT ARE ALSO USEFUL IN THEIR OWN RIGHT. THESE " |
35 | "INCLUDE COMPONENTS FOR ACCESSING MEMORY-MAPPED FILES, " |
36 | "FOR FILE ACCESS VIA OPERATING SYSTEM FILE DESCRIPTORS, " |
37 | "FOR CODE CONVERSION, FOR TEXT FILTERING WITH REGULAR " |
38 | "EXPRESSIONS, FOR LINE-ENDING CONVERSION AND FOR " |
39 | "COMPRESSION AND DECOMPRESSION IN THE ZLIB, GZIP AND " |
40 | "BZIP2 FORMATS." ; |
41 | |
42 | struct toupper_dual_use_filter : public dual_use_filter { |
43 | template<typename Source> |
44 | int get(Source& s) |
45 | { |
46 | int c = boost::iostreams::get(s); |
47 | return c != EOF && c != WOULD_BLOCK ? |
48 | std::toupper(c: (unsigned char) c) : |
49 | c; |
50 | } |
51 | template<typename Sink> |
52 | bool put(Sink& s, char c) |
53 | { |
54 | return boost::iostreams::put( |
55 | s, (char) std::toupper(c: (unsigned char) c) |
56 | ); |
57 | } |
58 | }; |
59 | |
60 | struct tolower_dual_use_filter : public dual_use_filter { |
61 | template<typename Source> |
62 | int get(Source& s) |
63 | { |
64 | int c = boost::iostreams::get(s); |
65 | return c != EOF && c != WOULD_BLOCK ? |
66 | std::tolower(c: (unsigned char) c) : |
67 | c; |
68 | } |
69 | template<typename Sink> |
70 | bool put(Sink& s, char c) |
71 | { |
72 | return boost::iostreams::put( |
73 | s, (char) std::tolower(c: (unsigned char) c) |
74 | ); |
75 | } |
76 | }; |
77 | |
78 | void filter_test() |
79 | { |
80 | BOOST_CHECK(test_input_filter(toupper_filter(), lower, upper)); |
81 | BOOST_CHECK(test_input_filter(toupper_multichar_filter(), lower, upper)); |
82 | BOOST_CHECK(test_input_filter(toupper_dual_use_filter(), lower, upper)); |
83 | BOOST_CHECK(test_output_filter(tolower_filter(), upper, lower)); |
84 | BOOST_CHECK(test_output_filter(tolower_multichar_filter(), upper, lower)); |
85 | BOOST_CHECK(test_output_filter(tolower_dual_use_filter(), upper, lower)); |
86 | BOOST_CHECK(test_filter_pair(tolower_filter(), toupper_filter(), upper)); |
87 | BOOST_CHECK( |
88 | test_filter_pair( tolower_multichar_filter(), |
89 | toupper_multichar_filter(), upper ) |
90 | ); |
91 | } |
92 | |
93 | test_suite* init_unit_test_suite(int, char* []) |
94 | { |
95 | test_suite* test = BOOST_TEST_SUITE("filter test" ); |
96 | test->add(BOOST_TEST_CASE(&filter_test)); |
97 | return test; |
98 | } |
99 | |