From 8b43d003f0a45e797c1c0cbe861dac6b08a0b616 Mon Sep 17 00:00:00 2001 From: Aleksandar Fabijanic Date: Thu, 17 May 2012 02:05:44 +0000 Subject: [PATCH] NumberParser limits test --- Foundation/testsuite/src/NumberParserTest.cpp | 34 ++++++++++++++++++- Foundation/testsuite/src/NumberParserTest.h | 34 ++++++++++++++++++- 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/Foundation/testsuite/src/NumberParserTest.cpp b/Foundation/testsuite/src/NumberParserTest.cpp index 2c40b73cf..c997daeb2 100644 --- a/Foundation/testsuite/src/NumberParserTest.cpp +++ b/Foundation/testsuite/src/NumberParserTest.cpp @@ -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()); + assert(testLowerLimit()); + assert(testUpperLimit()); + assert(testUpperLimit()); + assert(testLowerLimit()); + assert(testUpperLimit()); + assert(testUpperLimit()); + assert(testUpperLimit()); + +#if defined(POCO_HAVE_INT64) + assert(testUpperLimit64()); + assert(testLowerLimit64()); + assert(testUpperLimit64()); +#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; diff --git a/Foundation/testsuite/src/NumberParserTest.h b/Foundation/testsuite/src/NumberParserTest.h index 97776297a..8f4825cb4 100644 --- a/Foundation/testsuite/src/NumberParserTest.h +++ b/Foundation/testsuite/src/NumberParserTest.h @@ -38,7 +38,9 @@ #include "Poco/Foundation.h" #include "CppUnit/TestCase.h" - +#include "Poco/NumberParser.h" +#include "Poco/NumberFormatter.h" +#include 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 bool testUpperLimit() + { + T n = std::numeric_limits::max(); + std::string s = Poco::NumberFormatter::format(n); + return Poco::NumberParser::parse(s) == n; + } + + template bool testLowerLimit() + { + T n = std::numeric_limits::min(); + std::string s = Poco::NumberFormatter::format(n); + return Poco::NumberParser::parse(s) == n; + } + +#if defined(POCO_HAVE_INT64) + template bool testUpperLimit64() + { + T n = std::numeric_limits::max(); + std::string s = Poco::NumberFormatter::format(n); + return Poco::NumberParser::parse64(s) == n; + } + + template bool testLowerLimit64() + { + T n = std::numeric_limits::min(); + std::string s = Poco::NumberFormatter::format(n); + return Poco::NumberParser::parse64(s) == n; + } +#endif };