From 6b5a524ca4cdcce50613c1d84cf524a4fca687bd Mon Sep 17 00:00:00 2001
From: Tomas Oberhuber <tomas.oberhuber@fjfi.cvut.cz>
Date: Tue, 11 Sep 2018 10:30:30 +0200
Subject: [PATCH] Fixed operator << overloading for MeshFunction and added unit
 tests.

---
 src/TNL/Functions/MeshFunction.h             |  5 ++
 src/TNL/Functions/MeshFunction_impl.h        | 12 ++++-
 src/UnitTests/Functions/CMakeLists.txt       | 17 ++++--
 src/UnitTests/Functions/MeshFunctionTest.cpp | 11 ++++
 src/UnitTests/Functions/MeshFunctionTest.cu  | 11 ++++
 src/UnitTests/Functions/MeshFunctionTest.h   | 57 ++++++++++++++++++++
 6 files changed, 107 insertions(+), 6 deletions(-)
 create mode 100644 src/UnitTests/Functions/MeshFunctionTest.cpp
 create mode 100644 src/UnitTests/Functions/MeshFunctionTest.cu
 create mode 100644 src/UnitTests/Functions/MeshFunctionTest.h

diff --git a/src/TNL/Functions/MeshFunction.h b/src/TNL/Functions/MeshFunction.h
index b0a44c27f5..3eb99fc871 100644
--- a/src/TNL/Functions/MeshFunction.h
+++ b/src/TNL/Functions/MeshFunction.h
@@ -184,6 +184,11 @@ class MeshFunction :
    
 };
 
+template< typename Mesh,
+          int MeshEntityDimension,
+          typename Real >
+std::ostream& operator << ( std::ostream& str, const MeshFunction< Mesh, MeshEntityDimension, Real >& f );
+
 } // namespace Functions
 } // namespace TNL
 
diff --git a/src/TNL/Functions/MeshFunction_impl.h b/src/TNL/Functions/MeshFunction_impl.h
index 872751f8ea..9a7867b5e4 100644
--- a/src/TNL/Functions/MeshFunction_impl.h
+++ b/src/TNL/Functions/MeshFunction_impl.h
@@ -571,7 +571,17 @@ setupSynchronizer( DistributedMeshType *distributedMesh )
       this->synchronizer.setDistributedGrid( distributedMesh );
 }
 
- 
+template< typename Mesh,
+          int MeshEntityDimension,
+          typename Real >
+std::ostream&
+operator << ( std::ostream& str, const MeshFunction< Mesh, MeshEntityDimension, Real >& f )
+{
+   str << f.getData();
+   return str;
+}
+
+
 } // namespace Functions
 } // namespace TNL
 
diff --git a/src/UnitTests/Functions/CMakeLists.txt b/src/UnitTests/Functions/CMakeLists.txt
index 9641a5b329..cf9466de45 100644
--- a/src/UnitTests/Functions/CMakeLists.txt
+++ b/src/UnitTests/Functions/CMakeLists.txt
@@ -1,13 +1,20 @@
 IF( BUILD_CUDA )
+   CUDA_ADD_EXECUTABLE( MeshFunctionTest MeshFunctionTest.h MeshFunctionTest.cu OPTIONS ${CXX_TESTS_FLAGS} )
+   TARGET_COMPILE_OPTIONS( MeshFunctionTest PRIVATE ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( MeshFunctionTest ${GTEST_BOTH_LIBRARIES} tnl )
+
    CUDA_ADD_EXECUTABLE( BoundaryMeshFunctionTest BoundaryMeshFunctionTest.h BoundaryMeshFunctionTest.cu OPTIONS ${CXX_TESTS_FLAGS} )
-   TARGET_LINK_LIBRARIES( BoundaryMeshFunctionTest ${GTEST_BOTH_LIBRARIES}
-                                                           tnl )
+   TARGET_COMPILE_OPTIONS( BoundaryMeshFunctionTest PRIVATE ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( BoundaryMeshFunctionTest ${GTEST_BOTH_LIBRARIES} tnl )
 ELSE(  BUILD_CUDA )
+   ADD_EXECUTABLE( MeshFunctionTest MeshFunctionTest.h MeshFunctionTest.cpp )
+   TARGET_COMPILE_OPTIONS( MeshFunctionTest PRIVATE ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( MeshFunctionTest ${GTEST_BOTH_LIBRARIES} tnl )
+
    ADD_EXECUTABLE( BoundaryMeshFunctionTest BoundaryMeshFunctionTest.h BoundaryMeshFunctionTest.cpp )
    TARGET_COMPILE_OPTIONS( BoundaryMeshFunctionTest PRIVATE ${CXX_TESTS_FLAGS} )
-   TARGET_LINK_LIBRARIES( BoundaryMeshFunctionTest ${GTEST_BOTH_LIBRARIES}
-                                                           tnl )
+   TARGET_LINK_LIBRARIES( BoundaryMeshFunctionTest ${GTEST_BOTH_LIBRARIES} tnl )
 ENDIF( BUILD_CUDA )
 
-
+ADD_TEST( MeshFunctionTest ${EXECUTABLE_OUTPUT_PATH}/MeshFunctionTest${CMAKE_EXECUTABLE_SUFFIX} )
 ADD_TEST( BoundaryMeshFunctionTest ${EXECUTABLE_OUTPUT_PATH}/BoundaryMeshFunctionTest${CMAKE_EXECUTABLE_SUFFIX} )
\ No newline at end of file
diff --git a/src/UnitTests/Functions/MeshFunctionTest.cpp b/src/UnitTests/Functions/MeshFunctionTest.cpp
new file mode 100644
index 0000000000..85943c29fe
--- /dev/null
+++ b/src/UnitTests/Functions/MeshFunctionTest.cpp
@@ -0,0 +1,11 @@
+/***************************************************************************
+                          MeshFunctionTest.cpp  -  description
+                             -------------------
+    begin                : Sep 11, 2018
+    copyright            : (C) 2018 by oberhuber
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#include "MeshFunctionTest.h"
diff --git a/src/UnitTests/Functions/MeshFunctionTest.cu b/src/UnitTests/Functions/MeshFunctionTest.cu
new file mode 100644
index 0000000000..9f8affcd9c
--- /dev/null
+++ b/src/UnitTests/Functions/MeshFunctionTest.cu
@@ -0,0 +1,11 @@
+/***************************************************************************
+                          MeshFunctionTest.cu  -  description
+                             -------------------
+    begin                : Sep 11, 2018
+    copyright            : (C) 2018 by oberhuber
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#include "MeshFunctionTest.h"
diff --git a/src/UnitTests/Functions/MeshFunctionTest.h b/src/UnitTests/Functions/MeshFunctionTest.h
new file mode 100644
index 0000000000..b60daf091a
--- /dev/null
+++ b/src/UnitTests/Functions/MeshFunctionTest.h
@@ -0,0 +1,57 @@
+/***************************************************************************
+                          MeshFunctionTest.h  -  description
+                             -------------------
+    begin                : Sep 11, 2018
+    copyright            : (C) 2018 by oberhuber
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#pragma once
+
+#include "../GtestMissingError.h"
+
+#ifdef HAVE_GTEST 
+#include <gtest/gtest.h>
+#include <sstream>
+#include <TNL/Functions/MeshFunction.h>
+#include <TNL/Meshes/Grid.h>
+#include <TNL/Pointers/SharedPointer.h>
+
+TEST( MeshFunctionTest, BasicConstructor )
+{
+   using Grid = TNL::Meshes::Grid< 2 >;
+   TNL::Functions::MeshFunction< Grid > meshFunction;
+}
+
+TEST( MeshFunctionTest, OstreamOperatorTest )
+{
+   using GridType = TNL::Meshes::Grid< 2 >;
+   using GridPointer = TNL::Pointers::SharedPointer< GridType >;
+   using CoordinatesType = typename GridType::CoordinatesType;
+   using MeshFunctionType = TNL::Functions::MeshFunction< GridType >;
+   GridPointer grid;
+   grid->setDimensions( CoordinatesType( 3, 3 ) );
+   MeshFunctionType meshFunction( grid );
+   meshFunction.getData().setValue( 1.0 );
+   
+   const char* str = "[ 1, 1, 1, 1, 1, 1, 1, 1, 1 ]";
+   std::stringstream string_stream1, string_stream2( str );
+   string_stream1 << meshFunction;
+   EXPECT_EQ( string_stream1.str(), string_stream2.str() );
+}
+
+
+#endif
+
+
+int main( int argc, char* argv[] )
+{
+#ifdef HAVE_GTEST
+   ::testing::InitGoogleTest( &argc, argv );
+   return RUN_ALL_TESTS();
+#else
+   throw GtestMissingError();
+#endif
+}
-- 
GitLab