From beeed49d763f9eda26698470df81d3f57a72e634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 31 Mar 2022 18:23:46 +0200 Subject: [PATCH 1/2] Added assertions to getView/getConstView methods in Array/ArrayView/Vector/VectorView. --- src/TNL/Containers/Array.hpp | 12 ++++++++++++ src/TNL/Containers/ArrayView.hpp | 12 ++++++++++++ src/TNL/Containers/Vector.hpp | 12 ++++++++++++ src/TNL/Containers/VectorView.hpp | 12 ++++++++++++ 4 files changed, 48 insertions(+) diff --git a/src/TNL/Containers/Array.hpp b/src/TNL/Containers/Array.hpp index e766432ea..22e54f737 100644 --- a/src/TNL/Containers/Array.hpp +++ b/src/TNL/Containers/Array.hpp @@ -258,6 +258,12 @@ template< typename Value, typename Device, typename Index, typename Allocator > typename Array< Value, Device, Index, Allocator >::ViewType Array< Value, Device, Index, Allocator >::getView( IndexType begin, IndexType end ) { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = getSize(); return ViewType( getData() + begin, end - begin ); @@ -267,6 +273,12 @@ template< typename Value, typename Device, typename Index, typename Allocator > typename Array< Value, Device, Index, Allocator >::ConstViewType Array< Value, Device, Index, Allocator >::getConstView( IndexType begin, IndexType end ) const { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = getSize(); return ConstViewType( getData() + begin, end - begin ); diff --git a/src/TNL/Containers/ArrayView.hpp b/src/TNL/Containers/ArrayView.hpp index c94e9e05f..6f31f8d57 100644 --- a/src/TNL/Containers/ArrayView.hpp +++ b/src/TNL/Containers/ArrayView.hpp @@ -59,6 +59,12 @@ __cuda_callable__ typename ArrayView< Value, Device, Index >::ViewType ArrayView< Value, Device, Index >::getView( IndexType begin, IndexType end ) { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array view." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array view." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ViewType( getData() + begin, end - begin ); @@ -70,6 +76,12 @@ __cuda_callable__ typename ArrayView< Value, Device, Index >::ConstViewType ArrayView< Value, Device, Index >::getConstView( IndexType begin, IndexType end ) const { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array view." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array view." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ConstViewType( getData() + begin, end - begin ); diff --git a/src/TNL/Containers/Vector.hpp b/src/TNL/Containers/Vector.hpp index 12d2d4337..3036a20c3 100644 --- a/src/TNL/Containers/Vector.hpp +++ b/src/TNL/Containers/Vector.hpp @@ -28,6 +28,12 @@ template< typename Real, typename Device, typename Index, typename Allocator > typename Vector< Real, Device, Index, Allocator >::ViewType Vector< Real, Device, Index, Allocator >::getView( IndexType begin, IndexType end ) { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ViewType( this->getData() + begin, end - begin ); @@ -37,6 +43,12 @@ template< typename Real, typename Device, typename Index, typename Allocator > typename Vector< Real, Device, Index, Allocator >::ConstViewType Vector< Real, Device, Index, Allocator >::getConstView( IndexType begin, IndexType end ) const { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ConstViewType( this->getData() + begin, end - begin ); diff --git a/src/TNL/Containers/VectorView.hpp b/src/TNL/Containers/VectorView.hpp index bc993be30..1d352d5ea 100644 --- a/src/TNL/Containers/VectorView.hpp +++ b/src/TNL/Containers/VectorView.hpp @@ -17,6 +17,12 @@ __cuda_callable__ typename VectorView< Real, Device, Index >::ViewType VectorView< Real, Device, Index >::getView( IndexType begin, IndexType end ) { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector view." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector view." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ViewType( this->getData() + begin, end - begin ); @@ -28,6 +34,12 @@ __cuda_callable__ typename VectorView< Real, Device, Index >::ConstViewType VectorView< Real, Device, Index >::getConstView( const IndexType begin, IndexType end ) const { + TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector view." ); + TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector view." ); + TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); + if( end == 0 ) end = this->getSize(); return ConstViewType( this->getData() + begin, end - begin ); -- GitLab From 71b1d8541467515a6ab6de6fe2d1ad0506e9e360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sat, 2 Apr 2022 16:15:08 +0200 Subject: [PATCH 2/2] Fised index type in assertions in getView/getConstView methods in Array/ArrayView/Vector/VectorView. --- src/TNL/Containers/Array.hpp | 8 ++++---- src/TNL/Containers/ArrayView.hpp | 8 ++++---- src/TNL/Containers/Vector.hpp | 8 ++++---- src/TNL/Containers/VectorView.hpp | 8 ++++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/TNL/Containers/Array.hpp b/src/TNL/Containers/Array.hpp index 22e54f737..ef66bdc56 100644 --- a/src/TNL/Containers/Array.hpp +++ b/src/TNL/Containers/Array.hpp @@ -258,9 +258,9 @@ template< typename Value, typename Device, typename Index, typename Allocator > typename Array< Value, Device, Index, Allocator >::ViewType Array< Value, Device, Index, Allocator >::getView( IndexType begin, IndexType end ) { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); @@ -273,9 +273,9 @@ template< typename Value, typename Device, typename Index, typename Allocator > typename Array< Value, Device, Index, Allocator >::ConstViewType Array< Value, Device, Index, Allocator >::getConstView( IndexType begin, IndexType end ) const { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); diff --git a/src/TNL/Containers/ArrayView.hpp b/src/TNL/Containers/ArrayView.hpp index 6f31f8d57..c7db08cce 100644 --- a/src/TNL/Containers/ArrayView.hpp +++ b/src/TNL/Containers/ArrayView.hpp @@ -59,9 +59,9 @@ __cuda_callable__ typename ArrayView< Value, Device, Index >::ViewType ArrayView< Value, Device, Index >::getView( IndexType begin, IndexType end ) { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array view." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array view." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); @@ -76,9 +76,9 @@ __cuda_callable__ typename ArrayView< Value, Device, Index >::ConstViewType ArrayView< Value, Device, Index >::getConstView( IndexType begin, IndexType end ) const { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, getSize(), "Parameter 'begin' must be lower or equal to size of the array view." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, getSize(), "Parameter 'end' must be lower or equal to size of the array view." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); diff --git a/src/TNL/Containers/Vector.hpp b/src/TNL/Containers/Vector.hpp index 3036a20c3..2572b3ff7 100644 --- a/src/TNL/Containers/Vector.hpp +++ b/src/TNL/Containers/Vector.hpp @@ -28,9 +28,9 @@ template< typename Real, typename Device, typename Index, typename Allocator > typename Vector< Real, Device, Index, Allocator >::ViewType Vector< Real, Device, Index, Allocator >::getView( IndexType begin, IndexType end ) { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); @@ -43,9 +43,9 @@ template< typename Real, typename Device, typename Index, typename Allocator > typename Vector< Real, Device, Index, Allocator >::ConstViewType Vector< Real, Device, Index, Allocator >::getConstView( IndexType begin, IndexType end ) const { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); diff --git a/src/TNL/Containers/VectorView.hpp b/src/TNL/Containers/VectorView.hpp index 1d352d5ea..65aa56af9 100644 --- a/src/TNL/Containers/VectorView.hpp +++ b/src/TNL/Containers/VectorView.hpp @@ -17,9 +17,9 @@ __cuda_callable__ typename VectorView< Real, Device, Index >::ViewType VectorView< Real, Device, Index >::getView( IndexType begin, IndexType end ) { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector view." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector view." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); @@ -34,9 +34,9 @@ __cuda_callable__ typename VectorView< Real, Device, Index >::ConstViewType VectorView< Real, Device, Index >::getConstView( const IndexType begin, IndexType end ) const { - TNL_ASSERT_GE( begin, 0, "Parameter 'begin' must be non-negative." ); + TNL_ASSERT_GE( begin, ( Index ) 0, "Parameter 'begin' must be non-negative." ); TNL_ASSERT_LE( begin, this->getSize(), "Parameter 'begin' must be lower or equal to size of the vector view." ); - TNL_ASSERT_GE( end, 0, "Parameter 'end' must be non-negative." ); + TNL_ASSERT_GE( end, ( Index ) 0, "Parameter 'end' must be non-negative." ); TNL_ASSERT_LE( end, this->getSize(), "Parameter 'end' must be lower or equal to size of the vector view." ); TNL_ASSERT_LE( begin, end, "Parameter 'begin' must be lower or equal to the parameter 'end'." ); -- GitLab