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