Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_VtkMeshDriver.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
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.
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 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include <MEDMEM_VtkMeshDriver.hxx>
24 #include <MEDMEM_MedMeshDriver.hxx>
25 #include <MEDMEM_Mesh.hxx>
26
27 // use this define to enable lines, execution of which leads to Segmentation Fault
28 //#define ENABLE_FAULTS
29
30 // use this define to enable CPPUNIT asserts and fails, showing bugs
31 //#define ENABLE_FORCED_FAILURES
32
33 using namespace std;
34 using namespace MEDMEM;
35
36 /*!
37  *  Check methods (11), defined in MEDMEM_VtkMeshDriver.hxx:
38  *  class VTK_MESH_DRIVER : public GENDRIVER {
39  *   (+) VTK_MESH_DRIVER();
40  *   (+) VTK_MESH_DRIVER(const string & fileName,  MESH * ptrMesh);
41  *   (+) VTK_MESH_DRIVER(const VTK_MESH_DRIVER & driver);
42  *   (+) ~VTK_MESH_DRIVER();
43  *   (+) void open();
44  *   (+) void close();
45  *   (+) void openConst() const throw (MEDEXCEPTION);
46  *   (+) void closeConst() const throw (MEDEXCEPTION);
47  *   (+) void write(void) const throw (MEDEXCEPTION);
48  *   (+) void read (void) throw (MEDEXCEPTION);
49  *   (+) void   setMeshName(const string & meshName);
50  *   (+) string getMeshName() const;
51  *  }
52  */
53 void MEDMEMTest::testVtkMeshDriver()
54 {
55   MESH * aMesh = new MESH;
56
57   string filename_rd               = getResourceFile("pointe.med");
58   string emptyfilename             = "";
59   string meshname_rd               = "maa1";
60   string meshname                  = "MyMesh";
61   string fileNotExistsName         = "/path_not_exists/file_not_exists.vtk";
62   string filename                  = makeTmpFile("my_pointe.vtk");
63
64   MEDMEMTest_TmpFilesRemover aRemover;
65   aRemover.Register(filename);
66
67   {
68     //Creation a invalid Vtk driver
69     VTK_MESH_DRIVER *aInvalidVtkDriver = new VTK_MESH_DRIVER(fileNotExistsName, aMesh);
70
71     //Trying open not existing file
72     CPPUNIT_ASSERT_THROW(aInvalidVtkDriver->open(), MEDEXCEPTION);
73     CPPUNIT_ASSERT_THROW(aInvalidVtkDriver->openConst(), MEDEXCEPTION);
74
75     CPPUNIT_ASSERT_NO_THROW(delete aInvalidVtkDriver);
76   }
77
78   {
79     //Create Vtk driver with empty file name
80     VTK_MESH_DRIVER *aEmptyVtkDriver = new VTK_MESH_DRIVER(emptyfilename, aMesh);
81
82     //Test open() and openConst() methods with empty file name
83     CPPUNIT_ASSERT_THROW(aEmptyVtkDriver->open(), MEDEXCEPTION);
84     CPPUNIT_ASSERT_THROW(aEmptyVtkDriver->openConst(), MEDEXCEPTION);
85
86     delete aEmptyVtkDriver;
87   }
88
89   //Creation corect driver
90   VTK_MESH_DRIVER *aVtkDriver = new VTK_MESH_DRIVER(filename, aMesh);
91   CPPUNIT_ASSERT(aVtkDriver);
92
93   //Create a Mesh
94   MED_MESH_RDONLY_DRIVER *aMedMeshRdDriver22 = new MED_MESH_RDONLY_DRIVER(filename_rd, aMesh);
95   aMedMeshRdDriver22->open();
96   aMedMeshRdDriver22->setMeshName(meshname_rd);
97
98   //Test openConst() and closeConst() methods
99   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->openConst());
100   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->closeConst());
101
102   //Test open() method
103   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->open());
104
105   //Trying open file secondary
106   // (NOT!!! BUG) No exception on attempt to open a file for the second time
107   //CPPUNIT_ASSERT_NO_THROW(aVtkDriver->open());
108
109   //Test read method
110   CPPUNIT_ASSERT_THROW(aVtkDriver->read(), MEDEXCEPTION);
111
112   //Trying write empty mesh
113   CPPUNIT_ASSERT_THROW(aVtkDriver->write(), MEDEXCEPTION);
114
115   //Read mesh from Med file
116   aMedMeshRdDriver22->read();
117
118   //Check mesh
119   CPPUNIT_ASSERT(aMesh);
120
121   //Test SetMeshName and getMeshName methods
122   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->setMeshName(meshname));
123   CPPUNIT_ASSERT_EQUAL(meshname, aVtkDriver->getMeshName());
124
125   //Test write method
126   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->write());
127
128   //Test close method
129   CPPUNIT_ASSERT_NO_THROW(aVtkDriver->close());
130
131   //Test Default constructor
132   VTK_MESH_DRIVER aVtkDriverCpy_1;
133
134   //Test copy constructor
135   VTK_MESH_DRIVER aVtkDriverCpy_2 (*aVtkDriver);
136
137   //Test (bool operator ==) defined in GENDRIVER class
138   //CPPUNIT_ASSERT(aVtkDriverCpy_2.GENDRIVER::operator==(aVtkDriver));
139
140   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
141   //ostringstream ostr1, ostr2;
142   //ostr1 << aVtkDriverCpy_2;
143   //ostr2 << aVtkDriver;
144   //CPPUNIT_ASSERT(ostr1.str() != "");
145   //CPPUNIT_ASSERT_EQUAL(ostr1.str(), ostr2.str());
146
147   //Delete objects
148   delete aVtkDriver;
149   delete aMedMeshRdDriver22;
150   aMesh->removeReference();
151 }