mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-03 21:44:30 +08:00
Fixes to integer conversions and their tests (#4278)
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user