compose_test.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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 <cctype>
  7. #include <boost/iostreams/compose.hpp>
  8. #include <boost/iostreams/device/file.hpp>
  9. #include <boost/iostreams/filtering_stream.hpp>
  10. #include <boost/test/test_tools.hpp>
  11. #include <boost/test/unit_test.hpp>
  12. #include "detail/closable.hpp"
  13. #include "detail/operation_sequence.hpp"
  14. #include "detail/filters.hpp"
  15. #include "detail/temp_file.hpp"
  16. #include "detail/verification.hpp"
  17. // Must come last.
  18. #include <boost/iostreams/detail/config/disable_warnings.hpp> // BCC 5.x.
  19. using namespace std;
  20. using namespace boost::iostreams;
  21. using namespace boost::iostreams::test;
  22. using boost::unit_test::test_suite;
  23. namespace io = boost::iostreams;
  24. void read_composite()
  25. {
  26. test_file src1, src2;
  27. filtering_istream first, second;
  28. // Test composite device
  29. first.push(toupper_filter());
  30. first.push(padding_filter('a'));
  31. first.push(file_source(src1.name(), in_mode));
  32. second.push( compose( toupper_filter(),
  33. compose( padding_filter('a'),
  34. file_source(src1.name(), in_mode) ) ) );
  35. BOOST_CHECK_MESSAGE(
  36. compare_streams_in_chunks(first, second),
  37. "failed reading from a stdio_filter"
  38. );
  39. // Test composite filter
  40. first.reset();
  41. second.reset();
  42. first.push(toupper_filter());
  43. first.push(padding_filter('a'));
  44. first.push(file_source(src1.name(), in_mode));
  45. second.push( compose( compose( toupper_filter(),
  46. padding_filter('a') ),
  47. file_source(src1.name(), in_mode) ) );
  48. BOOST_CHECK_MESSAGE(
  49. compare_streams_in_chunks(first, second),
  50. "failed reading from a stdio_filter"
  51. );
  52. }
  53. void write_composite()
  54. {
  55. temp_file dest1, dest2;
  56. filtering_ostream out1, out2;
  57. // Test composite device
  58. out1.push(tolower_filter());
  59. out1.push(padding_filter('a'));
  60. out1.push(file_sink(dest1.name(), in_mode));
  61. out2.push( compose( tolower_filter(),
  62. compose( padding_filter('a'),
  63. file_sink(dest2.name(), in_mode) ) ) );
  64. write_data_in_chunks(out1);
  65. write_data_in_chunks(out2);
  66. out1.reset();
  67. out2.reset();
  68. {
  69. ifstream first(dest1.name().c_str());
  70. ifstream second(dest2.name().c_str());
  71. BOOST_CHECK_MESSAGE(
  72. compare_streams_in_chunks(first, second),
  73. "failed writing to a stdio_filter"
  74. );
  75. }
  76. // Test composite filter
  77. out1.push(tolower_filter());
  78. out1.push(padding_filter('a'));
  79. out1.push(file_sink(dest1.name(), in_mode));
  80. out2.push( compose( compose( tolower_filter(),
  81. padding_filter('a') ),
  82. file_sink(dest2.name(), in_mode) ) );
  83. write_data_in_chunks(out1);
  84. write_data_in_chunks(out2);
  85. out1.reset();
  86. out2.reset();
  87. {
  88. ifstream first(dest1.name().c_str());
  89. ifstream second(dest2.name().c_str());
  90. BOOST_CHECK_MESSAGE(
  91. compare_streams_in_chunks(first, second),
  92. "failed writing to a stdio_filter"
  93. );
  94. }
  95. }
  96. void close_composite_device()
  97. {
  98. // Compose an input filter with a source
  99. {
  100. operation_sequence seq;
  101. chain<input> ch;
  102. ch.push(
  103. io::compose(
  104. closable_filter<input>(seq.new_operation(2)),
  105. closable_device<input>(seq.new_operation(1))
  106. )
  107. );
  108. BOOST_CHECK_NO_THROW(ch.reset());
  109. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  110. }
  111. // Compose a bidirectional filter with a source
  112. {
  113. operation_sequence seq;
  114. chain<input> ch;
  115. ch.push(
  116. io::compose(
  117. closable_filter<bidirectional>(
  118. seq.new_operation(2),
  119. seq.new_operation(3)
  120. ),
  121. closable_device<input>(seq.new_operation(1))
  122. )
  123. );
  124. BOOST_CHECK_NO_THROW(ch.reset());
  125. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  126. }
  127. // Compose a seekable filter with a source
  128. {
  129. operation_sequence seq;
  130. chain<input> ch;
  131. ch.push(
  132. io::compose(
  133. closable_filter<seekable>(seq.new_operation(2)),
  134. closable_device<input>(seq.new_operation(1))
  135. )
  136. );
  137. BOOST_CHECK_NO_THROW(ch.reset());
  138. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  139. }
  140. // Compose a dual-use filter with a source
  141. {
  142. operation_sequence seq;
  143. chain<input> ch;
  144. operation dummy;
  145. ch.push(
  146. io::compose(
  147. closable_filter<dual_use>(
  148. seq.new_operation(2),
  149. dummy
  150. ),
  151. closable_device<input>(seq.new_operation(1))
  152. )
  153. );
  154. BOOST_CHECK_NO_THROW(ch.reset());
  155. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  156. }
  157. // Compose an output filter with a sink
  158. {
  159. operation_sequence seq;
  160. chain<output> ch;
  161. ch.push(
  162. io::compose(
  163. closable_filter<output>(seq.new_operation(1)),
  164. closable_device<output>(seq.new_operation(2))
  165. )
  166. );
  167. BOOST_CHECK_NO_THROW(ch.reset());
  168. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  169. }
  170. // Compose a bidirectional filter with a sink
  171. {
  172. operation_sequence seq;
  173. chain<output> ch;
  174. ch.push(
  175. io::compose(
  176. closable_filter<bidirectional>(
  177. seq.new_operation(1),
  178. seq.new_operation(2)
  179. ),
  180. closable_device<output>(seq.new_operation(3))
  181. )
  182. );
  183. BOOST_CHECK_NO_THROW(ch.reset());
  184. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  185. }
  186. // Compose a seekable filter with a sink
  187. {
  188. operation_sequence seq;
  189. chain<output> ch;
  190. ch.push(
  191. io::compose(
  192. closable_filter<seekable>(seq.new_operation(1)),
  193. closable_device<output>(seq.new_operation(2))
  194. )
  195. );
  196. BOOST_CHECK_NO_THROW(ch.reset());
  197. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  198. }
  199. // Compose a dual-use filter with a sink
  200. {
  201. operation_sequence seq;
  202. chain<output> ch;
  203. operation dummy;
  204. ch.push(
  205. io::compose(
  206. closable_filter<dual_use>(
  207. dummy,
  208. seq.new_operation(1)
  209. ),
  210. closable_device<output>(seq.new_operation(2))
  211. )
  212. );
  213. BOOST_CHECK_NO_THROW(ch.reset());
  214. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  215. }
  216. // Compose a bidirectional filter with a bidirectional device
  217. {
  218. operation_sequence seq;
  219. chain<bidirectional> ch;
  220. ch.push(
  221. io::compose(
  222. closable_filter<bidirectional>(
  223. seq.new_operation(2),
  224. seq.new_operation(3)
  225. ),
  226. closable_device<bidirectional>(
  227. seq.new_operation(1),
  228. seq.new_operation(4)
  229. )
  230. )
  231. );
  232. BOOST_CHECK_NO_THROW(ch.reset());
  233. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  234. }
  235. // Compose a seekable filter with a seekable device
  236. {
  237. operation_sequence seq;
  238. chain<seekable> ch;
  239. ch.push(
  240. io::compose(
  241. closable_filter<seekable>(seq.new_operation(1)),
  242. closable_device<seekable>(seq.new_operation(2))
  243. )
  244. );
  245. BOOST_CHECK_NO_THROW(ch.reset());
  246. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  247. }
  248. }
  249. void close_composite_filter()
  250. {
  251. // Compose two input filters
  252. {
  253. operation_sequence seq;
  254. chain<input> ch;
  255. ch.push(
  256. io::compose(
  257. closable_filter<input>(seq.new_operation(3)),
  258. closable_filter<input>(seq.new_operation(2))
  259. )
  260. );
  261. ch.push(closable_device<input>(seq.new_operation(1)));
  262. BOOST_CHECK_NO_THROW(ch.reset());
  263. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  264. }
  265. // Compose a bidirectional filter with an input filter
  266. {
  267. operation_sequence seq;
  268. chain<input> ch;
  269. ch.push(
  270. io::compose(
  271. closable_filter<bidirectional>(
  272. seq.new_operation(3),
  273. seq.new_operation(4)
  274. ),
  275. closable_filter<input>(seq.new_operation(2))
  276. )
  277. );
  278. ch.push(closable_device<input>(seq.new_operation(1)));
  279. BOOST_CHECK_NO_THROW(ch.reset());
  280. BOOST_CHECK_MESSAGE(seq.is_success(), seq.message());
  281. }
  282. // Compose a seekable filter with an input filter
  283. {
  284. operation_sequence seq;
  285. chain<input> ch;
  286. ch.push(
  287. io::compose(
  288. closable_filter<seekable>(seq.new_operation(3)),
  289. closable_filter<input>(seq.new_operation(2))
  290. )
  291. );
  292. ch.push(closable_device<input>(seq.new_operation(1)));
  293. BOOST_CHECK_NO_THROW(ch.reset());
  294. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  295. }
  296. // Compose a dual-use filter with an input filter
  297. {
  298. operation_sequence seq;
  299. chain<input> ch;
  300. operation dummy;
  301. ch.push(
  302. io::compose(
  303. closable_filter<dual_use>(
  304. seq.new_operation(3),
  305. dummy
  306. ),
  307. closable_filter<input>(seq.new_operation(2))
  308. )
  309. );
  310. ch.push(closable_device<input>(seq.new_operation(1)));
  311. BOOST_CHECK_NO_THROW(ch.reset());
  312. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  313. }
  314. // Compose two output filters
  315. {
  316. operation_sequence seq;
  317. chain<output> ch;
  318. ch.push(
  319. io::compose(
  320. closable_filter<output>(seq.new_operation(1)),
  321. closable_filter<output>(seq.new_operation(2))
  322. )
  323. );
  324. ch.push(closable_device<output>(seq.new_operation(3)));
  325. BOOST_CHECK_NO_THROW(ch.reset());
  326. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  327. }
  328. // Compose a bidirectional filter with an output filter
  329. {
  330. operation_sequence seq;
  331. chain<output> ch;
  332. ch.push(
  333. io::compose(
  334. closable_filter<bidirectional>(
  335. seq.new_operation(1),
  336. seq.new_operation(2)
  337. ),
  338. closable_filter<output>(seq.new_operation(3))
  339. )
  340. );
  341. ch.push(closable_device<output>(seq.new_operation(4)));
  342. BOOST_CHECK_NO_THROW(ch.reset());
  343. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  344. }
  345. // Compose a seekable filter with an output filter
  346. {
  347. operation_sequence seq;
  348. chain<output> ch;
  349. ch.push(
  350. io::compose(
  351. closable_filter<seekable>(seq.new_operation(1)),
  352. closable_filter<output>(seq.new_operation(2))
  353. )
  354. );
  355. ch.push(closable_device<output>(seq.new_operation(3)));
  356. BOOST_CHECK_NO_THROW(ch.reset());
  357. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  358. }
  359. // Compose a dual-use filter with an output filter
  360. {
  361. operation_sequence seq;
  362. chain<output> ch;
  363. operation dummy;
  364. ch.push(
  365. io::compose(
  366. closable_filter<dual_use>(
  367. dummy,
  368. seq.new_operation(1)
  369. ),
  370. closable_filter<output>(seq.new_operation(2))
  371. )
  372. );
  373. ch.push(closable_device<output>(seq.new_operation(3)));
  374. BOOST_CHECK_NO_THROW(ch.reset());
  375. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  376. }
  377. // Compose two bidirectional filters
  378. {
  379. operation_sequence seq;
  380. chain<bidirectional> ch;
  381. ch.push(
  382. io::compose(
  383. closable_filter<bidirectional>(
  384. seq.new_operation(3),
  385. seq.new_operation(4)
  386. ),
  387. closable_filter<bidirectional>(
  388. seq.new_operation(2),
  389. seq.new_operation(5)
  390. )
  391. )
  392. );
  393. ch.push(
  394. closable_device<bidirectional>(
  395. seq.new_operation(1),
  396. seq.new_operation(6)
  397. )
  398. );
  399. BOOST_CHECK_NO_THROW(ch.reset());
  400. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  401. }
  402. // Compose two seekable filters
  403. {
  404. operation_sequence seq;
  405. chain<seekable> ch;
  406. ch.push(
  407. io::compose(
  408. closable_filter<seekable>(seq.new_operation(1)),
  409. closable_filter<seekable>(seq.new_operation(2))
  410. )
  411. );
  412. ch.push(closable_device<seekable>(seq.new_operation(3)));
  413. BOOST_CHECK_NO_THROW(ch.reset());
  414. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  415. }
  416. // Compose two dual-use filters for input
  417. {
  418. operation_sequence seq;
  419. chain<input> ch;
  420. operation dummy;
  421. ch.push(
  422. io::compose(
  423. closable_filter<dual_use>(
  424. seq.new_operation(3),
  425. dummy
  426. ),
  427. closable_filter<dual_use>(
  428. seq.new_operation(2),
  429. dummy
  430. )
  431. )
  432. );
  433. ch.push(closable_device<input>(seq.new_operation(1)));
  434. BOOST_CHECK_NO_THROW(ch.reset());
  435. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  436. }
  437. // Compose two dual-use filters for output
  438. {
  439. operation_sequence seq;
  440. chain<output> ch;
  441. operation dummy;
  442. ch.push(
  443. io::compose(
  444. closable_filter<dual_use>(
  445. dummy,
  446. seq.new_operation(1)
  447. ),
  448. closable_filter<dual_use>(
  449. dummy,
  450. seq.new_operation(2)
  451. )
  452. )
  453. );
  454. ch.push(closable_device<output>(seq.new_operation(3)));
  455. BOOST_CHECK_NO_THROW(ch.reset());
  456. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  457. }
  458. }
  459. test_suite* init_unit_test_suite(int, char* [])
  460. {
  461. test_suite* test = BOOST_TEST_SUITE("line_filter test");
  462. test->add(BOOST_TEST_CASE(&read_composite));
  463. test->add(BOOST_TEST_CASE(&write_composite));
  464. test->add(BOOST_TEST_CASE(&close_composite_device));
  465. test->add(BOOST_TEST_CASE(&close_composite_filter));
  466. return test;
  467. }
  468. #include <boost/iostreams/detail/config/enable_warnings.hpp> // BCC 5.x.