tee_test.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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/compose.hpp>
  8. #include <boost/iostreams/device/file.hpp>
  9. #include <boost/iostreams/filtering_stream.hpp>
  10. #include <boost/iostreams/tee.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. #include "detail/closable.hpp"
  14. #include "detail/operation_sequence.hpp"
  15. #include "detail/temp_file.hpp"
  16. #include "detail/verification.hpp"
  17. using namespace std;
  18. using namespace boost;
  19. using namespace boost::iostreams;
  20. using namespace boost::iostreams::test;
  21. using boost::unit_test::test_suite;
  22. void read_write_test()
  23. {
  24. {
  25. test_file src1, src2;
  26. temp_file dest;
  27. filtering_istream first, second;
  28. first.push(tee(file_sink(dest.name(), out_mode)));
  29. first.push(file_source(src1.name(), in_mode));
  30. second.push(file_source(src2.name(), in_mode));
  31. compare_streams_in_chars(first, second); // ignore return value
  32. first.reset();
  33. BOOST_CHECK_MESSAGE(
  34. compare_files(dest.name(), src1.name()),
  35. "failed reading from a tee_filter in chars"
  36. );
  37. }
  38. {
  39. test_file src1, src2;
  40. temp_file dest;
  41. filtering_istream first, second;
  42. first.push(tee(file_sink(dest.name(), out_mode)));
  43. first.push(file_source(src1.name(), in_mode));
  44. second.push(file_source(src2.name(), in_mode));
  45. compare_streams_in_chunks(first, second); // ignore return value
  46. first.reset();
  47. BOOST_CHECK_MESSAGE(
  48. compare_files(dest.name(), src1.name()),
  49. "failed reading from a tee_filter in chunks"
  50. );
  51. }
  52. {
  53. temp_file dest1;
  54. temp_file dest2;
  55. filtering_ostream out;
  56. out.push(tee(file_sink(dest1.name(), out_mode)));
  57. out.push(file_sink(dest2.name(), out_mode));
  58. write_data_in_chars(out);
  59. out.reset();
  60. BOOST_CHECK_MESSAGE(
  61. compare_files(dest1.name(), dest2.name()),
  62. "failed writing to a tee_filter in chars"
  63. );
  64. }
  65. {
  66. temp_file dest1;
  67. temp_file dest2;
  68. filtering_ostream out;
  69. out.push(tee(file_sink(dest1.name(), out_mode)));
  70. out.push(file_sink(dest2.name(), out_mode));
  71. write_data_in_chunks(out);
  72. out.reset();
  73. BOOST_CHECK_MESSAGE(
  74. compare_files(dest1.name(), dest2.name()),
  75. "failed writing to a tee_filter in chunks"
  76. );
  77. }
  78. {
  79. test_file src1, src2;
  80. temp_file dest;
  81. filtering_istream first, second;
  82. first.push( tee( file_source(src1.name(), in_mode),
  83. file_sink(dest.name(), out_mode) ) );
  84. second.push(file_source(src2.name(), in_mode));
  85. compare_streams_in_chars(first, second); // ignore return value
  86. first.reset();
  87. BOOST_CHECK_MESSAGE(
  88. compare_files(dest.name(), src1.name()),
  89. "failed reading from a tee_device in chars"
  90. );
  91. }
  92. {
  93. test_file src1, src2;
  94. temp_file dest;
  95. filtering_istream first, second;
  96. first.push( tee( file_source(src1.name(), in_mode),
  97. file_sink(dest.name(), out_mode) ) );
  98. second.push(file_source(src2.name(), in_mode));
  99. compare_streams_in_chunks(first, second); // ignore return value
  100. first.reset();
  101. BOOST_CHECK_MESSAGE(
  102. compare_files(dest.name(), src1.name()),
  103. "failed reading from a tee_device in chunks"
  104. );
  105. }
  106. {
  107. temp_file dest1;
  108. temp_file dest2;
  109. filtering_ostream out;
  110. out.push( tee( file_sink(dest1.name(), out_mode),
  111. file_sink(dest2.name(), out_mode) ) );
  112. write_data_in_chars(out);
  113. out.reset();
  114. BOOST_CHECK_MESSAGE(
  115. compare_files(dest1.name(), dest2.name()),
  116. "failed writing to a tee_device in chars"
  117. );
  118. }
  119. {
  120. temp_file dest1;
  121. temp_file dest2;
  122. filtering_ostream out;
  123. out.push( tee( file_sink(dest1.name(), out_mode),
  124. file_sink(dest2.name(), out_mode) ) );
  125. write_data_in_chunks(out);
  126. out.reset();
  127. BOOST_CHECK_MESSAGE(
  128. compare_files(dest1.name(), dest2.name()),
  129. "failed writing to a tee_device in chunks"
  130. );
  131. }
  132. }
  133. void close_test()
  134. {
  135. // Note: The implementation of tee_device closes the first
  136. // sink before the second
  137. // Tee two sinks (Borland <= 5.8.2 needs a little help compiling this case,
  138. // but it executes the closing algorithm correctly)
  139. {
  140. operation_sequence seq;
  141. chain<output> ch;
  142. ch.push(
  143. boost::iostreams::tee(
  144. closable_device<output>(seq.new_operation(1)),
  145. closable_device<
  146. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582))
  147. borland_output
  148. #else
  149. output
  150. #endif
  151. >(seq.new_operation(2))
  152. )
  153. );
  154. BOOST_CHECK_NO_THROW(ch.reset());
  155. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  156. }
  157. // Tee two bidirectional devices
  158. {
  159. operation_sequence seq;
  160. chain<output> ch;
  161. ch.push(
  162. boost::iostreams::tee(
  163. closable_device<bidirectional>(
  164. seq.new_operation(1),
  165. seq.new_operation(2)
  166. ),
  167. closable_device<bidirectional>(
  168. seq.new_operation(3),
  169. seq.new_operation(4)
  170. )
  171. )
  172. );
  173. BOOST_CHECK_NO_THROW(ch.reset());
  174. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  175. }
  176. // Tee two seekable devices
  177. {
  178. operation_sequence seq;
  179. chain<output> ch;
  180. ch.push(
  181. boost::iostreams::tee(
  182. closable_device<seekable>(seq.new_operation(1)),
  183. closable_device<seekable>(seq.new_operation(2))
  184. )
  185. );
  186. BOOST_CHECK_NO_THROW(ch.reset());
  187. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  188. }
  189. // Tee a sink
  190. {
  191. operation_sequence seq;
  192. chain<output> ch;
  193. ch.push(boost::iostreams::tee(closable_device<output>(seq.new_operation(1))));
  194. ch.push(closable_device<output>(seq.new_operation(2)));
  195. BOOST_CHECK_NO_THROW(ch.reset());
  196. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  197. }
  198. // Tee a bidirectional device
  199. {
  200. operation_sequence seq;
  201. chain<output> ch;
  202. ch.push(
  203. boost::iostreams::tee(
  204. closable_device<bidirectional>(
  205. seq.new_operation(1),
  206. seq.new_operation(2)
  207. )
  208. )
  209. );
  210. ch.push(closable_device<output>(seq.new_operation(3)));
  211. BOOST_CHECK_NO_THROW(ch.reset());
  212. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  213. }
  214. // Tee a seekable device
  215. {
  216. operation_sequence seq;
  217. chain<output> ch;
  218. ch.push(boost::iostreams::tee(closable_device<seekable>(seq.new_operation(1))));
  219. ch.push(closable_device<seekable>(seq.new_operation(2)));
  220. BOOST_CHECK_NO_THROW(ch.reset());
  221. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  222. }
  223. }
  224. void test_std_io()
  225. {
  226. {
  227. temp_file dest1;
  228. temp_file dest2;
  229. filtering_ostream out;
  230. std::ofstream stream1(dest1.name().c_str(), out_mode);
  231. out.push(tee(stream1));
  232. out.push(file_sink(dest2.name(), out_mode));
  233. write_data_in_chunks(out);
  234. BOOST_CHECK_MESSAGE(
  235. compare_files(dest1.name(), dest2.name()),
  236. "failed writing to a tee_device in chunks"
  237. );
  238. }
  239. {
  240. temp_file dest1;
  241. temp_file dest2;
  242. filtering_ostream out;
  243. std::ofstream stream1(dest1.name().c_str(), out_mode);
  244. out.push( tee ( stream1,
  245. file_sink(dest2.name(), out_mode) ) );
  246. write_data_in_chunks(out);
  247. BOOST_CHECK_MESSAGE(
  248. compare_files(dest1.name(), dest2.name()),
  249. "failed writing to a tee_device in chunks"
  250. );
  251. }
  252. {
  253. temp_file dest1;
  254. temp_file dest2;
  255. filtering_ostream out;
  256. std::ofstream stream2(dest2.name().c_str(), out_mode);
  257. out.push( tee ( file_sink(dest1.name(), out_mode),
  258. stream2 ) );
  259. write_data_in_chunks(out);
  260. BOOST_CHECK_MESSAGE(
  261. compare_files(dest1.name(), dest2.name()),
  262. "failed writing to a tee_device in chunks"
  263. );
  264. }
  265. {
  266. temp_file dest1;
  267. temp_file dest2;
  268. filtering_ostream out;
  269. std::ofstream stream1(dest1.name().c_str(), out_mode);
  270. std::ofstream stream2(dest2.name().c_str(), out_mode);
  271. out.push(tee(stream1, stream2));
  272. write_data_in_chunks(out);
  273. BOOST_CHECK_MESSAGE(
  274. compare_files(dest1.name(), dest2.name()),
  275. "failed writing to a tee_device in chunks"
  276. );
  277. }
  278. }
  279. void tee_composite_test()
  280. {
  281. // This test is probably redundant, given the above test and the tests in
  282. // compose_test.cpp, but it verifies that ticket #1002 is fixed
  283. // Tee a composite sink with a sink
  284. {
  285. operation_sequence seq;
  286. chain<output> ch;
  287. ch.push(
  288. boost::iostreams::tee(
  289. boost::iostreams::compose(
  290. closable_filter<output>(seq.new_operation(1)),
  291. closable_device<output>(seq.new_operation(2))
  292. ),
  293. closable_device<output>(seq.new_operation(3))
  294. )
  295. );
  296. BOOST_CHECK_NO_THROW(ch.reset());
  297. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  298. }
  299. // Tee a composite bidirectional device with a sink
  300. {
  301. operation_sequence seq;
  302. chain<output> ch;
  303. ch.push(
  304. boost::iostreams::tee(
  305. boost::iostreams::compose(
  306. closable_filter<bidirectional>(
  307. seq.new_operation(2),
  308. seq.new_operation(3)
  309. ),
  310. closable_device<bidirectional>(
  311. seq.new_operation(1),
  312. seq.new_operation(4)
  313. )
  314. ),
  315. closable_device<output>(seq.new_operation(5))
  316. )
  317. );
  318. BOOST_CHECK_NO_THROW(ch.reset());
  319. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  320. }
  321. // Tee a composite composite seekable device with a sink
  322. {
  323. operation_sequence seq;
  324. chain<output> ch;
  325. ch.push(
  326. boost::iostreams::tee(
  327. boost::iostreams::compose(
  328. closable_filter<seekable>(seq.new_operation(1)),
  329. closable_device<seekable>(seq.new_operation(2))
  330. ),
  331. closable_device<output>(seq.new_operation(3))
  332. )
  333. );
  334. BOOST_CHECK_NO_THROW(ch.reset());
  335. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  336. }
  337. // Tee a composite sink
  338. {
  339. operation_sequence seq;
  340. chain<output> ch;
  341. ch.push(
  342. boost::iostreams::tee(
  343. boost::iostreams::compose(
  344. closable_filter<output>(seq.new_operation(1)),
  345. closable_device<output>(seq.new_operation(2))
  346. )
  347. )
  348. );
  349. ch.push(closable_device<output>(seq.new_operation(3)));
  350. BOOST_CHECK_NO_THROW(ch.reset());
  351. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  352. }
  353. // Tee a composite bidirectional device with a sink
  354. {
  355. operation_sequence seq;
  356. chain<output> ch;
  357. ch.push(
  358. boost::iostreams::tee(
  359. boost::iostreams::compose(
  360. closable_filter<bidirectional>(
  361. seq.new_operation(2),
  362. seq.new_operation(3)
  363. ),
  364. closable_device<bidirectional>(
  365. seq.new_operation(1),
  366. seq.new_operation(4)
  367. )
  368. )
  369. )
  370. );
  371. ch.push(closable_device<output>(seq.new_operation(5)));
  372. BOOST_CHECK_NO_THROW(ch.reset());
  373. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  374. }
  375. // Tee a composite composite seekable device with a sink
  376. {
  377. operation_sequence seq;
  378. chain<output> ch;
  379. ch.push(
  380. boost::iostreams::tee(
  381. boost::iostreams::compose(
  382. closable_filter<seekable>(seq.new_operation(1)),
  383. closable_device<seekable>(seq.new_operation(2))
  384. )
  385. )
  386. );
  387. ch.push(closable_device<output>(seq.new_operation(3)));
  388. BOOST_CHECK_NO_THROW(ch.reset());
  389. BOOST_CHECK_OPERATION_SEQUENCE(seq);
  390. }
  391. }
  392. test_suite* init_unit_test_suite(int, char* [])
  393. {
  394. test_suite* test = BOOST_TEST_SUITE("tee test");
  395. test->add(BOOST_TEST_CASE(&read_write_test));
  396. test->add(BOOST_TEST_CASE(&close_test));
  397. return test;
  398. }