regex_filter_test.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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. #include <fstream>
  7. #include <boost/iostreams/device/file.hpp>
  8. #include <boost/iostreams/filtering_stream.hpp>
  9. #include <boost/iostreams/filter/regex.hpp>
  10. #include <boost/test/test_tools.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include "detail/temp_file.hpp"
  13. #include "detail/verification.hpp"
  14. using namespace std;
  15. using namespace boost::iostreams;
  16. using namespace boost::iostreams::test;
  17. using boost::unit_test::test_suite;
  18. struct replace_lower {
  19. std::string operator() (const boost::match_results<const char*>&)
  20. { return "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  21. #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
  22. std::wstring operator() (const boost::match_results<const wchar_t*>&)
  23. { return L"ABCDEFGHIJKLMNOPQRSTUVWXYZ"; }
  24. #endif
  25. };
  26. void regex_filter_test()
  27. {
  28. // Note: Given the basic stream and filter tests, two regex tests
  29. // are probably sufficient: reading with a filter based on a function,
  30. // and writing with a filter based on a format string.
  31. test_file test;
  32. uppercase_file upper;
  33. boost::regex match_lower("[a-z]+");
  34. std::string fmt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  35. {
  36. // Note: the ifstream second is placed in a nested scope because
  37. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  38. // Test reading from a regex filter based on a function in chars.
  39. filtering_istream
  40. first(boost::iostreams::regex_filter(match_lower, replace_lower()));
  41. first.push(file_source(test.name(), in_mode));
  42. {
  43. ifstream second(upper.name().c_str(), in_mode);
  44. BOOST_CHECK_MESSAGE(
  45. compare_streams_in_chars(first, second),
  46. "failed reading from function-based regex_filter in chars"
  47. );
  48. }
  49. first.pop();
  50. // Test reading from a regex filter based on a function in chunks.
  51. // (Also tests reusing the regex filter.)
  52. first.push(file_source(test.name(), in_mode));
  53. {
  54. ifstream second(upper.name().c_str(), in_mode);
  55. BOOST_CHECK_MESSAGE(
  56. compare_streams_in_chunks(first, second),
  57. "failed reading from function-based regex_filter in chunks"
  58. );
  59. }
  60. }
  61. {
  62. // Note: the ifstream second is placed in a nested scope because
  63. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  64. // Test reading from a regex filter based on a format string in chars.
  65. filtering_istream
  66. first(boost::iostreams::regex_filter(match_lower, fmt));
  67. first.push(file_source(test.name(), in_mode));
  68. {
  69. ifstream second(upper.name().c_str(), in_mode);
  70. BOOST_CHECK_MESSAGE(
  71. compare_streams_in_chars(first, second),
  72. "failed reading from format-string-based regex_filter in chars"
  73. );
  74. }
  75. first.pop();
  76. // Test reading from a regex filter based on a format string in chunks.
  77. // (Also tests reusing the regex filter.)
  78. first.push(file_source(test.name(), in_mode));
  79. {
  80. ifstream second(upper.name().c_str(), in_mode);
  81. BOOST_CHECK_MESSAGE(
  82. compare_streams_in_chars(first, second),
  83. "failed reading from format-string-based regex_filter in chunks"
  84. );
  85. }
  86. }
  87. {
  88. test_file dest1;
  89. test_file dest2;
  90. // Test writing to a regex filter based on a function in chars.
  91. filtering_ostream
  92. out(boost::iostreams::regex_filter(match_lower, replace_lower()));
  93. out.push(file_sink(dest1.name(), out_mode));
  94. write_data_in_chars(out);
  95. out.pop();
  96. BOOST_CHECK_MESSAGE(
  97. compare_files(dest1.name(), upper.name()),
  98. "failed writing to function-based regex_filter in chars"
  99. );
  100. // Test writing to a regex filter based on a function in chunks.
  101. // (Also tests reusing the regex filter.)
  102. out.push(file_sink(dest2.name(), out_mode));
  103. write_data_in_chunks(out);
  104. out.pop();
  105. BOOST_CHECK_MESSAGE(
  106. compare_files(dest2.name(), upper.name()),
  107. "failed writing to function-based regex_filter in chunks"
  108. );
  109. }
  110. {
  111. test_file dest1;
  112. test_file dest2;
  113. // Test writing to a regex filter based on a format string in chars.
  114. filtering_ostream
  115. out(boost::iostreams::regex_filter(match_lower, fmt));
  116. out.push(file_sink(dest1.name(), out_mode));
  117. write_data_in_chars(out);
  118. out.pop();
  119. BOOST_CHECK_MESSAGE(
  120. compare_files(dest1.name(), upper.name()),
  121. "failed writing to format-string-based regex_filter in chars"
  122. );
  123. // Test writing to a regex filter based on a format string in chunks.
  124. // (Also tests reusing the regex filter.)
  125. out.push(file_sink(dest2.name(), out_mode));
  126. write_data_in_chunks(out);
  127. out.pop();
  128. BOOST_CHECK_MESSAGE(
  129. compare_files(dest2.name(), upper.name()),
  130. "failed writing to format-string-based regex_filter in chunks"
  131. );
  132. }
  133. {
  134. // Note: the ifstream second is placed in a nested scope because
  135. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  136. // Test reading from a regex filter with no matches; this checks that
  137. // Ticket #1139 is fixed
  138. boost::regex match_xxx("xxx");
  139. test_file test2;
  140. filtering_istream
  141. first(boost::iostreams::regex_filter(match_xxx, replace_lower()));
  142. first.push(file_source(test.name(), in_mode));
  143. {
  144. ifstream second(test2.name().c_str(), in_mode);
  145. BOOST_CHECK_MESSAGE(
  146. compare_streams_in_chars(first, second),
  147. "failed reading from a regex filter with no matches"
  148. );
  149. }
  150. }
  151. }
  152. #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
  153. void wregex_filter_test()
  154. {
  155. // Note: Given the basic stream and filter tests, two regex tests
  156. // are probably sufficient: reading with a filter based on a function,
  157. // and writing with a filter based on a format string.
  158. test_file test;
  159. uppercase_file upper;
  160. boost::wregex match_lower(L"[a-z]+");
  161. std::wstring fmt = L"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  162. {
  163. // Note: the ifstream second is placed in a nested scope because
  164. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  165. // Test reading from a regex filter based on a function in chars.
  166. filtering_wistream
  167. first(boost::iostreams::wregex_filter(match_lower, replace_lower()));
  168. first.push(wfile_source(test.name(), in_mode));
  169. {
  170. wifstream second(upper.name().c_str(), in_mode);
  171. BOOST_CHECK_MESSAGE(
  172. compare_streams_in_chars(first, second),
  173. "failed reading from function-based regex_filter in chars"
  174. );
  175. }
  176. first.pop();
  177. // Test reading from a regex filter based on a function in chunks.
  178. // (Also tests reusing the regex filter.)
  179. first.push(wfile_source(test.name(), in_mode));
  180. {
  181. wifstream second(upper.name().c_str(), in_mode);
  182. BOOST_CHECK_MESSAGE(
  183. compare_streams_in_chunks(first, second),
  184. "failed reading from function-based regex_filter in chunks"
  185. );
  186. }
  187. }
  188. {
  189. // Note: the ifstream second is placed in a nested scope because
  190. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  191. // Test reading from a regex filter based on a format string in chars.
  192. filtering_wistream
  193. first(boost::iostreams::wregex_filter(match_lower, fmt));
  194. first.push(wfile_source(test.name(), in_mode));
  195. {
  196. wifstream second(upper.name().c_str(), in_mode);
  197. BOOST_CHECK_MESSAGE(
  198. compare_streams_in_chars(first, second),
  199. "failed reading from format-string-based regex_filter in chars"
  200. );
  201. }
  202. first.pop();
  203. // Test reading from a regex filter based on a format string in chunks.
  204. // (Also tests reusing the regex filter.)
  205. first.push(wfile_source(test.name(), in_mode));
  206. {
  207. wifstream second(upper.name().c_str(), in_mode);
  208. BOOST_CHECK_MESSAGE(
  209. compare_streams_in_chars(first, second),
  210. "failed reading from format-string-based regex_filter in chunks"
  211. );
  212. }
  213. }
  214. {
  215. test_file dest1;
  216. test_file dest2;
  217. // Test writing to a regex filter based on a function in chars.
  218. filtering_wostream
  219. out(boost::iostreams::wregex_filter(match_lower, replace_lower()));
  220. out.push(wfile_sink(dest1.name(), out_mode));
  221. write_data_in_chars(out);
  222. out.pop();
  223. BOOST_CHECK_MESSAGE(
  224. compare_files(dest1.name(), upper.name()),
  225. "failed writing to function-based regex_filter in chars"
  226. );
  227. // Test writing to a regex filter based on a function in chunks.
  228. // (Also tests reusing the regex filter.)
  229. out.push(wfile_sink(dest2.name(), out_mode));
  230. write_data_in_chunks(out);
  231. out.pop();
  232. BOOST_CHECK_MESSAGE(
  233. compare_files(dest2.name(), upper.name()),
  234. "failed writing to function-based regex_filter in chunks"
  235. );
  236. }
  237. {
  238. test_file dest1;
  239. test_file dest2;
  240. // Test writing to a regex filter based on a format string in chars.
  241. filtering_wostream
  242. out(boost::iostreams::wregex_filter(match_lower, fmt));
  243. out.push(wfile_sink(dest1.name(), out_mode));
  244. write_data_in_chars(out);
  245. out.pop();
  246. BOOST_CHECK_MESSAGE(
  247. compare_files(dest1.name(), upper.name()),
  248. "failed writing to format-string-based regex_filter in chars"
  249. );
  250. // Test writing to a regex filter based on a format string in chunks.
  251. // (Also tests reusing the regex filter.)
  252. out.push(wfile_sink(dest2.name(), out_mode));
  253. write_data_in_chunks(out);
  254. out.pop();
  255. BOOST_CHECK_MESSAGE(
  256. compare_files(dest2.name(), upper.name()),
  257. "failed writing to format-string-based regex_filter in chunks"
  258. );
  259. }
  260. {
  261. // Note: the ifstream second is placed in a nested scope because
  262. // closing and reopening a single ifstream failed for CW 9.4 on Windows.
  263. // Test reading from a regex filter with no matches; this checks that
  264. // Ticket #1139 is fixed
  265. boost::wregex match_xxx(L"xxx");
  266. test_file test2;
  267. filtering_wistream
  268. first(boost::iostreams::wregex_filter(match_xxx, replace_lower()));
  269. first.push(wfile_source(test.name(), in_mode));
  270. {
  271. wifstream second(test2.name().c_str(), in_mode);
  272. BOOST_CHECK_MESSAGE(
  273. compare_streams_in_chars(first, second),
  274. "failed reading from a regex filter with no matches"
  275. );
  276. }
  277. }
  278. }
  279. #endif
  280. test_suite* init_unit_test_suite(int, char* [])
  281. {
  282. test_suite* test = BOOST_TEST_SUITE("regex_filter test");
  283. test->add(BOOST_TEST_CASE(&regex_filter_test));
  284. #ifndef BOOST_IOSTREAMS_NO_WIDE_STREAMS
  285. test->add(BOOST_TEST_CASE(&wregex_filter_test));
  286. #endif
  287. return test;
  288. }