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