Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_MedMeshDriver.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 #include <MEDMEM_MedMeshDriver.hxx>
23 #include <MEDMEM_Mesh.hxx>
24
25 // use this define to enable lines, execution of which leads to Segmentation Fault
26 //#define ENABLE_FAULTS
27
28 // use this define to enable CPPUNIT asserts and fails, showing bugs
29 //#define ENABLE_FORCED_FAILURES
30
31 using namespace std;
32 using namespace MEDMEM;
33
34 /*!
35  *  Check methods (13), defined in MEDMEM_MedMeshDriver.hxx:
36  *  class MED_MESH_DRIVER {
37  *   (+) MED_MESH_DRIVER();
38  *   (+) MED_MESH_DRIVER(const string & fileName, MESH * ptrMesh, MED_EN::med_mode_acces accessMode);
39  *   (+) MED_MESH_DRIVER(const MED_MESH_DRIVER22 & driver);
40  *   (+) virtual ~MED_MESH_DRIVER();
41  *   (+) void open();
42  *   (+) void close();
43  *  }
44  *  class MED_MESH_RDONLY_DRIVER : public virtual MED_MESH_DRIVER,
45  *  {
46  *   (+) MED_MESH_RDONLY_DRIVER();
47  *   (+) MED_MESH_RDONLY_DRIVER(const string & fileName, MESH * ptrMesh);
48  *   (+) MED_MESH_RDONLY_DRIVER(const MED_MESH_RDONLY_DRIVER & driver);
49  *   (+) virtual ~MED_MESH_RDONLY_DRIVER();
50  *   (+) void read (void);
51  *  }
52  *  class MED_MESH_WRONLY_DRIVER : public virtual MED_MESH_DRIVER
53  *  {
54  *   (+) MED_MESH_WRONLY_DRIVER();
55  *   (+) MED_MESH_WRONLY_DRIVER(const string & fileName, MESH * ptrMesh);
56  *   (+) MED_MESH_WRONLY_DRIVER(const MED_MESH_WRONLY_DRIVER & driver);
57  *   (+) virtual ~MED_MESH_WRONLY_DRIVER();
58  *   (+) void write(void) const;
59  *  }
60  *  class MED_MESH_RDWR_DRIVER : public MED_MESH_RDONLY_DRIVER, public MED_MESH_WRONLY_DRIVER
61  *  {
62  *   (+) MED_MESH_RDWR_DRIVER();
63  *   (+) MED_MESH_RDWR_DRIVER(const string & fileName, MESH * ptrMesh);
64  *   (+) MED_MESH_RDWR_DRIVER(const MED_MESH_RDWR_DRIVER & driver);
65  *   (+) ~MED_MESH_RDWR_DRIVER();
66  *   (+) void write(void) const;
67  *   (+) void read (void);
68  *  }
69  */
70 void MEDMEMTest::testMedMeshDriver()
71 {
72   MESH *aMesh                      = new MESH;
73   MESH *aMesh_1                    = new MESH;
74
75   string filename_rd               = getResourceFile("pointe.med");
76   string filename_wr               = makeTmpFile("myWr_pointe22.med");
77   string tmpfile                   = makeTmpFile("tmp.med");
78   string meshname                  = "maa1";
79   string newmeshname               = "new" + meshname;
80   string fileNotExistsName_rd      = "notExists.med";
81   string fileNotExistsName_wr      = "/path_not_exists/file_not_exists.med";
82   string filename_rdwr             = makeTmpFile("myRdWr_pointe22.med", filename_rd);
83   char* longmeshname               = new char[MED_NAME_SIZE+2];
84   for (int i = 0; i<MED_NAME_SIZE+2; ++i)
85     longmeshname[i] = 'a';
86   longmeshname[MED_NAME_SIZE+1] = '\0';
87
88   // To remove tmp files from disk
89   MEDMEMTest_TmpFilesRemover aRemover;
90   aRemover.Register(filename_wr);
91   aRemover.Register(filename_rdwr);
92   aRemover.Register(tmpfile);
93
94   //----------------------------------Test READ ONLY part---------------------------------------------------//
95
96   //Creation a incorrect read only driver
97   MED_MESH_RDONLY_DRIVER *aInvalidRdDriver22 = new MED_MESH_RDONLY_DRIVER(fileNotExistsName_rd, aMesh);
98
99   //Trying open not existing file
100   CPPUNIT_ASSERT_THROW(aInvalidRdDriver22->open(), MEDEXCEPTION);
101
102   //Creation a correct read only driver (normal constructor)
103   MED_MESH_RDONLY_DRIVER *aRdDriver22 = new MED_MESH_RDONLY_DRIVER(filename_rd, aMesh);
104
105   //Check driver
106   CPPUNIT_ASSERT(aRdDriver22);
107
108   //Trying read mesh from file, if file is not open
109   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
110
111   //Test open() method
112   try
113   {
114     aRdDriver22->open();
115   }
116   catch(MEDEXCEPTION &e)
117   {
118     CPPUNIT_FAIL(e.what());
119   }
120   catch( ... )
121   {
122     CPPUNIT_FAIL("Unknown exception");
123   }
124
125   //#ifdef  ENABLE_FORCED_FAILURES
126   //Trying open file secondary.
127   //CPPUNIT_ASSERT_THROW(aRdDriver22->open(), MEDEXCEPTION);
128   CPPUNIT_ASSERT_NO_THROW(aRdDriver22->open());
129   //This case is not work, seems it BUG
130   //#endif
131
132   //Trying read mesh from file, if mesh name is not set, i.e. empty
133   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
134
135   //Trying read mesh from file with very long name
136   aRdDriver22->setMeshName(longmeshname);
137   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
138
139   //Set correct  Mesh name
140   //Test setMeshName() and getMeshName() methods
141   try
142   {
143     aRdDriver22->setMeshName(meshname);
144   }
145   catch(MEDEXCEPTION &e)
146   {
147     CPPUNIT_FAIL(e.what());
148   }
149   catch( ... )
150   {
151     CPPUNIT_FAIL("Unknown exception");
152   }
153
154   CPPUNIT_ASSERT_EQUAL(meshname, aRdDriver22->getMeshName());
155
156
157   //Test read() method
158   try
159   {
160     aRdDriver22->read();
161   }
162   catch(MEDEXCEPTION &e)
163   {
164     CPPUNIT_FAIL(e.what());
165   }
166   catch( ... )
167   {
168     CPPUNIT_FAIL("Unknown exception");
169   }
170
171   //Test write() method for READ ONLY driver
172   CPPUNIT_ASSERT_THROW(aRdDriver22->write(), MEDEXCEPTION);
173
174   //Check Mesh
175   CPPUNIT_ASSERT(aMesh);
176
177   //Default constructor
178   MED_MESH_RDONLY_DRIVER aRdDriver22Cpy_1;
179
180   //Test (void operator =) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
181   //aRdDriver22Cpy_1 = *aRdDriver22;
182
183   //Test (bool operator ==) defined GENDRIVER class in MEDMEM_GenDriver.hxx
184   CPPUNIT_ASSERT(aRdDriver22Cpy_1.GENDRIVER::operator==(*aRdDriver22));
185
186   //Test copy constructor
187   MED_MESH_RDONLY_DRIVER aRdDriver22Cpy_2 (aRdDriver22Cpy_1);
188   CPPUNIT_ASSERT_EQUAL(aRdDriver22Cpy_2, *aRdDriver22);
189
190   //Test close() method
191   try
192   {
193     aRdDriver22->close();
194   }
195   catch(MEDEXCEPTION &e)
196   {
197     CPPUNIT_FAIL(e.what());
198   }
199   catch( ... )
200   {
201     CPPUNIT_FAIL("Unknown exception");
202   }
203
204   //Trying read mesh from copy closed driver
205   CPPUNIT_ASSERT_THROW(aRdDriver22Cpy_1.read(), MEDEXCEPTION);
206   CPPUNIT_ASSERT_THROW(aRdDriver22Cpy_2.read(), MEDEXCEPTION);
207
208   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
209   ostringstream rostr1, rostr2;
210   rostr1<<aRdDriver22Cpy_1;
211   rostr2<<aRdDriver22Cpy_2;
212   CPPUNIT_ASSERT(rostr1.str() != "");
213   CPPUNIT_ASSERT(rostr1.str() == rostr2.str());
214
215
216   //----------------------------------Test WRITE ONLY part------------------------------------------//
217
218   //Creation a incorrect write only driver
219   MED_MESH_WRONLY_DRIVER *aInvalidWrDriver22 = new MED_MESH_WRONLY_DRIVER(fileNotExistsName_wr, aMesh);
220
221   //Trying open non existing file
222   CPPUNIT_ASSERT_THROW(aInvalidWrDriver22->open(), MEDEXCEPTION);
223
224   //Trying write empty mesh
225   MED_MESH_WRONLY_DRIVER *aTmpWrDriver22 = new MED_MESH_WRONLY_DRIVER(tmpfile, aMesh_1);
226
227   //#ifdef ENABLE_FORCED_FAILURES
228   aTmpWrDriver22->open();
229   aTmpWrDriver22->setMeshName("EmptyMesh");
230   CPPUNIT_ASSERT_THROW(aTmpWrDriver22->write(),MEDEXCEPTION);
231   aTmpWrDriver22->close();
232   //Unknown exception in this case
233   //#endif
234
235   //Creation a correct write only driver
236   MED_MESH_WRONLY_DRIVER *aWrDriver22 = new MED_MESH_WRONLY_DRIVER(filename_wr, aMesh);
237
238   //Check driver
239   CPPUNIT_ASSERT(aWrDriver22);
240
241   //Trying write mesh to file, if file is not open
242   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
243
244   //Test open() method
245   try
246   {
247     aWrDriver22->open();
248   }
249   catch(MEDEXCEPTION &e)
250     {
251     CPPUNIT_FAIL(e.what());
252   }
253   catch( ... )
254   {
255     CPPUNIT_FAIL("Unknown exception");
256   }
257
258   //#ifdef ENABLE_FORCED_FAILURES
259   //Trying open file secondary.
260   //CPPUNIT_ASSERT_THROW(aWrDriver22->open(), MEDEXCEPTION);
261   //This case is not work, seems it BUG
262   //#endif
263
264   //Test case: trying write mesh to file, if mesh name is not set, i.e empty
265   aMesh->setName("");
266   aWrDriver22->setMeshName("");
267   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
268
269   //Trying write mesh to file with very long name
270   aWrDriver22->setMeshName(longmeshname);
271   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
272
273   //Set initialy mesh name
274   aMesh->setName(meshname);
275   //Test setMeshName() and getMeshName() methods
276   try
277   {
278     aWrDriver22->setMeshName(meshname);
279   }
280   catch(MEDEXCEPTION &e)
281   {
282     CPPUNIT_FAIL(e.what());
283   }
284   catch( ... )
285   {
286     CPPUNIT_FAIL("Unknown exception");
287   }
288
289   CPPUNIT_ASSERT_EQUAL(meshname, aWrDriver22->getMeshName());
290
291   //  aWrDriver22->open();
292   //Test write() method
293   try
294   {
295     aWrDriver22->write();
296   }
297   catch(MEDEXCEPTION &e)
298   {
299     CPPUNIT_FAIL(e.what());
300   }
301   catch( ... )
302   {
303     CPPUNIT_FAIL("Unknown exception");
304   }
305
306   //Test read() method for WRITE ONLY driver
307   CPPUNIT_ASSERT_THROW(aWrDriver22->read(), MEDEXCEPTION);
308
309   //Default constructor
310    MED_MESH_WRONLY_DRIVER aWrDriver22Cpy_1;
311
312   //Test (void operator =) defined in GENDRIVER class
313   //aWrDriver22Cpy_1 = *aWrDriver22;
314
315   //Test (bool operator ==) defined GENDRIVER class
316   CPPUNIT_ASSERT(aWrDriver22Cpy_1.GENDRIVER::operator==(*aWrDriver22));
317
318   //Test copy constructor
319   MED_MESH_WRONLY_DRIVER aWrDriver22Cpy_2 (aWrDriver22Cpy_1);
320   CPPUNIT_ASSERT_EQUAL(aWrDriver22Cpy_2 , *aWrDriver22);
321
322   try
323   {
324     aWrDriver22->close();
325   }
326   catch(MEDEXCEPTION &e)
327   {
328     CPPUNIT_FAIL(e.what());
329   }
330   catch( ... )
331   {
332     CPPUNIT_FAIL("Unknown exception");
333   }
334
335   //Test case: Trying write mesh using copy closed driver
336   CPPUNIT_ASSERT_THROW(aWrDriver22Cpy_1.write(), MEDEXCEPTION);
337   CPPUNIT_ASSERT_THROW(aWrDriver22Cpy_2.write(), MEDEXCEPTION);
338
339   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
340   ostringstream wostr1, wostr2;
341   wostr1<<aWrDriver22Cpy_1;
342   wostr2<<aWrDriver22Cpy_2;
343   CPPUNIT_ASSERT(wostr1.str() != "");
344   CPPUNIT_ASSERT(wostr1.str() == wostr2.str());
345
346
347   //----------------------------------Test READ / WRITE part------------------------------------------//
348
349   //Creation a incorrect read/write driver
350   MED_MESH_RDWR_DRIVER *aInvalidRdWrDriver22 = new  MED_MESH_RDWR_DRIVER(fileNotExistsName_wr, aMesh_1);
351
352   //Test case: trying open non existing file
353   CPPUNIT_ASSERT_THROW(aInvalidRdWrDriver22->open(), MEDEXCEPTION);
354
355   //Creation a correct read/write driver
356   MED_MESH_RDWR_DRIVER *aRdWrDriver22 = new MED_MESH_RDWR_DRIVER(filename_rdwr, aMesh_1);
357
358   //Check driver
359   CPPUNIT_ASSERT(aRdWrDriver22);
360
361   //Test case: trying write mesh to file, if file is not open
362   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
363
364   //Test case: trying read mesh from file, if file is not open
365   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
366
367   //Test open() method
368   try
369   {
370     aRdWrDriver22->open();
371   }
372   catch(MEDEXCEPTION &e)
373     {
374     CPPUNIT_FAIL(e.what());
375   }
376   catch( ... )
377   {
378     CPPUNIT_FAIL("Unknown exception");
379   }
380
381   //#ifdef ENABLE_FORCED_FAILURES
382   //Test case: trying open file secondary.
383   //CPPUNIT_ASSERT_THROW(aRdWrDriver22->open(), MEDEXCEPTION);
384   CPPUNIT_ASSERT_NO_THROW(aRdWrDriver22->open());
385   //This case is not work, seems it BUG
386   //#endif
387
388   //#ifdef ENABLE_FORCED_FAILURES
389   //Set mesh name
390   aMesh_1->setName("EmptyMesh");
391   aRdWrDriver22->setMeshName("EmptyMesh");
392   //Trying write in file empty mesh
393   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(),MEDEXCEPTION);
394   //No exception in this case, seems it BUG
395   //#endif
396
397   //Set initialy mesh name
398   aMesh_1->setName(meshname);
399   //Test setMeshName() and getMeshName() methods
400   try
401   {
402     aRdWrDriver22->setMeshName(meshname);
403   }
404   catch(MEDEXCEPTION &e)
405   {
406     CPPUNIT_FAIL(e.what());
407   }
408   catch( ... )
409   {
410     CPPUNIT_FAIL("Unknown exception");
411   }
412
413   CPPUNIT_ASSERT_EQUAL(meshname, aRdWrDriver22->getMeshName());
414
415   //Test read() method
416   try
417   {
418     aRdWrDriver22->read();
419   }
420   catch(MEDEXCEPTION &e)
421   {
422     CPPUNIT_FAIL(e.what());
423   }
424   catch( ... )
425   {
426     CPPUNIT_FAIL("Unknown exception");
427   }
428
429   //Trying read mesh from file, if mesh name is not set, i.e. empty
430   aRdWrDriver22->setMeshName("");
431   aMesh_1->setName("");
432   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
433
434   //Trying write mesh to file, if mesh name is not set, i.e empty
435   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
436
437   //Trying read mesh from file with very long name
438   aRdWrDriver22->setMeshName(longmeshname);
439   aMesh_1->setName(longmeshname);
440   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
441
442   //Trying write mesh to file with very long name
443   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
444
445   //Test write() method
446   aRdWrDriver22->setMeshName(newmeshname);
447   aMesh_1->setName(newmeshname);
448   try
449   {
450     aRdWrDriver22->write();
451   }
452   catch(MEDEXCEPTION &e)
453   {
454     CPPUNIT_FAIL(e.what());
455   }
456   catch( ... )
457   {
458     CPPUNIT_FAIL("Unknown exception");
459   }
460
461   //Check Mesh
462   CPPUNIT_ASSERT(aMesh_1);
463
464   //Default constructor
465   MED_MESH_RDWR_DRIVER aRdWrDriver22Cpy_1;
466
467   //Test (void operator =) defined in GENDRIVER class
468   aRdWrDriver22Cpy_1= *aRdWrDriver22;
469
470   //Test (bool operator ==) defined GENDRIVER class
471   CPPUNIT_ASSERT(aRdWrDriver22Cpy_1.GENDRIVER::operator==(*aRdWrDriver22));
472
473   //Test copy constructor
474   MED_MESH_RDWR_DRIVER aRdWrDriver22Cpy_2 (aRdWrDriver22Cpy_1);
475   // CPPUNIT_ASSERT(aRdWrDriver22Cpy_2.GENDRIVER::operator==( *aRdWrDriver22));
476
477   try
478   {
479     aRdWrDriver22->close();
480   }
481   catch(MEDEXCEPTION &e)
482   {
483     CPPUNIT_FAIL(e.what());
484   }
485   catch( ... )
486   {
487     CPPUNIT_FAIL("Unknown exception");
488   }
489
490   //Test case: trying read/write mesh using copy closed driver
491   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_1.read(), MEDEXCEPTION);
492   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_2.read(), MEDEXCEPTION);
493   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_1.write(), MEDEXCEPTION);
494   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_2.write(), MEDEXCEPTION);
495
496   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
497   ostringstream rwostr1, rwostr2;
498   rwostr1<<aRdWrDriver22Cpy_1;
499   rwostr2<<aRdWrDriver22Cpy_2;
500   cout << aRdWrDriver22Cpy_1;
501   cout << aRdWrDriver22Cpy_2;
502   CPPUNIT_ASSERT(rwostr1.str() != "");
503   CPPUNIT_ASSERT(rwostr1.str() == rwostr2.str());
504
505   //Delete all objects
506   delete []longmeshname;
507
508   delete aInvalidRdDriver22;
509   delete aRdDriver22;
510
511   delete aInvalidWrDriver22;
512   delete aWrDriver22;
513
514   delete aInvalidRdWrDriver22;
515   delete aRdWrDriver22;
516   delete aTmpWrDriver22;
517
518   aMesh->removeReference();
519   aMesh_1->removeReference();
520 }