Commit ac26f0ba authored by Spencer Sherwin's avatar Spencer Sherwin

Merge branch '192-number-of-steps-overflows-to-negative-value' into 'master'

Resolve "Number of Steps Overflows to Negative Value"

Closes #192

See merge request !1113
parents 456f9bf5 4e9d5d0e
......@@ -27,6 +27,7 @@ v5.0.1
------
**CI**
- Switched to Gitlab CI (!1120, !1120, !1128, !1129)
- Added checked conversion from double to int in SessionReader (!1113)
v5.0.0
------
......
///////////////////////////////////////////////////////////////////////////////
//
// File CheckedCast.hpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: simple routines to check if the casting is narrowing
//
///////////////////////////////////////////////////////////////////////////////
#ifndef NEKTAR_LIB_UTILITIES_CHECKEDCAST_H
#define NEKTAR_LIB_UTILITIES_CHECKEDCAST_H
#include <LibUtilities/LibUtilitiesDeclspec.h>
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
#include <limits>
#include <type_traits>
namespace Nektar
{
namespace LibUtilities
{
/// checked cast from float types only to int types
template
<
class To, class Ti,
class = typename std::enable_if
<
std::is_floating_point<typename std::remove_reference<Ti>::type>::value
&& std::is_integral <typename std::remove_reference<To>::type>::value
>::type
>
inline LIB_UTILITIES_EXPORT To checked_cast(const Ti param)
{
Ti min = std::numeric_limits<To>::min();
Ti max = std::numeric_limits<To>::max();
ASSERTL0(param >= min, "Casting would narrow (underflow).");
ASSERTL0(param <= max, "Casting would narrow (overflow).");
return static_cast<To>(param);
}
}
}
#endif
......@@ -55,6 +55,7 @@
#include <LibUtilities/Memory/NekMemoryManager.hpp>
#include <LibUtilities/BasicUtils/ParseUtils.h>
#include <LibUtilities/BasicUtils/FileSystem.h>
#include <LibUtilities/BasicUtils/CheckedCast.hpp>
#include <LibUtilities/Interpreter/Interpreter.h>
#include <boost/program_options.hpp>
......@@ -207,7 +208,7 @@ namespace Nektar
}
m_interpreter = MemoryManager<Interpreter>::AllocateSharedPtr();
m_interpreter->SetRandomSeed((m_comm->GetRank() + 1)
m_interpreter->SetRandomSeed((m_comm->GetRank() + 1)
* (unsigned int)time(NULL));
// Split up the communicator
......@@ -253,7 +254,7 @@ namespace Nektar
}
m_interpreter = MemoryManager<Interpreter>::AllocateSharedPtr();
m_interpreter->SetRandomSeed((m_comm->GetRank() + 1)
m_interpreter->SetRandomSeed((m_comm->GetRank() + 1)
* (unsigned int)time(NULL));
// Split up the communicator
......@@ -736,7 +737,8 @@ namespace Nektar
auto paramIter = m_parameters.find(vName);
ASSERTL0(paramIter != m_parameters.end(), "Required parameter '" +
pName + "' not specified in session.");
pVar = (int)round(paramIter->second);
NekDouble param = round(paramIter->second);
pVar = checked_cast<int>(param);
}
......@@ -750,7 +752,8 @@ namespace Nektar
auto paramIter = m_parameters.find(vName);
if(paramIter != m_parameters.end())
{
pVar = (int)round(paramIter->second);
NekDouble param = round(paramIter->second);
pVar = checked_cast<int>(param);
}
else
{
......
......@@ -26,6 +26,7 @@ SET(BasicUtilsHeaders
./BasicUtils/Timer.h
./BasicUtils/RawType.hpp
./BasicUtils/SessionReader.h
./BasicUtils/CheckedCast.hpp
./BasicUtils/ShapeType.hpp
./BasicUtils/SharedArray.hpp
./BasicUtils/Thread.h
......
......@@ -3,11 +3,12 @@ SET(LibUtilitiesUnitTestSources
TestRawType.cpp
TestInterpreter.cpp
TestSharedArray.cpp
TestCheckedCast.cpp
main.cpp
../util.cpp
)
SET(UnitTestSources ${PrecompiledHeaderSources} main.cpp)
SET(UnitTestSources ${PrecompiledHeaderSources} main.cpp)
SET(UnitTestHeaders
LibUtilitiesUnitTestsPrecompiledHeader.h
......
#include <LibUtilities/BasicUtils/CheckedCast.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <boost/test/test_case_template.hpp>
#include <boost/test/floating_point_comparison.hpp>
#include <boost/test/unit_test.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <iostream>
namespace Nektar
{
namespace LibUtilities
{
namespace CheckCastUnitTest
{
BOOST_AUTO_TEST_CASE(TestDoubleToInt)
{
// expecting to convert
{
double adouble = std::numeric_limits<int>::max();
int aint = checked_cast<int>(adouble);
BOOST_CHECK_EQUAL(aint, adouble);
}
{
double adouble = std::numeric_limits<int>::min();
int aint = checked_cast<int>(adouble);
BOOST_CHECK_EQUAL(aint, adouble);
}
// expecting to fail and throw
try
{
double adouble = std::numeric_limits<int>::max()+1.0;
int aint = checked_cast<int>(adouble);
BOOST_CHECK_EQUAL(aint, adouble);
}
catch (std::runtime_error& e)
{
std::string errmss = e.what();
BOOST_CHECK_EQUAL("Level 0 assertion violation", errmss.substr(0,27));
}
try
{
double adouble = std::numeric_limits<int>::min()-1.0;
int aint = checked_cast<int>(adouble);
BOOST_CHECK_EQUAL(aint, adouble);
}
catch (std::runtime_error& e)
{
std::string errmss = e.what();
BOOST_CHECK_EQUAL("Level 0 assertion violation", errmss.substr(0,27));
}
}
}
}
}
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment