NumberParser limits test

This commit is contained in:
Aleksandar Fabijanic 2012-05-17 02:05:44 +00:00
parent 425587f499
commit 8b43d003f0
2 changed files with 66 additions and 2 deletions

View File

@ -33,12 +33,23 @@
#include "NumberParserTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/NumberParser.h"
#include "Poco/Exception.h"
#include "Poco/Types.h"
using Poco::NumberParser;
using Poco::NumberFormatter;
using Poco::SyntaxException;
using Poco::Int8;
using Poco::UInt8;
using Poco::Int16;
using Poco::UInt16;
using Poco::Int32;
using Poco::UInt32;
#if defined(POCO_HAVE_INT64)
using Poco::Int64;
using Poco::UInt64;
#endif
NumberParserTest::NumberParserTest(const std::string& name): CppUnit::TestCase(name)
@ -75,6 +86,26 @@ void NumberParserTest::testParse()
assertEqualDelta(12.34, NumberParser::parseFloat("12.34"), 0.01);
}
void NumberParserTest::testLimits()
{
assert(testUpperLimit<Int8>());
assert(testLowerLimit<Int8>());
assert(testUpperLimit<UInt8>());
assert(testUpperLimit<Int16>());
assert(testLowerLimit<Int16>());
assert(testUpperLimit<UInt16>());
assert(testUpperLimit<Int32>());
assert(testUpperLimit<UInt32>());
#if defined(POCO_HAVE_INT64)
assert(testUpperLimit64<Int64>());
assert(testLowerLimit64<Int64>());
assert(testUpperLimit64<UInt64>());
#endif
}
void NumberParserTest::testParseError()
{
try
@ -199,6 +230,7 @@ CppUnit::Test* NumberParserTest::suite()
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("NumberParserTest");
CppUnit_addTest(pSuite, NumberParserTest, testParse);
CppUnit_addTest(pSuite, NumberParserTest, testLimits);
CppUnit_addTest(pSuite, NumberParserTest, testParseError);
return pSuite;

View File

@ -38,7 +38,9 @@
#include "Poco/Foundation.h"
#include "CppUnit/TestCase.h"
#include "Poco/NumberParser.h"
#include "Poco/NumberFormatter.h"
#include <limits>
class NumberParserTest: public CppUnit::TestCase
{
@ -47,6 +49,7 @@ public:
~NumberParserTest();
void testParse();
void testLimits();
void testParseError();
void setUp();
@ -55,6 +58,35 @@ public:
static CppUnit::Test* suite();
private:
template <class T> bool testUpperLimit()
{
T n = std::numeric_limits<T>::max();
std::string s = Poco::NumberFormatter::format(n);
return Poco::NumberParser::parse(s) == n;
}
template <class T> bool testLowerLimit()
{
T n = std::numeric_limits<T>::min();
std::string s = Poco::NumberFormatter::format(n);
return Poco::NumberParser::parse(s) == n;
}
#if defined(POCO_HAVE_INT64)
template <class T> bool testUpperLimit64()
{
T n = std::numeric_limits<T>::max();
std::string s = Poco::NumberFormatter::format(n);
return Poco::NumberParser::parse64(s) == n;
}
template <class T> bool testLowerLimit64()
{
T n = std::numeric_limits<T>::min();
std::string s = Poco::NumberFormatter::format(n);
return Poco::NumberParser::parse64(s) == n;
}
#endif
};