Add a small string optimization test with a buffer of 126.

Cache size() outside the loop for RandomString.
Modify Num2String so that npos is properly taken into account.
Add Tristate2String.
Modify seedForThisIteration so that its value is count + rand() instead of just rand(): using just rand meant that a lot of the initial seeds repeated because the pseudo-randomness of rand isn't good. Adding count should somewhat reduce the repetitions.
I'd consider using another random number generator, rand() is slow and has a bad distribution.


git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@999 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
jfbastien 2009-03-05 21:09:20 +00:00
parent 8d792783e0
commit 2d56e06cff

View file

@ -79,6 +79,13 @@ namespace StringsToTest
SmallStringOpt<SimpleStringStorage<char, std::allocator<char> >, 31> SmallStringOpt<SimpleStringStorage<char, std::allocator<char> >, 31>
> my_string_SmallStringSimple; > my_string_SmallStringSimple;
typedef flex_string<
char,
std::char_traits<char>,
std::allocator<char>,
SmallStringOpt<SimpleStringStorage<char, std::allocator<char> >, 126>
> my_string_SmallStringSimpleBigBuffer;
typedef flex_string< typedef flex_string<
char, char,
std::char_traits<char>, std::char_traits<char>,
@ -119,13 +126,20 @@ String RandomString(size_t maxSize)
const typename String::size_type size = random(0, maxSize); const typename String::size_type size = random(0, maxSize);
String result(size, '\0'); String result(size, '\0');
size_t i = 0; size_t i = 0;
for (; i != result.size(); ++i) for (; i != size; ++i)
{ {
result[i] = random('a', 'z'); result[i] = random('a', 'z');
} }
return result; return result;
} }
// Specialize this method for different String types.
template<class String>
String Npos()
{
return "{npos}";
}
template<class String, class Integral> template<class String, class Integral>
String Num2String(Integral value) String Num2String(Integral value)
{ {
@ -135,6 +149,37 @@ String Num2String(Integral value)
return stream.str().c_str(); return stream.str().c_str();
} }
template<class String>
String Num2String(typename String::size_type value)
{
if(String::npos != value)
{
typedef typename String::value_type CharType;
std::basic_ostringstream<CharType, std::char_traits<CharType>, std::allocator<CharType> > stream;
stream << value;
return stream.str().c_str();
}
else
{
// Not all strings will have the same value for npos.
// Since methods like find return npos on failure we want to represent npos in an implementation-independent manner.
return Npos<String>();
}
}
// Some comparison functions return 0 or a value greater/smaller than zero.
// This function makes the greater/smaller than zero specification implementation-independent.
template<class String>
String Tristate2String(int tristate)
{
if(0 == tristate)
return Num2String<String>(0);
else if(0 < tristate)
return Num2String<String>(1);
else
return Num2String<String>(2);
}
template<class String> template<class String>
std::list<typename String::value_type> RandomList(typename String::size_type maxSize) std::list<typename String::value_type> RandomList(typename String::size_type maxSize)
{ {
@ -1308,10 +1353,7 @@ namespace Tests
String compare_selfcopy(String & test) String compare_selfcopy(String & test)
{ {
int tristate = test.compare(String(test)); int tristate = test.compare(String(test));
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1319,10 +1361,7 @@ namespace Tests
{ {
String str(RandomString<String>(MaxString<String>::value)); String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(str); int tristate = test.compare(str);
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1331,10 +1370,7 @@ namespace Tests
const typename String::size_type index = random(0, test.size()); const typename String::size_type index = random(0, test.size());
const typename String::size_type length = random(0, test.size()); const typename String::size_type length = random(0, test.size());
int tristate = test.compare(index, length, String(test)); int tristate = test.compare(index, length, String(test));
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1344,10 +1380,7 @@ namespace Tests
const typename String::size_type length = random(0, test.size()); const typename String::size_type length = random(0, test.size());
String str(RandomString<String>(MaxString<String>::value)); String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(index, length, str); int tristate = test.compare(index, length, str);
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1359,10 +1392,7 @@ namespace Tests
const typename String::size_type index2 = random(0, str.size()); const typename String::size_type index2 = random(0, str.size());
const typename String::size_type length2 = random(0, str.size()); const typename String::size_type length2 = random(0, str.size());
int tristate = test.compare(index, length, str, index2, length2); int tristate = test.compare(index, length, str, index2, length2);
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1374,10 +1404,7 @@ namespace Tests
const typename String::size_type index2 = random(0, str.size()); const typename String::size_type index2 = random(0, str.size());
const typename String::size_type length2 = random(0, str.size()); const typename String::size_type length2 = random(0, str.size());
int tristate = test.compare(index, length, str, index2, length2); int tristate = test.compare(index, length, str, index2, length2);
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1385,10 +1412,7 @@ namespace Tests
{ {
String str(RandomString<String>(MaxString<String>::value)); String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(str.c_str()); int tristate = test.compare(str.c_str());
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1399,10 +1423,7 @@ namespace Tests
const typename String::size_type length = random(0, test.size()); const typename String::size_type length = random(0, test.size());
const typename String::size_type index2 = random(0, str.size()); const typename String::size_type index2 = random(0, str.size());
int tristate = test.compare(index, length, str.c_str(), index2); int tristate = test.compare(index, length, str.c_str(), index2);
if (tristate > 0) tristate = 1; return Tristate2String<String>(tristate);
else if (tristate < 0) tristate = 2;
test = Num2String<String>(tristate);
return test;
} }
template<class String> template<class String>
@ -1647,21 +1668,22 @@ void Compare()
size_t count = 0; size_t count = 0;
using namespace StringsToTest; using namespace StringsToTest;
TestFunctions<std::string> testFunctions_std; TestFunctions<std::string> testFunctions_std;
TestFunctions<my_string_SimpleStorage> testFunctions_SimpleStorage; TestFunctions<my_string_SimpleStorage> testFunctions_SimpleStorage;
TestFunctions<my_string_AllocatorStorage> testFunctions_AllocatorStorage; TestFunctions<my_string_AllocatorStorage> testFunctions_AllocatorStorage;
TestFunctions<my_string_MallocatorStorage> testFunctions_MallocatorStorage; TestFunctions<my_string_MallocatorStorage> testFunctions_MallocatorStorage;
TestFunctions<my_string_VectorStorage> testFunctions_VectorStorage; TestFunctions<my_string_VectorStorage> testFunctions_VectorStorage;
TestFunctions<my_string_SmallStringSimple> testFunctions_SmallStringSimple; TestFunctions<my_string_SmallStringSimple> testFunctions_SmallStringSimple;
TestFunctions<my_string_SmallStringVector> testFunctions_SmallStringVector; TestFunctions<my_string_SmallStringSimpleBigBuffer> testFunctions_smallStringSimpleBigBuffer;
TestFunctions<my_string_CowSimple> testFunctions_CowSimple; TestFunctions<my_string_SmallStringVector> testFunctions_SmallStringVector;
TestFunctions<my_string_CowAllocator> testFunctions_CowAllocator; TestFunctions<my_string_CowSimple> testFunctions_CowSimple;
TestFunctions<my_string_CowAllocator> testFunctions_CowAllocator;
for (;;) for (;;)
{ {
std::cout << ++count << '\r'; std::cout << ++count << '\r';
const unsigned int seedForThisIteration = rand(); const unsigned int seedForThisIteration = count + rand();
srand(seedForThisIteration); srand(seedForThisIteration);
const std::string reference(Test<std::string>(count, testFunctions_std)); const std::string reference(Test<std::string>(count, testFunctions_std));
@ -1696,6 +1718,12 @@ void Compare()
checkResults(reference, tested, testFunctions_SmallStringSimple, seedForThisIteration, count); checkResults(reference, tested, testFunctions_SmallStringSimple, seedForThisIteration, count);
} }
{
srand(seedForThisIteration);
const my_string_SmallStringSimpleBigBuffer tested(Test<my_string_SmallStringSimpleBigBuffer>(count, testFunctions_smallStringSimpleBigBuffer));
checkResults(reference, tested, testFunctions_smallStringSimpleBigBuffer, seedForThisIteration, count);
}
{ {
srand(seedForThisIteration); srand(seedForThisIteration);
const my_string_SmallStringVector tested(Test<my_string_SmallStringVector>(count, testFunctions_SmallStringVector)); const my_string_SmallStringVector tested(Test<my_string_SmallStringVector>(count, testFunctions_SmallStringVector));