Salome HOME
65aaa478dead707764dde23f225a949e0bfaea84
[tools/medcoupling.git] / src / ParaMEDMEMTest / MPIMainTest.hxx
1 // Copyright (C) 2007-2020  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef _MPIMAINTEST_HXX_
21 #define _MPIMAINTEST_HXX_
22
23 #include <cppunit/CompilerOutputter.h>
24 #include <cppunit/TestResult.h>
25 #include <cppunit/TestResultCollector.h>
26 #include <cppunit/TextTestProgressListener.h>
27 #include <cppunit/BriefTestProgressListener.h>
28 #include <cppunit/extensions/TestFactoryRegistry.h>
29 #include <cppunit/TestRunner.h>
30 #include <stdexcept>
31
32 #include <mpi.h>
33
34 #include <iostream>
35 #include <fstream>
36 #ifndef WIN32
37 #include <fpu_control.h>
38 #endif
39
40 // ============================================================================
41 /*!
42  *  Main program source for Unit Tests with cppunit package does not depend
43  *  on actual tests, so we use the same for all partial unit tests.
44  */
45 // ============================================================================
46
47 int main(int argc, char* argv[])
48 {
49 #ifndef WIN32
50   fpu_control_t cw = _FPU_DEFAULT & ~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM);
51   _FPU_SETCW(cw);
52 #endif
53   MPI_Init(&argc,&argv);
54   int rank;
55   MPI_Comm_rank(MPI_COMM_WORLD,&rank);
56   
57   // --- Create the event manager and test controller
58   CPPUNIT_NS::TestResult controller;
59
60   // ---  Add a listener that colllects test result
61   CPPUNIT_NS::TestResultCollector result;
62   controller.addListener( &result );        
63
64   // ---  Add a listener that print dots as test run.
65 #ifdef WIN32
66   CPPUNIT_NS::TextTestProgressListener progress;
67 #else
68   CPPUNIT_NS::BriefTestProgressListener progress;
69 #endif
70   controller.addListener( &progress );      
71
72   // ---  Get the top level suite from the registry
73
74   CPPUNIT_NS::Test *suite =
75     CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
76
77   // ---  Adds the test to the list of test to run
78
79   CPPUNIT_NS::TestRunner runner;
80   runner.addTest( suite );
81   runner.run( controller);
82
83   // ---  Print test in a compiler compatible format.
84
85   std::ostringstream testFileName;
86   testFileName<<"UnitTestsResult"<<rank;
87   std::ofstream testFile;
88   testFile.open(testFileName.str().c_str(), std::ios::out |  std::ios::trunc);
89   //CPPUNIT_NS::CompilerOutputter outputter( &result, std::cerr );
90   CPPUNIT_NS::CompilerOutputter outputter( &result, testFile );
91   outputter.write(); 
92
93   // ---  Run the tests.
94
95   bool wasSucessful = result.wasSuccessful();
96   testFile.close();
97
98   // ---  Return error code 1 if the one of test failed.
99
100   MPI_Finalize();
101   
102   return wasSucessful ? 0 : 1;
103 }
104
105 #endif