| 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 <stdexcept>  // out_of_range. | 
|---|
| 8 | #include <typeinfo> | 
|---|
| 9 | #include <utility>    // pair. | 
|---|
| 10 | #include <boost/config.hpp>              // BOOST_MSVC. | 
|---|
| 11 | #include <boost/detail/workaround.hpp> | 
|---|
| 12 | #include <boost/iostreams/device/file.hpp> | 
|---|
| 13 | #include <boost/iostreams/filtering_stream.hpp> | 
|---|
| 14 | #include <boost/iostreams/stream.hpp> | 
|---|
| 15 | #include <boost/iostreams/stream_buffer.hpp> | 
|---|
| 16 | #include <boost/test/test_tools.hpp> | 
|---|
| 17 | #include <boost/test/unit_test.hpp> | 
|---|
| 18 | #include "detail/constants.hpp" | 
|---|
| 19 | #include "detail/filters.hpp" | 
|---|
| 20 | #include "detail/temp_file.hpp" | 
|---|
| 21 | #include "detail/verification.hpp" | 
|---|
| 22 |  | 
|---|
| 23 | namespace io = boost::iostreams; | 
|---|
| 24 | using boost::unit_test::test_suite; | 
|---|
| 25 |  | 
|---|
| 26 | #define COMPARE_TYPE_ID(x, y) BOOST_IOSTREAMS_COMPARE_TYPE_ID(x, y) | 
|---|
| 27 |  | 
|---|
| 28 | struct indirect_source : io::source { | 
|---|
| 29 |     void foo() { } | 
|---|
| 30 |     std::streamsize read(char*, std::streamsize) { return 0; } | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 | struct direct_source { | 
|---|
| 34 |     typedef char char_type; | 
|---|
| 35 |     struct category  | 
|---|
| 36 |         : io::input, io::device_tag, io::direct_tag  | 
|---|
| 37 |         { }; | 
|---|
| 38 |     void foo() { } | 
|---|
| 39 |     std::pair<char*, char*> input_sequence()  | 
|---|
| 40 |     {  | 
|---|
| 41 |         return std::pair<char*, char*>(0, 0); | 
|---|
| 42 |     } | 
|---|
| 43 | }; | 
|---|
| 44 |  | 
|---|
| 45 | void compile_time_test() | 
|---|
| 46 | { | 
|---|
| 47 |     using namespace io; | 
|---|
| 48 |  | 
|---|
| 49 |     stream_buffer<indirect_source> indirect_buf; | 
|---|
| 50 |     indirect_buf.open(indirect_source()); | 
|---|
| 51 |     indirect_buf->foo(); | 
|---|
| 52 |  | 
|---|
| 53 |     stream_buffer<direct_source> direct_buf; | 
|---|
| 54 |     direct_buf.open(direct_source()); | 
|---|
| 55 |     direct_buf->foo(); | 
|---|
| 56 |  | 
|---|
| 57 |     stream<indirect_source> indirect_stream; | 
|---|
| 58 |     indirect_stream.open(indirect_source()); | 
|---|
| 59 |     indirect_stream->foo(); | 
|---|
| 60 |  | 
|---|
| 61 |     stream<direct_source> direct_stream; | 
|---|
| 62 |     direct_stream.open(direct_source()); | 
|---|
| 63 |     direct_stream->foo(); | 
|---|
| 64 | } | 
|---|
| 65 |  | 
|---|
| 66 | void component_type_test() | 
|---|
| 67 | { | 
|---|
| 68 |     using namespace std; | 
|---|
| 69 |     using namespace io; | 
|---|
| 70 |     using namespace boost::iostreams::test; | 
|---|
| 71 |  | 
|---|
| 72 |     temp_file       dest; | 
|---|
| 73 |     lowercase_file  lower; | 
|---|
| 74 |  | 
|---|
| 75 |     filtering_ostream out; | 
|---|
| 76 |     out.push(tolower_filter()); | 
|---|
| 77 |     out.push(tolower_multichar_filter()); | 
|---|
| 78 |     out.push(file_sink(dest.name(), out_mode)); | 
|---|
| 79 |  | 
|---|
| 80 |     // Check index 0. | 
|---|
| 81 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 82 |         out.component_type(0), | 
|---|
| 83 |         typeid(tolower_filter) | 
|---|
| 84 |     )); | 
|---|
| 85 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 86 |         BOOST_IOSTREAMS_COMPONENT_TYPE(out, 0),  | 
|---|
| 87 |         typeid(tolower_filter) | 
|---|
| 88 |     )); | 
|---|
| 89 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 90 |         BOOST_IOSTREAMS_COMPONENT(out, 0, tolower_filter) | 
|---|
| 91 |     )); | 
|---|
| 92 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) | 
|---|
| 93 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 94 |         out.component<tolower_filter>(0) | 
|---|
| 95 |     )); | 
|---|
| 96 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 97 |         out.component<0, tolower_filter>() | 
|---|
| 98 |     )); | 
|---|
| 99 | #endif | 
|---|
| 100 |  | 
|---|
| 101 |     // Check index 1. | 
|---|
| 102 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 103 |         out.component_type(1), | 
|---|
| 104 |         typeid(tolower_multichar_filter) | 
|---|
| 105 |     )); | 
|---|
| 106 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 107 |         BOOST_IOSTREAMS_COMPONENT_TYPE(out, 1),  | 
|---|
| 108 |         typeid(tolower_multichar_filter) | 
|---|
| 109 |     )); | 
|---|
| 110 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 111 |         BOOST_IOSTREAMS_COMPONENT(out, 1, tolower_multichar_filter) | 
|---|
| 112 |     )); | 
|---|
| 113 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) | 
|---|
| 114 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 115 |         out.component<tolower_multichar_filter>(1) | 
|---|
| 116 |     )); | 
|---|
| 117 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 118 |         out.component<1, tolower_multichar_filter>() | 
|---|
| 119 |     )); | 
|---|
| 120 | #endif | 
|---|
| 121 |  | 
|---|
| 122 |     // Check index 2. | 
|---|
| 123 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 124 |         out.component_type(2), | 
|---|
| 125 |         typeid(file_sink) | 
|---|
| 126 |     )); | 
|---|
| 127 |     BOOST_CHECK(COMPARE_TYPE_ID( | 
|---|
| 128 |         BOOST_IOSTREAMS_COMPONENT_TYPE(out, 2),  | 
|---|
| 129 |         typeid(file_sink) | 
|---|
| 130 |     )); | 
|---|
| 131 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 132 |         BOOST_IOSTREAMS_COMPONENT(out, 2, file_sink) | 
|---|
| 133 |     )); | 
|---|
| 134 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1310) | 
|---|
| 135 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 136 |         out.component<file_sink>(2) | 
|---|
| 137 |     )); | 
|---|
| 138 |     BOOST_CHECK_NO_THROW(( | 
|---|
| 139 |         out.component<2, file_sink>() | 
|---|
| 140 |     )); | 
|---|
| 141 | #endif | 
|---|
| 142 |  | 
|---|
| 143 |     // Check index 3. | 
|---|
| 144 |     BOOST_CHECK_THROW( | 
|---|
| 145 |         out.component_type(3), | 
|---|
| 146 |         std::out_of_range | 
|---|
| 147 |     ); | 
|---|
| 148 |     BOOST_CHECK_THROW( | 
|---|
| 149 |         BOOST_IOSTREAMS_COMPONENT_TYPE(out, 3), | 
|---|
| 150 |         std::out_of_range | 
|---|
| 151 |     ); | 
|---|
| 152 |  | 
|---|
| 153 |     // Check components. | 
|---|
| 154 |  | 
|---|
| 155 |     filtering_ostream out2; | 
|---|
| 156 |     out2.push(*(BOOST_IOSTREAMS_COMPONENT(out, 0, tolower_filter))); | 
|---|
| 157 |     out2.push(*(BOOST_IOSTREAMS_COMPONENT(out, 1, tolower_multichar_filter))); | 
|---|
| 158 |     out2.push(*(BOOST_IOSTREAMS_COMPONENT(out, 2, file_sink))); | 
|---|
| 159 |     write_data_in_chunks(out); | 
|---|
| 160 |     out.reset(); | 
|---|
| 161 |     BOOST_CHECK_MESSAGE( | 
|---|
| 162 |         compare_files(dest.name(), lower.name()), | 
|---|
| 163 |         "failed accessing components of chain" | 
|---|
| 164 |     ); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | test_suite* init_unit_test_suite(int, char* [])  | 
|---|
| 168 | { | 
|---|
| 169 |     test_suite* test = BOOST_TEST_SUITE("component_type test"); | 
|---|
| 170 |     test->add(BOOST_TEST_CASE(&component_type_test)); | 
|---|
| 171 |     return test; | 
|---|
| 172 | } | 
|---|