diff --git a/vtkm/Assert.h b/vtkm/Assert.h new file mode 100644 index 000000000..15af5e243 --- /dev/null +++ b/vtkm/Assert.h @@ -0,0 +1,45 @@ +//============================================================================ +// Copyright (c) Kitware, Inc. +// All rights reserved. +// See LICENSE.txt for details. +// This software is distributed WITHOUT ANY WARRANTY; without even +// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR +// PURPOSE. See the above copyright notice for more information. +// +// Copyright 2016 Sandia Corporation. +// Copyright 2016 UT-Battelle, LLC. +// Copyright 2016 Los Alamos National Security. +// +// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, +// the U.S. Government retains certain rights in this software. +// +// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National +// Laboratory (LANL), the U.S. Government retains certain rights in +// this software. +//============================================================================ + +#ifndef vtk_m_Assert_h +#define vtk_m_Assert_h + +#include + +/// \def VTKM_ASSERT(condition) +/// +/// Asserts that \a condition resolves to true. If \a condition is false, +/// then a diagnostic message is outputted and execution is terminated. The +/// behavior is essentially the same as the POSIX assert macro, but is +/// wrapped for added portability. +/// +/// Like the POSIX assert macro, the check will be removed when compiling +/// in non-debug mode (specifically when NDEBUG is defined), so be prepared +/// for the possibility that the condition is never evaluated. +/// +#if !defined(NDEBUG) +#define VTKM_ASSERT(condition) \ + assert(condition) +#else +#define VTKM_ASSERT(condition) +#endif + + +#endif //vtk_m_Assert_h diff --git a/vtkm/CMakeLists.txt b/vtkm/CMakeLists.txt index a5ccea274..802caed90 100644 --- a/vtkm/CMakeLists.txt +++ b/vtkm/CMakeLists.txt @@ -21,6 +21,7 @@ include_directories(${Boost_INCLUDE_DIRS}) set(headers + Assert.h BinaryPredicates.h BinaryOperators.h CellShape.h diff --git a/vtkm/Matrix.h b/vtkm/Matrix.h index 12ebcd19c..2a842cb04 100644 --- a/vtkm/Matrix.h +++ b/vtkm/Matrix.h @@ -22,6 +22,7 @@ #ifndef vtk_m_Matrix_h #define vtk_m_Matrix_h +#include #include #include #include @@ -59,6 +60,8 @@ public: VTKM_EXEC_CONT_EXPORT const vtkm::Vec & operator[](vtkm::IdComponent rowIndex) const { + VTKM_ASSERT(rowIndex >= 0); + VTKM_ASSERT(rowIndex < this->NUM_ROWS); return this->Components[rowIndex]; } @@ -68,6 +71,8 @@ public: VTKM_EXEC_CONT_EXPORT vtkm::Vec & operator[](vtkm::IdComponent rowIndex) { + VTKM_ASSERT(rowIndex >= 0); + VTKM_ASSERT(rowIndex < this->NUM_ROWS); return this->Components[rowIndex]; } @@ -77,6 +82,10 @@ public: VTKM_EXEC_CONT_EXPORT const ComponentType & operator()(vtkm::IdComponent rowIndex, vtkm::IdComponent colIndex) const { + VTKM_ASSERT(rowIndex >= 0); + VTKM_ASSERT(rowIndex < this->NUM_ROWS); + VTKM_ASSERT(colIndex >= 0); + VTKM_ASSERT(colIndex < this->NUM_COLUMNS); return (*this)[rowIndex][colIndex]; } @@ -86,6 +95,10 @@ public: VTKM_EXEC_CONT_EXPORT ComponentType & operator()(vtkm::IdComponent rowIndex, vtkm::IdComponent colIndex) { + VTKM_ASSERT(rowIndex >= 0); + VTKM_ASSERT(rowIndex < this->NUM_ROWS); + VTKM_ASSERT(colIndex >= 0); + VTKM_ASSERT(colIndex < this->NUM_COLUMNS); return (*this)[rowIndex][colIndex]; } diff --git a/vtkm/Types.h b/vtkm/Types.h index 95b2f35f5..a7d0f1095 100644 --- a/vtkm/Types.h +++ b/vtkm/Types.h @@ -24,6 +24,8 @@ #include #include +#include + VTKM_THIRDPARTY_PRE_INCLUDE #include #include @@ -806,11 +808,15 @@ public: VTKM_EXEC_CONT_EXPORT const ComponentType &operator[](vtkm::IdComponent idx) const { + VTKM_ASSERT(idx >= 0); + VTKM_ASSERT(idx < this->NUM_COMPONENTS); return this->Components[idx]; } VTKM_EXEC_CONT_EXPORT ComponentType &operator[](vtkm::IdComponent idx) { + VTKM_ASSERT(idx >= 0); + VTKM_ASSERT(idx < this->NUM_COMPONENTS); return this->Components[idx]; } diff --git a/vtkm/VecVariable.h b/vtkm/VecVariable.h index 0d8e36933..64b01739b 100644 --- a/vtkm/VecVariable.h +++ b/vtkm/VecVariable.h @@ -20,6 +20,7 @@ #ifndef vtk_m_VecVariable_h #define vtk_m_VecVariable_h +#include #include #include #include @@ -48,6 +49,7 @@ public: VecVariable(const vtkm::VecVariable &src) : NumComponents(src.GetNumberOfComponents()) { + VTKM_ASSERT(this->NumComponents <= MaxSize); for (vtkm::IdComponent index = 0; index < this->NumComponents; index++) { this->Data[index] = src[index]; @@ -59,6 +61,7 @@ public: VecVariable(const vtkm::Vec &src) : NumComponents(SrcSize) { + VTKM_ASSERT(this->NumComponents <= MaxSize); for (vtkm::IdComponent index = 0; index < this->NumComponents; index++) { this->Data[index] = src[index]; @@ -96,6 +99,7 @@ public: VTKM_EXEC_CONT_EXPORT void Append(ComponentType value) { + VTKM_ASSERT(this->NumComponents < MaxSize); this->Data[this->NumComponents] = value; this->NumComponents++; }