search_test4.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright (c) Marshall Clow 2010-2012.
  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. For more information, see http://www.boost.org
  6. Testing the range-based interfaces
  7. */
  8. #include <boost/algorithm/searching/boyer_moore.hpp>
  9. #include <boost/algorithm/searching/boyer_moore_horspool.hpp>
  10. #include <boost/algorithm/searching/knuth_morris_pratt.hpp>
  11. #define BOOST_TEST_MAIN
  12. #include <boost/test/unit_test.hpp>
  13. #include <iostream>
  14. #include <fstream>
  15. #include <iomanip>
  16. #include <algorithm>
  17. #include <vector>
  18. #include <string>
  19. typedef std::vector<std::string> vec;
  20. #define NUM_TRIES 100
  21. #define runOne(call, refDiff) { \
  22. res = boost::algorithm::call ( haystack, needle ); \
  23. if ( res != exp ) { \
  24. std::cout << "Expected " \
  25. << exp.first - haystack.begin () << " got " \
  26. << res.first - haystack.begin () << std::endl; \
  27. throw std::runtime_error \
  28. ( "Unexpected result from " #call ); \
  29. } \
  30. }
  31. #define runObject(obj, refDiff) { \
  32. boost::algorithm::obj <vec::const_iterator> s_o = \
  33. boost::algorithm::make_##obj ( needle ); \
  34. res = s_o ( haystack ); \
  35. if ( res != exp ) { \
  36. std::cout << "Expected " \
  37. << exp.first - haystack.begin () << " got " \
  38. << res.first - haystack.begin () << std::endl; \
  39. throw std::runtime_error \
  40. ( "Unexpected result from " #obj " object" ); \
  41. } \
  42. }
  43. namespace {
  44. vec ReadFromFile ( const char *name ) {
  45. std::ifstream in ( name, std::ios_base::binary | std::ios_base::in );
  46. std::string temp;
  47. vec retVal;
  48. while ( std::getline ( in, temp ))
  49. retVal.push_back ( temp );
  50. return retVal;
  51. }
  52. void check_one ( const vec &haystack, const vec &needle, int expected ) {
  53. std::pair<vec::const_iterator, vec::const_iterator> res;
  54. std::pair<vec::const_iterator, vec::const_iterator> exp; // the expected result
  55. vec::const_iterator exp_start;
  56. if ( expected >= 0 )
  57. exp_start = haystack.begin () + expected;
  58. else if ( expected == -1 )
  59. exp_start = haystack.end (); // we didn't find it1
  60. else if ( expected == -2 )
  61. exp_start = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
  62. else
  63. throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
  64. if ( expected == -1 )
  65. exp = std::make_pair(haystack.end(), haystack.end());
  66. else
  67. exp = std::make_pair(exp_start, exp_start + needle.size());
  68. std::cout << "Pattern is " << needle.size () << " entries long" << std::endl;
  69. std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
  70. // First, the std library search
  71. vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
  72. if ( s_res != exp.first ) {
  73. std::cout << "Expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
  74. throw std::runtime_error ( "Unexpected result from std::search" );
  75. }
  76. runOne ( boyer_moore_search, stdDiff );
  77. runObject ( boyer_moore, stdDiff );
  78. runOne ( boyer_moore_horspool_search, stdDiff );
  79. runObject ( boyer_moore_horspool, stdDiff );
  80. runOne ( knuth_morris_pratt_search, stdDiff );
  81. runObject ( knuth_morris_pratt, stdDiff );
  82. }
  83. }
  84. BOOST_AUTO_TEST_CASE( test_main )
  85. {
  86. vec c1 = ReadFromFile ( "search_test_data/0001.corpus" );
  87. vec p1b = ReadFromFile ( "search_test_data/0002b.pat" );
  88. vec p1e = ReadFromFile ( "search_test_data/0002e.pat" );
  89. vec p1n = ReadFromFile ( "search_test_data/0002n.pat" );
  90. vec p1f = ReadFromFile ( "search_test_data/0002f.pat" );
  91. std::cout << std::ios::fixed << std::setprecision(4);
  92. // std::cout << "Corpus is " << c1.size () << " entries long\n";
  93. std::cout << "--- Beginning ---" << std::endl;
  94. check_one ( c1, p1b, 0 ); // Find it at position zero
  95. std::cout << "---- Middle -----" << std::endl;
  96. check_one ( c1, p1f, -2 ); // Don't know answer
  97. std::cout << "------ End ------" << std::endl;
  98. check_one ( c1, p1e, c1.size() - p1e.size ());
  99. std::cout << "--- Not found ---" << std::endl;
  100. check_one ( c1, p1n, -1 ); // Not found
  101. }