|
From: <jfb...@us...> - 2009-03-05 21:09:24
|
Revision: 999
http://loki-lib.svn.sourceforge.net/loki-lib/?rev=999&view=rev
Author: jfbastien
Date: 2009-03-05 21:09:20 +0000 (Thu, 05 Mar 2009)
Log Message:
-----------
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.
Modified Paths:
--------------
trunk/test/flex_string/main.cpp
Modified: trunk/test/flex_string/main.cpp
===================================================================
--- trunk/test/flex_string/main.cpp 2009-03-05 16:13:57 UTC (rev 998)
+++ trunk/test/flex_string/main.cpp 2009-03-05 21:09:20 UTC (rev 999)
@@ -83,6 +83,13 @@
char,
std::char_traits<char>,
std::allocator<char>,
+ SmallStringOpt<SimpleStringStorage<char, std::allocator<char> >, 126>
+ > my_string_SmallStringSimpleBigBuffer;
+
+ typedef flex_string<
+ char,
+ std::char_traits<char>,
+ std::allocator<char>,
SmallStringOpt<VectorStringStorage<char, std::allocator<char> >, 23>
> my_string_SmallStringVector;
@@ -119,13 +126,20 @@
const typename String::size_type size = random(0, maxSize);
String result(size, '\0');
size_t i = 0;
- for (; i != result.size(); ++i)
+ for (; i != size; ++i)
{
result[i] = random('a', 'z');
}
return result;
}
+// Specialize this method for different String types.
+template<class String>
+String Npos()
+{
+ return "{npos}";
+}
+
template<class String, class Integral>
String Num2String(Integral value)
{
@@ -136,6 +150,37 @@
}
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>
std::list<typename String::value_type> RandomList(typename String::size_type maxSize)
{
const typename String::size_type size = random(0, maxSize);
@@ -1308,10 +1353,7 @@
String compare_selfcopy(String & test)
{
int tristate = test.compare(String(test));
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1319,10 +1361,7 @@
{
String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(str);
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1331,10 +1370,7 @@
const typename String::size_type index = random(0, test.size());
const typename String::size_type length = random(0, test.size());
int tristate = test.compare(index, length, String(test));
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1344,10 +1380,7 @@
const typename String::size_type length = random(0, test.size());
String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(index, length, str);
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1359,10 +1392,7 @@
const typename String::size_type index2 = random(0, str.size());
const typename String::size_type length2 = random(0, str.size());
int tristate = test.compare(index, length, str, index2, length2);
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1374,10 +1404,7 @@
const typename String::size_type index2 = random(0, str.size());
const typename String::size_type length2 = random(0, str.size());
int tristate = test.compare(index, length, str, index2, length2);
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1385,10 +1412,7 @@
{
String str(RandomString<String>(MaxString<String>::value));
int tristate = test.compare(str.c_str());
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1399,10 +1423,7 @@
const typename String::size_type length = random(0, test.size());
const typename String::size_type index2 = random(0, str.size());
int tristate = test.compare(index, length, str.c_str(), index2);
- if (tristate > 0) tristate = 1;
- else if (tristate < 0) tristate = 2;
- test = Num2String<String>(tristate);
- return test;
+ return Tristate2String<String>(tristate);
}
template<class String>
@@ -1647,21 +1668,22 @@
size_t count = 0;
using namespace StringsToTest;
- TestFunctions<std::string> testFunctions_std;
- TestFunctions<my_string_SimpleStorage> testFunctions_SimpleStorage;
- TestFunctions<my_string_AllocatorStorage> testFunctions_AllocatorStorage;
- TestFunctions<my_string_MallocatorStorage> testFunctions_MallocatorStorage;
- TestFunctions<my_string_VectorStorage> testFunctions_VectorStorage;
- TestFunctions<my_string_SmallStringSimple> testFunctions_SmallStringSimple;
- TestFunctions<my_string_SmallStringVector> testFunctions_SmallStringVector;
- TestFunctions<my_string_CowSimple> testFunctions_CowSimple;
- TestFunctions<my_string_CowAllocator> testFunctions_CowAllocator;
+ TestFunctions<std::string> testFunctions_std;
+ TestFunctions<my_string_SimpleStorage> testFunctions_SimpleStorage;
+ TestFunctions<my_string_AllocatorStorage> testFunctions_AllocatorStorage;
+ TestFunctions<my_string_MallocatorStorage> testFunctions_MallocatorStorage;
+ TestFunctions<my_string_VectorStorage> testFunctions_VectorStorage;
+ TestFunctions<my_string_SmallStringSimple> testFunctions_SmallStringSimple;
+ TestFunctions<my_string_SmallStringSimpleBigBuffer> testFunctions_smallStringSimpleBigBuffer;
+ TestFunctions<my_string_SmallStringVector> testFunctions_SmallStringVector;
+ TestFunctions<my_string_CowSimple> testFunctions_CowSimple;
+ TestFunctions<my_string_CowAllocator> testFunctions_CowAllocator;
for (;;)
{
std::cout << ++count << '\r';
- const unsigned int seedForThisIteration = rand();
+ const unsigned int seedForThisIteration = count + rand();
srand(seedForThisIteration);
const std::string reference(Test<std::string>(count, testFunctions_std));
@@ -1698,6 +1720,12 @@
{
srand(seedForThisIteration);
+ const my_string_SmallStringSimpleBigBuffer tested(Test<my_string_SmallStringSimpleBigBuffer>(count, testFunctions_smallStringSimpleBigBuffer));
+ checkResults(reference, tested, testFunctions_smallStringSimpleBigBuffer, seedForThisIteration, count);
+ }
+
+ {
+ srand(seedForThisIteration);
const my_string_SmallStringVector tested(Test<my_string_SmallStringVector>(count, testFunctions_SmallStringVector));
checkResults(reference, tested, testFunctions_SmallStringVector, seedForThisIteration, count);
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|