flush_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 <algorithm> // equal.
  7. #include <fstream>
  8. #include <boost/iostreams/device/back_inserter.hpp>
  9. #include <boost/iostreams/device/file.hpp>
  10. #include <boost/iostreams/device/null.hpp>
  11. #include <boost/iostreams/filtering_stream.hpp>
  12. #include <boost/iostreams/stream.hpp>
  13. #include <boost/iostreams/operations.hpp>
  14. #include <boost/test/test_tools.hpp>
  15. #include <boost/test/unit_test.hpp>
  16. #include "detail/filters.hpp"
  17. #include "detail/temp_file.hpp"
  18. #include "detail/verification.hpp"
  19. using namespace std;
  20. using namespace boost;
  21. using namespace boost::iostreams;
  22. using namespace boost::iostreams::test;
  23. using boost::unit_test::test_suite;
  24. void flush_test()
  25. {
  26. {
  27. stream_buffer<null_sink> null;
  28. null.open(null_sink());
  29. BOOST_CHECK_MESSAGE(
  30. iostreams::flush(null),
  31. "failed flushing stream_buffer"
  32. );
  33. BOOST_CHECK_MESSAGE(
  34. null.strict_sync(),
  35. "failed strict-syncing stream_buffer with "
  36. "non-flushable resource"
  37. );
  38. }
  39. {
  40. stream<null_sink> null;
  41. null.open(null_sink());
  42. BOOST_CHECK_MESSAGE(
  43. iostreams::flush(null),
  44. "failed flushing stream"
  45. );
  46. BOOST_CHECK_MESSAGE(
  47. null.strict_sync(),
  48. "failed strict-syncing stream with "
  49. "non-flushable resource"
  50. );
  51. }
  52. {
  53. filtering_ostream null;
  54. null.push(null_sink());
  55. BOOST_CHECK_MESSAGE(
  56. iostreams::flush(null),
  57. "failed flushing filtering_ostream"
  58. );
  59. BOOST_CHECK_MESSAGE(
  60. null.strict_sync(),
  61. "failed strict-syncing filtering_ostream with "
  62. "non-flushable resource"
  63. );
  64. }
  65. {
  66. filtering_ostream null;
  67. null.push(tolower_filter());
  68. null.push(null_sink());
  69. BOOST_CHECK_MESSAGE(
  70. iostreams::flush(null),
  71. "failed flushing filtering_ostream with non-flushable filter"
  72. );
  73. BOOST_CHECK_MESSAGE(
  74. !null.strict_sync(),
  75. "strict-syncing filtering_ostream with "
  76. "non-flushable filter succeeded"
  77. );
  78. }
  79. {
  80. vector<char> dest1;
  81. vector<char> dest2;
  82. filtering_ostream out;
  83. out.set_auto_close(false);
  84. out.push(flushable_output_filter());
  85. // Write to dest1.
  86. out.push(iostreams::back_inserter(dest1));
  87. write_data_in_chunks(out);
  88. out.flush();
  89. // Write to dest2.
  90. out.pop();
  91. out.push(iostreams::back_inserter(dest2));
  92. write_data_in_chunks(out);
  93. out.flush();
  94. BOOST_CHECK_MESSAGE(
  95. dest1.size() == dest2.size() &&
  96. std::equal(dest1.begin(), dest1.end(), dest2.begin()),
  97. "failed flush filtering_ostream with auto_close disabled"
  98. );
  99. }
  100. {
  101. vector<char> dest1;
  102. vector<char> dest2;
  103. filtering_ostream out;
  104. out.set_auto_close(false);
  105. out.push(flushable_output_filter());
  106. out.push(flushable_output_filter());
  107. // Write to dest1.
  108. out.push(iostreams::back_inserter(dest1));
  109. write_data_in_chunks(out);
  110. out.flush();
  111. // Write to dest2.
  112. out.pop();
  113. out.push(iostreams::back_inserter(dest2));
  114. write_data_in_chunks(out);
  115. out.flush();
  116. BOOST_CHECK_MESSAGE(
  117. dest1.size() == dest2.size() &&
  118. std::equal(dest1.begin(), dest1.end(), dest2.begin()),
  119. "failed flush filtering_ostream with two flushable filters "
  120. "with auto_close disabled"
  121. );
  122. }
  123. }
  124. test_suite* init_unit_test_suite(int, char* [])
  125. {
  126. test_suite* test = BOOST_TEST_SUITE("flush test");
  127. test->add(BOOST_TEST_CASE(&flush_test));
  128. return test;
  129. }