Fixes to integer conversions and their tests (#4278)

This commit is contained in:
JLVarjo
2021-07-01 06:12:24 +03:00
committed by GitHub
parent b50e799da1
commit 333ae7d655
2 changed files with 28 additions and 4 deletions

View File

@@ -443,7 +443,7 @@ inline unsigned int rdvalue_cast<unsigned int>(RDValue_cast_t v) {
template <>
inline std::uint8_t rdvalue_cast<std::uint8_t>(RDValue_cast_t v) {
if (rdvalue_is<int>(v)) return boost::numeric_cast<std::uint8_t>(v.value.u);
if (rdvalue_is<int>(v)) return boost::numeric_cast<std::uint8_t>(v.value.i);
if (rdvalue_is<unsigned int>(v))
return boost::numeric_cast<std::uint8_t>(v.value.u);
throw boost::bad_any_cast();
@@ -451,9 +451,9 @@ inline std::uint8_t rdvalue_cast<std::uint8_t>(RDValue_cast_t v) {
template <>
inline std::uint16_t rdvalue_cast<std::uint16_t>(RDValue_cast_t v) {
if (rdvalue_is<int>(v)) return boost::numeric_cast<std::uint8_t>(v.value.i);
if (rdvalue_is<int>(v)) return boost::numeric_cast<std::uint16_t>(v.value.i);
if (rdvalue_is<unsigned int>(v))
return boost::numeric_cast<std::uint8_t>(v.value.u);
return boost::numeric_cast<std::uint16_t>(v.value.u);
throw boost::bad_any_cast();
}

View File

@@ -269,6 +269,29 @@ void testIntConversions() {
p.getProp<std::uint8_t>("foo");
p.getProp<std::uint16_t>("foo");
p.getProp<std::int16_t>("foo");
// Test that min/max values of smaller types do not under/overflow
p.setProp<unsigned int>("foo", 0);
p.getProp<std::uint8_t>("foo");
p.getProp<std::uint16_t>("foo");
p.setProp<unsigned int>("foo", 255);
p.getProp<std::uint8_t>("foo");
p.setProp<unsigned int>("foo", 65535);
p.getProp<std::uint16_t>("foo");
p.setProp<int>("foo", -128);
p.getProp<std::int8_t>("foo");
p.setProp<int>("foo", -32768);
p.getProp<std::int16_t>("foo");
p.setProp<int>("foo", 127);
p.getProp<std::int8_t>("foo");
p.setProp<int>("foo", 32767);
p.getProp<std::int16_t>("foo");
// Test some overflows
@@ -288,6 +311,7 @@ void testIntConversions() {
TEST_ASSERT(0);
} catch (boost::numeric::positive_overflow&) {
}
p.setProp<int>("foo", 65535 + 1);
try {
p.getProp<std::uint16_t>("foo"); // should fail
TEST_ASSERT(0);
@@ -298,7 +322,7 @@ void testIntConversions() {
try {
p.getProp<std::uint8_t>("foo"); // should fail
TEST_ASSERT(0);
} catch (boost::numeric::positive_overflow&) {
} catch (boost::numeric::negative_overflow&) {
}
p.getProp<std::int16_t>("foo"); // should pass