filter_test.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 <string>
  7. #include <boost/iostreams/filter/test.hpp>
  8. #include <boost/test/test_tools.hpp>
  9. #include <boost/test/unit_test.hpp>
  10. #include "detail/filters.hpp"
  11. using namespace boost::iostreams;
  12. using namespace boost::iostreams::test;
  13. using boost::unit_test::test_suite;
  14. const std::string lower =
  15. "in addition to providing an abstract framework the "
  16. "library provides a number of concrete filters, sources "
  17. "and sinks which serve as example applications of the "
  18. "library but are also useful in their own right. these "
  19. "include components for accessing memory-mapped files, "
  20. "for file access via operating system file descriptors, "
  21. "for code conversion, for text filtering with regular "
  22. "expressions, for line-ending conversion and for "
  23. "compression and decompression in the zlib, gzip and "
  24. "bzip2 formats.";
  25. const std::string upper =
  26. "IN ADDITION TO PROVIDING AN ABSTRACT FRAMEWORK THE "
  27. "LIBRARY PROVIDES A NUMBER OF CONCRETE FILTERS, SOURCES "
  28. "AND SINKS WHICH SERVE AS EXAMPLE APPLICATIONS OF THE "
  29. "LIBRARY BUT ARE ALSO USEFUL IN THEIR OWN RIGHT. THESE "
  30. "INCLUDE COMPONENTS FOR ACCESSING MEMORY-MAPPED FILES, "
  31. "FOR FILE ACCESS VIA OPERATING SYSTEM FILE DESCRIPTORS, "
  32. "FOR CODE CONVERSION, FOR TEXT FILTERING WITH REGULAR "
  33. "EXPRESSIONS, FOR LINE-ENDING CONVERSION AND FOR "
  34. "COMPRESSION AND DECOMPRESSION IN THE ZLIB, GZIP AND "
  35. "BZIP2 FORMATS.";
  36. struct toupper_dual_use_filter : public dual_use_filter {
  37. template<typename Source>
  38. int get(Source& s)
  39. {
  40. int c = boost::iostreams::get(s);
  41. return c != EOF && c != WOULD_BLOCK ?
  42. std::toupper((unsigned char) c) :
  43. c;
  44. }
  45. template<typename Sink>
  46. bool put(Sink& s, char c)
  47. {
  48. return boost::iostreams::put(
  49. s, (char) std::toupper((unsigned char) c)
  50. );
  51. }
  52. };
  53. struct tolower_dual_use_filter : public dual_use_filter {
  54. template<typename Source>
  55. int get(Source& s)
  56. {
  57. int c = boost::iostreams::get(s);
  58. return c != EOF && c != WOULD_BLOCK ?
  59. std::tolower((unsigned char) c) :
  60. c;
  61. }
  62. template<typename Sink>
  63. bool put(Sink& s, char c)
  64. {
  65. return boost::iostreams::put(
  66. s, (char) std::tolower((unsigned char) c)
  67. );
  68. }
  69. };
  70. void filter_test()
  71. {
  72. BOOST_CHECK(test_input_filter(toupper_filter(), lower, upper));
  73. BOOST_CHECK(test_input_filter(toupper_multichar_filter(), lower, upper));
  74. BOOST_CHECK(test_input_filter(toupper_dual_use_filter(), lower, upper));
  75. BOOST_CHECK(test_output_filter(tolower_filter(), upper, lower));
  76. BOOST_CHECK(test_output_filter(tolower_multichar_filter(), upper, lower));
  77. BOOST_CHECK(test_output_filter(tolower_dual_use_filter(), upper, lower));
  78. BOOST_CHECK(test_filter_pair(tolower_filter(), toupper_filter(), upper));
  79. BOOST_CHECK(
  80. test_filter_pair( tolower_multichar_filter(),
  81. toupper_multichar_filter(), upper )
  82. );
  83. }
  84. test_suite* init_unit_test_suite(int, char* [])
  85. {
  86. test_suite* test = BOOST_TEST_SUITE("filter test");
  87. test->add(BOOST_TEST_CASE(&filter_test));
  88. return test;
  89. }