diff --git a/test/flex_string/main.cpp b/test/flex_string/main.cpp index 0c3aaf8..a6d71cb 100644 --- a/test/flex_string/main.cpp +++ b/test/flex_string/main.cpp @@ -79,6 +79,13 @@ namespace StringsToTest SmallStringOpt >, 31> > my_string_SmallStringSimple; + typedef flex_string< + char, + std::char_traits, + std::allocator, + SmallStringOpt >, 126> + > my_string_SmallStringSimpleBigBuffer; + typedef flex_string< char, std::char_traits, @@ -119,13 +126,20 @@ String RandomString(size_t maxSize) 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 +String Npos() +{ + return "{npos}"; +} + template String Num2String(Integral value) { @@ -135,6 +149,37 @@ String Num2String(Integral value) return stream.str().c_str(); } +template +String Num2String(typename String::size_type value) +{ + if(String::npos != value) + { + typedef typename String::value_type CharType; + std::basic_ostringstream, std::allocator > 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(); + } +} + +// Some comparison functions return 0 or a value greater/smaller than zero. +// This function makes the greater/smaller than zero specification implementation-independent. +template +String Tristate2String(int tristate) +{ + if(0 == tristate) + return Num2String(0); + else if(0 < tristate) + return Num2String(1); + else + return Num2String(2); +} + template std::list RandomList(typename String::size_type maxSize) { @@ -1308,10 +1353,7 @@ namespace Tests String compare_selfcopy(String & test) { int tristate = test.compare(String(test)); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; - test = Num2String(tristate); - return test; + return Tristate2String(tristate); } template @@ -1319,10 +1361,7 @@ namespace Tests { String str(RandomString(MaxString::value)); int tristate = test.compare(str); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; - test = Num2String(tristate); - return test; + return Tristate2String(tristate); } template @@ -1331,10 +1370,7 @@ namespace Tests 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(tristate); - return test; + return Tristate2String(tristate); } template @@ -1344,10 +1380,7 @@ namespace Tests const typename String::size_type length = random(0, test.size()); String str(RandomString(MaxString::value)); int tristate = test.compare(index, length, str); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; - test = Num2String(tristate); - return test; + return Tristate2String(tristate); } template @@ -1359,10 +1392,7 @@ namespace Tests 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(tristate); - return test; + return Tristate2String(tristate); } template @@ -1374,10 +1404,7 @@ namespace Tests 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(tristate); - return test; + return Tristate2String(tristate); } template @@ -1385,10 +1412,7 @@ namespace Tests { String str(RandomString(MaxString::value)); int tristate = test.compare(str.c_str()); - if (tristate > 0) tristate = 1; - else if (tristate < 0) tristate = 2; - test = Num2String(tristate); - return test; + return Tristate2String(tristate); } template @@ -1399,10 +1423,7 @@ namespace Tests 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(tristate); - return test; + return Tristate2String(tristate); } template @@ -1647,21 +1668,22 @@ void Compare() size_t count = 0; using namespace StringsToTest; - TestFunctions testFunctions_std; - TestFunctions testFunctions_SimpleStorage; - TestFunctions testFunctions_AllocatorStorage; - TestFunctions testFunctions_MallocatorStorage; - TestFunctions testFunctions_VectorStorage; - TestFunctions testFunctions_SmallStringSimple; - TestFunctions testFunctions_SmallStringVector; - TestFunctions testFunctions_CowSimple; - TestFunctions testFunctions_CowAllocator; + TestFunctions testFunctions_std; + TestFunctions testFunctions_SimpleStorage; + TestFunctions testFunctions_AllocatorStorage; + TestFunctions testFunctions_MallocatorStorage; + TestFunctions testFunctions_VectorStorage; + TestFunctions testFunctions_SmallStringSimple; + TestFunctions testFunctions_smallStringSimpleBigBuffer; + TestFunctions testFunctions_SmallStringVector; + TestFunctions testFunctions_CowSimple; + TestFunctions 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(count, testFunctions_std)); @@ -1696,6 +1718,12 @@ void Compare() checkResults(reference, tested, testFunctions_SmallStringSimple, seedForThisIteration, count); } + { + srand(seedForThisIteration); + const my_string_SmallStringSimpleBigBuffer tested(Test(count, testFunctions_smallStringSimpleBigBuffer)); + checkResults(reference, tested, testFunctions_smallStringSimpleBigBuffer, seedForThisIteration, count); + } + { srand(seedForThisIteration); const my_string_SmallStringVector tested(Test(count, testFunctions_SmallStringVector));