DynamicAny conversion limits

This commit is contained in:
Aleksandar Fabijanic
2007-04-25 01:11:49 +00:00
parent 109a6f47bd
commit f29f7cda53
4 changed files with 394 additions and 105 deletions

View File

@@ -38,7 +38,6 @@
#include "Poco/Bugcheck.h"
using namespace Poco;
@@ -940,11 +939,91 @@ void DynamicAnyTest::testConversionOperator()
assert (i == 42);
any = 123;
std::string s(any);
std::string s = any;//'s(any)' bombs on gcc 3.4.4
assert (s == "123");
}
void DynamicAnyTest::testLimitsInt()
{
testLimitsSigned<Int16, Int8>();
testLimitsSigned<Int32, Int8>();
testLimitsSigned<Int64, Int8>();
testLimitsFloatToInt<float, Int8>();
testLimitsFloatToInt<double, Int8>();
testLimitsSigned<Int16, char>();
testLimitsSigned<Int32, char>();
testLimitsSigned<Int64, char>();
testLimitsFloatToInt<float, char>();
testLimitsFloatToInt<double, char>();
testLimitsSigned<Int32, Int16>();
testLimitsSigned<Int64, Int16>();
testLimitsFloatToInt<float, Int16>();
testLimitsFloatToInt<double, Int16>();
testLimitsSigned<Int64, Int32>();
testLimitsFloatToInt<float, Int32>();
testLimitsFloatToInt<double, Int32>();
testLimitsSignedUnsigned<Int8, UInt8>();
testLimitsSignedUnsigned<Int16, UInt8>();
testLimitsSignedUnsigned<Int32, UInt8>();
testLimitsSignedUnsigned<Int64, UInt8>();
testLimitsFloatToInt<float, UInt8>();
testLimitsFloatToInt<double, UInt8>();
testLimitsSignedUnsigned<Int8, UInt16>();
testLimitsSignedUnsigned<Int16, UInt16>();
testLimitsSignedUnsigned<Int32, UInt16>();
testLimitsSignedUnsigned<Int64, UInt16>();
testLimitsFloatToInt<float, UInt16>();
testLimitsFloatToInt<double, UInt16>();
testLimitsSignedUnsigned<Int8, UInt32>();
testLimitsSignedUnsigned<Int16, UInt32>();
testLimitsSignedUnsigned<Int32, UInt32>();
testLimitsSignedUnsigned<Int64, UInt32>();
testLimitsFloatToInt<float, UInt32>();
testLimitsFloatToInt<double, UInt32>();
testLimitsSignedUnsigned<Int8, UInt64>();
testLimitsSignedUnsigned<Int16, UInt64>();
testLimitsSignedUnsigned<Int32, UInt64>();
testLimitsSignedUnsigned<Int64, UInt64>();
testLimitsFloatToInt<float, UInt64>();
testLimitsFloatToInt<double, UInt64>();
testLimitsUnsigned<UInt16, UInt8>();
testLimitsUnsigned<UInt32, UInt8>();
testLimitsUnsigned<UInt64, UInt8>();
testLimitsUnsigned<UInt32, UInt16>();
testLimitsUnsigned<UInt64, UInt16>();
testLimitsUnsigned<UInt64, UInt32>();
}
void DynamicAnyTest::testLimitsFloat()
{
if (std::numeric_limits<double>::max() != std::numeric_limits<float>::max())
{
double iMin = -1 * std::numeric_limits<float>::max();
DynamicAny da = iMin * 10;
try { float f; f = da; fail("must fail"); }
catch (RangeException&) {}
double iMax = std::numeric_limits<float>::max();
da = iMax * 10;
try { float f; f = da; fail("must fail"); }
catch (RangeException&) {}
}
}
void DynamicAnyTest::setUp()
{
}
@@ -975,6 +1054,8 @@ CppUnit::Test* DynamicAnyTest::suite()
CppUnit_addTest(pSuite, DynamicAnyTest, testLong);
CppUnit_addTest(pSuite, DynamicAnyTest, testULong);
CppUnit_addTest(pSuite, DynamicAnyTest, testConversionOperator);
CppUnit_addTest(pSuite, DynamicAnyTest, testLimitsInt);
CppUnit_addTest(pSuite, DynamicAnyTest, testLimitsFloat);
return pSuite;
}

View File

@@ -36,6 +36,8 @@
#include "Poco/Foundation.h"
#include "Poco/DynamicAny.h"
#include "Poco/Exception.h"
#include "CppUnit/TestCase.h"
@@ -61,10 +63,67 @@ public:
void testULong();
void testString();
void testConversionOperator();
void testLimitsInt();
void testLimitsFloat();
void setUp();
void tearDown();
static CppUnit::Test* suite();
private:
template<typename TL, typename TS>
void testLimitsSigned()
{
TL iMin = std::numeric_limits<TS>::min();
Poco::DynamicAny da = iMin - 1;
try { TS i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
TL iMax = std::numeric_limits<TS>::max();
da = iMax + 1;
try { TS i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
}
template<typename TL, typename TS>
void testLimitsFloatToInt()
{
Poco::DynamicAny da;
if (std::numeric_limits<TS>::is_signed)
{
TL iMin = static_cast<TL>(std::numeric_limits<TS>::min());
da = iMin * 10;
try { TS i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
}
TL iMax = static_cast<TL>(std::numeric_limits<TS>::max());
da = iMax * 10;
try { TS i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
}
template<typename TS, typename TU>
void testLimitsSignedUnsigned()
{
assert (std::numeric_limits<TS>::is_signed);
assert (!std::numeric_limits<TU>::is_signed);
TS iMin = std::numeric_limits<TS>::min();
Poco::DynamicAny da = iMin;
try { TU i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
}
template<typename TL, typename TS>
void testLimitsUnsigned()
{
TL iMax = std::numeric_limits<TS>::max();
Poco::DynamicAny da = iMax + 1;
try { TS i; i = da; fail("must fail"); }
catch (Poco::RangeException&) {}
}
};