1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 #include "MEDMEMTest.hxx"
23 #include <cppunit/TestAssert.h>
25 #include <MEDMEM_MedMeshDriver21.hxx>
26 #include <MEDMEM_Mesh.hxx>
28 // use this define to enable lines, execution of which leads to Segmentation Fault
29 //#define ENABLE_FAULTS
31 // use this define to enable CPPUNIT asserts and fails, showing bugs
32 //#define ENABLE_FORCED_FAILURES
35 using namespace MEDMEM;
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();
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);
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;
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);
73 void MEDMEMTest::testMedMeshDriver21()
75 MESH *aMesh = new MESH();
76 MESH *aMesh_1 = new MESH();
78 string data_dir = getenv("MED_ROOT_DIR");
79 string tmp_dir = getenv("TMP") ? getenv("TMP") : "/tmp";
80 if (tmp_dir == "") tmp_dir = "/tmp";
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';
95 //Copy file in the TMP dir for testing READ/WRITE case
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);
104 //----------------------------------Test READ ONLY part---------------------------------------------------//
106 //Creation a incorrect read only driver
107 MED_MESH_RDONLY_DRIVER21 *aInvalidRdDriver21 = new MED_MESH_RDONLY_DRIVER21(fileNotExistsName_rd, aMesh);
109 //Trying open not existing file
110 CPPUNIT_ASSERT_THROW(aInvalidRdDriver21->open(), MEDEXCEPTION);
112 //Creation a correct read only driver (normal constructor)
113 MED_MESH_RDONLY_DRIVER21 *aRdDriver21 = new MED_MESH_RDONLY_DRIVER21(filename_rd, aMesh);
116 CPPUNIT_ASSERT(aRdDriver21);
118 //Trying read mesh from file, if file is not open
119 CPPUNIT_ASSERT_THROW(aRdDriver21->read(), MEDEXCEPTION);
126 catch(MEDEXCEPTION &e)
128 CPPUNIT_FAIL(e.what());
132 CPPUNIT_FAIL("Unknown exception");
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
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
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
150 //Set correct Mesh name
151 //Test setMeshName() and getMeshName() methods
154 aRdDriver21->setMeshName(meshname);
156 catch(MEDEXCEPTION &e)
158 CPPUNIT_FAIL(e.what());
162 CPPUNIT_FAIL("Unknown exception");
165 CPPUNIT_ASSERT_EQUAL(meshname, aRdDriver21->getMeshName());
173 catch(MEDEXCEPTION &e)
175 CPPUNIT_FAIL(e.what());
179 CPPUNIT_FAIL("Unknown exception");
182 //Test write() method for READ ONLY driver
183 CPPUNIT_ASSERT_THROW(aRdDriver21->write(), MEDEXCEPTION);
186 CPPUNIT_ASSERT(aMesh);
188 //Default constructor
189 MED_MESH_RDONLY_DRIVER21 aRdDriver21Cpy_1;
191 //Test (void operator =) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
192 //aRdDriver21Cpy_1 = *aRdDriver21;
194 //Test (bool operator ==) defined GENDRIVER class in MEDMEM_GenDriver.hxx
195 CPPUNIT_ASSERT(aRdDriver21Cpy_1.GENDRIVER::operator==(*aRdDriver21));
197 //Test copy constructor
198 MED_MESH_RDONLY_DRIVER21 aRdDriver21Cpy_2 (aRdDriver21Cpy_1);
199 CPPUNIT_ASSERT_EQUAL(aRdDriver21Cpy_2, *aRdDriver21);
201 //Test close() method
204 aRdDriver21->close();
206 catch(MEDEXCEPTION &e)
208 CPPUNIT_FAIL(e.what());
212 CPPUNIT_FAIL("Unknown exception");
215 //Trying read mesh from copy closed driver
216 CPPUNIT_ASSERT_THROW(aRdDriver21Cpy_1.read(), MEDEXCEPTION);
217 CPPUNIT_ASSERT_THROW(aRdDriver21Cpy_2.read(), MEDEXCEPTION);
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());
227 //----------------------------------Test WRITE ONLY part------------------------------------------//
229 //Creation a incorrect write only driver
230 MED_MESH_WRONLY_DRIVER21 *aInvalidWrDriver21 = new MED_MESH_WRONLY_DRIVER21(fileNotExistsName_wr, aMesh);
232 //Trying open non existing file
233 CPPUNIT_ASSERT_THROW(aInvalidWrDriver21->open(), MEDEXCEPTION);
235 //Trying write empty mesh
236 MED_MESH_WRONLY_DRIVER21 *aTmpWrDriver21 = new MED_MESH_WRONLY_DRIVER21(tmpfile, aMesh_1);
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
246 //Creation a correct write only driver
247 MED_MESH_WRONLY_DRIVER21 *aWrDriver21 = new MED_MESH_WRONLY_DRIVER21(filename_wr, aMesh);
250 CPPUNIT_ASSERT(aWrDriver21);
252 //Trying write mesh to file, if file is not open
253 CPPUNIT_ASSERT_THROW(aWrDriver21->write(), MEDEXCEPTION);
260 catch(MEDEXCEPTION &e)
262 CPPUNIT_FAIL(e.what());
266 CPPUNIT_FAIL("Unknown exception");
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
276 //#ifdef ENABLE_FORCED_FAILURES
277 //Test case: trying write mesh to file, if mesh name is not set, i.e empty
279 aWrDriver21->setMeshName("");
280 CPPUNIT_ASSERT_THROW(aWrDriver21->write(), MEDEXCEPTION);
281 //No exception in this case
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
289 //Set initialy mesh name
290 aMesh->setName(meshname);
291 //Test setMeshName() and getMeshName() methods
294 aWrDriver21->setMeshName(meshname);
296 catch(MEDEXCEPTION &e)
298 CPPUNIT_FAIL(e.what());
302 CPPUNIT_FAIL("Unknown exception");
305 CPPUNIT_ASSERT_EQUAL(meshname, aWrDriver21->getMeshName());
307 //Test write() method
310 aWrDriver21->write();
312 catch(MEDEXCEPTION &e)
314 CPPUNIT_FAIL(e.what());
318 CPPUNIT_FAIL("Unknown exception");
321 //Test read() method for WRITE ONLY driver
322 CPPUNIT_ASSERT_THROW(aWrDriver21->read(), MEDEXCEPTION);
324 //Default constructor
325 MED_MESH_WRONLY_DRIVER21 aWrDriver21Cpy_1;
327 //Test (void operator =) defined in GENDRIVER class
328 //aWrDriver21Cpy_1 = *aWrDriver21;
330 //Test (bool operator ==) defined GENDRIVER class
331 CPPUNIT_ASSERT(aWrDriver21Cpy_1.GENDRIVER::operator==(*aWrDriver21));
333 //Test copy constructor
334 MED_MESH_WRONLY_DRIVER21 aWrDriver21Cpy_2 (aWrDriver21Cpy_1);
335 CPPUNIT_ASSERT_EQUAL(aWrDriver21Cpy_2 , *aWrDriver21);
339 aWrDriver21->close();
341 catch(MEDEXCEPTION &e)
343 CPPUNIT_FAIL(e.what());
347 CPPUNIT_FAIL("Unknown exception");
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);
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());
362 //----------------------------------Test READ / WRITE part------------------------------------------//
364 //Creation a incorrect read/write driver
365 MED_MESH_RDWR_DRIVER21 *aInvalidRdWrDriver21 = new MED_MESH_RDWR_DRIVER21(fileNotExistsName_wr, aMesh_1);
367 //Test case: trying open non existing file
368 CPPUNIT_ASSERT_THROW(aInvalidRdWrDriver21->open(), MEDEXCEPTION);
370 //Creation a correct read/write driver
371 MED_MESH_RDWR_DRIVER21 *aRdWrDriver21 = new MED_MESH_RDWR_DRIVER21(filename_rdwr, aMesh_1);
374 CPPUNIT_ASSERT(aRdWrDriver21);
376 //Test case: trying write mesh to file, if file is not open
377 CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(), MEDEXCEPTION);
379 //Test case: trying read mesh from file, if file is not open
380 CPPUNIT_ASSERT_THROW(aRdWrDriver21->read(), MEDEXCEPTION);
385 aRdWrDriver21->open();
387 catch(MEDEXCEPTION &e)
389 CPPUNIT_FAIL(e.what());
393 CPPUNIT_FAIL("Unknown exception");
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
402 //#ifdef ENABLE_FORCED_FAILURES
404 aMesh_1->setName("EmptyMesh");
405 aRdWrDriver21->setMeshName("EmptyMesh");
407 //Trying write in file empty mesh
408 CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(),MEDEXCEPTION);
409 //No exception in this case, seems it BUG
412 //Set initialy mesh name
413 aMesh_1->setName(meshname);
414 //Test setMeshName() and getMeshName() methods
417 aRdWrDriver21->setMeshName(meshname);
419 catch(MEDEXCEPTION &e)
421 CPPUNIT_FAIL(e.what());
425 CPPUNIT_FAIL("Unknown exception");
428 CPPUNIT_ASSERT_EQUAL(meshname, aRdWrDriver21->getMeshName());
430 // Test read() method
433 aRdWrDriver21->read();
435 catch(MEDEXCEPTION &e)
437 CPPUNIT_FAIL(e.what());
441 CPPUNIT_FAIL("Unknown exception");
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
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
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
461 //Trying write mesh to file with very long name
462 CPPUNIT_ASSERT_THROW(aRdWrDriver21->write(), MEDEXCEPTION);
463 //No correct exception in this case
466 //Test write() method
467 aRdWrDriver21->setMeshName(newmeshname);
468 aMesh_1->setName(newmeshname);
471 aRdWrDriver21->write();
473 catch(MEDEXCEPTION &e)
475 CPPUNIT_FAIL(e.what());
479 CPPUNIT_FAIL("Unknown exception");
483 CPPUNIT_ASSERT(aMesh_1);
485 //Default constructor
486 MED_MESH_RDWR_DRIVER21 aRdWrDriver21Cpy_1;
488 //Test (void operator =) defined in GENDRIVER class
489 aRdWrDriver21Cpy_1 = *aRdWrDriver21;
491 //Test (bool operator ==) defined GENDRIVER class
492 CPPUNIT_ASSERT(aRdWrDriver21Cpy_1.GENDRIVER::operator==(*aRdWrDriver21));
494 //Test copy constructor
495 MED_MESH_RDWR_DRIVER21 aRdWrDriver21Cpy_2 (aRdWrDriver21Cpy_1);
496 CPPUNIT_ASSERT_EQUAL(aRdWrDriver21Cpy_2, *aRdWrDriver21);
500 aRdWrDriver21->close();
502 catch(MEDEXCEPTION &e)
504 CPPUNIT_FAIL(e.what());
508 CPPUNIT_FAIL("Unknown exception");
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);
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());
527 delete aInvalidRdDriver21;
530 delete aInvalidWrDriver21;
532 delete aTmpWrDriver21;
534 delete aInvalidRdWrDriver21;
535 delete aRdWrDriver21;