Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_VtkFieldDriver.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_VtkFieldDriver.hxx>
24 #include <MEDMEM_Field.hxx>
25
26 // use this define to enable lines, execution of which leads to Segmentation Fault
27 //#define ENABLE_FAULTS
28
29 // use this define to enable CPPUNIT asserts and fails, showing bugs
30 //#define ENABLE_FORCED_FAILURES
31
32 using namespace std;
33 using namespace MEDMEM;
34 using namespace MED_EN;
35
36 /*!
37  *  Check methods (14), defined in MEDMEM_VtkFieldDriver.hxx:
38  *  template <class T> class VTK_FIELD_DRIVER : public GENDRIVER {
39  *       //MUST BE PRIVATE, because it is impossible to set _ptrField after this constructor.
40  *       //AND cannot compile, so maybe it is specially implemented to prevent usage of it.
41  *   (!) template <class INTERLACING_TAG> VTK_FIELD_DRIVER();
42  *   (+) template <class INTERLACING_TAG> VTK_FIELD_DRIVER
43  *               (const string & fileName, FIELD<T, INTERLACING_TAG> * ptrField)
44  *   (+) VTK_FIELD_DRIVER(const VTK_FIELD_DRIVER & fieldDriver)
45  *   (+) ~VTK_FIELD_DRIVER()
46  *   (+) void openConst() const throw (MEDEXCEPTION)
47  *   (+) void openConstAppend() const throw (MEDEXCEPTION)
48  *   (+) void open() throw (MEDEXCEPTION)
49  *   (+) void openAppend() throw (MEDEXCEPTION)
50  *   (+) void closeConst() const throw (MEDEXCEPTION)
51  *   (+) void close()
52  *   (+) void setFieldName(const string & fieldName);
53  *   (+) string getFieldName() const;
54  *   (+) void read (void) throw (MEDEXCEPTION);
55  *   (+) void write(void) const throw (MEDEXCEPTION);
56  *   (+) void writeAppend(void) const throw (MEDEXCEPTION);
57  *  }
58  */
59 void MEDMEMTest::testVtkFieldDriver()
60 {
61   FIELD<int> *aField                = new FIELD<int>();
62   FIELD<double> *aField_1           = new FIELD<double>();
63
64   string filename_rd                = getResourceFile("pointe.med");
65   string emptyfilename              = "";
66   string fileNotExistsName          = "/path_not_exists/file_not_exists.vtk";
67   string filename_wr                = makeTmpFile( "myField_pointe.vtk" );
68
69   string fieldname_rd_int           = "fieldnodeint";
70   string fieldname_wr_int           = "myintfield";
71   string fieldname_rd_double        = "fieldnodedouble";
72   string fieldname_wr_double        = "mydoublefield";
73
74   // To remove tmp files from disk
75   MEDMEMTest_TmpFilesRemover aRemover;
76   aRemover.Register(filename_wr);
77
78   /////////////////////////////////////
79   //  TEST1: Open not existing file  //
80   /////////////////////////////////////
81   //Creation Invalid VtkFieldDriver (file not exist)
82   VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_1 = new VTK_FIELD_DRIVER<int>(fileNotExistsName, aField);
83   //Trying open not existing file
84   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->open(), MEDEXCEPTION);
85   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConst(), MEDEXCEPTION);
86   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConstAppend(), MEDEXCEPTION);
87
88   /////////////////////////////////////////////
89   //  TEST2: Open file with empty file name  //
90   /////////////////////////////////////////////
91   //Creation Invalid VtkFieldDriver (empty file name)
92   VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_2 = new VTK_FIELD_DRIVER<int>(emptyfilename, aField);
93
94   //Trying file with empty name
95   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->open(), MEDEXCEPTION);
96   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConst(), MEDEXCEPTION);
97   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConstAppend(), MEDEXCEPTION);
98
99   ////////////////////////
100   //  TEST2: Main test  //
101   ////////////////////////
102   //Read Fields from file
103   MED_FIELD_RDONLY_DRIVER<int> *aMedRdFieldDriver22_int = new MED_FIELD_RDONLY_DRIVER<int>(filename_rd, aField);
104   aMedRdFieldDriver22_int->open();
105   aMedRdFieldDriver22_int->setFieldName(fieldname_rd_int);
106   aMedRdFieldDriver22_int->read();
107   aMedRdFieldDriver22_int->close();
108   MESH *mesh=new MESH(MED_DRIVER,filename_rd,"maa1");
109   aField->getSupport()->setMesh(mesh);
110
111   MED_FIELD_RDONLY_DRIVER<double> *aMedRdFieldDriver22_double = new MED_FIELD_RDONLY_DRIVER<double>(filename_rd, aField_1);
112   aMedRdFieldDriver22_double->open();
113   aMedRdFieldDriver22_double->setFieldName(fieldname_rd_double);
114   aMedRdFieldDriver22_double->read();
115   aMedRdFieldDriver22_double->close();
116   aField_1->getSupport()->setMesh(mesh);
117   mesh->removeReference();
118   //Check fields
119   CPPUNIT_ASSERT(aField);
120
121   //Creation correct VtkFieldDriver
122   VTK_FIELD_DRIVER<int> *aVtkFieldDriver_int = new VTK_FIELD_DRIVER<int>(filename_wr, aField);
123
124   //Check driver
125   CPPUNIT_ASSERT(aVtkFieldDriver_int);
126
127   //Test setFieldName() and getFieldName() methods
128   try
129   {
130     aVtkFieldDriver_int->setFieldName(fieldname_wr_int);
131   }
132   catch(MEDEXCEPTION &e)
133   {
134     CPPUNIT_FAIL(e.what());
135   }
136   catch( ... )
137   {
138     CPPUNIT_FAIL("Unknown exception");
139   }
140   CPPUNIT_ASSERT_EQUAL(fieldname_wr_int, aVtkFieldDriver_int->getFieldName());
141
142   //Test open() method
143   try
144   {
145     aVtkFieldDriver_int->open();
146   }
147   catch(MEDEXCEPTION &e)
148   {
149     CPPUNIT_FAIL(e.what());
150   }
151   catch( ... )
152   {
153     CPPUNIT_FAIL("Unknown exception");
154   }
155
156   //Test write() method
157   try
158   {
159     aVtkFieldDriver_int->write();
160   }
161   catch(MEDEXCEPTION &e)
162   {
163     CPPUNIT_FAIL(e.what());
164   }
165   catch( ... )
166   {
167     CPPUNIT_FAIL("Unknown exception");
168   }
169
170   //Test read() method for Vtk Field Driver
171   CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
172
173   //Test close() method
174   try
175   {
176     aVtkFieldDriver_int->close();
177   }
178   catch(MEDEXCEPTION &e)
179   {
180     CPPUNIT_FAIL(e.what());
181   }
182   catch( ... )
183   {
184     CPPUNIT_FAIL("Unknown exception");
185   }
186
187   //Test openAppend() method
188   try
189   {
190     aVtkFieldDriver_int->close();
191   }
192   catch(MEDEXCEPTION &e)
193   {
194     CPPUNIT_FAIL(e.what());
195   }
196   catch( ... )
197   {
198     CPPUNIT_FAIL("Unknown exception");
199   }
200
201
202   /////////////////////////////////////////////////////////
203   //  TEST? Test openAppend() and writeAppend() methods  //
204   /////////////////////////////////////////////////////////
205
206   //Creation correct VtkFieldDriver
207   VTK_FIELD_DRIVER<double> *aVtkFieldDriver_double = new VTK_FIELD_DRIVER<double>(filename_wr, aField_1);
208
209   //Check driver
210   CPPUNIT_ASSERT(aVtkFieldDriver_double);
211
212   //Test openAppend() method
213   try
214   {
215     aVtkFieldDriver_double->openAppend();
216   }
217   catch(MEDEXCEPTION &e)
218   {
219     CPPUNIT_FAIL(e.what());
220   }
221   catch( ... )
222   {
223     CPPUNIT_FAIL("Unknown exception");
224   }
225
226   aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
227
228   //Test writeAppend() method
229   try
230   {
231     aVtkFieldDriver_double->writeAppend();
232   }
233   catch(MEDEXCEPTION &e)
234   {
235     CPPUNIT_FAIL(e.what());
236   }
237   catch( ... )
238   {
239     CPPUNIT_FAIL("Unknown exception");
240   }
241
242   try
243   {
244     aVtkFieldDriver_double->close();
245   }
246   catch(MEDEXCEPTION &e)
247   {
248     CPPUNIT_FAIL(e.what());
249   }
250   catch( ... )
251   {
252     CPPUNIT_FAIL("Unknown exception");
253   }
254
255   /////////////////////////
256   //  TEST BINARY FORMAT //
257   /////////////////////////
258
259   DRIVERFACTORY::setVtkBinaryFormatForWriting( true );
260
261   //BINARY: Test open() method
262   try
263   {
264     aVtkFieldDriver_int->open();
265   }
266   catch(MEDEXCEPTION &e)
267   {
268     CPPUNIT_FAIL(e.what());
269   }
270   catch( ... )
271   {
272     CPPUNIT_FAIL("Unknown exception");
273   }
274
275   //BINARY: Test write() method
276   try
277   {
278     aVtkFieldDriver_int->write();
279   }
280   catch(MEDEXCEPTION &e)
281   {
282     CPPUNIT_FAIL(e.what());
283   }
284   catch( ... )
285   {
286     CPPUNIT_FAIL("Unknown exception");
287   }
288
289   //BINARY: Test read() method for Vtk Field Driver
290   CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
291
292   //BINARY: Test close() method
293   try
294   {
295     aVtkFieldDriver_int->close();
296   }
297   catch(MEDEXCEPTION &e)
298   {
299     CPPUNIT_FAIL(e.what());
300   }
301   catch( ... )
302   {
303     CPPUNIT_FAIL("Unknown exception");
304   }
305
306   //BINARY: Test openAppend() method
307   try
308   {
309     aVtkFieldDriver_int->close();
310   }
311   catch(MEDEXCEPTION &e)
312   {
313     CPPUNIT_FAIL(e.what());
314   }
315   catch( ... )
316   {
317     CPPUNIT_FAIL("Unknown exception");
318   }
319
320
321   /////////////////////////////////////////////////////////
322   //  BINARY: Test openAppend() and writeAppend() methods  //
323   /////////////////////////////////////////////////////////
324
325   try
326   {
327     aVtkFieldDriver_double->openAppend();
328   }
329   catch(MEDEXCEPTION &e)
330   {
331     CPPUNIT_FAIL(e.what());
332   }
333   catch( ... )
334   {
335     CPPUNIT_FAIL("Unknown exception");
336   }
337
338   aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
339
340   //BINARY: Test writeAppend() method
341   try
342   {
343     aVtkFieldDriver_double->writeAppend();
344   }
345   catch(MEDEXCEPTION &e)
346   {
347     CPPUNIT_FAIL(e.what());
348   }
349   catch( ... )
350   {
351     CPPUNIT_FAIL("Unknown exception");
352   }
353
354   try
355   {
356     aVtkFieldDriver_double->close();
357   }
358   catch(MEDEXCEPTION &e)
359   {
360     CPPUNIT_FAIL(e.what());
361   }
362   catch( ... )
363   {
364     CPPUNIT_FAIL("Unknown exception");
365   }
366
367   DRIVERFACTORY::setVtkBinaryFormatForWriting( false );
368
369   VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_1;
370
371   //Test copy constructor
372   VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_2 (*aVtkFieldDriver_int);
373
374   CPPUNIT_ASSERT_EQUAL(aVtkFieldDriver_intCpy_2, *aVtkFieldDriver_int);
375   //Test (bool operator ==) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
376   CPPUNIT_ASSERT(aVtkFieldDriver_intCpy_2.GENDRIVER::operator== (*aVtkFieldDriver_int));
377
378   //Test (friend ostream & operator <<) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
379   ostringstream ostr1, ostr2;
380   ostr1<<*aVtkFieldDriver_int;
381   ostr2<<aVtkFieldDriver_intCpy_2;
382
383   CPPUNIT_ASSERT(ostr1.str() != "");
384   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
385
386   //Delete all objects
387   aField->removeReference();
388   aField_1->removeReference();
389   delete aInvalidVtkFieldDriver_1;
390   delete aInvalidVtkFieldDriver_2;
391   delete aMedRdFieldDriver22_int;
392   delete aMedRdFieldDriver22_double;
393   delete aVtkFieldDriver_int;
394   delete aVtkFieldDriver_double;
395 }