backport NumericString from develop (fixes #2250)

This commit is contained in:
Alex Fabijanic
2018-06-03 13:52:25 -05:00
parent 5fb10f6746
commit b30683fd6d
6 changed files with 638 additions and 107 deletions

View File

@@ -15,9 +15,10 @@
#include "Poco/Foundation.h"
#include "CppUnit/TestCase.h"
#include "Poco/CppUnit/TestCase.h"
#include "Poco/NumericString.h"
#include "Poco/MemoryStream.h"
#include "Poco/NumberFormatter.h"
class StringTest: public CppUnit::TestCase
@@ -42,10 +43,14 @@ public:
void testReplace();
void testReplaceInPlace();
void testCat();
void testStartsWith();
void testEndsWith();
void testStringToInt();
void testStringToFloat();
void testStringToDouble();
void testNumericStringPadding();
void testNumericStringLimit();
void testStringToFloatError();
void testNumericLocale();
void benchmarkStrToFloat();
@@ -91,6 +96,53 @@ private:
assertTrue (Poco::strToInt("000", result, 010)); assertTrue (result == 0);
}
template <typename Larger, typename Smaller>
void numericStringLimitSameSign()
{
Larger l = std::numeric_limits<Smaller>::max();
std::string str = Poco::NumberFormatter::format(l);
Smaller s;
assertTrue(Poco::strToInt<Smaller>(str, s, 10));
assertTrue(s == std::numeric_limits<Smaller>::max());
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
++l; str = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(str, s, 10));
}
template <typename Larger, typename Smaller>
void numericStringLowerLimit()
{
Larger l = std::numeric_limits<Smaller>::min();
std::string val = Poco::NumberFormatter::format(l);
Smaller s = -1;
assertFalse(s == std::numeric_limits<Smaller>::min());
assertTrue(Poco::strToInt<Smaller>(val, s, 10));
assertTrue (s == std::numeric_limits<Smaller>::min());
assertTrue(s == std::numeric_limits<Smaller>::min());
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
--l; val = Poco::NumberFormatter::format(l);
assertFalse(Poco::strToInt<Smaller>(val, s, 10));
}
template <typename T>
bool parseStream(const std::string& s, T& value)
{