Refactor.

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@991 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
jfbastien 2009-03-02 19:43:49 +00:00
parent 272dc6a91b
commit 0e615e0932

View file

@ -36,21 +36,69 @@
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <ctime> #include <ctime>
using namespace std; using namespace std;
namespace StringsToTest
{
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
SimpleStringStorage<char, std::allocator<char> >
> my_string_SimpleStorage;
typedef flex_string< typedef flex_string<
char, char,
std::char_traits<char>, std::char_traits<char>,
std::allocator<char>, std::allocator<char>,
AllocatorStringStorage<char, std::allocator<char> > AllocatorStringStorage<char, std::allocator<char> >
> my_string; > my_string_AllocatorStorage;
template class flex_string< typedef flex_string<
char,
std::char_traits<char>,
mallocator<char>,
AllocatorStringStorage<char, mallocator<char> >
> my_string_MallocatorStorage;
typedef flex_string<
char, char,
std::char_traits<char>, std::char_traits<char>,
std::allocator<char>, std::allocator<char>,
AllocatorStringStorage<char, std::allocator<char> > VectorStringStorage<char, std::allocator<char> >
>; > my_string_VectorStorage;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
SmallStringOpt<SimpleStringStorage<char, std::allocator<char> >, 31>
> my_string_SmallStringSimple;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
SmallStringOpt<VectorStringStorage<char, std::allocator<char> >, 23>
> my_string_SmallStringVector;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
CowStringOpt<SimpleStringStorage<char, std::allocator<char> > >
> my_string_CowSimple;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
CowStringOpt<AllocatorStringStorage<char, std::allocator<char> > >
> my_string_CowAllocator;
}
template <class Integral1, class Integral2> template <class Integral1, class Integral2>
Integral2 random(Integral1 low, Integral2 up) Integral2 random(Integral1 low, Integral2 up)
@ -98,14 +146,13 @@ std::list<char> RandomList(unsigned int maxSize)
int currentTest = 0; int currentTest = 0;
template <class String> template <class String>
String Test(String, unsigned int count, bool avoidAliasing) String Test(bool avoidAliasing)
{ {
typedef typename String::size_type size_type; typedef typename String::size_type size_type;
const size_type maxString = 1000; const size_type maxString = 1000;
String test; String test;
while (count--)
{
test = RandomString(&test, maxString); test = RandomString(&test, maxString);
static unsigned int functionSelector = 0; static unsigned int functionSelector = 0;
@ -861,7 +908,7 @@ String Test(String, unsigned int count, bool avoidAliasing)
assert(((functionSelector + 1) % 96) == 0); assert(((functionSelector + 1) % 96) == 0);
break; break;
} }
}
return test; return test;
} }
@ -893,95 +940,55 @@ void Compare()
unsigned long t = rand(); //time(0); unsigned long t = rand(); //time(0);
srand(t); srand(t);
const std::string reference = Test(std::string(), 1, true); const std::string reference = Test<std::string>(true);
using namespace StringsToTest;
{ {
srand(t); srand(t);
typedef flex_string< const my_string_SimpleStorage tested = Test<my_string_SimpleStorage>(false);
char,
std::char_traits<char>,
std::allocator<char>,
SimpleStringStorage<char, std::allocator<char> >
> my_string;
const my_string tested = Test(my_string(), 1, false);
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef flex_string< const my_string_AllocatorStorage tested = Test<my_string_AllocatorStorage>(false);
char,
std::char_traits<char>,
std::allocator<char>,
AllocatorStringStorage<char, std::allocator<char> >
> my_string;
const my_string tested = Test(my_string(), 1, false);
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef flex_string< const my_string_MallocatorStorage tested = Test<my_string_MallocatorStorage>(false);
char,
std::char_traits<char>,
mallocator<char>,
AllocatorStringStorage<char, mallocator<char> >
> my_string;
const my_string tested = Test(my_string(), 1, false);
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef flex_string< const my_string_VectorStorage tested = Test<my_string_VectorStorage>(false);
char,
std::char_traits<char>,
std::allocator<char>,
VectorStringStorage<char, std::allocator<char> >
> my_string;
const my_string tested = Test(my_string(), 1, false);
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef VectorStringStorage<char, std::allocator<char> > const my_string_SmallStringSimple tested(Test<my_string_SmallStringSimple>(false));
Storage;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
SmallStringOpt<Storage, 23>
> my_string;
static my_string sample;
const my_string tested(Test(sample, 1, false));
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef SimpleStringStorage<char, std::allocator<char> > const my_string_SmallStringVector tested(Test<my_string_SmallStringVector>(false));
Storage;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
CowStringOpt<Storage>
> my_string;
static my_string sample;
const my_string tested(Test(sample, 1, false));
checkResults(reference, tested); checkResults(reference, tested);
} }
{ {
srand(t); srand(t);
typedef AllocatorStringStorage<char, std::allocator<char> > const my_string_CowSimple tested(Test<my_string_CowSimple>(false));
Storage; checkResults(reference, tested);
typedef flex_string< }
char,
std::char_traits<char>, {
std::allocator<char>, srand(t);
CowStringOpt<Storage> const my_string_CowAllocator tested(Test<my_string_CowAllocator>(false));
> my_string;
static my_string sample;
const my_string tested(Test(sample, 1, false));
checkResults(reference, tested); checkResults(reference, tested);
} }
/* /*
@ -995,8 +1002,7 @@ void Compare()
std::allocator<unicode::UTF16Char>, std::allocator<unicode::UTF16Char>,
UTF16Encoding<Storage> UTF16Encoding<Storage>
> my_string; > my_string;
static my_string sample; const my_string tested(Test<my_string>(false));
const my_string tested(Test(sample, 1, false));
assert(tested.size() == reference.size()); assert(tested.size() == reference.size());
//assert(std::string(tested.data(), tested.size()) == reference); //assert(std::string(tested.data(), tested.size()) == reference);
} }
@ -1005,28 +1011,28 @@ void Compare()
} }
/// This function tests out a bug found by Jean-Francois Bastien in the find function. /// This function tests out a bug found by Jean-Francois Bastien in the find function.
void TestBug2536490( void ) template<class StringType>
void TestBug2536490()
{ {
StringType bug;
my_string bug; assert(StringType::npos == bug.find('6'));
std::cout << "Index of '6' in \"" << bug.c_str() << "\": " << bug.find('6') << std::endl;
bug = "12345"; bug = "12345";
std::cout << "Index of '6' in \"" << bug.c_str() << "\": " << bug.find('6') << std::endl; assert(StringType::npos == bug.find('6'));
bug = "123456"; bug = "123456";
std::cout << "Index of '6' in \"" << bug.c_str() << "\": " << bug.find('6') << std::endl; assert(5 == bug.find('6'));
bug = "12345"; bug = "12345";
std::cout << "Index of '6' in \"" << bug.c_str() << "\": " << bug.find('6') << std::endl; assert(StringType::npos == bug.find('6'));
bug = "12345"; bug = "12345";
std::cout << "Index of '123' in \"" << bug.c_str() << "\": " << bug.find("123") << std::endl; assert(0 == bug.find("123"));
bug = "12345"; bug = "12345";
std::cout << "Index of '12345' in \"" << bug.c_str() << "\": " << bug.find("12345") << std::endl; assert(0 == bug.find("12345"));
bug = "12345"; bug = "12345";
std::cout << "Index of '345' in \"" << bug.c_str() << "\": " << bug.find("345") << std::endl; assert(2 == bug.find("345"));
bug = "123456"; bug = "123456";
std::cout << "Index of '456' in \"" << bug.c_str() << "\": " << bug.find("456") << std::endl; assert(3 == bug.find("456"));
bug = "12345"; bug = "12345";
std::cout << "Index of '456' in \"" << bug.c_str() << "\": " << bug.find("456") << std::endl; assert(StringType::npos == bug.find("456"));
} }
/* /*
@ -1059,8 +1065,20 @@ int main()
*/ */
int main() int main()
{ {
TestBug2536490(); using namespace StringsToTest;
TestBug2536490<std::string>();
TestBug2536490<my_string_SimpleStorage>();
TestBug2536490<my_string_AllocatorStorage>();
TestBug2536490<my_string_MallocatorStorage>();
TestBug2536490<my_string_VectorStorage>();
TestBug2536490<my_string_SmallStringSimple>();
TestBug2536490<my_string_SmallStringVector>();
TestBug2536490<my_string_CowSimple>();
TestBug2536490<my_string_CowAllocator>();
srand(unsigned(time(0))); srand(unsigned(time(0)));
Compare(); Compare();
return 0; return 0;
} }