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 | #ifndef BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED |
---|
8 | #define BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED |
---|
9 | |
---|
10 | #include <boost/iostreams/device/file.hpp> |
---|
11 | #include <boost/iostreams/filtering_stream.hpp> |
---|
12 | #include <boost/iostreams/putback.hpp> |
---|
13 | #include "detail/constants.hpp" |
---|
14 | #include "detail/temp_file.hpp" |
---|
15 | |
---|
16 | using boost::iostreams::test::chunk_size; |
---|
17 | |
---|
18 | bool putback_test_one(std::istream& is) |
---|
19 | { |
---|
20 | try { |
---|
21 | do { |
---|
22 | char buf[chunk_size]; |
---|
23 | is.read(buf, chunk_size); |
---|
24 | if (is.gcount() < static_cast<std::streamsize>(chunk_size)) |
---|
25 | break; |
---|
26 | is.putback('a'); |
---|
27 | if (is.get() != 'a') |
---|
28 | return false; |
---|
29 | } while (!is.eof()); |
---|
30 | return true; |
---|
31 | } catch (std::exception&) { return false; } |
---|
32 | } |
---|
33 | |
---|
34 | bool putback_test_two(std::istream& is) |
---|
35 | { |
---|
36 | try { |
---|
37 | do { |
---|
38 | char buf[chunk_size]; |
---|
39 | is.read(buf, chunk_size); |
---|
40 | if (is.gcount() < static_cast<std::streamsize>(chunk_size)) |
---|
41 | break; |
---|
42 | is.putback('a'); |
---|
43 | is.putback('b'); |
---|
44 | is.putback('c'); |
---|
45 | is.putback('d'); |
---|
46 | if ( is.get() != 'd' || is.get() != 'c' || |
---|
47 | is.get() != 'b' || is.get() != 'a' ) |
---|
48 | { |
---|
49 | return false; |
---|
50 | } |
---|
51 | } while (!is.eof()); |
---|
52 | return true; |
---|
53 | } catch (std::exception&) { return false; } |
---|
54 | } |
---|
55 | |
---|
56 | template<typename Source> |
---|
57 | bool putback_test_three(Source& src) |
---|
58 | { |
---|
59 | try { |
---|
60 | while (true) { |
---|
61 | char buf[chunk_size]; |
---|
62 | if (boost::iostreams::read(src, buf, chunk_size) < chunk_size) |
---|
63 | break; |
---|
64 | boost::iostreams::putback(src, 'a'); |
---|
65 | if (boost::iostreams::get(src) != 'a') |
---|
66 | return false; |
---|
67 | } |
---|
68 | return true; |
---|
69 | } catch (std::exception&) { return false; } |
---|
70 | } |
---|
71 | |
---|
72 | template<typename Source> |
---|
73 | bool putback_test_four(Source& src) |
---|
74 | { |
---|
75 | try { |
---|
76 | while (true) { |
---|
77 | char buf[chunk_size]; |
---|
78 | if (boost::iostreams::read(src, buf, chunk_size) < chunk_size) |
---|
79 | break; |
---|
80 | boost::iostreams::putback(src, 'a'); |
---|
81 | boost::iostreams::putback(src, 'b'); |
---|
82 | boost::iostreams::putback(src, 'c'); |
---|
83 | boost::iostreams::putback(src, 'd'); |
---|
84 | if ( boost::iostreams::get(src) != 'd' || |
---|
85 | boost::iostreams::get(src) != 'c' || |
---|
86 | boost::iostreams::get(src) != 'b' || |
---|
87 | boost::iostreams::get(src) != 'a' ) |
---|
88 | { |
---|
89 | return false; |
---|
90 | } |
---|
91 | } |
---|
92 | return true; |
---|
93 | } catch (std::exception&) { return false; } |
---|
94 | } |
---|
95 | |
---|
96 | void putback_test() |
---|
97 | { |
---|
98 | using namespace std; |
---|
99 | using namespace boost; |
---|
100 | using namespace boost::iostreams; |
---|
101 | using namespace boost::iostreams::test; |
---|
102 | |
---|
103 | test_file test; |
---|
104 | |
---|
105 | { |
---|
106 | filtering_istream is; |
---|
107 | is.set_device_buffer_size(0); |
---|
108 | is.push(file_source(test.name())); |
---|
109 | BOOST_CHECK_MESSAGE( |
---|
110 | putback_test_one(is), |
---|
111 | "failed putting back to unbuffered filtering_istream" |
---|
112 | ); |
---|
113 | } |
---|
114 | |
---|
115 | { |
---|
116 | filtering_istream is; |
---|
117 | is.set_pback_size(4); |
---|
118 | is.push(file_source(test.name())); |
---|
119 | BOOST_CHECK_MESSAGE( |
---|
120 | putback_test_two(is), |
---|
121 | "failed putting back to buffered filtering_istream" |
---|
122 | ); |
---|
123 | } |
---|
124 | |
---|
125 | { |
---|
126 | filtering_istream is; |
---|
127 | is.set_device_buffer_size(0); |
---|
128 | is.push(file_source(test.name())); |
---|
129 | BOOST_CHECK_MESSAGE( |
---|
130 | putback_test_three(is), |
---|
131 | "failed putting back to unbuffered filtering_istream" |
---|
132 | ); |
---|
133 | } |
---|
134 | |
---|
135 | { |
---|
136 | filtering_istream is; |
---|
137 | is.set_pback_size(4); |
---|
138 | is.push(file_source(test.name())); |
---|
139 | BOOST_CHECK_MESSAGE( |
---|
140 | putback_test_four(is), |
---|
141 | "failed putting back to buffered filtering_istream" |
---|
142 | ); |
---|
143 | } |
---|
144 | |
---|
145 | { |
---|
146 | filtering_istreambuf sb; |
---|
147 | sb.set_device_buffer_size(0); |
---|
148 | sb.push(file_source(test.name())); |
---|
149 | BOOST_CHECK_MESSAGE( |
---|
150 | putback_test_three(sb), |
---|
151 | "failed putting back to unbuffered filtering_istream" |
---|
152 | ); |
---|
153 | } |
---|
154 | |
---|
155 | { |
---|
156 | filtering_istreambuf sb; |
---|
157 | sb.set_pback_size(4); |
---|
158 | sb.push(file_source(test.name())); |
---|
159 | BOOST_CHECK_MESSAGE( |
---|
160 | putback_test_four(sb), |
---|
161 | "failed putting back to buffered filtering_istream" |
---|
162 | ); |
---|
163 | } |
---|
164 | } |
---|
165 | |
---|
166 | #endif // #ifndef BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED |
---|