Initial commit of string_ref; Basic sanity tests

[SVN r81360]
This commit is contained in:
Marshall Clow
2012-11-15 19:45:53 +00:00
parent 234656765a
commit 8b5c0d9114
4 changed files with 606 additions and 0 deletions

View File

@ -48,6 +48,10 @@ import testing ;
[ run hex_test4.cpp : : : : hex_test4 ]
[ compile-fail hex_fail1.cpp ]
# StringRef tests
[ run string_ref_test1.cpp : : : : string_ref_test1 ]
[ run string_ref_test2.cpp : : : : string_ref_test2 ]
# Wrapper tests
[ run wrapper_test1.cpp : : : : wrapper_test1 ]
;

66
test/string_ref_test1.cpp Normal file
View File

@ -0,0 +1,66 @@
/*
Copyright (c) Marshall Clow 2012-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
*/
#include <iostream>
#include <algorithm>
#include <string>
#include <boost/algorithm/string_ref.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
typedef boost::string_ref string_ref;
// Should be equal
void interop ( const std::string &str, string_ref ref ) {
// BOOST_CHECK ( str == ref );
BOOST_CHECK ( str.size () == ref.size ());
BOOST_CHECK ( std::equal ( str.begin (), str.end (), ref.begin ()));
}
void substr ( const std::string &str ) {
const size_t sz = str.size ();
string_ref ref ( str );
// Substrings at the end
for ( size_t i = 0; i <= sz; ++ i )
interop ( str.substr ( i ), ref.substr ( i ));
// Substrings at the beginning
for ( size_t i = 0; i <= sz; ++ i )
interop ( str.substr ( 0, i ), ref.substr ( 0, i ));
// All possible substrings
for ( size_t i = 0; i < sz; ++i )
for ( size_t j = i; j < sz; ++j )
interop ( str.substr ( i, j ), ref.substr ( i, j ));
}
const char *test_strings [] = {
"",
"1",
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"0123456789",
NULL
};
int test_main( int , char* [] ) {
const char **p = &test_strings[0];
while ( *p != NULL ) {
interop ( *p, *p );
substr ( *p );
p++;
}
return 0;
}

139
test/string_ref_test2.cpp Normal file
View File

@ -0,0 +1,139 @@
/*
Copyright (c) Marshall Clow 2012-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
*/
#include <iostream>
#include <boost/algorithm/string_ref.hpp>
#include <boost/test/included/test_exec_monitor.hpp>
typedef boost::string_ref string_ref;
void ends_with ( const char *arg ) {
string_ref sr ( arg );
string_ref sr2 ( arg );
const char *p = arg;
while ( !*p ) {
BOOST_CHECK ( sr.ends_with ( p ));
++p;
}
while ( !sr2.empty ()) {
BOOST_CHECK ( sr.ends_with ( sr2 ));
sr2.remove_prefix (1);
}
sr2 = arg;
while ( !sr2.empty ()) {
BOOST_CHECK ( sr.ends_with ( sr2 ));
sr2.remove_prefix (1);
}
BOOST_CHECK ( sr.ends_with ( string_ref ()));
}
void starts_with ( const char *arg ) {
string_ref sr ( arg );
string_ref sr2 ( arg );
const char *p = arg + std::strlen ( arg ) - 1;
while ( p >= arg ) {
std::string foo ( arg, p + 1 );
BOOST_CHECK ( sr.starts_with ( foo ));
--p;
}
while ( !sr2.empty ()) {
BOOST_CHECK ( sr.starts_with ( sr2 ));
sr2.remove_suffix (1);
}
BOOST_CHECK ( sr.starts_with ( string_ref ()));
}
void reverse ( const char *arg ) {
// Round trip
string_ref sr1 ( arg );
std::string string1 ( sr1.rbegin (), sr1.rend ());
string_ref sr2 ( string1 );
std::string string2 ( sr2.rbegin (), sr2.rend ());
BOOST_CHECK ( std::equal ( sr2.rbegin (), sr2.rend (), arg ));
BOOST_CHECK ( string2 == arg );
BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ()));
}
void find ( const char *arg ) {
string_ref sr1 ( arg );
const char *p = arg;
// Find everything at the start
while ( !sr1.empty ()) {
string_ref::size_type pos = sr1.find(*p);
BOOST_CHECK ( pos == 0 );
sr1.remove_prefix (1);
++p;
}
// Find everything at the end
sr1 = arg;
p = arg + strlen ( arg ) - 1;
while ( !sr1.empty ()) {
string_ref::size_type pos = sr1.rfind(*p);
BOOST_CHECK ( pos == sr1.size () - 1 );
sr1.remove_suffix (1);
--p;
}
// Find everything at the start
sr1 = arg;
p = arg;
while ( !sr1.empty ()) {
string_ref::size_type pos = sr1.find_first_of(*p);
BOOST_CHECK ( pos == 0 );
sr1.remove_prefix (1);
++p;
}
// Find everything at the end
sr1 = arg;
p = arg + strlen ( arg ) - 1;
while ( !sr1.empty ()) {
string_ref::size_type pos = sr1.find_last_of(*p);
BOOST_CHECK ( pos == sr1.size () - 1 );
sr1.remove_suffix (1);
--p;
}
}
const char *test_strings [] = {
"",
"0",
"abc",
"adsfadadiaef;alkdg;aljt;j agl;sjrl;tjs;lga;lretj;srg[w349u5209dsfadfasdfasdfadsf",
"abc\0asdfadsfasf",
NULL
};
int test_main( int , char* [] ) {
const char **p = &test_strings[0];
while ( *p != NULL ) {
starts_with ( *p );
ends_with ( *p );
reverse ( *p );
find ( *p );
p++;
}
return 0;
}