Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDMEM / Test / MEDMEMTest_VtkMedDriver.cxx
1 // Copyright (C) 2006  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include <MEDMEM_Compatibility21_22.hxx>
24 #include <MEDMEM_VtkMedDriver.hxx>
25 #include <MEDMEM_MedMedDriver21.hxx>
26 #include <MEDMEM_Med.hxx>
27
28 // use this define to enable lines, execution of which leads to Segmentation Fault
29 //#define ENABLE_FAULTS
30
31 // use this define to enable CPPUNIT asserts and fails, showing bugs
32 #define ENABLE_FORCED_FAILURES
33
34 using namespace std;
35 using namespace MEDMEM;
36 using namespace MED_EN;
37
38 /*!
39  *  Check methods (5), defined in MEDMEM_VtkMedDriver.hxx:
40  *  class VTK_MED_DRIVER : public GENDRIVER {
41  *   (+) VTK_MED_DRIVER(const string & fileName,  MED * const ptrMed);
42  *   (+) VTK_MED_DRIVER(const VTK_MED_DRIVER & driver);
43  *   (+) ~VTK_MED_DRIVER();
44  *   (+) void write (void) const;
45  *   (-) virtual void read (void)  {};
46  *   (+) GENDRIVER * copy (void) const;
47  *  }
48  */
49 void MEDMEMTest::testVtkMedDriver()
50 {
51   MED *aMed                = new MED();
52   string data_dir          = getenv("DATA_DIR");
53   string tmp_dir           = getenv("TMP");
54   if (tmp_dir == "")
55     tmp_dir = "/tmp";
56   string filename_rd       = data_dir + "/MedFiles/pointe.med";
57   string emptyfilename     = "";
58   string fileNotExistsName = "/path_not_exists/file_not_exists.vtk";
59   string filename_wr       =  tmp_dir  + "/myMED_pointe.vtk";
60
61   MEDMEMTest_TmpFilesRemover aRemover;
62   aRemover.Register(filename_wr);
63
64   //Read MED structure from file
65   MED_MED_RDONLY_DRIVER21 *aMedMedRdDriver21 = new MED_MED_RDONLY_DRIVER21(filename_rd, aMed);
66   aMedMedRdDriver21->open();
67   aMedMedRdDriver21->read();
68   aMedMedRdDriver21->close();
69   //Check Med
70   CPPUNIT_ASSERT(aMed);
71
72   //Creation incorrect Vtk Med Driver (file is not exists)
73   VTK_MED_DRIVER *aInvalidVtkMedDriver = new VTK_MED_DRIVER(fileNotExistsName,aMed);
74
75   //Trying write data in the not existing file
76   CPPUNIT_ASSERT_THROW(aInvalidVtkMedDriver->write(),MEDEXCEPTION);
77
78   //Creation correct Vtk Med Driver
79   VTK_MED_DRIVER *aVtkMedDriver = new VTK_MED_DRIVER(filename_wr ,aMed);
80
81   //Check driver
82   CPPUNIT_ASSERT(aVtkMedDriver);
83
84   //Test write() method
85
86   try
87   {
88     aVtkMedDriver->write();
89   }
90   catch(MEDEXCEPTION &e)
91   {
92     CPPUNIT_FAIL(e.what());
93   }
94   catch( ... )
95   {
96     CPPUNIT_FAIL("Unknown exception");
97   }
98
99   //Test copy constructor
100   VTK_MED_DRIVER *aVtkMedDriverCpy = new VTK_MED_DRIVER(*aVtkMedDriver);
101 #ifdef ENABLE_FORCED_FAILURES
102   CPPUNIT_ASSERT_EQUAL(*aVtkMedDriverCpy, *aVtkMedDriver);
103 #endif
104
105   //Test (operator ==) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
106 #ifdef ENABLE_FORCED_FAILURES
107   CPPUNIT_ASSERT(*aVtkMedDriverCpy == *aVtkMedDriver);
108 #endif
109
110   //Test copy() function
111   VTK_MED_DRIVER *aVtkMedDriverCpy_1 = (VTK_MED_DRIVER*)aVtkMedDriver->copy();
112 #ifdef ENABLE_FORCED_FAILURES
113   CPPUNIT_ASSERT_EQUAL(*aVtkMedDriverCpy_1, *aVtkMedDriver);
114 #endif
115
116   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
117   ostringstream ostr1, ostr2;
118   ostr1<<*aVtkMedDriverCpy;
119   ostr2<<*aVtkMedDriverCpy_1;
120   CPPUNIT_ASSERT(ostr1.str() != "");
121   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
122
123
124   //Delete objects
125   delete aMedMedRdDriver21;
126 #ifdef ENABLE_FORCED_FAILURES
127   delete aInvalidVtkMedDriver;
128   delete aVtkMedDriver;
129   delete aVtkMedDriverCpy;
130   delete aVtkMedDriverCpy_1;
131   //MEDEXCEPTION in the destructor (after trying close file)
132 #endif
133 }