From ef60ae5d74419fc4bc60683a8fa5ab01a6270995 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= <oberhuber.tomas@gmail.com>
Date: Wed, 10 Jul 2019 23:22:26 +0200
Subject: [PATCH] Added MpiCommunicatorTest.

---
 src/UnitTests/CMakeLists.txt                  |  1 +
 src/UnitTests/Communicators/CMakeLists.txt    |  9 ++++
 .../Communicators/MpiCommunicatorTest.cpp     | 51 +++++++++++++++++++
 3 files changed, 61 insertions(+)
 create mode 100644 src/UnitTests/Communicators/CMakeLists.txt
 create mode 100644 src/UnitTests/Communicators/MpiCommunicatorTest.cpp

diff --git a/src/UnitTests/CMakeLists.txt b/src/UnitTests/CMakeLists.txt
index 95cca1efb9..564a7d1347 100644
--- a/src/UnitTests/CMakeLists.txt
+++ b/src/UnitTests/CMakeLists.txt
@@ -1,3 +1,4 @@
+ADD_SUBDIRECTORY( Communicators )
 ADD_SUBDIRECTORY( Containers )
 ADD_SUBDIRECTORY( Functions )
 ADD_SUBDIRECTORY( Matrices )
diff --git a/src/UnitTests/Communicators/CMakeLists.txt b/src/UnitTests/Communicators/CMakeLists.txt
new file mode 100644
index 0000000000..8e97ff3b3f
--- /dev/null
+++ b/src/UnitTests/Communicators/CMakeLists.txt
@@ -0,0 +1,9 @@
+if( ${BUILD_MPI} )
+   ADD_EXECUTABLE( MpiCommunicatorTest MpiCommunicatorTest.cpp )
+   TARGET_COMPILE_OPTIONS( MpiCommunicatorTest PRIVATE ${CXX_TESTS_FLAGS} )
+   TARGET_LINK_LIBRARIES( MpiCommunicatorTest ${GTEST_BOTH_LIBRARIES} )
+
+   SET( mpi_test_parameters -np 4 -H localhost:4 "${EXECUTABLE_OUTPUT_PATH}/MpiCommunicatorTest${CMAKE_EXECUTABLE_SUFFIX}" )
+   ADD_TEST( NAME MpiCommunicatorTest COMMAND "mpirun" ${mpi_test_parameters})
+
+endif()
diff --git a/src/UnitTests/Communicators/MpiCommunicatorTest.cpp b/src/UnitTests/Communicators/MpiCommunicatorTest.cpp
new file mode 100644
index 0000000000..0056e41a87
--- /dev/null
+++ b/src/UnitTests/Communicators/MpiCommunicatorTest.cpp
@@ -0,0 +1,51 @@
+/***************************************************************************
+                          MpiCommunicatorTest.h  -  description
+                             -------------------
+    begin                : Jul 10, 2019
+    copyright            : (C) 2019 by Tomas Oberhuber
+    email                : tomas.oberhuber@fjfi.cvut.cz
+ ***************************************************************************/
+
+/* See Copyright Notice in tnl/Copyright */
+
+#ifdef HAVE_GTEST
+
+#include "gtest/gtest.h"
+#include <TNL/Communicators/MpiCommunicator.h>
+
+using namespace TNL;
+using namespace TNL::Communicators;
+
+// test fixture for typed tests
+template< typename Real >
+class MpiCommunicatorTest : public ::testing::Test
+{
+   protected:
+      using RealType = Real;
+      using CommunicatorType = MpiCommunicator;
+};
+
+// types for which MpiCommunicatorTest is instantiated
+using MpiCommunicatorTypes = ::testing::Types<
+   short,
+   int,
+   long,
+   float,
+   double
+>;
+
+TYPED_TEST_CASE( MpiCommunicatorTest, MpiCommunicatorTypes );
+
+TYPED_TEST( MpiCommunicatorTest, allReduce )
+{
+   using RealType = typename TestFixture::RealType;
+   using CommunicatorType = typename TestFixture::CommunicatorType;
+   RealType a = CommunicatorType::GetRank();
+   RealType b = 0;
+   CommunicatorType::Allreduce( &a, &b, 1, MPI_MAX, MPI_COMM_WORLD );
+   EXPECT_EQ( b, CommunicatorType::GetSize() - 1  );
+}
+
+#endif // HAVE_GTEST
+
+#include "../main_mpi.h"
\ No newline at end of file
-- 
GitLab