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 | |
---|
8 | #ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED |
---|
9 | #define BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED |
---|
10 | |
---|
11 | #include <exception> |
---|
12 | #include <string> |
---|
13 | #include <string.h> |
---|
14 | #include <fstream> |
---|
15 | #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES |
---|
16 | # include <istream> |
---|
17 | # include <ostream> |
---|
18 | #else |
---|
19 | # include <iostream.h> |
---|
20 | #endif |
---|
21 | |
---|
22 | #include <boost/config.hpp> |
---|
23 | #include <boost/detail/workaround.hpp> |
---|
24 | #include <boost/iostreams/detail/char_traits.hpp> |
---|
25 | #include <boost/iostreams/detail/config/wide_streams.hpp> |
---|
26 | #include "./constants.hpp" |
---|
27 | |
---|
28 | // Code generation bugs cause tests to fail with global optimization. |
---|
29 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
30 | # pragma optimize("g", off) |
---|
31 | #endif |
---|
32 | |
---|
33 | // Included only by tests; no need to #undef. |
---|
34 | #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES |
---|
35 | # define BOOST_TEMPLATE_DECL template<typename Ch, typename Tr> |
---|
36 | # define BOOST_CHAR Ch |
---|
37 | # define BOOST_ISTREAM std::basic_istream<Ch, Tr> |
---|
38 | # define BOOST_OSTREAM std::basic_ostream<Ch, Tr> |
---|
39 | #else |
---|
40 | # define BOOST_TEMPLATE_DECL |
---|
41 | # define BOOST_CHAR char |
---|
42 | # define BOOST_ISTREAM std::istream |
---|
43 | # define BOOST_OSTREAM std::ostream |
---|
44 | #endif |
---|
45 | |
---|
46 | namespace boost { namespace iostreams { namespace test { |
---|
47 | |
---|
48 | BOOST_TEMPLATE_DECL |
---|
49 | bool compare_streams_in_chars(BOOST_ISTREAM& first, BOOST_ISTREAM& second) |
---|
50 | { |
---|
51 | for (int z = 0; z < 10; ++z) |
---|
52 | for (int w = 0; w < data_length(); ++w) |
---|
53 | if (first.eof() != second.eof() || first.get() != second.get()) |
---|
54 | return false; |
---|
55 | return true; |
---|
56 | } |
---|
57 | |
---|
58 | BOOST_TEMPLATE_DECL |
---|
59 | bool compare_streams_in_chunks(BOOST_ISTREAM& first, BOOST_ISTREAM& second) |
---|
60 | { |
---|
61 | int i = 0; |
---|
62 | do { |
---|
63 | BOOST_CHAR buf_one[chunk_size]; |
---|
64 | BOOST_CHAR buf_two[chunk_size]; |
---|
65 | first.read(buf_one, chunk_size); |
---|
66 | second.read(buf_two, chunk_size); |
---|
67 | std::streamsize amt = first.gcount(); |
---|
68 | if ( amt != static_cast<std::streamsize>(second.gcount()) || |
---|
69 | BOOST_IOSTREAMS_CHAR_TRAITS(BOOST_CHAR):: |
---|
70 | compare(buf_one, buf_two, amt) != 0 ) |
---|
71 | return false; |
---|
72 | ++i; |
---|
73 | } while (!first.eof()); |
---|
74 | return true; |
---|
75 | } |
---|
76 | |
---|
77 | bool compare_files(const std::string& first, const std::string& second) |
---|
78 | { |
---|
79 | using namespace std; |
---|
80 | ifstream one(first.c_str(), BOOST_IOS::in | BOOST_IOS::binary); |
---|
81 | ifstream two(second.c_str(), BOOST_IOS::in | BOOST_IOS::binary); |
---|
82 | return compare_streams_in_chunks(one, two); |
---|
83 | } |
---|
84 | |
---|
85 | #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES |
---|
86 | template<typename Container, typename Ch, typename Tr> |
---|
87 | #else |
---|
88 | template<typename Container> |
---|
89 | #endif |
---|
90 | bool compare_container_and_stream(Container& cnt, BOOST_ISTREAM& is) |
---|
91 | { |
---|
92 | typename Container::iterator first = cnt.begin(); |
---|
93 | typename Container::iterator last = cnt.end(); |
---|
94 | do { |
---|
95 | if ((first == last) != is.eof()) return false; |
---|
96 | if (first != last && *first++ != is.get()) return false; |
---|
97 | } while (first != last); |
---|
98 | return true; |
---|
99 | } |
---|
100 | |
---|
101 | BOOST_TEMPLATE_DECL |
---|
102 | void write_data_in_chars(BOOST_OSTREAM& os) |
---|
103 | { |
---|
104 | for (int z = 0; z < data_reps; ++z) |
---|
105 | for (int w = 0; w < data_length(); ++w) |
---|
106 | os.put(detail::data((BOOST_CHAR*)0)[w]); |
---|
107 | os.flush(); |
---|
108 | } |
---|
109 | |
---|
110 | BOOST_TEMPLATE_DECL |
---|
111 | void write_data_in_chunks(BOOST_OSTREAM& os) |
---|
112 | { |
---|
113 | const BOOST_CHAR* buf = detail::data((BOOST_CHAR*)0); |
---|
114 | for (int z = 0; z < data_reps; ++z) |
---|
115 | os.write(buf, data_length()); |
---|
116 | os.flush(); |
---|
117 | } |
---|
118 | |
---|
119 | bool test_seekable_in_chars(std::iostream& io) |
---|
120 | { |
---|
121 | int i; // old 'for' scope workaround. |
---|
122 | |
---|
123 | // Test seeking with ios::cur |
---|
124 | for (i = 0; i < data_reps; ++i) { |
---|
125 | int j; |
---|
126 | for (j = 0; j < chunk_size; ++j) |
---|
127 | io.put(narrow_data()[j]); |
---|
128 | io.seekp(-chunk_size, BOOST_IOS::cur); |
---|
129 | for (j = 0; j < chunk_size; ++j) |
---|
130 | if (io.get() != narrow_data()[j]) |
---|
131 | return false; |
---|
132 | io.seekp(-chunk_size, BOOST_IOS::cur); |
---|
133 | for (j = 0; j < chunk_size; ++j) |
---|
134 | io.put(narrow_data()[j]); |
---|
135 | } |
---|
136 | |
---|
137 | // Test seeking with ios::beg |
---|
138 | std::streamoff off = 0; |
---|
139 | io.seekp(0, BOOST_IOS::beg); |
---|
140 | for (i = 0; i < data_reps; ++i, off+= chunk_size) { |
---|
141 | int j; |
---|
142 | for (j = 0; j < chunk_size; ++j) |
---|
143 | io.put(narrow_data()[j]); |
---|
144 | io.seekp(off, BOOST_IOS::beg); |
---|
145 | for (j = 0; j < chunk_size; ++j) |
---|
146 | if (io.get() != narrow_data()[j]) |
---|
147 | return false; |
---|
148 | io.seekp(off, BOOST_IOS::beg); |
---|
149 | for (j = 0; j < chunk_size; ++j) |
---|
150 | io.put(narrow_data()[j]); |
---|
151 | } |
---|
152 | |
---|
153 | // Test seeking with ios::end |
---|
154 | io.seekp(0, BOOST_IOS::end); |
---|
155 | off = io.tellp(); |
---|
156 | io.seekp(-off, BOOST_IOS::end); |
---|
157 | for (i = 0; i < data_reps; ++i, off -= chunk_size) { |
---|
158 | int j; |
---|
159 | for (j = 0; j < chunk_size; ++j) |
---|
160 | io.put(narrow_data()[j]); |
---|
161 | io.seekp(-off, BOOST_IOS::end); |
---|
162 | for (j = 0; j < chunk_size; ++j) |
---|
163 | if (io.get() != narrow_data()[j]) |
---|
164 | return false; |
---|
165 | io.seekp(-off, BOOST_IOS::end); |
---|
166 | for (j = 0; j < chunk_size; ++j) |
---|
167 | io.put(narrow_data()[j]); |
---|
168 | } |
---|
169 | return true; |
---|
170 | } |
---|
171 | |
---|
172 | bool test_seekable_in_chunks(std::iostream& io) |
---|
173 | { |
---|
174 | int i; // old 'for' scope workaround. |
---|
175 | |
---|
176 | // Test seeking with ios::cu |
---|
177 | for (i = 0; i < data_reps; ++i) { |
---|
178 | io.write(narrow_data(), chunk_size); |
---|
179 | io.seekp(-chunk_size, BOOST_IOS::cur); |
---|
180 | char buf[chunk_size]; |
---|
181 | io.read(buf, chunk_size); |
---|
182 | if (strncmp(buf, narrow_data(), chunk_size) != 0) |
---|
183 | return false; |
---|
184 | io.seekp(-chunk_size, BOOST_IOS::cur); |
---|
185 | io.write(narrow_data(), chunk_size); |
---|
186 | } |
---|
187 | |
---|
188 | // Test seeking with ios::beg |
---|
189 | std::streamoff off = 0; |
---|
190 | io.seekp(0, BOOST_IOS::beg); |
---|
191 | for (i = 0; i < data_reps; ++i) { |
---|
192 | io.write(narrow_data(), chunk_size); |
---|
193 | io.seekp(off, BOOST_IOS::beg); |
---|
194 | char buf[chunk_size]; |
---|
195 | io.read(buf, chunk_size); |
---|
196 | if (strncmp(buf, narrow_data(), chunk_size) != 0) |
---|
197 | return false; |
---|
198 | io.seekp(off, BOOST_IOS::beg); |
---|
199 | io.write(narrow_data(), chunk_size); |
---|
200 | } |
---|
201 | |
---|
202 | // Test seeking with ios::end |
---|
203 | io.seekp(0, BOOST_IOS::end); |
---|
204 | off = io.tellp(); |
---|
205 | io.seekp(-off, BOOST_IOS::end); |
---|
206 | for (i = 0; i < data_reps; ++i) { |
---|
207 | io.write(narrow_data(), chunk_size); |
---|
208 | io.seekp(-off, BOOST_IOS::end); |
---|
209 | char buf[chunk_size]; |
---|
210 | io.read(buf, chunk_size); |
---|
211 | if (strncmp(buf, narrow_data(), chunk_size) != 0) |
---|
212 | return false; |
---|
213 | io.seekp(-off, BOOST_IOS::end); |
---|
214 | io.write(narrow_data(), chunk_size); |
---|
215 | } |
---|
216 | return true; |
---|
217 | } |
---|
218 | |
---|
219 | } } } // End namespaces test, iostreams, boost. |
---|
220 | |
---|
221 | #if BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
---|
222 | # pragma optimize("", on) |
---|
223 | #endif |
---|
224 | |
---|
225 | #endif // #ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED |
---|