From ef637fc84b77618637e622f8a0482c8638058c53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com> Date: Thu, 2 May 2019 21:50:41 +0200 Subject: [PATCH] Adding unit tests for vertical operations. --- .../Expressions/VerticalOperations.h | 3 +- src/UnitTests/Containers/CMakeLists.txt | 14 + src/UnitTests/Containers/VectorTest-1.h | 2 +- src/UnitTests/Containers/VectorTest-2.h | 2 +- src/UnitTests/Containers/VectorTest-3.h | 2 +- src/UnitTests/Containers/VectorTest-4.h | 2 +- src/UnitTests/Containers/VectorTest-5.h | 345 +--------------- src/UnitTests/Containers/VectorTest-6.cpp | 11 + src/UnitTests/Containers/VectorTest-6.cu | 11 + src/UnitTests/Containers/VectorTest-6.h | 391 ++++++++++++++++++ src/UnitTests/Containers/VectorTest-7.cpp | 11 + src/UnitTests/Containers/VectorTest-7.cu | 11 + src/UnitTests/Containers/VectorTest-7.h | 68 +++ 13 files changed, 523 insertions(+), 350 deletions(-) create mode 100644 src/UnitTests/Containers/VectorTest-6.cpp create mode 100644 src/UnitTests/Containers/VectorTest-6.cu create mode 100644 src/UnitTests/Containers/VectorTest-6.h create mode 100644 src/UnitTests/Containers/VectorTest-7.cpp create mode 100644 src/UnitTests/Containers/VectorTest-7.cu create mode 100644 src/UnitTests/Containers/VectorTest-7.h diff --git a/src/TNL/Containers/Expressions/VerticalOperations.h b/src/TNL/Containers/Expressions/VerticalOperations.h index a4ae2b010a..250659d1ac 100644 --- a/src/TNL/Containers/Expressions/VerticalOperations.h +++ b/src/TNL/Containers/Expressions/VerticalOperations.h @@ -178,7 +178,7 @@ bool ExpressionLogicalOr( const Expression& a ) auto fetch = [=] __cuda_callable__ ( IndexType i ) { return a[ i ]; }; auto reduction = [=] __cuda_callable__ ( ResultType& a, const ResultType& b ) { a = a || b; }; auto volatileReduction = [=] __cuda_callable__ ( volatile ResultType& a, volatile ResultType& b ) { a = a || b; }; - return Algorithms::Reduction< typename Expression::DeviceType >::reduce( a.getSize(), reduction, volatileReduction, fetch, ); + return Algorithms::Reduction< typename Expression::DeviceType >::reduce( a.getSize(), reduction, volatileReduction, fetch, ( ResultType ) 0 ); } template< typename Expression > @@ -205,7 +205,6 @@ auto ExpressionBinaryOr( const Expression& a ) -> decltype( a[ 0 ] ) auto reduction = [=] __cuda_callable__ ( ResultType& a, const ResultType& b ) { a = a | b; }; auto volatileReduction = [=] __cuda_callable__ ( volatile ResultType& a, volatile ResultType& b ) { a = a | b; }; return Algorithms::Reduction< typename Expression::DeviceType >::reduce( a.getSize(), reduction, volatileReduction, fetch, ( ResultType ) 0 ); - } } //namespace Expressions diff --git a/src/UnitTests/Containers/CMakeLists.txt b/src/UnitTests/Containers/CMakeLists.txt index a5b81a698c..24cae4dc0a 100644 --- a/src/UnitTests/Containers/CMakeLists.txt +++ b/src/UnitTests/Containers/CMakeLists.txt @@ -27,16 +27,22 @@ ADD_EXECUTABLE( VectorTest-2 VectorTest-2.cpp ) ADD_EXECUTABLE( VectorTest-3 VectorTest-3.cpp ) ADD_EXECUTABLE( VectorTest-4 VectorTest-4.cpp ) ADD_EXECUTABLE( VectorTest-5 VectorTest-5.cpp ) +ADD_EXECUTABLE( VectorTest-6 VectorTest-6.cpp ) +ADD_EXECUTABLE( VectorTest-7 VectorTest-7.cpp ) TARGET_COMPILE_OPTIONS( VectorTest-1 PRIVATE ${CXX_TESTS_FLAGS} ) TARGET_COMPILE_OPTIONS( VectorTest-2 PRIVATE ${CXX_TESTS_FLAGS} ) TARGET_COMPILE_OPTIONS( VectorTest-3 PRIVATE ${CXX_TESTS_FLAGS} ) TARGET_COMPILE_OPTIONS( VectorTest-4 PRIVATE ${CXX_TESTS_FLAGS} ) TARGET_COMPILE_OPTIONS( VectorTest-5 PRIVATE ${CXX_TESTS_FLAGS} ) +TARGET_COMPILE_OPTIONS( VectorTest-6 PRIVATE ${CXX_TESTS_FLAGS} ) +TARGET_COMPILE_OPTIONS( VectorTest-7 PRIVATE ${CXX_TESTS_FLAGS} ) TARGET_LINK_LIBRARIES( VectorTest-1 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTest-2 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTest-3 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTest-4 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTest-5 ${GTEST_BOTH_LIBRARIES} ) +TARGET_LINK_LIBRARIES( VectorTest-6 ${GTEST_BOTH_LIBRARIES} ) +TARGET_LINK_LIBRARIES( VectorTest-7 ${GTEST_BOTH_LIBRARIES} ) IF( BUILD_CUDA ) CUDA_ADD_EXECUTABLE( ArrayTestCuda ArrayTest.cu @@ -52,11 +58,15 @@ IF( BUILD_CUDA ) CUDA_ADD_EXECUTABLE( VectorTestCuda-3 VectorTest-3.cu OPTIONS ${CXX_TESTS_FLAGS} ) CUDA_ADD_EXECUTABLE( VectorTestCuda-4 VectorTest-4.cu OPTIONS ${CXX_TESTS_FLAGS} ) CUDA_ADD_EXECUTABLE( VectorTestCuda-5 VectorTest-5.cu OPTIONS ${CXX_TESTS_FLAGS} ) + CUDA_ADD_EXECUTABLE( VectorTestCuda-6 VectorTest-6.cu OPTIONS ${CXX_TESTS_FLAGS} ) + CUDA_ADD_EXECUTABLE( VectorTestCuda-7 VectorTest-7.cu OPTIONS ${CXX_TESTS_FLAGS} ) TARGET_LINK_LIBRARIES( VectorTestCuda-1 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTestCuda-2 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTestCuda-3 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTestCuda-4 ${GTEST_BOTH_LIBRARIES} ) TARGET_LINK_LIBRARIES( VectorTestCuda-5 ${GTEST_BOTH_LIBRARIES} ) + TARGET_LINK_LIBRARIES( VectorTestCuda-6 ${GTEST_BOTH_LIBRARIES} ) + TARGET_LINK_LIBRARIES( VectorTestCuda-7 ${GTEST_BOTH_LIBRARIES} ) ENDIF( BUILD_CUDA ) IF( BUILD_CUDA ) @@ -87,6 +97,8 @@ ADD_TEST( VectorTest-2 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-2${CMAKE_EXECUTABLE_ ADD_TEST( VectorTest-3 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-3${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( VectorTest-4 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-4${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( VectorTest-5 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-5${CMAKE_EXECUTABLE_SUFFIX} ) +ADD_TEST( VectorTest-6 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-6${CMAKE_EXECUTABLE_SUFFIX} ) +ADD_TEST( VectorTest-7 ${EXECUTABLE_OUTPUT_PATH}/VectorTest-7${CMAKE_EXECUTABLE_SUFFIX} ) IF( BUILD_CUDA ) ADD_TEST( ArrayTestCuda ${EXECUTABLE_OUTPUT_PATH}/ArrayTestCuda${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( ArrayViewTestCuda ${EXECUTABLE_OUTPUT_PATH}/ArrayViewTestCuda${CMAKE_EXECUTABLE_SUFFIX} ) @@ -95,6 +107,8 @@ IF( BUILD_CUDA ) ADD_TEST( VectorTestCuda-3 ${EXECUTABLE_OUTPUT_PATH}/VectorTestCuda-3${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( VectorTestCuda-4 ${EXECUTABLE_OUTPUT_PATH}/VectorTestCuda-4${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( VectorTestCuda-5 ${EXECUTABLE_OUTPUT_PATH}/VectorTestCuda-5${CMAKE_EXECUTABLE_SUFFIX} ) + ADD_TEST( VectorTestCuda-6 ${EXECUTABLE_OUTPUT_PATH}/VectorTestCuda-6${CMAKE_EXECUTABLE_SUFFIX} ) + ADD_TEST( VectorTestCuda-7 ${EXECUTABLE_OUTPUT_PATH}/VectorTestCuda-7${CMAKE_EXECUTABLE_SUFFIX} ) ENDIF() ADD_TEST( MultireductionTest ${EXECUTABLE_OUTPUT_PATH}/MultireductionTest${CMAKE_EXECUTABLE_SUFFIX} ) ADD_TEST( StaticArrayTest ${EXECUTABLE_OUTPUT_PATH}/StaticArrayTest${CMAKE_EXECUTABLE_SUFFIX} ) diff --git a/src/UnitTests/Containers/VectorTest-1.h b/src/UnitTests/Containers/VectorTest-1.h index 7e7380a66a..1b09e1f6b9 100644 --- a/src/UnitTests/Containers/VectorTest-1.h +++ b/src/UnitTests/Containers/VectorTest-1.h @@ -1,5 +1,5 @@ /*************************************************************************** - VectorTest.h - description + VectorTest-1.h - description ------------------- begin : Oct 25, 2010 copyright : (C) 2010 by Tomas Oberhuber diff --git a/src/UnitTests/Containers/VectorTest-2.h b/src/UnitTests/Containers/VectorTest-2.h index 6c32fbd7ef..91c6d7de2b 100644 --- a/src/UnitTests/Containers/VectorTest-2.h +++ b/src/UnitTests/Containers/VectorTest-2.h @@ -1,5 +1,5 @@ /*************************************************************************** - VectorTest.h - description + VectorTest-2.h - description ------------------- begin : Oct 25, 2010 copyright : (C) 2010 by Tomas Oberhuber diff --git a/src/UnitTests/Containers/VectorTest-3.h b/src/UnitTests/Containers/VectorTest-3.h index 53c6e5ef72..a07c72a58e 100644 --- a/src/UnitTests/Containers/VectorTest-3.h +++ b/src/UnitTests/Containers/VectorTest-3.h @@ -1,5 +1,5 @@ /*************************************************************************** - VectorTest.h - description + VectorTest-3.h - description ------------------- begin : Oct 25, 2010 copyright : (C) 2010 by Tomas Oberhuber diff --git a/src/UnitTests/Containers/VectorTest-4.h b/src/UnitTests/Containers/VectorTest-4.h index e683d1b2b5..5a913c4e86 100644 --- a/src/UnitTests/Containers/VectorTest-4.h +++ b/src/UnitTests/Containers/VectorTest-4.h @@ -1,5 +1,5 @@ /*************************************************************************** - VectorTest.h - description + VectorTest-4.h - description ------------------- begin : Oct 25, 2010 copyright : (C) 2010 by Tomas Oberhuber diff --git a/src/UnitTests/Containers/VectorTest-5.h b/src/UnitTests/Containers/VectorTest-5.h index 6d4bc218d5..e13e90422f 100644 --- a/src/UnitTests/Containers/VectorTest-5.h +++ b/src/UnitTests/Containers/VectorTest-5.h @@ -1,5 +1,5 @@ /*************************************************************************** - VectorTest.h - description + VectorTest-5.h - description ------------------- begin : Oct 25, 2010 copyright : (C) 2010 by Tomas Oberhuber @@ -204,349 +204,6 @@ TYPED_TEST( VectorTest, ceil ) } -TYPED_TEST( VectorTest, acos ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType )( i - size / 2 ) / ( RealType ) size ); - v.setElement( i, TNL::acos( ( RealType )( i - size / 2 ) / ( RealType ) size ) ); - } - - //EXPECT_EQ( acos( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( acos( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, asin ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) ( i - size / 2 ) / ( RealType ) size ); - v.setElement( i, TNL::asin( ( RealType )( i - size / 2 ) / ( RealType ) size ) ); - } - - //EXPECT_EQ( asin( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( asin( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, atan ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); - v.setElement( i, TNL::atan( ( RealType ) i - ( RealType ) size / 2 ) ); - } - - //EXPECT_EQ( atan( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( atan( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, cosh ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - RealType h = 2.0 / ( RealType ) size; - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, i * h - ( RealType ) 1.0 ); - v.setElement( i, TNL::cosh( i * h - ( RealType ) 1.0 ) ); - } - - // EXPECT_EQ( cosh( u ), v ) does not work here for float, maybe because - // of some fast-math optimization - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( cosh( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, tanh ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); - v.setElement( i, TNL::tanh( ( RealType ) i - ( RealType ) size / 2 ) ); - } - - //EXPECT_EQ( tanh( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( tanh( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, log ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i + 1 ); - v.setElement( i, TNL::log( ( RealType ) i + 1 ) ); - } - - //EXPECT_EQ( log( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( log( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); - -} - -TYPED_TEST( VectorTest, log10 ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i + 1 ); - v.setElement( i, TNL::log10( ( RealType ) i + 1 ) ); - } - - // EXPECT_EQ( log10( u ), v ) does not work here for float, maybe because - // of some fast-math optimization - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( log10( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, log2 ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i + 1 ); - v.setElement( i, TNL::log2( ( RealType ) i + 1 ) ); - } - - //EXPECT_EQ( log2( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( log2( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); - -} - -TYPED_TEST( VectorTest, exp ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - const double h = 10.0 / size; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - const RealType x = -5.0 + i * h; - u.setElement( i, x ); - v.setElement( i, TNL::exp( x ) ); - } - - //EXPECT_EQ( exp( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( exp( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -TYPED_TEST( VectorTest, sign ) -{ - using VectorType = typename TestFixture::VectorType; - using ViewType = typename TestFixture::ViewType; - using RealType = typename VectorType::RealType; - const int size = VECTOR_TEST_SIZE; - - VectorType _u( size ), _v( size ); - ViewType u( _u ), v( _v ); - for( int i = 0; i < size; i++ ) - { - u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); - v.setElement( i, TNL::sign( ( RealType ) i - ( RealType ) size / 2 ) ); - } - - //EXPECT_EQ( sign( u ), v ); - for( int i = 0; i < size; i++ ) - EXPECT_NEAR( sign( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); -} - -// TODO: test prefix sum with custom begin and end parameters - -TEST( VectorSpecialCasesTest, sumOfBoolVector ) -{ - using VectorType = Containers::Vector< bool, Devices::Host >; - using ViewType = VectorView< bool, Devices::Host >; - const float epsilon = 64 * std::numeric_limits< float >::epsilon(); - - VectorType v( 512 ), w( 512 ); - ViewType v_view( v ), w_view( w ); - v.setValue( true ); - w.setValue( false ); - - const int sum = v.sum< int >(); - const int l1norm = v.lpNorm< int >( 1.0 ); - const float l2norm = v.lpNorm< float >( 2.0 ); - const float l3norm = v.lpNorm< float >( 3.0 ); - EXPECT_EQ( sum, 512 ); - EXPECT_EQ( l1norm, 512 ); - EXPECT_NEAR( l2norm, std::sqrt( 512 ), epsilon ); - EXPECT_NEAR( l3norm, std::cbrt( 512 ), epsilon ); - - const int diff_sum = v.differenceSum< int >( w ); - const int diff_l1norm = v.differenceLpNorm< int >( w, 1.0 ); - const float diff_l2norm = v.differenceLpNorm< float >( w, 2.0 ); - const float diff_l3norm = v.differenceLpNorm< float >( w, 3.0 ); - EXPECT_EQ( diff_sum, 512 ); - EXPECT_EQ( diff_l1norm, 512 ); - EXPECT_NEAR( diff_l2norm, std::sqrt( 512 ), epsilon ); - EXPECT_NEAR( diff_l3norm, std::cbrt( 512 ), epsilon ); - - // test views - const int sum_view = v_view.sum< int >(); - const int l1norm_view = v_view.lpNorm< int >( 1.0 ); - const float l2norm_view = v_view.lpNorm< float >( 2.0 ); - const float l3norm_view = v_view.lpNorm< float >( 3.0 ); - EXPECT_EQ( sum_view, 512 ); - EXPECT_EQ( l1norm_view, 512 ); - EXPECT_NEAR( l2norm_view, std::sqrt( 512 ), epsilon ); - EXPECT_NEAR( l3norm_view, std::cbrt( 512 ), epsilon ); - - const int diff_sum_view = v_view.differenceSum< int >( w_view ); - const int diff_l1norm_view = v_view.differenceLpNorm< int >( w_view, 1.0 ); - const float diff_l2norm_view = v_view.differenceLpNorm< float >( w_view, 2.0 ); - const float diff_l3norm_view = v_view.differenceLpNorm< float >( w_view, 3.0 ); - EXPECT_EQ( diff_sum_view, 512 ); - EXPECT_EQ( diff_l1norm_view, 512 ); - EXPECT_NEAR( diff_l2norm_view, std::sqrt( 512 ), epsilon ); - EXPECT_NEAR( diff_l3norm_view, std::cbrt( 512 ), epsilon ); -} - -TEST( VectorSpecialCasesTest, assignmentThroughView ) -{ - using VectorType = Containers::Vector< int, Devices::Host >; - using ViewType = VectorView< int, Devices::Host >; - - static_assert( Algorithms::Details::HasSubscriptOperator< VectorType >::value, "Subscript operator detection by SFINAE does not work for Vector." ); - static_assert( Algorithms::Details::HasSubscriptOperator< ViewType >::value, "Subscript operator detection by SFINAE does not work for VectorView." ); - - VectorType u( 100 ), v( 100 ); - ViewType u_view( u ), v_view( v ); - - u.setValue( 42 ); - v.setValue( 0 ); - v_view = u_view; - EXPECT_EQ( u_view.getData(), u.getData() ); - EXPECT_EQ( v_view.getData(), v.getData() ); - for( int i = 0; i < 100; i++ ) - EXPECT_EQ( v_view[ i ], 42 ); - - u.setValue( 42 ); - v.setValue( 0 ); - v_view = u; - EXPECT_EQ( u_view.getData(), u.getData() ); - EXPECT_EQ( v_view.getData(), v.getData() ); - for( int i = 0; i < 100; i++ ) - EXPECT_EQ( v_view[ i ], 42 ); -} - -TEST( VectorSpecialCasesTest, operationsOnConstView ) -{ - using VectorType = Containers::Vector< int, Devices::Host >; - using ViewType = VectorView< const int, Devices::Host >; - - VectorType u( 100 ), v( 100 ); - ViewType u_view( u ), v_view( v ); - - u.setValue( 1 ); - v.setValue( 1 ); - - EXPECT_EQ( u_view.max(), 1 ); - EXPECT_EQ( u_view.min(), 1 ); - EXPECT_EQ( u_view.absMax(), 1 ); - EXPECT_EQ( u_view.absMin(), 1 ); - EXPECT_EQ( u_view.lpNorm( 1 ), 100 ); - EXPECT_EQ( u_view.differenceMax( v_view ), 0 ); - EXPECT_EQ( u_view.differenceMin( v_view ), 0 ); - EXPECT_EQ( u_view.differenceAbsMax( v_view ), 0 ); - EXPECT_EQ( u_view.differenceAbsMin( v_view ), 0 ); - EXPECT_EQ( u_view.differenceLpNorm( v_view, 1 ), 0 ); - EXPECT_EQ( u_view.differenceSum( v_view ), 0 ); - EXPECT_EQ( u_view.scalarProduct( v_view ), 100 ); -} - -TEST( VectorSpecialCasesTest, initializationOfVectorViewByArrayView ) -{ - using ArrayType = Containers::Array< int, Devices::Host >; - using VectorViewType = VectorView< const int, Devices::Host >; - using ArrayViewType = ArrayView< int, Devices::Host >; - - ArrayType a( 100 ); - a.setValue( 0 ); - ArrayViewType a_view( a ); - - VectorViewType v_view( a_view ); - EXPECT_EQ( v_view.getData(), a_view.getData() ); - EXPECT_EQ( v_view.sum(), 0 ); -} - -TEST( VectorSpecialCasesTest, defaultConstructors ) -{ - using ArrayType = Containers::Array< int, Devices::Host >; - using VectorViewType = VectorView< int, Devices::Host >; - using ArrayViewType = ArrayView< int, Devices::Host >; - - ArrayType a( 100 ); - a.setValue( 0 ); - - ArrayViewType a_view; - a_view.bind( a ); - - VectorViewType v_view; - v_view.bind( a ); - EXPECT_EQ( v_view.getData(), a_view.getData() ); -} - #endif // HAVE_GTEST diff --git a/src/UnitTests/Containers/VectorTest-6.cpp b/src/UnitTests/Containers/VectorTest-6.cpp new file mode 100644 index 0000000000..03849218c4 --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-6.cpp @@ -0,0 +1,11 @@ +/*************************************************************************** + VectorTest-6.cpp - description + ------------------- + begin : Apr 30, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#include "VectorTest-6.h" diff --git a/src/UnitTests/Containers/VectorTest-6.cu b/src/UnitTests/Containers/VectorTest-6.cu new file mode 100644 index 0000000000..400f69109e --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-6.cu @@ -0,0 +1,11 @@ +/*************************************************************************** + VectorTest-6.cu - description + ------------------- + begin : Apr 30, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#include "VectorTest-6.h" diff --git a/src/UnitTests/Containers/VectorTest-6.h b/src/UnitTests/Containers/VectorTest-6.h new file mode 100644 index 0000000000..5ab7848c37 --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-6.h @@ -0,0 +1,391 @@ +/*************************************************************************** + VectorTest-6.h - description + ------------------- + begin : Oct 25, 2010 + copyright : (C) 2010 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +// NOTE: Vector = Array + VectorOperations, so we test Vector and VectorOperations at the same time + +#pragma once + +#ifdef HAVE_GTEST +#include <limits> + +#include <TNL/Experimental/Arithmetics/Quad.h> +#include <TNL/Containers/Vector.h> +#include <TNL/Containers/VectorView.h> +#include "VectorTestSetup.h" + +#include "gtest/gtest.h" + +using namespace TNL; +using namespace TNL::Containers; +using namespace TNL::Containers::Algorithms; +using namespace TNL::Arithmetics; + +// should be small enough to have fast tests, but larger than minGPUReductionDataSize +// and large enough to require multiple CUDA blocks for reduction +constexpr int VECTOR_TEST_SIZE = 5000; + +TYPED_TEST( VectorTest, acos ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType )( i - size / 2 ) / ( RealType ) size ); + v.setElement( i, TNL::acos( ( RealType )( i - size / 2 ) / ( RealType ) size ) ); + } + + //EXPECT_EQ( acos( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( acos( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, asin ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) ( i - size / 2 ) / ( RealType ) size ); + v.setElement( i, TNL::asin( ( RealType )( i - size / 2 ) / ( RealType ) size ) ); + } + + //EXPECT_EQ( asin( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( asin( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, atan ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); + v.setElement( i, TNL::atan( ( RealType ) i - ( RealType ) size / 2 ) ); + } + + //EXPECT_EQ( atan( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( atan( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, cosh ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + RealType h = 2.0 / ( RealType ) size; + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, i * h - ( RealType ) 1.0 ); + v.setElement( i, TNL::cosh( i * h - ( RealType ) 1.0 ) ); + } + + // EXPECT_EQ( cosh( u ), v ) does not work here for float, maybe because + // of some fast-math optimization + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( cosh( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, tanh ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); + v.setElement( i, TNL::tanh( ( RealType ) i - ( RealType ) size / 2 ) ); + } + + //EXPECT_EQ( tanh( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( tanh( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, log ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i + 1 ); + v.setElement( i, TNL::log( ( RealType ) i + 1 ) ); + } + + //EXPECT_EQ( log( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( log( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); + +} + +TYPED_TEST( VectorTest, log10 ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i + 1 ); + v.setElement( i, TNL::log10( ( RealType ) i + 1 ) ); + } + + // EXPECT_EQ( log10( u ), v ) does not work here for float, maybe because + // of some fast-math optimization + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( log10( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, log2 ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i + 1 ); + v.setElement( i, TNL::log2( ( RealType ) i + 1 ) ); + } + + //EXPECT_EQ( log2( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( log2( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); + +} + +TYPED_TEST( VectorTest, exp ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + const double h = 10.0 / size; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + const RealType x = -5.0 + i * h; + u.setElement( i, x ); + v.setElement( i, TNL::exp( x ) ); + } + + //EXPECT_EQ( exp( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( exp( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +TYPED_TEST( VectorTest, sign ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ), _v( size ); + ViewType u( _u ), v( _v ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType ) i - ( RealType ) size / 2 ); + v.setElement( i, TNL::sign( ( RealType ) i - ( RealType ) size / 2 ) ); + } + + //EXPECT_EQ( sign( u ), v ); + for( int i = 0; i < size; i++ ) + EXPECT_NEAR( sign( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + +// TODO: test prefix sum with custom begin and end parameters + +TEST( VectorSpecialCasesTest, sumOfBoolVector ) +{ + using VectorType = Containers::Vector< bool, Devices::Host >; + using ViewType = VectorView< bool, Devices::Host >; + const float epsilon = 64 * std::numeric_limits< float >::epsilon(); + + VectorType v( 512 ), w( 512 ); + ViewType v_view( v ), w_view( w ); + v.setValue( true ); + w.setValue( false ); + + const int sum = v.sum< int >(); + const int l1norm = v.lpNorm< int >( 1.0 ); + const float l2norm = v.lpNorm< float >( 2.0 ); + const float l3norm = v.lpNorm< float >( 3.0 ); + EXPECT_EQ( sum, 512 ); + EXPECT_EQ( l1norm, 512 ); + EXPECT_NEAR( l2norm, std::sqrt( 512 ), epsilon ); + EXPECT_NEAR( l3norm, std::cbrt( 512 ), epsilon ); + + const int diff_sum = v.differenceSum< int >( w ); + const int diff_l1norm = v.differenceLpNorm< int >( w, 1.0 ); + const float diff_l2norm = v.differenceLpNorm< float >( w, 2.0 ); + const float diff_l3norm = v.differenceLpNorm< float >( w, 3.0 ); + EXPECT_EQ( diff_sum, 512 ); + EXPECT_EQ( diff_l1norm, 512 ); + EXPECT_NEAR( diff_l2norm, std::sqrt( 512 ), epsilon ); + EXPECT_NEAR( diff_l3norm, std::cbrt( 512 ), epsilon ); + + // test views + const int sum_view = v_view.sum< int >(); + const int l1norm_view = v_view.lpNorm< int >( 1.0 ); + const float l2norm_view = v_view.lpNorm< float >( 2.0 ); + const float l3norm_view = v_view.lpNorm< float >( 3.0 ); + EXPECT_EQ( sum_view, 512 ); + EXPECT_EQ( l1norm_view, 512 ); + EXPECT_NEAR( l2norm_view, std::sqrt( 512 ), epsilon ); + EXPECT_NEAR( l3norm_view, std::cbrt( 512 ), epsilon ); + + const int diff_sum_view = v_view.differenceSum< int >( w_view ); + const int diff_l1norm_view = v_view.differenceLpNorm< int >( w_view, 1.0 ); + const float diff_l2norm_view = v_view.differenceLpNorm< float >( w_view, 2.0 ); + const float diff_l3norm_view = v_view.differenceLpNorm< float >( w_view, 3.0 ); + EXPECT_EQ( diff_sum_view, 512 ); + EXPECT_EQ( diff_l1norm_view, 512 ); + EXPECT_NEAR( diff_l2norm_view, std::sqrt( 512 ), epsilon ); + EXPECT_NEAR( diff_l3norm_view, std::cbrt( 512 ), epsilon ); +} + +TEST( VectorSpecialCasesTest, assignmentThroughView ) +{ + using VectorType = Containers::Vector< int, Devices::Host >; + using ViewType = VectorView< int, Devices::Host >; + + static_assert( Algorithms::Details::HasSubscriptOperator< VectorType >::value, "Subscript operator detection by SFINAE does not work for Vector." ); + static_assert( Algorithms::Details::HasSubscriptOperator< ViewType >::value, "Subscript operator detection by SFINAE does not work for VectorView." ); + + VectorType u( 100 ), v( 100 ); + ViewType u_view( u ), v_view( v ); + + u.setValue( 42 ); + v.setValue( 0 ); + v_view = u_view; + EXPECT_EQ( u_view.getData(), u.getData() ); + EXPECT_EQ( v_view.getData(), v.getData() ); + for( int i = 0; i < 100; i++ ) + EXPECT_EQ( v_view[ i ], 42 ); + + u.setValue( 42 ); + v.setValue( 0 ); + v_view = u; + EXPECT_EQ( u_view.getData(), u.getData() ); + EXPECT_EQ( v_view.getData(), v.getData() ); + for( int i = 0; i < 100; i++ ) + EXPECT_EQ( v_view[ i ], 42 ); +} + +TEST( VectorSpecialCasesTest, operationsOnConstView ) +{ + using VectorType = Containers::Vector< int, Devices::Host >; + using ViewType = VectorView< const int, Devices::Host >; + + VectorType u( 100 ), v( 100 ); + ViewType u_view( u ), v_view( v ); + + u.setValue( 1 ); + v.setValue( 1 ); + + EXPECT_EQ( u_view.max(), 1 ); + EXPECT_EQ( u_view.min(), 1 ); + EXPECT_EQ( u_view.absMax(), 1 ); + EXPECT_EQ( u_view.absMin(), 1 ); + EXPECT_EQ( u_view.lpNorm( 1 ), 100 ); + EXPECT_EQ( u_view.differenceMax( v_view ), 0 ); + EXPECT_EQ( u_view.differenceMin( v_view ), 0 ); + EXPECT_EQ( u_view.differenceAbsMax( v_view ), 0 ); + EXPECT_EQ( u_view.differenceAbsMin( v_view ), 0 ); + EXPECT_EQ( u_view.differenceLpNorm( v_view, 1 ), 0 ); + EXPECT_EQ( u_view.differenceSum( v_view ), 0 ); + EXPECT_EQ( u_view.scalarProduct( v_view ), 100 ); +} + +TEST( VectorSpecialCasesTest, initializationOfVectorViewByArrayView ) +{ + using ArrayType = Containers::Array< int, Devices::Host >; + using VectorViewType = VectorView< const int, Devices::Host >; + using ArrayViewType = ArrayView< int, Devices::Host >; + + ArrayType a( 100 ); + a.setValue( 0 ); + ArrayViewType a_view( a ); + + VectorViewType v_view( a_view ); + EXPECT_EQ( v_view.getData(), a_view.getData() ); + EXPECT_EQ( v_view.sum(), 0 ); +} + +TEST( VectorSpecialCasesTest, defaultConstructors ) +{ + using ArrayType = Containers::Array< int, Devices::Host >; + using VectorViewType = VectorView< int, Devices::Host >; + using ArrayViewType = ArrayView< int, Devices::Host >; + + ArrayType a( 100 ); + a.setValue( 0 ); + + ArrayViewType a_view; + a_view.bind( a ); + + VectorViewType v_view; + v_view.bind( a ); + EXPECT_EQ( v_view.getData(), a_view.getData() ); +} + +#endif // HAVE_GTEST + + +#include "../GtestMissingError.h" +int main( int argc, char* argv[] ) +{ + //Test(); + //return 0; +#ifdef HAVE_GTEST + ::testing::InitGoogleTest( &argc, argv ); + return RUN_ALL_TESTS(); +#else + throw GtestMissingError(); +#endif +} diff --git a/src/UnitTests/Containers/VectorTest-7.cpp b/src/UnitTests/Containers/VectorTest-7.cpp new file mode 100644 index 0000000000..8db62ba86f --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-7.cpp @@ -0,0 +1,11 @@ +/*************************************************************************** + VectorTest-7.cpp - description + ------------------- + begin : Apr 30, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#include "VectorTest-7.h" diff --git a/src/UnitTests/Containers/VectorTest-7.cu b/src/UnitTests/Containers/VectorTest-7.cu new file mode 100644 index 0000000000..303dcd1b03 --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-7.cu @@ -0,0 +1,11 @@ +/*************************************************************************** + VectorTest-7.cu - description + ------------------- + begin : Apr 30, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#include "VectorTest-7.h" diff --git a/src/UnitTests/Containers/VectorTest-7.h b/src/UnitTests/Containers/VectorTest-7.h new file mode 100644 index 0000000000..02f2a00b20 --- /dev/null +++ b/src/UnitTests/Containers/VectorTest-7.h @@ -0,0 +1,68 @@ +/*************************************************************************** + VectorTest-6.h - description + ------------------- + begin : Oct 25, 2010 + copyright : (C) 2010 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +// NOTE: Vector = Array + VectorOperations, so we test Vector and VectorOperations at the same time + +#pragma once + +#ifdef HAVE_GTEST +#include <limits> + +#include <TNL/Experimental/Arithmetics/Quad.h> +#include <TNL/Containers/Vector.h> +#include <TNL/Containers/VectorView.h> +#include "VectorTestSetup.h" + +#include "gtest/gtest.h" + +using namespace TNL; +using namespace TNL::Containers; +using namespace TNL::Containers::Algorithms; +using namespace TNL::Arithmetics; + +// should be small enough to have fast tests, but larger than minGPUReductionDataSize +// and large enough to require multiple CUDA blocks for reduction +constexpr int VECTOR_TEST_SIZE = 500; + +TYPED_TEST( VectorTest, min ) +{ + using VectorType = typename TestFixture::VectorType; + using ViewType = typename TestFixture::ViewType; + using RealType = typename VectorType::RealType; + const int size = VECTOR_TEST_SIZE; + + VectorType _u( size ); + ViewType u( _u ); + for( int i = 0; i < size; i++ ) + { + u.setElement( i, ( RealType )( i - size / 2 ) / ( RealType ) size ); + } + + EXPECT_EQ( min( u ), u.getElement( 0 ) ); + //for( int i = 0; i < size; i++ ) + // EXPECT_NEAR( acos( u ).getElement( i ), v.getElement( i ), 1.0e-6 ); +} + + +#endif // HAVE_GTEST + + +#include "../GtestMissingError.h" +int main( int argc, char* argv[] ) +{ + //Test(); + //return 0; +#ifdef HAVE_GTEST + ::testing::InitGoogleTest( &argc, argv ); + return RUN_ALL_TESTS(); +#else + throw GtestMissingError(); +#endif +} -- GitLab