dual_seekable_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // (C) Copyright 2014 Jorge Lodos
  2. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  3. // (C) Copyright 2004-2007 Jonathan Turkanis
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
  6. // See http://www.boost.org/libs/iostreams for documentation.
  7. #include <sstream>
  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/temp_file.hpp"
  13. #include "detail/verification.hpp"
  14. using namespace std;
  15. using namespace boost;
  16. using namespace boost::iostreams;
  17. using namespace boost::iostreams::test;
  18. using boost::unit_test::test_suite;
  19. void verification_function_seekable_test()
  20. {
  21. {
  22. temp_file f;
  23. fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
  24. BOOST_CHECK_MESSAGE(
  25. test_seekable_in_chars(io),
  26. "failed using test_seekable_in_chars"
  27. );
  28. io.close();
  29. }
  30. {
  31. temp_file f;
  32. fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
  33. BOOST_CHECK_MESSAGE(
  34. test_seekable_in_chunks(io),
  35. "failed using test_seekable_in_chunks"
  36. );
  37. io.close();
  38. }
  39. {
  40. temp_file f;
  41. fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
  42. for (int i = 0; i < data_reps; ++i)
  43. io.write(narrow_data(), chunk_size);
  44. io.seekg(0, BOOST_IOS::beg);
  45. BOOST_CHECK_MESSAGE(
  46. test_input_seekable(io),
  47. "failed using test_input_seekable"
  48. );
  49. io.close();
  50. }
  51. {
  52. temp_file f;
  53. fstream io(f.name().c_str(), BOOST_IOS::in | BOOST_IOS::out | BOOST_IOS::binary | BOOST_IOS::trunc);
  54. BOOST_CHECK_MESSAGE(
  55. test_output_seekable(io),
  56. "failed using test_output_seekable"
  57. );
  58. io.close();
  59. }
  60. }
  61. void verification_function_dual_seekable_test()
  62. {
  63. {
  64. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  65. BOOST_CHECK_MESSAGE(
  66. test_seekable_in_chars(ss),
  67. "failed using test_seekable_in_chars"
  68. );
  69. }
  70. {
  71. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  72. BOOST_CHECK_MESSAGE(
  73. test_seekable_in_chunks(ss),
  74. "failed using test_seekable_in_chunks"
  75. );
  76. }
  77. {
  78. string s;
  79. for (int i = 0; i < data_reps; ++i)
  80. s.append(narrow_data(), chunk_size);
  81. stringstream ss(s, BOOST_IOS::in | BOOST_IOS::out);
  82. BOOST_CHECK_MESSAGE(
  83. test_input_seekable(ss),
  84. "failed using test_input_seekable"
  85. );
  86. }
  87. {
  88. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  89. BOOST_CHECK_MESSAGE(
  90. test_output_seekable(ss),
  91. "failed using test_output_seekable"
  92. );
  93. }
  94. {
  95. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  96. BOOST_CHECK_MESSAGE(
  97. test_dual_seekable(ss),
  98. "failed using test_dual_seekable"
  99. );
  100. }
  101. }
  102. void dual_seekable_test()
  103. {
  104. {
  105. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  106. filtering_stream<dual_seekable> io(ss);
  107. io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
  108. BOOST_CHECK_MESSAGE(
  109. test_seekable_in_chars(io),
  110. "failed seeking within a string, in chars"
  111. );
  112. }
  113. {
  114. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  115. filtering_stream<dual_seekable> io(ss);
  116. io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
  117. BOOST_CHECK_MESSAGE(
  118. test_seekable_in_chunks(io),
  119. "failed seeking within a string, in chunks"
  120. );
  121. }
  122. {
  123. string s;
  124. for (int i = 0; i < data_reps; ++i)
  125. s.append(narrow_data(), chunk_size);
  126. stringstream ss(s, BOOST_IOS::in | BOOST_IOS::out);
  127. filtering_stream<dual_seekable> io(ss);
  128. io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
  129. BOOST_CHECK_MESSAGE(
  130. test_input_seekable(io),
  131. "failed seeking within a string source"
  132. );
  133. }
  134. {
  135. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  136. filtering_stream<dual_seekable> io(ss);
  137. io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
  138. BOOST_CHECK_MESSAGE(
  139. test_output_seekable(io),
  140. "failed seeking within a string sink"
  141. );
  142. }
  143. {
  144. stringstream ss(BOOST_IOS::in | BOOST_IOS::out);
  145. filtering_stream<dual_seekable> io(ss);
  146. io.exceptions(BOOST_IOS::failbit | BOOST_IOS::badbit);
  147. BOOST_CHECK_MESSAGE(
  148. test_dual_seekable(io),
  149. "failed dual seeking within a string"
  150. );
  151. }
  152. }
  153. test_suite* init_unit_test_suite(int, char* [])
  154. {
  155. test_suite* test = BOOST_TEST_SUITE("dual seekable test");
  156. test->add(BOOST_TEST_CASE(&verification_function_seekable_test));
  157. test->add(BOOST_TEST_CASE(&verification_function_dual_seekable_test));
  158. test->add(BOOST_TEST_CASE(&dual_seekable_test));
  159. return test;
  160. }