mirror of
https://github.com/OPM/opm-simulators.git
synced 2025-02-25 18:55:30 -06:00
Added int to size_t conversion. Added some static_asserts.
This commit is contained in:
parent
9418d5311d
commit
5373fb7a9c
@ -24,6 +24,7 @@
|
||||
#include <fmt/format.h>
|
||||
#include <limits>
|
||||
#include <opm/common/ErrorMacros.hpp>
|
||||
#include <type_traits>
|
||||
|
||||
|
||||
/**
|
||||
@ -38,7 +39,7 @@ namespace Opm::cuistl::detail
|
||||
{
|
||||
|
||||
/**
|
||||
* @brief convert converts a (on most relevant platform) 64 bits unsigned size_t to a signed 32 bits signed int
|
||||
* @brief convert converts a (on most relevant platforms) 64 bits unsigned size_t to a signed 32 bits signed int
|
||||
* @param s the unsigned integer
|
||||
* @throw std::invalid_argument exception if s is out of range for an int
|
||||
* @return converted s to int if s is within the range of int
|
||||
@ -48,6 +49,18 @@ namespace Opm::cuistl::detail
|
||||
int
|
||||
convert(size_t s)
|
||||
{
|
||||
static_assert(
|
||||
std::is_signed_v<int>,
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
static_assert(
|
||||
!std::is_signed_v<size_t>,
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
|
||||
static_assert(
|
||||
sizeof(int) <= sizeof(size_t),
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
|
||||
|
||||
if (s > size_t(std::numeric_limits<int>::max())) {
|
||||
OPM_THROW(std::invalid_argument,
|
||||
fmt::format("Trying to convert {} to int, but it is out of range. Maximum possible int: {}. ",
|
||||
@ -58,6 +71,36 @@ convert(size_t s)
|
||||
// We know it will be in range here:
|
||||
return int(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief convert converts a (on most relevant platforms) a 32 bit signed int to a 64 bits unsigned int
|
||||
* @param i the signed integer
|
||||
* @return converted i to size_t if it is a non-negative integer.
|
||||
*
|
||||
* @throw std::invalid_argument if i is negative.
|
||||
* @todo This can be done for more generic types, but then it is probably wise to wait for C++20's cmp-functions
|
||||
*/
|
||||
size_t
|
||||
convert(int i)
|
||||
{
|
||||
static_assert(
|
||||
std::is_signed_v<int>,
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
static_assert(
|
||||
!std::is_signed_v<size_t>,
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
|
||||
static_assert(
|
||||
sizeof(int) <= sizeof(size_t),
|
||||
"Weird architecture or my understanding of the standard is flawed. Better have a look at this function.");
|
||||
|
||||
|
||||
if (i < int(0)) {
|
||||
OPM_THROW(std::invalid_argument, fmt::format("Trying to convert the negative number {} to size_t.", i));
|
||||
}
|
||||
|
||||
return size_t(i);
|
||||
}
|
||||
} // namespace Opm::cuistl::detail
|
||||
|
||||
#endif
|
||||
|
@ -23,13 +23,13 @@
|
||||
#include <boost/test/unit_test.hpp>
|
||||
#include <opm/simulators/linalg/cuistl/detail/safe_conversion.hpp>
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestThrowsOutofRange)
|
||||
BOOST_AUTO_TEST_CASE(TestToIntThrowsOutofRange)
|
||||
{
|
||||
BOOST_CHECK_THROW(Opm::cuistl::detail::convert(size_t(std::numeric_limits<int>::max()) + size_t(1));
|
||||
, std::invalid_argument);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestConvertInRange)
|
||||
BOOST_AUTO_TEST_CASE(TestToIntConvertInRange)
|
||||
{
|
||||
// This might seem slow, but it is really fast:
|
||||
for (size_t i = 0; i <= size_t(1024 * 1024); ++i) {
|
||||
@ -39,3 +39,20 @@ BOOST_AUTO_TEST_CASE(TestConvertInRange)
|
||||
BOOST_CHECK_EQUAL(std::numeric_limits<int>::max(),
|
||||
Opm::cuistl::detail::convert(size_t(std::numeric_limits<int>::max())));
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestToSizeTThrowsOutofRange)
|
||||
{
|
||||
BOOST_CHECK_THROW(Opm::cuistl::detail::convert(-1);, std::invalid_argument);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(TestToSizeTConvertInRange)
|
||||
{
|
||||
// This might seem slow, but it is really fast:
|
||||
for (int i = 0; i <= 1024 * 1024; ++i) {
|
||||
BOOST_CHECK_EQUAL(size_t(i), Opm::cuistl::detail::convert(i));
|
||||
}
|
||||
|
||||
BOOST_CHECK_EQUAL(size_t(std::numeric_limits<int>::max()),
|
||||
Opm::cuistl::detail::convert(std::numeric_limits<int>::max()));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user