regression_line_pos_iterator.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright (c) 2014 Tomoki Imai
  2. //
  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. #include <boost/spirit/include/support_line_pos_iterator.hpp>
  6. #include <boost/detail/lightweight_test.hpp>
  7. #include <boost/assign.hpp>
  8. #include <iostream>
  9. #include <string>
  10. #include <vector>
  11. struct validation {
  12. validation()
  13. : line(), column(), current(), is_defined(false) {
  14. }
  15. validation(size_t line, size_t column, std::string current)
  16. : line(line), column(column), current(current), is_defined(true) {
  17. }
  18. size_t line;
  19. size_t column;
  20. std::string current;
  21. bool is_defined;
  22. };
  23. typedef std::vector<validation> validations;
  24. void test(std::string const& input, validations const& validations) {
  25. typedef boost::spirit::line_pos_iterator<std::string::const_iterator> pos_iterator_t;
  26. pos_iterator_t const input_begin(input.begin());
  27. pos_iterator_t const input_end(input.end());
  28. pos_iterator_t position(input_begin);
  29. validations::const_iterator expected = validations.begin();
  30. for (; position != input_end && expected != validations.end(); ++position, ++expected) {
  31. if (!expected->is_defined)
  32. continue;
  33. boost::iterator_range<pos_iterator_t> const range = get_current_line(input_begin, position, input_end);
  34. std::string const current(range.begin(), range.end());
  35. BOOST_TEST_EQ(expected->line, get_line(position));
  36. BOOST_TEST_EQ(expected->column, get_column(input_begin, position));
  37. BOOST_TEST_EQ(expected->current, current);
  38. }
  39. BOOST_TEST(position == input_end);
  40. BOOST_TEST(expected == validations.end());
  41. }
  42. // LR and CR
  43. void testLRandCR(std::string const& line_break) {
  44. std::string const input = line_break + line_break;
  45. validations const validations = boost::assign::list_of<validation>
  46. (1,1,"")()
  47. (2,1,"")();
  48. test(input, validations);
  49. }
  50. void testLRandCR_foo_bar_git(std::string const& line_break) {
  51. std::string const input = "foo" + line_break + "bar" + line_break + "git";
  52. validations const validations = boost::assign::list_of<validation>
  53. (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")()
  54. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")()
  55. (3,1,"git")(3,2,"git")(3,3,"git");
  56. test(input, validations);
  57. }
  58. void testLRandCR_bar_git(std::string const& line_break) {
  59. std::string const input = line_break + "bar" + line_break + "git";
  60. validations const validations = boost::assign::list_of<validation>
  61. (1,1,"")()
  62. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")()
  63. (3,1,"git")(3,2,"git")(3,3,"git");
  64. test(input, validations);
  65. }
  66. void testLRandCR_foo_bar(std::string const& line_break) {
  67. std::string const input = "foo" + line_break + "bar" + line_break;
  68. validations const validations = boost::assign::list_of<validation>
  69. (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")()
  70. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")();
  71. test(input, validations);
  72. }
  73. // LR or CR
  74. void testLRorCR(std::string const& line_break) {
  75. std::string const input = line_break + line_break;
  76. validations const validations = boost::assign::list_of<validation>
  77. (1,1,"")
  78. (2,1,"");
  79. test(input, validations);
  80. }
  81. void testLRorCR_foo_bar_git(std::string const& line_break) {
  82. std::string const input = "foo" + line_break + "bar" + line_break + "git";
  83. validations const validations = boost::assign::list_of<validation>
  84. (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")
  85. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")
  86. (3,1,"git")(3,2,"git")(3,3,"git");
  87. test(input, validations);
  88. }
  89. void testLRorCR_bar_git(std::string const& line_break) {
  90. std::string const input = line_break + "bar" + line_break + "git";
  91. validations const validations = boost::assign::list_of<validation>
  92. (1,1,"")
  93. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar")
  94. (3,1,"git")(3,2,"git")(3,3,"git");
  95. test(input, validations);
  96. }
  97. void testLRorCR_foo_bar(std::string const& line_break) {
  98. std::string const input = "foo" + line_break + "bar" + line_break;
  99. validations const validations = boost::assign::list_of<validation>
  100. (1,1,"foo")(1,2,"foo")(1,3,"foo")(1,4,"foo")
  101. (2,1,"bar")(2,2,"bar")(2,3,"bar")(2,4,"bar");
  102. test(input, validations);
  103. }
  104. int main()
  105. {
  106. testLRandCR("\r\n");
  107. testLRandCR_foo_bar_git("\r\n");
  108. testLRandCR_bar_git("\r\n");
  109. testLRandCR_foo_bar("\r\n");
  110. testLRandCR("\n\r");
  111. testLRandCR_foo_bar_git("\n\r");
  112. testLRandCR_bar_git("\n\r");
  113. testLRandCR_foo_bar("\n\r");
  114. testLRorCR("\r");
  115. testLRorCR_foo_bar_git("\r");
  116. testLRorCR_bar_git("\r");
  117. testLRorCR_foo_bar("\r");
  118. testLRorCR("\n");
  119. testLRorCR_foo_bar_git("\n");
  120. testLRorCR_bar_git("\n");
  121. testLRorCR_foo_bar("\n");
  122. return boost::report_errors();
  123. }