Salome HOME
adding a new test for makeMesh method.
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_MedMeshDriver21.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 #include "MEDMEMTest.hxx"
23 #include <cppunit/TestAssert.h>
24
25 #include <MEDMEM_MedMeshDriver21.hxx>
26 #include <MEDMEM_Mesh.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
37 /*!
38  *  Check methods (13), defined in MEDMEM_MedMeshDriver21.hxx:
39  *  class MED_MESH_DRIVER21 : public virtual MED_MESH_DRIVER {
40  *   (+) MED_MESH_DRIVER21();
41  *   (+) MED_MESH_DRIVER21(const string & fileName, MESH * ptrMesh, MED_EN::med_mode_acces accessMode);
42  *   (+) MED_MESH_DRIVER21(const MED_MESH_DRIVER21 & driver);
43  *   (+) virtual ~MED_MESH_DRIVER21();
44  *   (+) void open();
45  *   (+) void close();
46  *  }
47  *  class MED_MESH_RDONLY_DRIVER21 : public virtual IMED_MESH_RDONLY_DRIVER,
48  *                                   public virtual MED_MESH_DRIVER21 {
49  *   (+) MED_MESH_RDONLY_DRIVER21();
50  *   (+) MED_MESH_RDONLY_DRIVER21(const string & fileName, MESH * ptrMesh);
51  *   (+) MED_MESH_RDONLY_DRIVER21(const MED_MESH_RDONLY_DRIVER21 & driver);
52  *   (+) virtual ~MED_MESH_RDONLY_DRIVER21();
53  *   (+) void read (void);
54  *  }
55  *  class MED_MESH_WRONLY_DRIVER21 : public virtual IMED_MESH_WRONLY_DRIVER,
56  *                                   public virtual MED_MESH_DRIVER21 {
57  *   (+) MED_MESH_WRONLY_DRIVER21();
58  *   (+) MED_MESH_WRONLY_DRIVER21(const string & fileName, MESH * ptrMesh);
59  *   (+) MED_MESH_WRONLY_DRIVER21(const MED_MESH_WRONLY_DRIVER21 & driver);
60  *   (+) virtual ~MED_MESH_WRONLY_DRIVER21();
61  *   (+) void write(void) const;
62  *  }
63  *  class MED_MESH_RDWR_DRIVER21 : public IMED_MESH_RDWR_DRIVER,
64  *                                 public MED_MESH_RDONLY_DRIVER21, public MED_MESH_WRONLY_DRIVER21 {
65  *   (+) MED_MESH_RDWR_DRIVER21();
66  *   (+) MED_MESH_RDWR_DRIVER21(const string & fileName, MESH * ptrMesh);
67  *   (+) MED_MESH_RDWR_DRIVER21(const MED_MESH_RDWR_DRIVER21 & driver);
68  *   (+) ~MED_MESH_RDWR_DRIVER21();
69  *   (+) void write(void) const;
70  *   (+) void read (void);
71  *  }
72  */
73 void MEDMEMTest::testMedMeshDriver21()
74 {
75   MESH *aMesh                      = new MESH();
76   MESH *aMesh_1                    = new MESH();
77
78   string data_dir                  = getenv("MED_ROOT_DIR");
79   string tmp_dir                   = getenv("TMP") ? getenv("TMP") : "/tmp";
80   if (tmp_dir == "") tmp_dir = "/tmp";
81
82   string filename_rd               = data_dir + "/share/salome/resources/med/pointe.med";
83   string filename_wr               = tmp_dir  + "/myWr_pointe21.med";
84   string tmpfile                   = tmp_dir  + "/tmp.med";
85   string meshname                  = "maa1";
86   string newmeshname               = "new" + meshname;
87   string fileNotExistsName_rd      = "notExists.med";
88   string fileNotExistsName_wr      = "/path_not_exists/file_not_exists.med";
89   string filename_rdwr             =  tmp_dir  + "/myRdWr_pointe22.med";
90   char* longmeshname               = new char[MED_TAILLE_NOM+2];
91   string fcopy                     = "cp " + filename_rd + " " + filename_rdwr;
92   for (int i = 0; i<MED_TAILLE_NOM+2; ++i)
93     longmeshname[i] = 'a';
94
95   //Copy file in the TMP dir for testing READ/WRITE case
96   system(fcopy.data());
97
98   // To remove tmp files from disk
99   MEDMEMTest_TmpFilesRemover aRemover;
100   aRemover.Register(filename_wr);
101   aRemover.Register(filename_rdwr);
102   aRemover.Register(tmpfile);
103
104   //----------------------------------Test READ ONLY part---------------------------------------------------//
105
106   //Creation a incorrect read only driver
107   MED_MESH_RDONLY_DRIVER21 *aInvalidRdDriver21 = new MED_MESH_RDONLY_DRIVER21(fileNotExistsName_rd, aMesh);
108
109   //Trying open not existing file
110   CPPUNIT_ASSERT_THROW(aInvalidRdDriver21->open(), MEDEXCEPTION);
111
112   //Creation a correct read only driver (normal constructor)
113   MED_MESH_RDONLY_DRIVER21 *aRdDriver21 = new MED_MESH_RDONLY_DRIVER21(filename_rd, aMesh);
114
115   //Check driver
116   CPPUNIT_ASSERT(aRdDriver21);
117
118   //Trying read mesh from file, if file is not open
119   CPPUNIT_ASSERT_THROW(aRdDriver21->read(), MEDEXCEPTION);
120
121   //Test open() method
122   try
123   {
124     aRdDriver21->open();
125   }
126   catch(MEDEXCEPTION &e)
127   {
128     CPPUNIT_FAIL(e.what());
129   }
130   catch( ... )
131   {
132     CPPUNIT_FAIL("Unknown exception");
133   }
134   //#ifdef ENABLE_FORCED_FAILURES
135   //Trying open file secondary.
136   //CPPUNIT_ASSERT_THROW(aRdDriver21->open(), MEDEXCEPTION);
137   CPPUNIT_ASSERT_NO_THROW(aRdDriver21->open());
138   //This case is not work, seems it BUG
139
140   //Trying read mesh from file, if mesh name is not set, i.e. empty
141   CPPUNIT_ASSERT_THROW(aRdDriver21->read(), MEDEXCEPTION);
142   //No correct exception in this case
143
144   //Trying read mesh from file with very long name
145   //aRdDriver21->setMeshName(longmeshname);
146   CPPUNIT_ASSERT_THROW(aRdDriver21->read(), MEDEXCEPTION);
147   //No correct exception in this case
148   //#endif
149
150   //Set correct  Mesh name
151   //Test setMeshName() and getMeshName() methods
152   try
153   {
154     aRdDriver21->setMeshName(meshname);
155   }
156   catch(MEDEXCEPTION &e)
157   {
158     CPPUNIT_FAIL(e.what());
159   }
160   catch( ... )
161   {
162     CPPUNIT_FAIL("Unknown exception");
163   }
164
165   CPPUNIT_ASSERT_EQUAL(meshname, aRdDriver21->getMeshName());
166
167
168   //Test read() method
169   try
170   {
171     aRdDriver21->read();
172   }
173   catch(MEDEXCEPTION &e)
174   {
175     CPPUNIT_FAIL(e.what());
176   }
177   catch( ... )
178   {
179     CPPUNIT_FAIL("Unknown exception");
180   }
181
182   //Test write() method for READ ONLY driver
183   CPPUNIT_ASSERT_THROW(aRdDriver21->write(), MEDEXCEPTION);
184
185   //Check Mesh
186   CPPUNIT_ASSERT(aMesh);
187
188   //Default constructor
189   MED_MESH_RDONLY_DRIVER21 aRdDriver21Cpy_1;
190
191   //Test (void operator =) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
192   //aRdDriver21Cpy_1 = *aRdDriver21;
193
194   //Test (bool operator ==) defined GENDRIVER class in MEDMEM_GenDriver.hxx
195   CPPUNIT_ASSERT(aRdDriver21Cpy_1.GENDRIVER::operator==(*aRdDriver21));
196
197   //Test copy constructor
198   MED_MESH_RDONLY_DRIVER21 aRdDriver21Cpy_2 (aRdDriver21Cpy_1);
199   CPPUNIT_ASSERT_EQUAL(aRdDriver21Cpy_2, *aRdDriver21);
200
201   //Test close() method
202   try
203   {
204     aRdDriver21->close();
205   }
206   catch(MEDEXCEPTION &e)
207   {
208     CPPUNIT_FAIL(e.what());
209   }
210   catch( ... )
211   {
212     CPPUNIT_FAIL("Unknown exception");
213   }
214
215   //Trying read mesh from copy closed driver
216   CPPUNIT_ASSERT_THROW(aRdDriver21Cpy_1.read(), MEDEXCEPTION);
217   CPPUNIT_ASSERT_THROW(aRdDriver21Cpy_2.read(), MEDEXCEPTION);
218
219   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
220   ostringstream rostr1, rostr2;
221   rostr1<<aRdDriver21Cpy_1;
222   rostr2<<aRdDriver21Cpy_2;
223   CPPUNIT_ASSERT(rostr1.str() != "");
224   CPPUNIT_ASSERT(rostr1.str() == rostr2.str());
225
226
227   //----------------------------------Test WRITE ONLY part------------------------------------------//
228
229   //Creation a incorrect write only driver
230   MED_MESH_WRONLY_DRIVER21 *aInvalidWrDriver21 = new MED_MESH_WRONLY_DRIVER21(fileNotExistsName_wr, aMesh);
231
232   //Trying open non existing file
233   CPPUNIT_ASSERT_THROW(aInvalidWrDriver21->open(), MEDEXCEPTION);
234
235   //Trying write empty mesh
236   MED_MESH_WRONLY_DRIVER21 *aTmpWrDriver21 = new MED_MESH_WRONLY_DRIVER21(tmpfile, aMesh_1);
237
238   //#ifdef ENABLE_FORCED_FAILURES
239   aTmpWrDriver21->open();
240   aTmpWrDriver21->setMeshName("EmptyMesh");
241   CPPUNIT_ASSERT_THROW(aTmpWrDriver21->write(),MEDEXCEPTION);
242   aTmpWrDriver21->close();
243   //Unknown exception in this case
244   //#endif
245
246   //Creation a correct write only driver
247   MED_MESH_WRONLY_DRIVER21 *aWrDriver21 = new MED_MESH_WRONLY_DRIVER21(filename_wr, aMesh);
248
249   //Check driver
250   CPPUNIT_ASSERT(aWrDriver21);
251
252   //Trying write mesh to file, if file is not open
253   CPPUNIT_ASSERT_THROW(aWrDriver21->write(), MEDEXCEPTION);
254
255   //Test open() method
256   try
257   {
258     aWrDriver21->open();
259   }
260   catch(MEDEXCEPTION &e)
261     {
262     CPPUNIT_FAIL(e.what());
263   }
264   catch( ... )
265   {
266     CPPUNIT_FAIL("Unknown exception");
267   }
268
269   //#ifdef ENABLE_FORCED_FAILURES
270   //Trying open file secondary.
271   //CPPUNIT_ASSERT_THROW(aWrDriver21->open(), MEDEXCEPTION);
272   CPPUNIT_ASSERT_NO_THROW(aWrDriver21->open());
273   //This case is not work, seems it BUG
274   //#endif
275
276   //#ifdef ENABLE_FORCED_FAILURES
277   //Test case: trying write mesh to file, if mesh name is not set, i.e empty
278   aMesh->setName("");
279   aWrDriver21->setMeshName("");
280   CPPUNIT_ASSERT_THROW(aWrDriver21->write(), MEDEXCEPTION);
281   //No exception in this case
282
283   //Trying write mesh to file with very long name
284   aWrDriver21->setMeshName(longmeshname);
285   CPPUNIT_ASSERT_THROW(aWrDriver21->write(), MEDEXCEPTION);
286   //No exception in this case
287   //#endif
288
289   //Set initialy mesh name
290   aMesh->setName(meshname);
291   //Test setMeshName() and getMeshName() methods
292   try
293   {
294     aWrDriver21->setMeshName(meshname);
295   }
296   catch(MEDEXCEPTION &e)
297   {
298     CPPUNIT_FAIL(e.what());
299   }
300   catch( ... )
301   {
302     CPPUNIT_FAIL("Unknown exception");
303   }
304
305   CPPUNIT_ASSERT_EQUAL(meshname, aWrDriver21->getMeshName());
306
307   //Test write() method
308   try
309   {
310     aWrDriver21->write();
311   }
312   catch(MEDEXCEPTION &e)
313   {
314     CPPUNIT_FAIL(e.what());
315   }
316   catch( ... )
317   {
318     CPPUNIT_FAIL("Unknown exception");
319   }
320
321   //Test read() method for WRITE ONLY driver
322   CPPUNIT_ASSERT_THROW(aWrDriver21->read(), MEDEXCEPTION);
323
324   //Default constructor
325    MED_MESH_WRONLY_DRIVER21 aWrDriver21Cpy_1;
326
327   //Test (void operator =) defined in GENDRIVER class
328   //aWrDriver21Cpy_1 = *aWrDriver21;
329
330   //Test (bool operator ==) defined GENDRIVER class
331   CPPUNIT_ASSERT(aWrDriver21Cpy_1.GENDRIVER::operator==(*aWrDriver21));
332
333   //Test copy constructor
334   MED_MESH_WRONLY_DRIVER21 aWrDriver21Cpy_2 (aWrDriver21Cpy_1);
335   CPPUNIT_ASSERT_EQUAL(aWrDriver21Cpy_2 , *aWrDriver21);
336
337   try
338   {
339     aWrDriver21->close();
340   }
341   catch(MEDEXCEPTION &e)
342   {
343     CPPUNIT_FAIL(e.what());
344   }
345   catch( ... )
346   {
347     CPPUNIT_FAIL("Unknown exception");
348   }
349
350   //Test case: Trying write mesh using copy closed driver
351   CPPUNIT_ASSERT_THROW(aWrDriver21Cpy_1.write(), MEDEXCEPTION);
352   CPPUNIT_ASSERT_THROW(aWrDriver21Cpy_2.write(), MEDEXCEPTION);
353
354   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
355   ostringstream wostr1, wostr2;
356   wostr1<<aWrDriver21Cpy_1;
357   wostr2<<aWrDriver21Cpy_2;
358   CPPUNIT_ASSERT(wostr1.str() != "");
359   CPPUNIT_ASSERT(wostr1.str() == wostr2.str());
360
361
362   //----------------------------------Test READ / WRITE part------------------------------------------//
363
364   //Creation a incorrect read/write driver
365   MED_MESH_RDWR_DRIVER21 *aInvalidRdWrDriver21 = new  MED_MESH_RDWR_DRIVER21(fileNotExistsName_wr, aMesh_1);
366
367   //Test case: trying open non existing file
368   CPPUNIT_ASSERT_THROW(aInvalidRdWrDriver21->open(), MEDEXCEPTION);
369
370   //Creation a correct read/write driver
371   MED_MESH_RDWR_DRIVER21 *aRdWrDriver21 = new MED_MESH_RDWR_DRIVER21(filename_rdwr, aMesh_1);
372
373   //Check driver
374   CPPUNIT_ASSERT(aRdWrDriver21);
375
376   //Test case: trying write mesh to file, if file is not open
377   CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(), MEDEXCEPTION);
378
379   //Test case: trying read mesh from file, if file is not open
380   CPPUNIT_ASSERT_THROW(aRdWrDriver21->read(), MEDEXCEPTION);
381
382   //Test open() method
383   try
384   {
385     aRdWrDriver21->open();
386   }
387   catch(MEDEXCEPTION &e)
388     {
389     CPPUNIT_FAIL(e.what());
390   }
391   catch( ... )
392   {
393     CPPUNIT_FAIL("Unknown exception");
394   }
395
396   //#ifdef ENABLE_FORCED_FAILURES
397   //Test case: trying open file secondary.
398   CPPUNIT_ASSERT_NO_THROW(aRdWrDriver21->open());
399   //This case is not work, seems it BUG
400   //#endif
401
402   //#ifdef ENABLE_FORCED_FAILURES
403   //Set mesh name
404   aMesh_1->setName("EmptyMesh");
405   aRdWrDriver21->setMeshName("EmptyMesh");
406
407   //Trying write in file empty mesh
408   CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(),MEDEXCEPTION);
409   //No exception in this case, seems it BUG
410   //#endif
411
412   //Set initialy mesh name
413   aMesh_1->setName(meshname);
414   //Test setMeshName() and getMeshName() methods
415   try
416   {
417     aRdWrDriver21->setMeshName(meshname);
418   }
419   catch(MEDEXCEPTION &e)
420   {
421     CPPUNIT_FAIL(e.what());
422   }
423   catch( ... )
424   {
425     CPPUNIT_FAIL("Unknown exception");
426   }
427
428   CPPUNIT_ASSERT_EQUAL(meshname, aRdWrDriver21->getMeshName());
429
430   // Test read() method
431   try
432   {
433     aRdWrDriver21->read();
434   }
435   catch(MEDEXCEPTION &e)
436     {
437     CPPUNIT_FAIL(e.what());
438   }
439   catch( ... )
440   {
441     CPPUNIT_FAIL("Unknown exception");
442   }
443
444   //Trying read mesh from file, if mesh name is not set, i.e. empty
445   //#ifdef ENABLE_FORCED_FAILURES
446   aRdWrDriver21->setMeshName("");
447   aMesh_1->setName("");
448   CPPUNIT_ASSERT_THROW(aRdWrDriver21->read(), MEDEXCEPTION);
449   //No correct exception in this case
450
451   //Trying write mesh to file, if mesh name is not set, i.e empty
452   CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(), MEDEXCEPTION);
453   //No correct exception in this case
454
455   //Trying read mesh from file with very long name
456   aRdWrDriver21->setMeshName(longmeshname);
457   aMesh_1->setName(longmeshname);
458   CPPUNIT_ASSERT_THROW(aRdWrDriver21->read(), MEDEXCEPTION);
459   //No correct exception in this case
460
461   //Trying write mesh to file with very long name
462   CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(), MEDEXCEPTION);
463   //No correct exception in this case
464   //#endif
465
466   //Test write() method
467   aRdWrDriver21->setMeshName(newmeshname);
468   aMesh_1->setName(newmeshname);
469   try
470   {
471     aRdWrDriver21->write();
472   }
473   catch(MEDEXCEPTION &e)
474   {
475     CPPUNIT_FAIL(e.what());
476   }
477   catch( ... )
478   {
479     CPPUNIT_FAIL("Unknown exception");
480   }
481
482   //Check Mesh
483   CPPUNIT_ASSERT(aMesh_1);
484
485   //Default constructor
486   MED_MESH_RDWR_DRIVER21 aRdWrDriver21Cpy_1;
487
488   //Test (void operator =) defined in GENDRIVER class
489   aRdWrDriver21Cpy_1 = *aRdWrDriver21;
490
491   //Test (bool operator ==) defined GENDRIVER class
492   CPPUNIT_ASSERT(aRdWrDriver21Cpy_1.GENDRIVER::operator==(*aRdWrDriver21));
493
494   //Test copy constructor
495   MED_MESH_RDWR_DRIVER21 aRdWrDriver21Cpy_2 (aRdWrDriver21Cpy_1);
496   CPPUNIT_ASSERT_EQUAL(aRdWrDriver21Cpy_2, *aRdWrDriver21);
497
498   try
499   {
500     aRdWrDriver21->close();
501   }
502   catch(MEDEXCEPTION &e)
503   {
504     CPPUNIT_FAIL(e.what());
505   }
506   catch( ... )
507   {
508     CPPUNIT_FAIL("Unknown exception");
509   }
510
511   //Test case: trying read/write mesh using copy closed driver
512   CPPUNIT_ASSERT_THROW(aRdWrDriver21Cpy_1.read(), MEDEXCEPTION);
513   CPPUNIT_ASSERT_THROW(aRdWrDriver21Cpy_2.read(), MEDEXCEPTION);
514   CPPUNIT_ASSERT_THROW(aRdWrDriver21Cpy_1.write(), MEDEXCEPTION);
515   CPPUNIT_ASSERT_THROW(aRdWrDriver21Cpy_2.write(), MEDEXCEPTION);
516
517   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
518   ostringstream rwostr1, rwostr2;
519   rwostr1<<aRdWrDriver21Cpy_1;
520   rwostr2<<aRdWrDriver21Cpy_2;
521   CPPUNIT_ASSERT(rwostr1.str() != "");
522   CPPUNIT_ASSERT(rwostr1.str() == rwostr2.str());
523
524   //Delete all objects
525   delete longmeshname;
526
527   delete aInvalidRdDriver21;
528   delete aRdDriver21;
529
530   delete aInvalidWrDriver21;
531   delete aWrDriver21;
532   delete aTmpWrDriver21;
533
534   delete aInvalidRdWrDriver21;
535   delete aRdWrDriver21;
536
537   delete aMesh;
538   delete aMesh_1;
539 }