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