verification.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // See http://www.boost.org/libs/iostreams for documentation.
  6. #ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED
  8. #include <iostream>
  9. #include <exception>
  10. #include <string>
  11. #include <string.h>
  12. #include <fstream>
  13. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  14. # include <istream>
  15. # include <ostream>
  16. #else
  17. # include <iostream.h>
  18. #endif
  19. #include <boost/config.hpp>
  20. #include <boost/detail/workaround.hpp>
  21. #include <boost/iostreams/detail/char_traits.hpp>
  22. #include <boost/iostreams/detail/config/wide_streams.hpp>
  23. #include "./constants.hpp"
  24. // Must come last.
  25. #include <boost/iostreams/detail/config/disable_warnings.hpp>
  26. // Code generation bugs cause tests to fail with global optimization.
  27. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  28. # pragma optimize("g", off)
  29. #endif
  30. // Included only by tests; no need to #undef.
  31. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  32. # define BOOST_TEMPLATE_DECL template<typename Ch, typename Tr>
  33. # define BOOST_CHAR Ch
  34. # define BOOST_ISTREAM std::basic_istream<Ch, Tr>
  35. # define BOOST_OSTREAM std::basic_ostream<Ch, Tr>
  36. #else
  37. # define BOOST_TEMPLATE_DECL
  38. # define BOOST_CHAR char
  39. # define BOOST_ISTREAM std::istream
  40. # define BOOST_OSTREAM std::ostream
  41. #endif
  42. namespace boost { namespace iostreams { namespace test {
  43. BOOST_TEMPLATE_DECL
  44. bool compare_streams_in_chars(BOOST_ISTREAM& first, BOOST_ISTREAM& second)
  45. {
  46. for (int z = 0; z < data_reps; ++z)
  47. for (int w = 0; w < data_length(); ++w)
  48. if (first.eof() != second.eof() || first.get() != second.get())
  49. return false;
  50. return true;
  51. }
  52. BOOST_TEMPLATE_DECL
  53. bool compare_streams_in_chunks(BOOST_ISTREAM& first, BOOST_ISTREAM& second)
  54. {
  55. int i = 0;
  56. do {
  57. BOOST_CHAR buf_one[chunk_size];
  58. BOOST_CHAR buf_two[chunk_size];
  59. first.read(buf_one, chunk_size);
  60. second.read(buf_two, chunk_size);
  61. std::streamsize amt = first.gcount();
  62. if ( amt != static_cast<std::streamsize>(second.gcount()) ||
  63. BOOST_IOSTREAMS_CHAR_TRAITS(BOOST_CHAR)::
  64. compare(buf_one, buf_two, static_cast<std::size_t>(amt)) != 0 )
  65. return false;
  66. ++i;
  67. } while (!first.eof());
  68. return true;
  69. }
  70. bool compare_files(const std::string& first, const std::string& second)
  71. {
  72. using namespace std;
  73. ifstream one(first.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  74. ifstream two(second.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  75. return compare_streams_in_chunks(one, two);
  76. }
  77. #ifndef BOOST_IOSTREAMS_NO_STREAM_TEMPLATES
  78. template<typename Container, typename Ch, typename Tr>
  79. #else
  80. template<typename Container>
  81. #endif
  82. bool compare_container_and_stream(Container& cnt, BOOST_ISTREAM& is)
  83. {
  84. typename Container::iterator first = cnt.begin();
  85. typename Container::iterator last = cnt.end();
  86. do {
  87. if ((first == last) != is.eof()) return false;
  88. if (first != last && *first++ != is.get()) return false;
  89. } while (first != last);
  90. return true;
  91. }
  92. template<typename Container>
  93. bool compare_container_and_file(Container& cnt, const std::string& file)
  94. {
  95. std::ifstream fstrm(file.c_str(), BOOST_IOS::in | BOOST_IOS::binary);
  96. return compare_container_and_stream(cnt, fstrm);
  97. }
  98. BOOST_TEMPLATE_DECL
  99. void write_data_in_chars(BOOST_OSTREAM& os)
  100. {
  101. for (int z = 0; z < data_reps; ++z)
  102. for (int w = 0; w < data_length(); ++w)
  103. os.put(detail::data((BOOST_CHAR*)0)[w]);
  104. os.flush();
  105. }
  106. BOOST_TEMPLATE_DECL
  107. void write_data_in_chunks(BOOST_OSTREAM& os)
  108. {
  109. const BOOST_CHAR* buf = detail::data((BOOST_CHAR*)0);
  110. for (int z = 0; z < data_reps; ++z)
  111. os.write(buf, data_length());
  112. os.flush();
  113. }
  114. bool test_seekable_in_chars(std::iostream& io)
  115. {
  116. int i; // old 'for' scope workaround.
  117. // Test seeking with ios::cur
  118. for (i = 0; i < data_reps; ++i) {
  119. int j;
  120. for (j = 0; j < chunk_size; ++j)
  121. io.put(narrow_data()[j]);
  122. io.seekp(-chunk_size, BOOST_IOS::cur);
  123. for (j = 0; j < chunk_size; ++j)
  124. if (io.get() != narrow_data()[j])
  125. return false;
  126. io.seekg(-chunk_size, BOOST_IOS::cur);
  127. for (j = 0; j < chunk_size; ++j)
  128. io.put(narrow_data()[j]);
  129. }
  130. // Test seeking with ios::beg
  131. std::streamoff off = 0;
  132. io.seekp(0, BOOST_IOS::beg);
  133. for (i = 0; i < data_reps; ++i, off += chunk_size) {
  134. int j;
  135. for (j = 0; j < chunk_size; ++j)
  136. io.put(narrow_data()[j]);
  137. io.seekp(off, BOOST_IOS::beg);
  138. for (j = 0; j < chunk_size; ++j)
  139. if (io.get() != narrow_data()[j])
  140. return false;
  141. io.seekg(off, BOOST_IOS::beg);
  142. for (j = 0; j < chunk_size; ++j)
  143. io.put(narrow_data()[j]);
  144. }
  145. // Test seeking with ios::end
  146. io.seekp(0, BOOST_IOS::end);
  147. off = io.tellp();
  148. io.seekp(-off, BOOST_IOS::end);
  149. for (i = 0; i < data_reps; ++i, off -= chunk_size) {
  150. int j;
  151. for (j = 0; j < chunk_size; ++j)
  152. io.put(narrow_data()[j]);
  153. io.seekp(-off, BOOST_IOS::end);
  154. for (j = 0; j < chunk_size; ++j)
  155. if (io.get() != narrow_data()[j])
  156. return false;
  157. io.seekg(-off, BOOST_IOS::end);
  158. for (j = 0; j < chunk_size; ++j)
  159. io.put(narrow_data()[j]);
  160. }
  161. return true;
  162. }
  163. bool test_seekable_in_chunks(std::iostream& io)
  164. {
  165. int i; // old 'for' scope workaround.
  166. // Test seeking with ios::cur
  167. for (i = 0; i < data_reps; ++i) {
  168. io.write(narrow_data(), chunk_size);
  169. io.seekp(-chunk_size, BOOST_IOS::cur);
  170. char buf[chunk_size];
  171. io.read(buf, chunk_size);
  172. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  173. return false;
  174. io.seekg(-chunk_size, BOOST_IOS::cur);
  175. io.write(narrow_data(), chunk_size);
  176. }
  177. // Test seeking with ios::beg
  178. std::streamoff off = 0;
  179. io.seekp(0, BOOST_IOS::beg);
  180. for (i = 0; i < data_reps; ++i, off += chunk_size) {
  181. io.write(narrow_data(), chunk_size);
  182. io.seekp(off, BOOST_IOS::beg);
  183. char buf[chunk_size];
  184. io.read(buf, chunk_size);
  185. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  186. return false;
  187. io.seekg(off, BOOST_IOS::beg);
  188. io.write(narrow_data(), chunk_size);
  189. }
  190. // Test seeking with ios::end
  191. io.seekp(0, BOOST_IOS::end);
  192. off = io.tellp();
  193. io.seekp(-off, BOOST_IOS::end);
  194. for (i = 0; i < data_reps; ++i, off -= chunk_size) {
  195. io.write(narrow_data(), chunk_size);
  196. io.seekp(-off, BOOST_IOS::end);
  197. char buf[chunk_size];
  198. io.read(buf, chunk_size);
  199. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  200. return false;
  201. io.seekg(-off, BOOST_IOS::end);
  202. io.write(narrow_data(), chunk_size);
  203. }
  204. return true;
  205. }
  206. bool test_input_seekable(std::istream& io)
  207. {
  208. int i; // old 'for' scope workaround.
  209. // Test seeking with ios::cur
  210. for (i = 0; i < data_reps; ++i) {
  211. for (int j = 0; j < chunk_size; ++j)
  212. if (io.get() != narrow_data()[j])
  213. return false;
  214. io.seekg(-chunk_size, BOOST_IOS::cur);
  215. char buf[chunk_size];
  216. io.read(buf, chunk_size);
  217. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  218. return false;
  219. }
  220. // Test seeking with ios::beg
  221. std::streamoff off = 0;
  222. io.seekg(0, BOOST_IOS::beg);
  223. for (i = 0; i < data_reps; ++i, off += chunk_size) {
  224. for (int j = 0; j < chunk_size; ++j)
  225. if (io.get() != narrow_data()[j])
  226. return false;
  227. io.seekg(off, BOOST_IOS::beg);
  228. char buf[chunk_size];
  229. io.read(buf, chunk_size);
  230. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  231. return false;
  232. }
  233. // Test seeking with ios::end
  234. io.seekg(0, BOOST_IOS::end);
  235. off = io.tellg();
  236. io.seekg(-off, BOOST_IOS::end);
  237. for (i = 0; i < data_reps; ++i, off -= chunk_size) {
  238. for (int j = 0; j < chunk_size; ++j)
  239. if (io.get() != narrow_data()[j])
  240. return false;
  241. io.seekg(-off, BOOST_IOS::end);
  242. char buf[chunk_size];
  243. io.read(buf, chunk_size);
  244. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  245. return false;
  246. }
  247. return true;
  248. }
  249. bool test_output_seekable(std::ostream& io)
  250. {
  251. int i; // old 'for' scope workaround.
  252. // Test seeking with ios::cur
  253. for (i = 0; i < data_reps; ++i) {
  254. for (int j = 0; j < chunk_size; ++j)
  255. io.put(narrow_data()[j]);
  256. io.seekp(-chunk_size, BOOST_IOS::cur);
  257. io.write(narrow_data(), chunk_size);
  258. }
  259. // Test seeking with ios::beg
  260. std::streamoff off = 0;
  261. io.seekp(0, BOOST_IOS::beg);
  262. for (i = 0; i < data_reps; ++i, off += chunk_size) {
  263. for (int j = 0; j < chunk_size; ++j)
  264. io.put(narrow_data()[j]);
  265. io.seekp(off, BOOST_IOS::beg);
  266. io.write(narrow_data(), chunk_size);
  267. }
  268. // Test seeking with ios::end
  269. io.seekp(0, BOOST_IOS::end);
  270. off = io.tellp();
  271. io.seekp(-off, BOOST_IOS::end);
  272. for (i = 0; i < data_reps; ++i, off -= chunk_size) {
  273. for (int j = 0; j < chunk_size; ++j)
  274. io.put(narrow_data()[j]);
  275. io.seekp(-off, BOOST_IOS::end);
  276. io.write(narrow_data(), chunk_size);
  277. }
  278. return true;
  279. }
  280. bool test_dual_seekable(std::iostream& io)
  281. {
  282. int i; // old 'for' scope workaround.
  283. // Test seeking with ios::cur
  284. for (i = 0; i < data_reps; ++i) {
  285. for (int j = 0; j < chunk_size; ++j)
  286. io.put(narrow_data()[j]);
  287. io.seekp(-chunk_size, BOOST_IOS::cur);
  288. for (int j = 0; j < chunk_size; ++j)
  289. if (io.get() != narrow_data()[j])
  290. return false;
  291. io.seekg(-chunk_size, BOOST_IOS::cur);
  292. io.write(narrow_data(), chunk_size);
  293. char buf[chunk_size];
  294. io.read(buf, chunk_size);
  295. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  296. return false;
  297. }
  298. // Test seeking with ios::beg
  299. std::streamoff off = 0;
  300. io.seekp(0, BOOST_IOS::beg);
  301. io.seekg(0, BOOST_IOS::beg);
  302. for (i = 0; i < data_reps; ++i, off += chunk_size) {
  303. for (int j = 0; j < chunk_size; ++j)
  304. io.put(narrow_data()[j]);
  305. io.seekp(off, BOOST_IOS::beg);
  306. for (int j = 0; j < chunk_size; ++j)
  307. if (io.get() != narrow_data()[j])
  308. return false;
  309. io.seekg(off, BOOST_IOS::beg);
  310. io.write(narrow_data(), chunk_size);
  311. char buf[chunk_size];
  312. io.read(buf, chunk_size);
  313. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  314. return false;
  315. }
  316. // Test seeking with ios::end
  317. io.seekp(0, BOOST_IOS::end);
  318. io.seekg(0, BOOST_IOS::end);
  319. off = io.tellp();
  320. io.seekp(-off, BOOST_IOS::end);
  321. io.seekg(-off, BOOST_IOS::end);
  322. for (i = 0; i < data_reps; ++i, off -= chunk_size) {
  323. for (int j = 0; j < chunk_size; ++j)
  324. io.put(narrow_data()[j]);
  325. io.seekp(-off, BOOST_IOS::end);
  326. for (int j = 0; j < chunk_size; ++j)
  327. if (io.get() != narrow_data()[j])
  328. return false;
  329. io.seekg(-off, BOOST_IOS::end);
  330. io.write(narrow_data(), chunk_size);
  331. char buf[chunk_size];
  332. io.read(buf, chunk_size);
  333. if (strncmp(buf, narrow_data(), chunk_size) != 0)
  334. return false;
  335. }
  336. return true;
  337. }
  338. } } } // End namespaces test, iostreams, boost.
  339. #include <boost/iostreams/detail/config/enable_warnings.hpp>
  340. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  341. # pragma optimize("", on)
  342. #endif
  343. #endif // #ifndef BOOST_IOSTREAMS_TEST_VERIFICATION_HPP_INCLUDED