forked from boostorg/algorithm
More tests for string_ref
[SVN r81884]
This commit is contained in:
@ -8,6 +8,7 @@
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring> // for std::strchr
|
||||
|
||||
#include <boost/algorithm/string_ref.hpp>
|
||||
|
||||
@ -16,6 +17,7 @@
|
||||
typedef boost::string_ref string_ref;
|
||||
|
||||
void ends_with ( const char *arg ) {
|
||||
const size_t sz = strlen ( arg );
|
||||
string_ref sr ( arg );
|
||||
string_ref sr2 ( arg );
|
||||
const char *p = arg;
|
||||
@ -36,10 +38,16 @@ void ends_with ( const char *arg ) {
|
||||
sr2.remove_prefix (1);
|
||||
}
|
||||
|
||||
BOOST_CHECK ( sr.ends_with ( string_ref ()));
|
||||
char ch = sz == 0 ? '\0' : arg [ sz - 1 ];
|
||||
sr2 = arg;
|
||||
if ( sz > 0 )
|
||||
BOOST_CHECK ( sr2.ends_with ( ch ));
|
||||
BOOST_CHECK ( !sr2.ends_with ( ++ch ));
|
||||
BOOST_CHECK ( sr2.ends_with ( string_ref ()));
|
||||
}
|
||||
|
||||
void starts_with ( const char *arg ) {
|
||||
const size_t sz = strlen ( arg );
|
||||
string_ref sr ( arg );
|
||||
string_ref sr2 ( arg );
|
||||
const char *p = arg + std::strlen ( arg ) - 1;
|
||||
@ -54,7 +62,12 @@ void starts_with ( const char *arg ) {
|
||||
sr2.remove_suffix (1);
|
||||
}
|
||||
|
||||
BOOST_CHECK ( sr.starts_with ( string_ref ()));
|
||||
char ch = *arg;
|
||||
sr2 = arg;
|
||||
if ( sz > 0 )
|
||||
BOOST_CHECK ( sr2.starts_with ( ch ));
|
||||
BOOST_CHECK ( !sr2.starts_with ( ++ch ));
|
||||
BOOST_CHECK ( sr2.starts_with ( string_ref ()));
|
||||
}
|
||||
|
||||
void reverse ( const char *arg ) {
|
||||
@ -71,10 +84,56 @@ void reverse ( const char *arg ) {
|
||||
|
||||
|
||||
void find ( const char *arg ) {
|
||||
string_ref sr1 ( arg );
|
||||
const char *p = arg;
|
||||
|
||||
string_ref sr1;
|
||||
string_ref sr2;
|
||||
const char *p;
|
||||
|
||||
// Look for each character in the string(searching from the start)
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_ref::size_type pos = sr1.find(*p);
|
||||
BOOST_CHECK ( pos != string_ref::npos && ( pos <= p - arg ));
|
||||
++p;
|
||||
}
|
||||
|
||||
// Look for each character in the string (searching from the end)
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_ref::size_type pos = sr1.rfind(*p);
|
||||
BOOST_CHECK ( pos != string_ref::npos && pos < sr1.size () && ( pos >= p - arg ));
|
||||
++p;
|
||||
}
|
||||
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
// for all possible chars, see if we find them in the right place.
|
||||
// Note that strchr will/might do the _wrong_ thing if we search for NULL
|
||||
for ( int ch = 1; ch < 256; ++ch ) {
|
||||
string_ref::size_type pos = sr1.find(ch);
|
||||
const char *strp = std::strchr ( arg, ch );
|
||||
BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos ));
|
||||
if ( strp != NULL )
|
||||
BOOST_CHECK (( strp - arg ) == pos );
|
||||
}
|
||||
|
||||
sr1 = arg;
|
||||
p = arg;
|
||||
// for all possible chars, see if we find them in the right place.
|
||||
// Note that strchr will/might do the _wrong_ thing if we search for NULL
|
||||
for ( int ch = 1; ch < 256; ++ch ) {
|
||||
string_ref::size_type pos = sr1.rfind(ch);
|
||||
const char *strp = std::strrchr ( arg, ch );
|
||||
BOOST_CHECK (( strp == NULL ) == ( pos == string_ref::npos ));
|
||||
if ( strp != NULL )
|
||||
BOOST_CHECK (( strp - arg ) == pos );
|
||||
}
|
||||
|
||||
|
||||
// Find everything at the start
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( !sr1.empty ()) {
|
||||
string_ref::size_type pos = sr1.find(*p);
|
||||
BOOST_CHECK ( pos == 0 );
|
||||
@ -112,12 +171,65 @@ void find ( const char *arg ) {
|
||||
sr1.remove_suffix (1);
|
||||
--p;
|
||||
}
|
||||
|
||||
// Basic sanity checking for "find_first_of / find_first_not_of"
|
||||
sr1 = arg;
|
||||
sr2 = arg;
|
||||
while ( !sr1.empty() ) {
|
||||
BOOST_CHECK ( sr1.find_first_of ( sr2 ) == 0 );
|
||||
BOOST_CHECK ( sr1.find_first_not_of ( sr2 ) == string_ref::npos );
|
||||
sr1.remove_prefix ( 1 );
|
||||
}
|
||||
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_ref::size_type pos1 = sr1.find_first_of(*p);
|
||||
string_ref::size_type pos2 = sr1.find_first_not_of(*p);
|
||||
BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 <= ( p - arg ));
|
||||
if ( pos2 != string_ref::npos ) {
|
||||
for ( size_t i = 0 ; i < pos2; ++i )
|
||||
BOOST_CHECK ( sr1[i] == *p );
|
||||
BOOST_CHECK ( sr1 [ pos2 ] != *p );
|
||||
}
|
||||
|
||||
BOOST_CHECK ( pos2 != pos1 );
|
||||
++p;
|
||||
}
|
||||
|
||||
// Basic sanity checking for "find_last_of / find_last_not_of"
|
||||
sr1 = arg;
|
||||
sr2 = arg;
|
||||
while ( !sr1.empty() ) {
|
||||
BOOST_CHECK ( sr1.find_last_of ( sr2 ) == ( sr1.size () - 1 ));
|
||||
BOOST_CHECK ( sr1.find_last_not_of ( sr2 ) == string_ref::npos );
|
||||
sr1.remove_suffix ( 1 );
|
||||
}
|
||||
|
||||
p = arg;
|
||||
sr1 = arg;
|
||||
while ( *p ) {
|
||||
string_ref::size_type pos1 = sr1.find_last_of(*p);
|
||||
string_ref::size_type pos2 = sr1.find_last_not_of(*p);
|
||||
BOOST_CHECK ( pos1 != string_ref::npos && pos1 < sr1.size () && pos1 >= ( p - arg ));
|
||||
BOOST_CHECK ( pos2 == string_ref::npos || pos1 < sr1.size ());
|
||||
if ( pos2 != string_ref::npos ) {
|
||||
for ( size_t i = sr1.size () -1 ; i > pos2; --i )
|
||||
BOOST_CHECK ( sr1[i] == *p );
|
||||
BOOST_CHECK ( sr1 [ pos2 ] != *p );
|
||||
}
|
||||
|
||||
BOOST_CHECK ( pos2 != pos1 );
|
||||
++p;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
const char *test_strings [] = {
|
||||
"",
|
||||
"0",
|
||||
"abc",
|
||||
"AAA", // all the same
|
||||
"adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
|
||||
"abc\0asdfadsfasf",
|
||||
NULL
|
||||
|
Reference in New Issue
Block a user