From 6f645c5a2afae4711f71a243cf9dc251d0be66bd Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Mon, 2 Dec 2019 17:58:48 +0100
Subject: [PATCH] Added unit tests for CSR matrix using Segments.

---
 src/UnitTests/Matrices/CMakeLists.txt         |  16 ++
 .../SparseMatrixTest_CSR_segments.cpp         |   1 +
 .../Matrices/SparseMatrixTest_CSR_segments.cu |   1 +
 .../Matrices/SparseMatrixTest_CSR_segments.h  | 141 ++++++++++++++++++
 4 files changed, 159 insertions(+)
 create mode 100644 src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cpp
 create mode 100644 src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cu
 create mode 100644 src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.h

diff --git a/src/UnitTests/Matrices/CMakeLists.txt b/src/UnitTests/Matrices/CMakeLists.txt
index 2a08be2198..f278934a6a 100644
--- a/src/UnitTests/Matrices/CMakeLists.txt
+++ b/src/UnitTests/Matrices/CMakeLists.txt
@@ -25,6 +25,12 @@ IF( BUILD_CUDA )
 
    CUDA_ADD_EXECUTABLE( DenseMatrixTest DenseMatrixTest.cu OPTIONS ${CXX_TESTS_FLAGS} )
    TARGET_LINK_LIBRARIES( DenseMatrixTest ${GTEST_BOTH_LIBRARIES} )
+
+   ####
+   # Segments tests
+   CUDA_ADD_EXECUTABLE( SparseMatrixTest_CSR_segments SparseMatrixTest_CSR_segments.cu OPTIONS ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( SparseMatrixTest_CSR_segments ${GTEST_BOTH_LIBRARIES} )
+
 ELSE(  BUILD_CUDA )
    ADD_EXECUTABLE( SparseMatrixCopyTest SparseMatrixCopyTest.cpp )
    TARGET_COMPILE_OPTIONS( SparseMatrixCopyTest PRIVATE ${CXX_TESTS_FLAGS} )
@@ -61,6 +67,13 @@ ELSE(  BUILD_CUDA )
    ADD_EXECUTABLE( DenseMatrixTest DenseMatrixTest.cpp )
    TARGET_COMPILE_OPTIONS( DenseMatrixTest PRIVATE ${CXX_TESTS_FLAGS} )
    TARGET_LINK_LIBRARIES( DenseMatrixTest ${GTEST_BOTH_LIBRARIES} )
+
+   ####
+   # Segments tests
+   ADD_EXECUTABLE( SparseMatrixTest_CSR_segments SparseMatrixTest_CSR_segments.cpp )
+   TARGET_COMPILE_OPTIONS( SparseMatrixTest_CSR_segments PRIVATE ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( SparseMatrixTest_CSR_segments ${GTEST_BOTH_LIBRARIES} )
+
 ENDIF( BUILD_CUDA )
 
 
@@ -76,6 +89,9 @@ ADD_TEST( SparseMatrixTest_SlicedEllpack ${EXECUTABLE_OUTPUT_PATH}/SparseMatrixT
 # TODO: DenseMatrixTest is not finished
 #ADD_TEST( DenseMatrixTest ${EXECUTABLE_OUTPUT_PATH}/DenseMatrixTest${CMAKE_EXECUTABLE_SUFFIX} )
 
+####
+# Segments tests
+ADD_TEST( SparseMatrixTest_CSR_segments ${EXECUTABLE_OUTPUT_PATH}/SparseMatrixTest_CSR_segments${CMAKE_EXECUTABLE_SUFFIX} )
 
 if( ${BUILD_MPI} )
    if( BUILD_CUDA )
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cpp b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cpp
new file mode 100644
index 0000000000..771c74b9a2
--- /dev/null
+++ b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cpp
@@ -0,0 +1 @@
+#include "SparseMatrixTest_CSR_segments.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cu b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cu
new file mode 100644
index 0000000000..771c74b9a2
--- /dev/null
+++ b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.cu
@@ -0,0 +1 @@
+#include "SparseMatrixTest_CSR_segments.h"
diff --git a/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.h b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.h
new file mode 100644
index 0000000000..00654de3c1
--- /dev/null
+++ b/src/UnitTests/Matrices/SparseMatrixTest_CSR_segments.h
@@ -0,0 +1,141 @@
+/***************************************************************************
+                          SparseMatrixTest_CSR.h -  description
+                             -------------------
+    begin                : Nov 2, 2018
+    copyright            : (C) 2018 by Tomas Oberhuber et al.
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#include <TNL/Containers/Segments/CSR.h>
+#include <TNL/Matrices/SparseMatrix.h>
+
+
+#include "SparseMatrixTest.hpp"
+#include <iostream>
+
+#ifdef HAVE_GTEST
+#include <gtest/gtest.h>
+
+// test fixture for typed tests
+template< typename Matrix >
+class CSRMatrixTest : public ::testing::Test
+{
+protected:
+   using CSRMatrixType = Matrix;
+};
+
+// types for which MatrixTest is instantiated
+using CSRMatrixTypes = ::testing::Types
+<
+    TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Host, short >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Host, short >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Host, short >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Host, short >,
+    TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Host, int   >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Host, int   >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Host, int   >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Host, int   >,
+    TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Host, long  >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Host, long  >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Host, long  >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Host, long  >
+#ifdef HAVE_CUDA                                                                             
+   ,TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Cuda, short >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Cuda, short >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Cuda, short >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Cuda, short >,
+    TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Cuda, int   >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Cuda, int   >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Cuda, int   >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Cuda, int   >,
+    TNL::Matrices::SparseMatrix< int,     TNL::Containers::Segments::CSR, TNL::Devices::Cuda, long  >,
+    TNL::Matrices::SparseMatrix< long,    TNL::Containers::Segments::CSR, TNL::Devices::Cuda, long  >,
+    TNL::Matrices::SparseMatrix< float,   TNL::Containers::Segments::CSR, TNL::Devices::Cuda, long  >,
+    TNL::Matrices::SparseMatrix< double,  TNL::Containers::Segments::CSR, TNL::Devices::Cuda, long  >
+#endif
+>;
+
+TYPED_TEST_SUITE( CSRMatrixTest, CSRMatrixTypes);
+
+TYPED_TEST( CSRMatrixTest, setDimensionsTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_SetDimensions< CSRMatrixType >();
+}
+
+//TYPED_TEST( CSRMatrixTest, setCompressedRowLengthsTest )
+//{
+////    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+//
+////    test_SetCompressedRowLengths< CSRMatrixType >();
+//
+//    bool testRan = false;
+//    EXPECT_TRUE( testRan );
+//    std::cout << "\nTEST DID NOT RUN. NOT WORKING.\n\n";
+//    std::cout << "      This test is dependent on the input format. \n";
+//    std::cout << "      Almost every format allocates elements per row differently.\n\n";
+//    std::cout << "\n    TODO: Finish implementation of getNonZeroRowLength (Only non-zero elements, not the number of allocated elements.)\n\n";
+//}
+
+TYPED_TEST( CSRMatrixTest, setLikeTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_SetLike< CSRMatrixType, CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, resetTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_Reset< CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, setElementTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_SetElement< CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, addElementTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_AddElement< CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, setRowTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_SetRow< CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, vectorProductTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_VectorProduct< CSRMatrixType >();
+}
+
+TYPED_TEST( CSRMatrixTest, saveAndLoadTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_SaveAndLoad< CSRMatrixType >( "test_SparseMatrixTest_CSR" );
+}
+
+TYPED_TEST( CSRMatrixTest, printTest )
+{
+    using CSRMatrixType = typename TestFixture::CSRMatrixType;
+
+    test_Print< CSRMatrixType >();
+}
+
+#endif
+
+#include "../main.h"
-- 
GitLab