putback_test.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. #ifndef BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED
  7. #define BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED
  8. #include <boost/iostreams/device/file.hpp>
  9. #include <boost/iostreams/filtering_stream.hpp>
  10. #include <boost/iostreams/putback.hpp>
  11. #include "detail/constants.hpp"
  12. #include "detail/temp_file.hpp"
  13. using boost::iostreams::test::chunk_size;
  14. bool putback_test_one(std::istream& is)
  15. {
  16. try {
  17. do {
  18. char buf[chunk_size];
  19. is.read(buf, chunk_size);
  20. if (is.gcount() < static_cast<std::streamsize>(chunk_size))
  21. break;
  22. is.putback('a');
  23. if (is.get() != 'a')
  24. return false;
  25. } while (!is.eof());
  26. return true;
  27. } catch (std::exception&) { return false; }
  28. }
  29. bool putback_test_two(std::istream& is)
  30. {
  31. try {
  32. do {
  33. char buf[chunk_size];
  34. is.read(buf, chunk_size);
  35. if (is.gcount() < static_cast<std::streamsize>(chunk_size))
  36. break;
  37. is.putback('a');
  38. is.putback('b');
  39. is.putback('c');
  40. is.putback('d');
  41. if ( is.get() != 'd' || is.get() != 'c' ||
  42. is.get() != 'b' || is.get() != 'a' )
  43. {
  44. return false;
  45. }
  46. } while (!is.eof());
  47. return true;
  48. } catch (std::exception&) { return false; }
  49. }
  50. template<typename Source>
  51. bool putback_test_three(Source& src)
  52. {
  53. try {
  54. while (true) {
  55. char buf[chunk_size];
  56. if (boost::iostreams::read(src, buf, chunk_size) < chunk_size)
  57. break;
  58. boost::iostreams::putback(src, 'a');
  59. if (boost::iostreams::get(src) != 'a')
  60. return false;
  61. }
  62. return true;
  63. } catch (std::exception&) { return false; }
  64. }
  65. template<typename Source>
  66. bool putback_test_four(Source& src)
  67. {
  68. try {
  69. while (true) {
  70. char buf[chunk_size];
  71. if (boost::iostreams::read(src, buf, chunk_size) < chunk_size)
  72. break;
  73. boost::iostreams::putback(src, 'a');
  74. boost::iostreams::putback(src, 'b');
  75. boost::iostreams::putback(src, 'c');
  76. boost::iostreams::putback(src, 'd');
  77. if ( boost::iostreams::get(src) != 'd' ||
  78. boost::iostreams::get(src) != 'c' ||
  79. boost::iostreams::get(src) != 'b' ||
  80. boost::iostreams::get(src) != 'a' )
  81. {
  82. return false;
  83. }
  84. }
  85. return true;
  86. } catch (std::exception&) { return false; }
  87. }
  88. void putback_test()
  89. {
  90. using namespace std;
  91. using namespace boost;
  92. using namespace boost::iostreams;
  93. using namespace boost::iostreams::test;
  94. test_file test;
  95. {
  96. filtering_istream is;
  97. is.set_device_buffer_size(0);
  98. is.push(file_source(test.name()));
  99. BOOST_CHECK_MESSAGE(
  100. putback_test_one(is),
  101. "failed putting back to unbuffered filtering_istream"
  102. );
  103. }
  104. {
  105. filtering_istream is;
  106. is.set_pback_size(4);
  107. is.push(file_source(test.name()));
  108. BOOST_CHECK_MESSAGE(
  109. putback_test_two(is),
  110. "failed putting back to buffered filtering_istream"
  111. );
  112. }
  113. {
  114. filtering_istream is;
  115. is.set_device_buffer_size(0);
  116. is.push(file_source(test.name()));
  117. BOOST_CHECK_MESSAGE(
  118. putback_test_three(is),
  119. "failed putting back to unbuffered filtering_istream"
  120. );
  121. }
  122. {
  123. filtering_istream is;
  124. is.set_pback_size(4);
  125. is.push(file_source(test.name()));
  126. BOOST_CHECK_MESSAGE(
  127. putback_test_four(is),
  128. "failed putting back to buffered filtering_istream"
  129. );
  130. }
  131. {
  132. filtering_istreambuf sb;
  133. sb.set_device_buffer_size(0);
  134. sb.push(file_source(test.name()));
  135. BOOST_CHECK_MESSAGE(
  136. putback_test_three(sb),
  137. "failed putting back to unbuffered filtering_istream"
  138. );
  139. }
  140. {
  141. filtering_istreambuf sb;
  142. sb.set_pback_size(4);
  143. sb.push(file_source(test.name()));
  144. BOOST_CHECK_MESSAGE(
  145. putback_test_four(sb),
  146. "failed putting back to buffered filtering_istream"
  147. );
  148. }
  149. }
  150. #endif // #ifndef BOOST_IOSTREAMS_TEST_PUTBACK_HPP_INCLUDED