finite_state_filter_test.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
  2. // (C) Copyright 2005-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 <boost/config.hpp>
  7. #ifdef BOOST_NO_STD_LOCALE
  8. # error std::locale not supported on this platform
  9. #else
  10. # include <map>
  11. # include <boost/iostreams/detail/ios.hpp> // failure.
  12. # include <boost/iostreams/filter/test.hpp>
  13. # include <boost/mpl/vector.hpp>
  14. # include <boost/test/test_tools.hpp>
  15. # include <boost/test/unit_test.hpp>
  16. # include "../example/finite_state_filter.hpp"
  17. using boost::unit_test::test_suite;
  18. namespace io = boost::iostreams;
  19. const std::string posix = // 'unix' is sometimes a macro.
  20. "When I was one-and-twenty\n"
  21. "I heard a wise man say,\n"
  22. "'Give crowns and pounds and guineas\n"
  23. "But not your heart away;\n"
  24. "\n"
  25. "Give pearls away and rubies\n"
  26. "But keep your fancy free.'\n"
  27. "But I was one-and-twenty,\n"
  28. "No use to talk to me.\n"
  29. "\n"
  30. "When I was one-and-twenty\n"
  31. "I heard him say again,\n"
  32. "'The heart out of the bosom\n"
  33. "Was never given in vain;\n"
  34. "'Tis paid with sighs a plenty\n"
  35. "And sold for endless rue.'\n"
  36. "And I am two-and-twenty,\n"
  37. "And oh, 'tis true, 'tis true.";
  38. const std::string dos =
  39. "When I was one-and-twenty\r\n"
  40. "I heard a wise man say,\r\n"
  41. "'Give crowns and pounds and guineas\r\n"
  42. "But not your heart away;\r\n"
  43. "\r\n"
  44. "Give pearls away and rubies\r\n"
  45. "But keep your fancy free.'\r\n"
  46. "But I was one-and-twenty,\r\n"
  47. "No use to talk to me.\r\n"
  48. "\r\n"
  49. "When I was one-and-twenty\r\n"
  50. "I heard him say again,\r\n"
  51. "'The heart out of the bosom\r\n"
  52. "Was never given in vain;\r\n"
  53. "'Tis paid with sighs a plenty\r\n"
  54. "And sold for endless rue.'\r\n"
  55. "And I am two-and-twenty,\r\n"
  56. "And oh, 'tis true, 'tis true.";
  57. const std::string comments =
  58. "When I was /*one-and-twenty\n"
  59. "I he*/ard a wise/ man say,\n"
  60. "'Give cr//*owns *and po**/unds and guineas\n"
  61. "But n*/ot yo*/ur he/*a*/rt /**/away;\n";
  62. const std::string no_comments =
  63. "When I was "
  64. "ard a wise/ man say,\n"
  65. "'Give cr/unds and guineas\n"
  66. "But n*/ot yo*/ur hert away;\n";
  67. struct identity_fsm
  68. : io::finite_state_machine<identity_fsm, char>
  69. {
  70. void on_any(char c) { push(c); }
  71. typedef boost::mpl::vector0<> transition_table;
  72. };
  73. struct dos2unix_fsm : io::finite_state_machine<dos2unix_fsm> {
  74. BOOST_IOSTREAMS_FSM(dos2unix_fsm) // Define skip and push.
  75. typedef dos2unix_fsm self;
  76. typedef boost::mpl::vector<
  77. row<initial_state, is<'\r'>, initial_state, &self::skip>,
  78. row<initial_state, is_any, initial_state, &self::push>
  79. > transition_table;
  80. };
  81. struct unix2dos_fsm : io::finite_state_machine<unix2dos_fsm> {
  82. BOOST_IOSTREAMS_FSM(unix2dos_fsm) // Define skip and push.
  83. typedef unix2dos_fsm self;
  84. void on_lf(char) { push('\r'); push('\n'); }
  85. typedef boost::mpl::vector<
  86. row<initial_state, is<'\n'>, initial_state, &self::on_lf>,
  87. row<initial_state, is_any, initial_state, &self::push>
  88. > transition_table;
  89. };
  90. struct uncommenting_fsm : io::finite_state_machine<uncommenting_fsm> {
  91. BOOST_IOSTREAMS_FSM(uncommenting_fsm) // Define skip and push.
  92. typedef uncommenting_fsm self;
  93. static const int no_comment = initial_state;
  94. static const int pre_comment = no_comment + 1;
  95. static const int comment = pre_comment + 1;
  96. static const int post_comment = comment + 1;
  97. void push_slash(char c) { push('/'); push(c); }
  98. typedef boost::mpl::vector<
  99. row<no_comment, is<'/'>, pre_comment, &self::skip>,
  100. row<no_comment, is_any, no_comment, &self::push>,
  101. row<pre_comment, is<'*'>, comment, &self::skip>,
  102. row<pre_comment, is<'/'>, pre_comment, &self::push>,
  103. row<pre_comment, is_any, no_comment, &self::push_slash>,
  104. row<comment, is<'*'>, post_comment, &self::skip>,
  105. row<comment, is_any, comment, &self::skip>,
  106. row<post_comment, is<'/'>, no_comment, &self::skip>,
  107. row<post_comment, is<'*'>, post_comment, &self::skip>,
  108. row<post_comment, is_any, comment, &self::skip>
  109. > transition_table;
  110. };
  111. void finite_state_filter_test()
  112. {
  113. using namespace std;
  114. typedef io::finite_state_filter<identity_fsm> identity_filter;
  115. typedef io::finite_state_filter<dos2unix_fsm> dos2unix_filter;
  116. typedef io::finite_state_filter<unix2dos_fsm> unix2dos_filter;
  117. typedef io::finite_state_filter<uncommenting_fsm> uncommenting_filter;
  118. // Test identity_filter.
  119. BOOST_CHECK(
  120. io::test_input_filter(identity_filter(), dos, dos)
  121. );
  122. BOOST_CHECK(
  123. io::test_output_filter(identity_filter(), dos, dos)
  124. );
  125. // Test dos2unix_filter.
  126. BOOST_CHECK(
  127. io::test_input_filter(dos2unix_filter(), dos, posix)
  128. );
  129. BOOST_CHECK(
  130. io::test_output_filter(dos2unix_filter(), dos, posix)
  131. );
  132. // Test unix2dos_filter.
  133. BOOST_CHECK(
  134. io::test_input_filter(unix2dos_filter(), posix, dos)
  135. );
  136. BOOST_CHECK(
  137. io::test_output_filter(unix2dos_filter(), posix, dos)
  138. );
  139. // Test uncommenting_filter.
  140. BOOST_CHECK(
  141. io::test_input_filter(uncommenting_filter(), comments, no_comments)
  142. );
  143. BOOST_CHECK(
  144. io::test_output_filter(uncommenting_filter(), comments, no_comments)
  145. );
  146. }
  147. test_suite* init_unit_test_suite(int, char* [])
  148. {
  149. test_suite* test = BOOST_TEST_SUITE("example test");
  150. test->add(BOOST_TEST_CASE(&finite_state_filter_test));
  151. return test;
  152. }
  153. #endif // #ifdef BOOST_NO_STD_LOCALE //---------------------------------------//