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