mirror of
https://github.com/boostorg/algorithm.git
synced 2025-07-29 12:07:18 +02:00
@ -16,6 +16,7 @@ import testing ;
|
||||
[ run search_test1.cpp : : : : search_test1 ]
|
||||
[ run search_test2.cpp : : : : search_test2 ]
|
||||
[ run search_test3.cpp : : : : search_test3 ]
|
||||
[ run search_test4.cpp : : : : search_test4 ]
|
||||
[ compile-fail search_fail1.cpp : : : : ]
|
||||
[ compile-fail search_fail2.cpp : : : : ]
|
||||
[ compile-fail search_fail3.cpp : : : : ]
|
||||
@ -43,6 +44,7 @@ import testing ;
|
||||
[ run hex_test1.cpp : : : : hex_test1 ]
|
||||
[ run hex_test2.cpp : : : : hex_test2 ]
|
||||
[ run hex_test3.cpp : : : : hex_test3 ]
|
||||
[ run hex_test4.cpp : : : : hex_test4 ]
|
||||
[ compile-fail hex_fail1.cpp ]
|
||||
;
|
||||
}
|
||||
|
145
test/hex_test4.cpp
Normal file
145
test/hex_test4.cpp
Normal file
@ -0,0 +1,145 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2011-2012.
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
|
||||
Try ostream_iterators
|
||||
*/
|
||||
|
||||
#include <boost/config.hpp>
|
||||
#include <boost/algorithm/hex.hpp>
|
||||
#include <boost/test/included/test_exec_monitor.hpp>
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
namespace ba = boost::algorithm;
|
||||
|
||||
void test_short_input1 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
|
||||
catch ( const std::exception &ex ) { return; }
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_short_input1" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_short_input2 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
|
||||
catch ( const ba::hex_decode_error &ex ) { return; }
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_short_input2" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_short_input3 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
|
||||
catch ( const ba::not_enough_input &ex ) { return; }
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch ba::not_enough_input in test_short_input3" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
// Make sure that the right thing is thrown
|
||||
void test_short_input4 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( std::string ( "A" ), std::back_inserter(s)); }
|
||||
catch ( const ba::non_hex_input &ex ) { BOOST_CHECK ( false ); }
|
||||
catch ( const ba::not_enough_input &ex ) { return; }
|
||||
catch ( ... ) { BOOST_CHECK ( false ); }
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_short_input () {
|
||||
// BOOST_TEST_MESSAGE ( "Short input tests for boost::algorithm::unhex" );
|
||||
test_short_input1 ();
|
||||
test_short_input2 ();
|
||||
test_short_input3 ();
|
||||
test_short_input4 ();
|
||||
}
|
||||
|
||||
|
||||
void test_nonhex_input1 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( "01234FG1234", std::back_inserter(s)); }
|
||||
catch ( const std::exception &ex ) {
|
||||
BOOST_CHECK ( 'G' == *boost::get_error_info<ba::bad_char>(ex));
|
||||
return;
|
||||
}
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch std::exception in test_nonhex_input1" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_nonhex_input2 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( "012Z4FA1234", std::back_inserter(s)); }
|
||||
catch ( const ba::hex_decode_error &ex ) {
|
||||
BOOST_CHECK ( 'Z' == *boost::get_error_info<ba::bad_char>(ex));
|
||||
return;
|
||||
}
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch ba::hex_decode_error in test_nonhex_input2" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_nonhex_input3 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( "01234FA12Q4", std::back_inserter(s)); }
|
||||
catch ( const ba::non_hex_input &ex ) {
|
||||
BOOST_CHECK ( 'Q' == *boost::get_error_info<ba::bad_char>(ex));
|
||||
return;
|
||||
}
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input3" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
// Make sure that the right thing is thrown
|
||||
void test_nonhex_input4 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( "P1234FA1234", std::back_inserter(s)); }
|
||||
catch ( const ba::not_enough_input &ex ) { BOOST_CHECK ( false ); }
|
||||
catch ( const ba::non_hex_input &ex ) { return; }
|
||||
catch ( ... ) { BOOST_CHECK ( false ); }
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
// Make sure that the right thing is thrown
|
||||
void test_nonhex_input5 () {
|
||||
std::string s;
|
||||
|
||||
try { ba::unhex ( "012", std::back_inserter(s)); }
|
||||
catch ( const ba::non_hex_input &ex ) {
|
||||
BOOST_CHECK ( '\000' == *boost::get_error_info<ba::bad_char>(ex));
|
||||
return;
|
||||
}
|
||||
BOOST_TEST_MESSAGE ( "Failed to catch ba::non_hex_input in test_nonhex_input4" );
|
||||
BOOST_CHECK ( false );
|
||||
}
|
||||
|
||||
void test_nonhex_input () {
|
||||
// BOOST_TEST_MESSAGE ( "Non hex input tests for for boost::algorithm::unhex" );
|
||||
test_nonhex_input1 ();
|
||||
test_nonhex_input2 ();
|
||||
test_nonhex_input3 ();
|
||||
test_nonhex_input4 ();
|
||||
test_nonhex_input5 ();
|
||||
}
|
||||
|
||||
|
||||
|
||||
int test_main( int , char* [] )
|
||||
{
|
||||
test_short_input ();
|
||||
test_nonhex_input ();
|
||||
|
||||
return 0;
|
||||
}
|
@ -54,10 +54,10 @@ test_ordered(void)
|
||||
BOOST_CHECK ( ba::is_sorted_until ( a_range(strictlyIncreasingValues), std::less<int>()) == boost::end(strictlyIncreasingValues));
|
||||
|
||||
// Check for const and non-const arrays
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less<int>()) != a_end(constantValues));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less<int>()) != boost::end(constantValues));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) != a_end(nonConstantArray));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) != boost::end(nonConstantArray));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(constantValues), std::less<int>()) == a_end(constantValues));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( a_range(constantValues), std::less<int>()) == boost::end(constantValues));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(nonConstantArray), std::less<int>()) == a_end(nonConstantArray));
|
||||
BOOST_CHECK ( ba::is_sorted_until ( a_range(nonConstantArray), std::less<int>()) == boost::end(nonConstantArray));
|
||||
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues), std::less<int>()) == &randomValues[2] );
|
||||
BOOST_CHECK ( ba::is_sorted_until ( b_e(randomValues)) == &randomValues[2] );
|
||||
|
116
test/search_test4.cpp
Normal file
116
test/search_test4.cpp
Normal file
@ -0,0 +1,116 @@
|
||||
/*
|
||||
Copyright (c) Marshall Clow 2010-2012.
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0. (See accompanying
|
||||
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
||||
|
||||
For more information, see http://www.boost.org
|
||||
|
||||
Testing the range-based interfaces
|
||||
*/
|
||||
|
||||
#include <boost/algorithm/searching/boyer_moore.hpp>
|
||||
#include <boost/algorithm/searching/boyer_moore_horspool.hpp>
|
||||
#include <boost/algorithm/searching/knuth_morris_pratt.hpp>
|
||||
|
||||
#include <boost/test/included/test_exec_monitor.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
typedef std::vector<std::string> vec;
|
||||
#define NUM_TRIES 100
|
||||
|
||||
#define runOne(call, refDiff) { \
|
||||
res = boost::algorithm::call ( haystack, needle ); \
|
||||
if ( res != exp ) { \
|
||||
std::cout << "Expected " \
|
||||
<< exp - haystack.begin () << " got " \
|
||||
<< res - haystack.begin () << std::endl; \
|
||||
throw std::runtime_error \
|
||||
( "Unexpected result from " #call ); \
|
||||
} \
|
||||
}
|
||||
|
||||
#define runObject(obj, refDiff) { \
|
||||
boost::algorithm::obj <vec::const_iterator> s_o = \
|
||||
boost::algorithm::make_##obj ( needle ); \
|
||||
res = s_o ( haystack ); \
|
||||
if ( res != exp ) { \
|
||||
std::cout << "Expected " \
|
||||
<< exp - haystack.begin () << " got " \
|
||||
<< res - haystack.begin () << std::endl; \
|
||||
throw std::runtime_error \
|
||||
( "Unexpected result from " #obj " object" ); \
|
||||
} \
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
vec ReadFromFile ( const char *name ) {
|
||||
std::ifstream in ( name, std::ios_base::binary | std::ios_base::in );
|
||||
std::string temp;
|
||||
vec retVal;
|
||||
while ( std::getline ( in, temp ))
|
||||
retVal.push_back ( temp );
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
void check_one ( const vec &haystack, const vec &needle, int expected ) {
|
||||
|
||||
vec::const_iterator res;
|
||||
vec::const_iterator exp; // the expected result
|
||||
|
||||
if ( expected >= 0 )
|
||||
exp = haystack.begin () + expected;
|
||||
else if ( expected == -1 )
|
||||
exp = haystack.end (); // we didn't find it1
|
||||
else if ( expected == -2 )
|
||||
exp = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
|
||||
else
|
||||
throw std::logic_error ( "Expected must be -2, -1, or >= 0" );
|
||||
|
||||
std::cout << "Pattern is " << needle.size () << " entries long" << std::endl;
|
||||
std::cout << "Corpus is " << haystack.size () << " entries long" << std::endl;
|
||||
|
||||
// First, the std library search
|
||||
res = std::search ( haystack.begin (), haystack.end (), needle.begin (), needle.end ());
|
||||
if ( res != exp ) {
|
||||
std::cout << "Expected " << exp - haystack.begin () << " got " << res - haystack.begin () << std::endl;
|
||||
throw std::runtime_error ( "Unexpected result from std::search" );
|
||||
}
|
||||
|
||||
runOne ( boyer_moore_search, stdDiff );
|
||||
runObject ( boyer_moore, stdDiff );
|
||||
runOne ( boyer_moore_horspool_search, stdDiff );
|
||||
runObject ( boyer_moore_horspool, stdDiff );
|
||||
runOne ( knuth_morris_pratt_search, stdDiff );
|
||||
runObject ( knuth_morris_pratt, stdDiff );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int test_main( int , char* [] )
|
||||
{
|
||||
vec c1 = ReadFromFile ( "search_test_data/0001.corpus" );
|
||||
vec p1b = ReadFromFile ( "search_test_data/0002b.pat" );
|
||||
vec p1e = ReadFromFile ( "search_test_data/0002e.pat" );
|
||||
vec p1n = ReadFromFile ( "search_test_data/0002n.pat" );
|
||||
vec p1f = ReadFromFile ( "search_test_data/0002f.pat" );
|
||||
|
||||
std::cout << std::ios::fixed << std::setprecision(4);
|
||||
// std::cout << "Corpus is " << c1.size () << " entries long\n";
|
||||
std::cout << "--- Beginning ---" << std::endl;
|
||||
check_one ( c1, p1b, 0 ); // Find it at position zero
|
||||
std::cout << "---- Middle -----" << std::endl;
|
||||
check_one ( c1, p1f, -2 ); // Don't know answer
|
||||
std::cout << "------ End ------" << std::endl;
|
||||
check_one ( c1, p1e, c1.size() - p1e.size ());
|
||||
std::cout << "--- Not found ---" << std::endl;
|
||||
check_one ( c1, p1n, -1 ); // Not found
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user