line_filter_test.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/copy.hpp>
  8. #include <boost/iostreams/device/file.hpp>
  9. #include <boost/iostreams/filter/line.hpp>
  10. #include <boost/iostreams/filtering_stream.hpp>
  11. #include <boost/test/test_tools.hpp>
  12. #include <boost/test/unit_test.hpp>
  13. #include "detail/constants.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;
  21. using namespace boost::iostreams;
  22. using namespace boost::iostreams::test;
  23. using boost::unit_test::test_suite;
  24. struct toupper_line_filter : line_filter {
  25. std::string do_filter(const std::string& line)
  26. {
  27. std::string result(line);
  28. for ( std::string::size_type z = 0, len = line.size();
  29. z < len;
  30. ++z )
  31. {
  32. result[z] = std::toupper((unsigned char) result[z]);
  33. }
  34. return result;
  35. }
  36. };
  37. bool compare_streams_in_lines(std::istream& first, std::istream& second)
  38. {
  39. do {
  40. std::string line_one;
  41. std::string line_two;
  42. std::getline(first, line_one);
  43. std::getline(second, line_two);
  44. if (line_one != line_two || first.eof() != second.eof())
  45. return false;
  46. } while (!first.eof());
  47. return true;
  48. }
  49. void read_line_filter()
  50. {
  51. test_file src;
  52. uppercase_file upper;
  53. filtering_istream first;
  54. first.push(toupper_line_filter());
  55. first.push(file_source(src.name(), in_mode));
  56. ifstream second(upper.name().c_str(), in_mode);
  57. BOOST_CHECK_MESSAGE(
  58. compare_streams_in_lines(first, second),
  59. "failed reading from a line_filter"
  60. );
  61. }
  62. void write_line_filter()
  63. {
  64. test_file data;
  65. temp_file dest;
  66. uppercase_file upper;
  67. filtering_ostream out;
  68. out.push(toupper_line_filter());
  69. out.push(file_sink(dest.name(), out_mode));
  70. copy(file_source(data.name(), in_mode), out);
  71. out.reset();
  72. ifstream first(dest.name().c_str());
  73. ifstream second(upper.name().c_str());
  74. BOOST_CHECK_MESSAGE(
  75. compare_streams_in_lines(first, second),
  76. "failed writing to a line_filter"
  77. );
  78. }
  79. test_suite* init_unit_test_suite(int, char* [])
  80. {
  81. test_suite* test = BOOST_TEST_SUITE("line_filter test");
  82. test->add(BOOST_TEST_CASE(&read_line_filter));
  83. test->add(BOOST_TEST_CASE(&write_line_filter));
  84. return test;
  85. }
  86. #include <boost/iostreams/detail/config/enable_warnings.hpp> // BCC 5.x.