• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2    Copyright (c) Marshall Clow 2010-2012.
3 
4    Distributed under the Boost Software License, Version 1.0. (See accompanying
5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 
7     For more information, see http://www.boost.org
8 */
9 
10 #include <boost/algorithm/searching/boyer_moore.hpp>
11 #include <boost/algorithm/searching/boyer_moore_horspool.hpp>
12 #include <boost/algorithm/searching/knuth_morris_pratt.hpp>
13 
14 #define BOOST_TEST_MAIN
15 #include <boost/test/unit_test.hpp>
16 
17 #include <ctime>        // for clock_t
18 #include <iostream>
19 #include <fstream>
20 #include <iomanip>
21 #include <algorithm>
22 #include <vector>
23 
24 typedef std::vector<char> vec;
25 #define NUM_TRIES   100
26 
27 #define runOne(call, refDiff)   { \
28     std::clock_t bTime, eTime;                              \
29     bTime = std::clock ();                                  \
30     for ( i = 0; i < NUM_TRIES; ++i ) {                     \
31         res = boost::algorithm::call                        \
32             ( haystack.begin (), haystack.end (),           \
33                         needle.begin (), needle.end ());    \
34         if ( res != exp ) {                                 \
35             std::cout << "On run # " << i << " expected "   \
36                 << exp.first - haystack.begin () << " got "       \
37                 << res.first - haystack.begin () << std::endl;    \
38             throw std::runtime_error                        \
39                 ( "Unexpected result from " #call );        \
40             }                                               \
41         }                                                   \
42     eTime = std::clock ();                                  \
43     printRes ( #call, eTime - bTime, refDiff ); }
44 
45 #define runObject(obj, refDiff) { \
46     std::clock_t bTime, eTime;                              \
47     bTime = std::clock ();                                  \
48     boost::algorithm::obj <vec::const_iterator>             \
49                 s_o ( needle.begin (), needle.end ());      \
50     for ( i = 0; i < NUM_TRIES; ++i ) {                     \
51         res = s_o ( haystack.begin (), haystack.end ());    \
52         if ( res != exp ) {                                 \
53             std::cout << "On run # " << i << " expected "   \
54             << exp.first - haystack.begin () << " got "           \
55             << res.first - haystack.begin () << std::endl;        \
56             throw std::runtime_error                        \
57             ( "Unexpected result from " #obj " object" );   \
58             }                                               \
59         }                                                   \
60     eTime = std::clock ();                                  \
61     printRes ( #obj " object", eTime - bTime, refDiff ); }
62 
63 
64 
65 namespace {
66 
ReadFromFile(const char * name)67     vec ReadFromFile ( const char *name ) {
68         std::ifstream in ( name, std::ios_base::binary | std::ios_base::in );
69         vec retVal;
70         std::istream_iterator<char, char> begin(in);
71         std::istream_iterator<char, char> end;
72 
73         std::copy ( begin, end, std::back_inserter ( retVal ));
74         return retVal;
75         }
76 
printRes(const char * prompt,unsigned long diff,unsigned long stdDiff)77     void printRes ( const char *prompt, unsigned long diff, unsigned long stdDiff ) {
78         std::cout
79             << std::setw(34) << prompt << " "
80             << std::setw(6)  << (  1.0 * diff) / CLOCKS_PER_SEC << " seconds\t"
81             << std::setw(5)  << (100.0 * diff) / stdDiff << "% \t"
82             << std::setw(12) << diff;
83         if ( diff > stdDiff )
84             std::cout << " !!";
85         std::cout << std::endl;
86         }
87 
check_one(const vec & haystack,const vec & needle,int expected)88     void check_one ( const vec &haystack, const vec &needle, int expected ) {
89         std::size_t i;
90         std::clock_t sTime;
91         unsigned long stdDiff;
92 
93         std::pair<vec::const_iterator, vec::const_iterator> res;
94         std::pair<vec::const_iterator, vec::const_iterator> exp;        // the expected result
95         vec::const_iterator exp_start;
96 
97         if ( expected >= 0 )
98             exp_start = haystack.begin () + expected;
99         else if ( expected == -1 )
100             exp_start = haystack.end ();      // we didn't find it!
101         else if ( expected == -2 )
102             exp_start = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
103         else
104             throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
105 
106 		if ( expected == -1 )
107 			exp = std::make_pair(haystack.end(), haystack.end());
108 		else
109 			exp = std::make_pair(exp_start, exp_start + needle.size());
110 
111         std::cout << "Pattern is " << needle.size ()   << " entries long" << std::endl;
112         std::cout << "Corpus  is " << haystack.size () << " entries long" << std::endl;
113 
114     //  First, the std library search
115         sTime = std::clock ();
116         for ( i = 0; i < NUM_TRIES; ++i ) {
117             vec::const_iterator s_res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
118             if ( s_res != exp.first ) {
119                 std::cout << "On run # " << i << " expected " << exp.first - haystack.begin () << " got " << s_res - haystack.begin () << std::endl;
120                 throw std::runtime_error ( "Unexpected result from std::search" );
121                 }
122             }
123         stdDiff = std::clock () - sTime;
124         printRes ( "std::search", stdDiff, stdDiff );
125 
126         runOne    ( boyer_moore_search,          stdDiff );
127         runObject ( boyer_moore,                 stdDiff );
128         runOne    ( boyer_moore_horspool_search, stdDiff );
129         runObject ( boyer_moore_horspool,        stdDiff );
130         runOne    ( knuth_morris_pratt_search,   stdDiff );
131         runObject ( knuth_morris_pratt,          stdDiff );
132         }
133     }
134 
BOOST_AUTO_TEST_CASE(test_main)135 BOOST_AUTO_TEST_CASE( test_main )
136 {
137     vec c1  = ReadFromFile ( "search_test_data/0001.corpus" );
138     vec p1b = ReadFromFile ( "search_test_data/0001b.pat" );
139     vec p1e = ReadFromFile ( "search_test_data/0001e.pat" );
140     vec p1n = ReadFromFile ( "search_test_data/0001n.pat" );
141     vec p1f = ReadFromFile ( "search_test_data/0001f.pat" );
142 
143     std::cout << std::ios::fixed << std::setprecision(4);
144 //  std::cout << "Corpus is " << c1.size () << " entries long\n";
145     std::cout << "--- Beginning ---" << std::endl;
146     check_one ( c1, p1b, 0 );       //  Find it at position zero
147     std::cout << "---- Middle -----" << std::endl;
148     check_one ( c1, p1f, -2 );      //  Don't know answer
149     std::cout << "------ End ------" << std::endl;
150     check_one ( c1, p1e, c1.size() - p1e.size ());
151     std::cout << "--- Not found ---" << std::endl;
152     check_one ( c1, p1n, -1 );      //  Not found
153     }
154