search_test1.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. */
  7. #include <boost/algorithm/searching/boyer_moore.hpp>
  8. #include <boost/algorithm/searching/boyer_moore_horspool.hpp>
  9. #include <boost/algorithm/searching/knuth_morris_pratt.hpp>
  10. #define BOOST_TEST_MAIN
  11. #include <boost/test/unit_test.hpp>
  12. #include <iostream>
  13. #include <string>
  14. #include <vector>
  15. namespace ba = boost::algorithm;
  16. template <typename Iter>
  17. std::string make_str ( Iter first, std::size_t len ) {
  18. std::string retVal ( len + 2, '\'' );
  19. std::copy ( first, first+len, retVal.begin () + 1);
  20. return retVal;
  21. }
  22. namespace {
  23. // Check using iterators
  24. template<typename Container>
  25. void check_one_iter ( const Container &haystack, const std::string &needle, int expected ) {
  26. typedef typename Container::const_iterator iter_type;
  27. typedef typename std::pair<iter_type, iter_type> ret_type;
  28. typedef std::string::const_iterator pattern_type;
  29. iter_type hBeg = haystack.begin ();
  30. iter_type hEnd = haystack.end ();
  31. pattern_type nBeg = needle.begin ();
  32. pattern_type nEnd = needle.end ();
  33. // iter_type ret0 = std::search (hBeg, hEnd, nBeg, nEnd);
  34. ret_type ret1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
  35. ret_type ret1r = ba::boyer_moore_search (haystack, nBeg, nEnd);
  36. ret_type ret2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
  37. ret_type ret3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
  38. iter_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
  39. // iter_type it1 = ret1.first;
  40. // iter_type it1r = ret1r.first;
  41. // iter_type it2 = ret2.first;
  42. // iter_type it3 = ret3.first;
  43. const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
  44. std::cout << "(Iterators) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
  45. try {
  46. if ( it0 != ret1.first ) {
  47. throw std::runtime_error (
  48. std::string ( "results mismatch between std::search and boyer-moore search" ));
  49. }
  50. if ( ret1.first != ret1r.first || ret1.second != ret1r.second ) {
  51. throw std::runtime_error (
  52. std::string ( "results mismatch between iterator and range boyer_moore search" ));
  53. }
  54. if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
  55. throw std::runtime_error (
  56. std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
  57. }
  58. if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
  59. throw std::runtime_error (
  60. std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
  61. }
  62. }
  63. catch ( ... ) {
  64. std::cout << "Searching for: " << needle << std::endl;
  65. std::cout << "Expected: " << expected << "\n";
  66. std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
  67. std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
  68. std::cout << " bm(r): " << std::distance ( hBeg, ret1r.first ) << "\n";
  69. std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
  70. std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
  71. std::cout << std::flush;
  72. throw ;
  73. }
  74. BOOST_CHECK_EQUAL ( dist, expected );
  75. }
  76. // Check using pointers
  77. // We're assuming that the container implements contiguous storage here.
  78. template<typename Container>
  79. void check_one_pointer ( const Container &haystack, const std::string &needle, int expected ) {
  80. typedef const typename Container::value_type *ptr_type;
  81. typedef typename std::pair<ptr_type, ptr_type> ret_type;
  82. ptr_type hBeg = haystack.size () == 0 ? NULL : &*haystack.begin ();
  83. ptr_type hEnd = hBeg + haystack.size ();
  84. ptr_type nBeg = needle.size () == 0 ? NULL : &*needle.begin ();
  85. ptr_type nEnd = nBeg + needle.size ();
  86. ptr_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
  87. ret_type ret1 = ba::boyer_moore_search (hBeg, hEnd, nBeg, nEnd);
  88. ret_type ret2 = ba::boyer_moore_horspool_search (hBeg, hEnd, nBeg, nEnd);
  89. ret_type ret3 = ba::knuth_morris_pratt_search (hBeg, hEnd, nBeg, nEnd);
  90. const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
  91. std::cout << "(Pointers) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
  92. try {
  93. if ( it0 != ret1.first ) {
  94. throw std::runtime_error (
  95. std::string ( "results mismatch between std::search and boyer-moore search" ));
  96. }
  97. if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
  98. throw std::runtime_error (
  99. std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
  100. }
  101. if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
  102. throw std::runtime_error (
  103. std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
  104. }
  105. }
  106. catch ( ... ) {
  107. std::cout << "Searching for: " << needle << std::endl;
  108. std::cout << "Expected: " << expected << "\n";
  109. std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
  110. std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
  111. std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
  112. std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
  113. std::cout << std::flush;
  114. throw ;
  115. }
  116. BOOST_CHECK_EQUAL ( dist, expected );
  117. }
  118. // Check using objects
  119. template<typename Container>
  120. void check_one_object ( const Container &haystack, const std::string &needle, int expected ) {
  121. typedef typename Container::const_iterator iter_type;
  122. typedef typename std::pair<iter_type, iter_type> ret_type;
  123. typedef std::string::const_iterator pattern_type;
  124. iter_type hBeg = haystack.begin ();
  125. iter_type hEnd = haystack.end ();
  126. pattern_type nBeg = needle.begin ();
  127. pattern_type nEnd = needle.end ();
  128. ba::boyer_moore<pattern_type> bm_r = ba::make_boyer_moore ( needle );
  129. ba::boyer_moore<pattern_type> bm ( nBeg, nEnd );
  130. ba::boyer_moore_horspool<pattern_type> bmh ( nBeg, nEnd );
  131. ba::knuth_morris_pratt<pattern_type> kmp ( nBeg, nEnd );
  132. iter_type it0 = std::search (hBeg, hEnd, nBeg, nEnd);
  133. ret_type ret1 = bm (hBeg, hEnd);
  134. ret_type ret1r = bm (haystack);
  135. ret_type retr1 = bm_r (hBeg, hEnd);
  136. ret_type retr1r = bm_r (haystack);
  137. ret_type ret2 = bmh (hBeg, hEnd);
  138. ret_type ret3 = kmp (hBeg, hEnd);
  139. const int dist = ret1.first == hEnd ? -1 : std::distance ( hBeg, ret1.first );
  140. std::cout << "(Objects) Pattern is " << needle.length () << ", haysstack is " << haystack.length () << " chars long; " << std::endl;
  141. try {
  142. if ( it0 != ret1.first ) {
  143. throw std::runtime_error (
  144. std::string ( "results mismatch between std::search and boyer-moore search" ));
  145. }
  146. if ( ret1.first != ret1r.first || ret1.second != ret1r.second ) {
  147. throw std::runtime_error (
  148. std::string ( "results mismatch between iterator and range boyer_moore search(1)" ));
  149. }
  150. if ( ret1.first != retr1.first || ret1.second != retr1.second ) {
  151. throw std::runtime_error (
  152. std::string ( "results mismatch between iterator and range boyer_moore search(2)" ));
  153. }
  154. if ( ret1.first != retr1r.first || ret1.second != retr1r.second ) {
  155. throw std::runtime_error (
  156. std::string ( "results mismatch between iterator and range boyer_moore search(3)" ));
  157. }
  158. if ( ret1.first != ret2.first || ret1.second != ret2.second ) {
  159. throw std::runtime_error (
  160. std::string ( "results mismatch between boyer-moore and boyer-moore-horspool search" ));
  161. }
  162. if ( ret1.first != ret3.first || ret1.second != ret3.second ) {
  163. throw std::runtime_error (
  164. std::string ( "results mismatch between boyer-moore and knuth-morris-pratt search" ));
  165. }
  166. }
  167. catch ( ... ) {
  168. std::cout << "Searching for: " << needle << std::endl;
  169. std::cout << "Expected: " << expected << "\n";
  170. std::cout << " std: " << std::distance ( hBeg, it0 ) << "\n";
  171. std::cout << " bm: " << std::distance ( hBeg, ret1.first ) << "\n";
  172. std::cout << " bm(r1): " << std::distance ( hBeg, ret1r.first ) << "\n";
  173. std::cout << " bm(r2): " << std::distance ( hBeg, retr1.first ) << "\n";
  174. std::cout << " bm(r3): " << std::distance ( hBeg, retr1r.first ) << "\n";
  175. std::cout << " bmh: " << std::distance ( hBeg, ret2.first ) << "\n";
  176. std::cout << " kpm: " << std::distance ( hBeg, ret3.first )<< "\n";
  177. std::cout << std::flush;
  178. throw ;
  179. }
  180. BOOST_CHECK_EQUAL ( dist, expected );
  181. }
  182. template<typename Container>
  183. void check_one ( const Container &haystack, const std::string &needle, int expected ) {
  184. check_one_iter ( haystack, needle, expected );
  185. check_one_pointer ( haystack, needle, expected );
  186. check_one_object ( haystack, needle, expected );
  187. }
  188. }
  189. BOOST_AUTO_TEST_CASE( test_main )
  190. {
  191. std::string haystack1 ( "NOW AN FOWE\220ER ANNMAN THE ANPANMANEND" );
  192. std::string needle1 ( "ANPANMAN" );
  193. std::string needle2 ( "MAN THE" );
  194. std::string needle3 ( "WE\220ER" );
  195. std::string needle4 ( "NOW " ); // At the beginning
  196. std::string needle5 ( "NEND" ); // At the end
  197. std::string needle6 ( "NOT FOUND" ); // Nowhere
  198. std::string needle7 ( "NOT FO\340ND" ); // Nowhere
  199. std::string haystack2 ( "ABC ABCDAB ABCDABCDABDE" );
  200. std::string needle11 ( "ABCDABD" );
  201. std::string haystack3 ( "abra abracad abracadabra" );
  202. std::string needle12 ( "abracadabra" );
  203. std::string needle13 ( "" );
  204. std::string haystack4 ( "" );
  205. check_one ( haystack1, needle1, 26 );
  206. check_one ( haystack1, needle2, 18 );
  207. check_one ( haystack1, needle3, 9 );
  208. check_one ( haystack1, needle4, 0 );
  209. check_one ( haystack1, needle5, 33 );
  210. check_one ( haystack1, needle6, -1 );
  211. check_one ( haystack1, needle7, -1 );
  212. check_one ( needle1, haystack1, -1 ); // cant find long pattern in short corpus
  213. check_one ( haystack1, haystack1, 0 ); // find something in itself
  214. check_one ( haystack2, haystack2, 0 ); // find something in itself
  215. check_one ( haystack2, needle11, 15 );
  216. check_one ( haystack3, needle12, 13 );
  217. check_one ( haystack1, needle13, 0 ); // find the empty string
  218. check_one ( haystack4, needle1, -1 ); // can't find in an empty haystack
  219. // Mikhail Levin <svarneticist@gmail.com> found a problem, and this was the test
  220. // that triggered it.
  221. const std::string mikhail_pattern =
  222. "GATACACCTACCTTCACCAGTTACTCTATGCACTAGGTGCGCCAGGCCCATGCACAAGGGCTTGAGTGGATGGGAAGGA"
  223. "TGTGCCCTAGTGATGGCAGCATAAGCTACGCAGAGAAGTTCCAGGGCAGAGTCACCATGACCAGGGACACATCCACGAG"
  224. "CACAGCCTACATGGAGCTGAGCAGCCTGAGATCTGAAGACACGGCCATGTATTACTGTGGGAGAGATGTCTGGAGTGGT"
  225. "TATTATTGCCCCGGTAATATTACTACTACTACTACTACATGGACGTCTGGGGCAAAGGGACCACG"
  226. ;
  227. const std::string mikhail_corpus = std::string (8, 'a') + mikhail_pattern;
  228. check_one ( mikhail_corpus, mikhail_pattern, 8 );
  229. }