Back to index

salome-med  6.5.0
MEDMEMTest_GibiMeshDriver.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDMEMTest.hxx"
00021 #include <cppunit/TestAssert.h>
00022 
00023 #include <MEDMEM_GibiMeshDriver.hxx>
00024 #include <MEDMEM_Mesh.hxx>
00025 
00026 // use this define to enable lines, execution of which leads to Segmentation Fault
00027 //#define ENABLE_FAULTS
00028 
00029 // use this define to enable CPPUNIT asserts and fails, showing bugs
00030 //#define ENABLE_FORCED_FAILURES
00031 
00032 using namespace std;
00033 using namespace MEDMEM;
00034 using namespace MED_EN;
00035 
00096 void MEDMEMTest::testGibiMeshDriver()
00097 {
00098   MESH *aMesh                      = new MESH;
00099   MESH *aMesh_NULL                 = NULL;
00100   MESH *aMesh_2                    = new MESH;
00101   vector<FIELD_*>       rdFields;
00102   vector<const FIELD_*> wrFields;
00103 
00104   string filename_rd               = getResourceFile("Darcy3_3D_H_10x10x10.sauve");
00105   string filenamemed_rd            = getResourceFile("test_2D.sauve");
00106   string filename_wr               = makeTmpFile("myWr_Darcy3_3D_H_10x10x10.sauve");
00107   string tmpfile                   = makeTmpFile("tmp.sauve");
00108   string tmpfile_rdwr              = makeTmpFile("rdwr_tmp.sauve");
00109   string filenamemed_wr            = makeTmpFile("myWrMed_elle_3D_HPr_10x10x10.sauve");
00110   string tmpfilemed                = makeTmpFile("tmpmed.sauve");
00111   string meshname                  = "Darcy3_3D_H_10x10x10";
00112   string newmeshname               = "new" + meshname;
00113   string fileNotExistsName_rd      = "notExists.sauve";
00114   string fileNotExistsName_wr      = "/path_not_exists/file_not_exists.sauve";
00115   string filename_rdwr             = makeTmpFile("myRdWr_Darcy3_3D_H_10x10x10.sauve", filename_rd);
00116 
00117   // To remove tmp files from disk
00118   MEDMEMTest_TmpFilesRemover aRemover;
00119   aRemover.Register(filename_wr);
00120   aRemover.Register(tmpfile);
00121   aRemover.Register(tmpfilemed);
00122   aRemover.Register(filenamemed_wr);
00123   aRemover.Register(tmpfile_rdwr);
00124   aRemover.Register(filename_rdwr);
00125 
00126   //Test gibi2medGeom() and med2gibiGeom() methods
00127   size_t aSize = 17;
00128   CPPUNIT_ASSERT_EQUAL(GIBI_MESH_DRIVER::gibi2medGeom(aSize), MED_PENTA15);
00129   CPPUNIT_ASSERT_EQUAL(GIBI_MESH_DRIVER::med2gibiGeom(MED_PENTA15), 17);
00130 
00131   //---------------------------Test GIBI MESH READ ONLY part--------------------------------//
00132   {
00133     {
00134       //Creation of an incorrect read only driver
00135       GIBI_MESH_RDONLY_DRIVER *aInvalidGibiRdDriver =
00136         new GIBI_MESH_RDONLY_DRIVER(fileNotExistsName_rd, aMesh);
00137       /************************************************************************/
00138       // WARNING: if you have memory access error just after this constructor,
00139       // this means that MEDMEMCppTest has been compiled w/o -DHAS_XDR
00140       // while MEDMEM, with -DHAS_XDR
00141       /************************************************************************/
00142 
00143       //Trying open not existing file
00144       CPPUNIT_ASSERT_THROW(aInvalidGibiRdDriver->open(), MEDEXCEPTION);
00145 
00146       delete aInvalidGibiRdDriver;
00147     }
00148 
00149     //Creation a correct Gibi read only driver (normal constructor)
00150     GIBI_MESH_RDONLY_DRIVER *aGibiRdDriver =
00151       new GIBI_MESH_RDONLY_DRIVER(filename_rd, aMesh);
00152 
00153     //Check driver
00154     CPPUNIT_ASSERT(aGibiRdDriver);
00155 
00156     //Trying read mesh from file, if file is not open
00157     CPPUNIT_ASSERT_THROW(aGibiRdDriver->read(), MEDEXCEPTION);
00158 
00159     //Test open() method
00160     CPPUNIT_ASSERT_NO_THROW(aGibiRdDriver->open());
00161 
00162     //Trying open file secondary.
00163     CPPUNIT_ASSERT_NO_THROW(aGibiRdDriver->open());
00164 
00165     //Test setMeshName() and getMeshName() methods
00166     CPPUNIT_ASSERT_NO_THROW(aGibiRdDriver->setMeshName(meshname));
00167     CPPUNIT_ASSERT_EQUAL(meshname, aGibiRdDriver->getMeshName());
00168 
00169     //Test read() method
00170     CPPUNIT_ASSERT_NO_THROW(aGibiRdDriver->read());
00171 
00172     //Trying fill not empty mesh
00173     CPPUNIT_ASSERT_THROW(aGibiRdDriver->read(), MEDEXCEPTION);
00174 
00175     //Test write() method for GIBI READ ONLY driver
00176     CPPUNIT_ASSERT_THROW(aGibiRdDriver->write(), MEDEXCEPTION);
00177 
00178     //Check Mesh
00179     CPPUNIT_ASSERT(aMesh);
00180 
00181     //Test close method
00182     CPPUNIT_ASSERT_NO_THROW(aGibiRdDriver->close());
00183 
00184     //Default constructor
00185     {
00186       GIBI_MESH_RDONLY_DRIVER aGibiRdDriverCpy_1;
00187     }
00188 
00189     //Test copy constructor and operator==(), defined in GENDRIVER class
00190     {
00191       GIBI_MESH_RDONLY_DRIVER aGibiRdDriverCpy_2 (*aGibiRdDriver);
00192       CPPUNIT_ASSERT(aGibiRdDriverCpy_2.GENDRIVER::operator==(*aGibiRdDriver));
00193 
00194       //Test (friend ostream & operator <<) defined GENDRIVER class
00195       ostringstream rostr1, rostr2;
00196       rostr1 << *aGibiRdDriver;
00197       rostr2 << aGibiRdDriverCpy_2;
00198       CPPUNIT_ASSERT(rostr1.str() != "");
00199       CPPUNIT_ASSERT(rostr1.str() == rostr2.str());
00200     }
00201 
00202     delete aGibiRdDriver;
00203   }
00204 
00205   //-------------------------------Test GIBI WRITE ONLY part------------------------------//
00206   {
00207     {
00208       //Creation a incorrect gibi write only driver
00209       GIBI_MESH_WRONLY_DRIVER *aInvalidGibiWrDriver =
00210         new GIBI_MESH_WRONLY_DRIVER(fileNotExistsName_wr, aMesh);
00211 
00212       //Trying open non existing file
00213       CPPUNIT_ASSERT_THROW(aInvalidGibiWrDriver->open(), MEDEXCEPTION);
00214 
00215       delete aInvalidGibiWrDriver;
00216     }
00217 
00218     //Trying write empty mesh
00219     {
00220       GIBI_MESH_WRONLY_DRIVER *aTmpGibiWrDriver =
00221         new GIBI_MESH_WRONLY_DRIVER(tmpfile, aMesh_NULL);
00222       aTmpGibiWrDriver->open();
00223 
00224       CPPUNIT_ASSERT_THROW(aTmpGibiWrDriver->write(),MEDEXCEPTION);
00225       aTmpGibiWrDriver->close();
00226 
00227       delete aTmpGibiWrDriver;
00228     }
00229 
00230     //Creation a correct gibi write only drivers
00231     GIBI_MESH_WRONLY_DRIVER *aGibiWrDriver =
00232       new GIBI_MESH_WRONLY_DRIVER(filename_wr, aMesh);
00233 
00234     //Check driver
00235     CPPUNIT_ASSERT(aGibiWrDriver);
00236 
00237     //Trying write mesh to file, if file is not open
00238     CPPUNIT_ASSERT_THROW(aGibiWrDriver->write(), MEDEXCEPTION);
00239 
00240     //Test open() method
00241     CPPUNIT_ASSERT_NO_THROW(aGibiWrDriver->open());
00242 
00243     CPPUNIT_ASSERT_NO_THROW(aGibiWrDriver->open());
00244 
00245     //Test setMeshName() and getMeshName() methods
00246     CPPUNIT_ASSERT_NO_THROW(aGibiWrDriver->setMeshName(newmeshname));
00247     CPPUNIT_ASSERT_EQUAL(newmeshname, aGibiWrDriver->getMeshName());
00248 
00249     //Test write() method
00250     CPPUNIT_ASSERT_NO_THROW(aGibiWrDriver->write());
00251 
00252     //Test read() method for GIBI WRITE ONLY driver
00253     CPPUNIT_ASSERT_THROW(aGibiWrDriver->read(), MEDEXCEPTION);
00254 
00255     //Test close method
00256     CPPUNIT_ASSERT_NO_THROW(aGibiWrDriver->close());
00257 
00258     //Default constructor
00259     GIBI_MESH_WRONLY_DRIVER aGibiWrDriverCpy_1;
00260 
00261     //Test copy constructor
00262     GIBI_MESH_WRONLY_DRIVER aGibiWrDriverCpy_2 (*aGibiWrDriver);
00263 
00264     //Test (bool operator ==) defined GENDRIVER class
00265     CPPUNIT_ASSERT(aGibiWrDriverCpy_2.GENDRIVER::operator==(*aGibiWrDriver));
00266 
00267     //Test (friend ostream & operator <<) defined GENDRIVER class
00268     ostringstream wostr1, wostr2;
00269     wostr1 << *aGibiWrDriver;
00270     wostr2 << aGibiWrDriverCpy_2;
00271     CPPUNIT_ASSERT(wostr1.str() != "");
00272     CPPUNIT_ASSERT(wostr1.str() == wostr2.str());
00273 
00274     delete aGibiWrDriver;
00275   }
00276 
00277   //-------------------------------Test GIBI READ/WRITE part---------------------------------//
00278   {
00279     {
00280       //Creation a incorrect gibi read/write driver
00281       GIBI_MESH_RDWR_DRIVER *aInvalidGibiRdWrDriver =
00282         new GIBI_MESH_RDWR_DRIVER(fileNotExistsName_wr, aMesh_2);
00283 
00284       //Trying read/write from not existing file
00285       CPPUNIT_ASSERT_THROW(aInvalidGibiRdWrDriver->read(), MEDEXCEPTION);
00286       CPPUNIT_ASSERT_THROW(aInvalidGibiRdWrDriver->write(), MEDEXCEPTION);
00287 
00288       delete aInvalidGibiRdWrDriver;
00289     }
00290 
00291     //Trying write empty mesh
00292     {
00293       GIBI_MESH_RDWR_DRIVER *aTmpGibiRdWrDriver =
00294         new GIBI_MESH_RDWR_DRIVER(tmpfile_rdwr, aMesh_NULL);
00295       CPPUNIT_ASSERT_THROW(aTmpGibiRdWrDriver->write(), MEDEXCEPTION);
00296       delete aTmpGibiRdWrDriver;
00297     }
00298 
00299     //Creation a correct read/write driver
00300     GIBI_MESH_RDWR_DRIVER *aGibiRdWrDriver =
00301       new GIBI_MESH_RDWR_DRIVER(filename_rdwr, aMesh_2);
00302 
00303     //Test open/close methods
00304     CPPUNIT_ASSERT_NO_THROW(aGibiRdWrDriver->open());
00305     CPPUNIT_ASSERT_NO_THROW(aGibiRdWrDriver->close());
00306 
00307     //Check driver
00308     CPPUNIT_ASSERT(aGibiRdWrDriver);
00309 
00310     //Test read() method
00311     CPPUNIT_ASSERT_NO_THROW(aGibiRdWrDriver->read());
00312 
00313     //Check Mesh
00314     CPPUNIT_ASSERT(aMesh);
00315 
00316     //Trying fill not empty mesh
00317     CPPUNIT_ASSERT_THROW(aGibiRdWrDriver->read(),MEDEXCEPTION);
00318 
00319     //Test write() method
00320     aGibiRdWrDriver->setMeshName(newmeshname);
00321     CPPUNIT_ASSERT_NO_THROW(aGibiRdWrDriver->write());
00322 
00323     //Default constructor
00324     GIBI_MESH_RDWR_DRIVER aGibiRdWrDriverCpy_1;
00325 
00326     //Test copy constructor
00327     GIBI_MESH_RDWR_DRIVER aGibiRdWrDriverCpy_2 (*aGibiRdWrDriver);
00328 
00329     //Test (bool operator ==)
00330     CPPUNIT_ASSERT(aGibiRdWrDriverCpy_2.GENDRIVER::operator==(*aGibiRdWrDriver));
00331 
00332     //Test (ostream & operator <<)
00333     ostringstream rwostr1, rwostr2;
00334     rwostr1 << *aGibiRdWrDriver;
00335     rwostr2 << aGibiRdWrDriverCpy_2;
00336     CPPUNIT_ASSERT(rwostr1.str() != "");
00337     CPPUNIT_ASSERT(rwostr1.str() == rwostr2.str());
00338 
00339     delete aGibiRdWrDriver;
00340   }
00341 
00342   //----------------------------Test GIBI_MED_RDONLY_DRIVER----------------------------
00343   {
00344     {
00345       GIBI_MED_RDONLY_DRIVER *aInvalidMedGibiRdDriver =
00346         new GIBI_MED_RDONLY_DRIVER(fileNotExistsName_rd, rdFields);
00347 
00348       //Trying open not exising file
00349       CPPUNIT_ASSERT_THROW(aInvalidMedGibiRdDriver->open(), MEDEXCEPTION);
00350 
00351       delete aInvalidMedGibiRdDriver;
00352     }
00353 
00354     //Creation a correct Gibi read only driver (normal constructor)
00355     GIBI_MED_RDONLY_DRIVER *aGibiMedRdDriver =
00356       new GIBI_MED_RDONLY_DRIVER(filenamemed_rd, rdFields);
00357 
00358     //Check driver
00359     CPPUNIT_ASSERT(aGibiMedRdDriver);
00360 
00361     //Trying read mesh from file, if file is not open
00362     CPPUNIT_ASSERT_THROW(aGibiMedRdDriver->read(), MEDEXCEPTION);
00363 
00364     //Test open() method
00365     CPPUNIT_ASSERT_NO_THROW(aGibiMedRdDriver->open());
00366 
00367     //#ifdef ENABLE_FORCED_FAILURES
00368     //Trying open file secondary.
00369     //CPPUNIT_ASSERT_THROW(aGibiMedRdDriver->open(), MEDEXCEPTION);
00370     CPPUNIT_ASSERT_NO_THROW(aGibiMedRdDriver->open());
00371     //This case is not work, seems it BUG
00372     //#endif
00373 
00374     //Test read() method
00375     CPPUNIT_ASSERT_NO_THROW(aGibiMedRdDriver->read());
00376 
00377     //Test write() method for GIBI MED READ ONLY driver
00378     CPPUNIT_ASSERT_THROW(aGibiMedRdDriver->write(), MEDEXCEPTION);
00379 
00380     //Check Med
00381     CPPUNIT_ASSERT_EQUAL(1,int(rdFields.size()));
00382 
00383     //Test close method
00384     CPPUNIT_ASSERT_NO_THROW(aGibiMedRdDriver->close());
00385 
00386     //Default constructor
00387     GIBI_MED_RDONLY_DRIVER aGibiMedRdDriverCpy_1;
00388 
00389     //Test (bool operator ==) defined in GENDRIVER class
00390     CPPUNIT_ASSERT(! aGibiMedRdDriverCpy_1.GENDRIVER::operator==(*aGibiMedRdDriver));
00391 
00392     //Test copy constructor
00393     // commented since this test is not important
00394     //GIBI_MED_RDONLY_DRIVER aGibiMedRdDriverCpy_2 (*aGibiMedRdDriver);
00395     //#ifdef ENABLE_FORCED_FAILURES
00396     // (BUG) Copy constructor of GIBI_MED_RDONLY_DRIVER is not implemented
00397     // Small importance
00398     //CPPUNIT_ASSERT(aGibiMedRdDriverCpy_2.GENDRIVER::operator==(*aGibiMedRdDriver));
00399     //#endif
00400 
00401     //Test (friend ostream & operator <<) defined in GENDRIVER class
00402     // commented since this test is not important
00403     //ostringstream medrostr1, medrostr2;
00404     //medrostr1 << *aGibiMedRdDriver;
00405     //medrostr2 << aGibiMedRdDriverCpy_2;
00406     //CPPUNIT_ASSERT(medrostr1.str() != "");
00407     //#ifdef ENABLE_FORCED_FAILURES
00408     // (BUG) Copy constructor of GIBI_MED_RDONLY_DRIVER is not implemented
00409     //CPPUNIT_ASSERT(medrostr1.str() == medrostr2.str());
00410     //#endif
00411 
00412     rdFields[0]->removeReference();
00413 
00414     delete aGibiMedRdDriver;
00415   }
00416 
00417   //----------------------------Test GIBI MED WRITE ONLY part----------------------------------//
00418   {
00419     {
00420       //Creation a incorrect gibi med write only driver
00421       GIBI_MED_WRONLY_DRIVER *aInvalidGibiMedWrDriver =
00422         new GIBI_MED_WRONLY_DRIVER(fileNotExistsName_wr, wrFields, aMesh);
00423 
00424       //Trying open non existing file
00425       CPPUNIT_ASSERT_THROW(aInvalidGibiMedWrDriver->open(), MEDEXCEPTION);
00426 
00427       delete aInvalidGibiMedWrDriver;
00428     }
00429 
00430     //Trying create gibi med write only driver with null MESH
00431     CPPUNIT_ASSERT_THROW(new GIBI_MED_WRONLY_DRIVER(tmpfilemed, wrFields, aMesh_NULL), MEDEXCEPTION);
00432 
00433     //Creation a correct gibi med write only drivers
00434     GIBI_MED_WRONLY_DRIVER *aGibiMedWrDriver =
00435       new GIBI_MED_WRONLY_DRIVER(filenamemed_wr, wrFields, aMesh);
00436 
00437     //Check driver
00438     CPPUNIT_ASSERT(aGibiMedWrDriver);
00439 
00440     //Trying write mesh to file, if file is not open
00441     CPPUNIT_ASSERT_THROW(aGibiMedWrDriver->write(), MEDEXCEPTION);
00442 
00443     //Test open() method
00444     CPPUNIT_ASSERT_NO_THROW(aGibiMedWrDriver->open());
00445 
00446     //Trying open file secondary.
00447     //#ifdef ENABLE_FORCED_FAILURES
00448     // (BUG) Exception is generated, but second call to open() function breaks driver
00449     //CPPUNIT_ASSERT_THROW(aGibiMedWrDriver->open(), MEDEXCEPTION);
00450     CPPUNIT_ASSERT_NO_THROW(aGibiMedWrDriver->open());
00451     //#endif
00452 
00453     //Test write() method
00454     CPPUNIT_ASSERT_NO_THROW(aGibiMedWrDriver->write());
00455 
00456     //Test read() method for GIBI MED WRITE ONLY driver
00457     CPPUNIT_ASSERT_THROW(aGibiMedWrDriver->read(), MEDEXCEPTION);
00458 
00459     //Test close method
00460     CPPUNIT_ASSERT_NO_THROW(aGibiMedWrDriver->close());
00461 
00462     //Default constructor
00463     GIBI_MED_WRONLY_DRIVER aGibiMedWrDriverCpy_1;
00464 
00465     //Test copy constructor
00466     {
00467       GIBI_MED_WRONLY_DRIVER aGibiMedWrDriverCpy_2 (*aGibiMedWrDriver);
00468 
00469       //Test (bool operator ==) defined in GENDRIVER class
00470       // commented since this test is not important
00471       //#ifdef ENABLE_FORCED_FAILURES
00472       // (BUG) Copy constructor of GIBI_MED_WRONLY_DRIVER is not implemented!!!
00473       //CPPUNIT_ASSERT(aGibiMedWrDriverCpy_2.GENDRIVER::operator==(*aGibiMedWrDriver));
00474       //#endif
00475 
00476       //Test (friend ostream & operator <<) defined GENDRIVER class
00477       // commented since this test is not important
00478       //ostringstream medwostr1, medwostr2;
00479       //medwostr1 << *aGibiMedWrDriver;
00480       //medwostr2 << aGibiMedWrDriverCpy_2;
00481       //CPPUNIT_ASSERT(medwostr1.str() != "");
00482       //#ifdef ENABLE_FORCED_FAILURES
00483       // Copy constructor of GIBI_MED_WRONLY_DRIVER is not implemented!!!
00484       //CPPUNIT_ASSERT(medwostr1.str() == medwostr2.str());
00485       //#endif
00486     }
00487 
00488     delete aGibiMedWrDriver;
00489   }
00490   //Delete all objects
00491   aMesh->removeReference();
00492   aMesh_2->removeReference();
00493 }