Back to index

salome-med  6.5.0
MEDMEMTest_MedMeshDriver.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 #include <MEDMEM_MedMeshDriver.hxx>
00023 #include <MEDMEM_Mesh.hxx>
00024 
00025 // use this define to enable lines, execution of which leads to Segmentation Fault
00026 //#define ENABLE_FAULTS
00027 
00028 // use this define to enable CPPUNIT asserts and fails, showing bugs
00029 //#define ENABLE_FORCED_FAILURES
00030 
00031 using namespace std;
00032 using namespace MEDMEM;
00033 
00070 void MEDMEMTest::testMedMeshDriver()
00071 {
00072   MESH *aMesh                      = new MESH;
00073   MESH *aMesh_1                    = new MESH;
00074 
00075   string filename_rd               = getResourceFile("pointe.med");
00076   string filename_wr               = makeTmpFile("myWr_pointe22.med");
00077   string tmpfile                   = makeTmpFile("tmp.med");
00078   string meshname                  = "maa1";
00079   string newmeshname               = "new" + meshname;
00080   string fileNotExistsName_rd      = "notExists.med";
00081   string fileNotExistsName_wr      = "/path_not_exists/file_not_exists.med";
00082   string filename_rdwr             = makeTmpFile("myRdWr_pointe22.med", filename_rd);
00083   char* longmeshname               = new char[MED_NAME_SIZE+2];
00084   for (int i = 0; i<MED_NAME_SIZE+2; ++i)
00085     longmeshname[i] = 'a';
00086   longmeshname[MED_NAME_SIZE+1] = '\0';
00087 
00088   // To remove tmp files from disk
00089   MEDMEMTest_TmpFilesRemover aRemover;
00090   aRemover.Register(filename_wr);
00091   aRemover.Register(filename_rdwr);
00092   aRemover.Register(tmpfile);
00093 
00094   //----------------------------------Test READ ONLY part---------------------------------------------------//
00095 
00096   //Creation a incorrect read only driver
00097   MED_MESH_RDONLY_DRIVER *aInvalidRdDriver22 = new MED_MESH_RDONLY_DRIVER(fileNotExistsName_rd, aMesh);
00098 
00099   //Trying open not existing file
00100   CPPUNIT_ASSERT_THROW(aInvalidRdDriver22->open(), MEDEXCEPTION);
00101 
00102   //Creation a correct read only driver (normal constructor)
00103   MED_MESH_RDONLY_DRIVER *aRdDriver22 = new MED_MESH_RDONLY_DRIVER(filename_rd, aMesh);
00104 
00105   //Check driver
00106   CPPUNIT_ASSERT(aRdDriver22);
00107 
00108   //Trying read mesh from file, if file is not open
00109   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
00110 
00111   //Test open() method
00112   try
00113   {
00114     aRdDriver22->open();
00115   }
00116   catch(MEDEXCEPTION &e)
00117   {
00118     CPPUNIT_FAIL(e.what());
00119   }
00120   catch( ... )
00121   {
00122     CPPUNIT_FAIL("Unknown exception");
00123   }
00124 
00125   //#ifdef  ENABLE_FORCED_FAILURES
00126   //Trying open file secondary.
00127   //CPPUNIT_ASSERT_THROW(aRdDriver22->open(), MEDEXCEPTION);
00128   CPPUNIT_ASSERT_NO_THROW(aRdDriver22->open());
00129   //This case is not work, seems it BUG
00130   //#endif
00131 
00132   //Trying read mesh from file, if mesh name is not set, i.e. empty
00133   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
00134 
00135   //Trying read mesh from file with very long name
00136   aRdDriver22->setMeshName(longmeshname);
00137   CPPUNIT_ASSERT_THROW(aRdDriver22->read(), MEDEXCEPTION);
00138 
00139   //Set correct  Mesh name
00140   //Test setMeshName() and getMeshName() methods
00141   try
00142   {
00143     aRdDriver22->setMeshName(meshname);
00144   }
00145   catch(MEDEXCEPTION &e)
00146   {
00147     CPPUNIT_FAIL(e.what());
00148   }
00149   catch( ... )
00150   {
00151     CPPUNIT_FAIL("Unknown exception");
00152   }
00153 
00154   CPPUNIT_ASSERT_EQUAL(meshname, aRdDriver22->getMeshName());
00155 
00156 
00157   //Test read() method
00158   try
00159   {
00160     aRdDriver22->read();
00161   }
00162   catch(MEDEXCEPTION &e)
00163   {
00164     CPPUNIT_FAIL(e.what());
00165   }
00166   catch( ... )
00167   {
00168     CPPUNIT_FAIL("Unknown exception");
00169   }
00170 
00171   //Test write() method for READ ONLY driver
00172   CPPUNIT_ASSERT_THROW(aRdDriver22->write(), MEDEXCEPTION);
00173 
00174   //Check Mesh
00175   CPPUNIT_ASSERT(aMesh);
00176 
00177   //Default constructor
00178   MED_MESH_RDONLY_DRIVER aRdDriver22Cpy_1;
00179 
00180   //Test (void operator =) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
00181   //aRdDriver22Cpy_1 = *aRdDriver22;
00182 
00183   //Test (bool operator ==) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00184   CPPUNIT_ASSERT(aRdDriver22Cpy_1.GENDRIVER::operator==(*aRdDriver22));
00185 
00186   //Test copy constructor
00187   MED_MESH_RDONLY_DRIVER aRdDriver22Cpy_2 (aRdDriver22Cpy_1);
00188   CPPUNIT_ASSERT_EQUAL(aRdDriver22Cpy_2, *aRdDriver22);
00189 
00190   //Test close() method
00191   try
00192   {
00193     aRdDriver22->close();
00194   }
00195   catch(MEDEXCEPTION &e)
00196   {
00197     CPPUNIT_FAIL(e.what());
00198   }
00199   catch( ... )
00200   {
00201     CPPUNIT_FAIL("Unknown exception");
00202   }
00203 
00204   //Trying read mesh from copy closed driver
00205   CPPUNIT_ASSERT_THROW(aRdDriver22Cpy_1.read(), MEDEXCEPTION);
00206   CPPUNIT_ASSERT_THROW(aRdDriver22Cpy_2.read(), MEDEXCEPTION);
00207 
00208   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00209   ostringstream rostr1, rostr2;
00210   rostr1<<aRdDriver22Cpy_1;
00211   rostr2<<aRdDriver22Cpy_2;
00212   CPPUNIT_ASSERT(rostr1.str() != "");
00213   CPPUNIT_ASSERT(rostr1.str() == rostr2.str());
00214 
00215 
00216   //----------------------------------Test WRITE ONLY part------------------------------------------//
00217 
00218   //Creation a incorrect write only driver
00219   MED_MESH_WRONLY_DRIVER *aInvalidWrDriver22 = new MED_MESH_WRONLY_DRIVER(fileNotExistsName_wr, aMesh);
00220 
00221   //Trying open non existing file
00222   CPPUNIT_ASSERT_THROW(aInvalidWrDriver22->open(), MEDEXCEPTION);
00223 
00224   //Trying write empty mesh
00225   MED_MESH_WRONLY_DRIVER *aTmpWrDriver22 = new MED_MESH_WRONLY_DRIVER(tmpfile, aMesh_1);
00226 
00227   //#ifdef ENABLE_FORCED_FAILURES
00228   aTmpWrDriver22->open();
00229   aTmpWrDriver22->setMeshName("EmptyMesh");
00230   CPPUNIT_ASSERT_THROW(aTmpWrDriver22->write(),MEDEXCEPTION);
00231   aTmpWrDriver22->close();
00232   //Unknown exception in this case
00233   //#endif
00234 
00235   //Creation a correct write only driver
00236   MED_MESH_WRONLY_DRIVER *aWrDriver22 = new MED_MESH_WRONLY_DRIVER(filename_wr, aMesh);
00237 
00238   //Check driver
00239   CPPUNIT_ASSERT(aWrDriver22);
00240 
00241   //Trying write mesh to file, if file is not open
00242   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
00243 
00244   //Test open() method
00245   try
00246   {
00247     aWrDriver22->open();
00248   }
00249   catch(MEDEXCEPTION &e)
00250     {
00251     CPPUNIT_FAIL(e.what());
00252   }
00253   catch( ... )
00254   {
00255     CPPUNIT_FAIL("Unknown exception");
00256   }
00257 
00258   //#ifdef ENABLE_FORCED_FAILURES
00259   //Trying open file secondary.
00260   //CPPUNIT_ASSERT_THROW(aWrDriver22->open(), MEDEXCEPTION);
00261   //This case is not work, seems it BUG
00262   //#endif
00263 
00264   //Test case: trying write mesh to file, if mesh name is not set, i.e empty
00265   aMesh->setName("");
00266   aWrDriver22->setMeshName("");
00267   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
00268 
00269   //Trying write mesh to file with very long name
00270   aWrDriver22->setMeshName(longmeshname);
00271   CPPUNIT_ASSERT_THROW(aWrDriver22->write(), MEDEXCEPTION);
00272 
00273   //Set initialy mesh name
00274   aMesh->setName(meshname);
00275   //Test setMeshName() and getMeshName() methods
00276   try
00277   {
00278     aWrDriver22->setMeshName(meshname);
00279   }
00280   catch(MEDEXCEPTION &e)
00281   {
00282     CPPUNIT_FAIL(e.what());
00283   }
00284   catch( ... )
00285   {
00286     CPPUNIT_FAIL("Unknown exception");
00287   }
00288 
00289   CPPUNIT_ASSERT_EQUAL(meshname, aWrDriver22->getMeshName());
00290 
00291   //  aWrDriver22->open();
00292   //Test write() method
00293   try
00294   {
00295     aWrDriver22->write();
00296   }
00297   catch(MEDEXCEPTION &e)
00298   {
00299     CPPUNIT_FAIL(e.what());
00300   }
00301   catch( ... )
00302   {
00303     CPPUNIT_FAIL("Unknown exception");
00304   }
00305 
00306   //Test read() method for WRITE ONLY driver
00307   CPPUNIT_ASSERT_THROW(aWrDriver22->read(), MEDEXCEPTION);
00308 
00309   //Default constructor
00310    MED_MESH_WRONLY_DRIVER aWrDriver22Cpy_1;
00311 
00312   //Test (void operator =) defined in GENDRIVER class
00313   //aWrDriver22Cpy_1 = *aWrDriver22;
00314 
00315   //Test (bool operator ==) defined GENDRIVER class
00316   CPPUNIT_ASSERT(aWrDriver22Cpy_1.GENDRIVER::operator==(*aWrDriver22));
00317 
00318   //Test copy constructor
00319   MED_MESH_WRONLY_DRIVER aWrDriver22Cpy_2 (aWrDriver22Cpy_1);
00320   CPPUNIT_ASSERT_EQUAL(aWrDriver22Cpy_2 , *aWrDriver22);
00321 
00322   try
00323   {
00324     aWrDriver22->close();
00325   }
00326   catch(MEDEXCEPTION &e)
00327   {
00328     CPPUNIT_FAIL(e.what());
00329   }
00330   catch( ... )
00331   {
00332     CPPUNIT_FAIL("Unknown exception");
00333   }
00334 
00335   //Test case: Trying write mesh using copy closed driver
00336   CPPUNIT_ASSERT_THROW(aWrDriver22Cpy_1.write(), MEDEXCEPTION);
00337   CPPUNIT_ASSERT_THROW(aWrDriver22Cpy_2.write(), MEDEXCEPTION);
00338 
00339   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00340   ostringstream wostr1, wostr2;
00341   wostr1<<aWrDriver22Cpy_1;
00342   wostr2<<aWrDriver22Cpy_2;
00343   CPPUNIT_ASSERT(wostr1.str() != "");
00344   CPPUNIT_ASSERT(wostr1.str() == wostr2.str());
00345 
00346 
00347   //----------------------------------Test READ / WRITE part------------------------------------------//
00348 
00349   //Creation a incorrect read/write driver
00350   MED_MESH_RDWR_DRIVER *aInvalidRdWrDriver22 = new  MED_MESH_RDWR_DRIVER(fileNotExistsName_wr, aMesh_1);
00351 
00352   //Test case: trying open non existing file
00353   CPPUNIT_ASSERT_THROW(aInvalidRdWrDriver22->open(), MEDEXCEPTION);
00354 
00355   //Creation a correct read/write driver
00356   MED_MESH_RDWR_DRIVER *aRdWrDriver22 = new MED_MESH_RDWR_DRIVER(filename_rdwr, aMesh_1);
00357 
00358   //Check driver
00359   CPPUNIT_ASSERT(aRdWrDriver22);
00360 
00361   //Test case: trying write mesh to file, if file is not open
00362   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
00363 
00364   //Test case: trying read mesh from file, if file is not open
00365   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
00366 
00367   //Test open() method
00368   try
00369   {
00370     aRdWrDriver22->open();
00371   }
00372   catch(MEDEXCEPTION &e)
00373     {
00374     CPPUNIT_FAIL(e.what());
00375   }
00376   catch( ... )
00377   {
00378     CPPUNIT_FAIL("Unknown exception");
00379   }
00380 
00381   //#ifdef ENABLE_FORCED_FAILURES
00382   //Test case: trying open file secondary.
00383   //CPPUNIT_ASSERT_THROW(aRdWrDriver22->open(), MEDEXCEPTION);
00384   CPPUNIT_ASSERT_NO_THROW(aRdWrDriver22->open());
00385   //This case is not work, seems it BUG
00386   //#endif
00387 
00388   //#ifdef ENABLE_FORCED_FAILURES
00389   //Set mesh name
00390   aMesh_1->setName("EmptyMesh");
00391   aRdWrDriver22->setMeshName("EmptyMesh");
00392   //Trying write in file empty mesh
00393   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(),MEDEXCEPTION);
00394   //No exception in this case, seems it BUG
00395   //#endif
00396 
00397   //Set initialy mesh name
00398   aMesh_1->setName(meshname);
00399   //Test setMeshName() and getMeshName() methods
00400   try
00401   {
00402     aRdWrDriver22->setMeshName(meshname);
00403   }
00404   catch(MEDEXCEPTION &e)
00405   {
00406     CPPUNIT_FAIL(e.what());
00407   }
00408   catch( ... )
00409   {
00410     CPPUNIT_FAIL("Unknown exception");
00411   }
00412 
00413   CPPUNIT_ASSERT_EQUAL(meshname, aRdWrDriver22->getMeshName());
00414 
00415   //Test read() method
00416   try
00417   {
00418     aRdWrDriver22->read();
00419   }
00420   catch(MEDEXCEPTION &e)
00421   {
00422     CPPUNIT_FAIL(e.what());
00423   }
00424   catch( ... )
00425   {
00426     CPPUNIT_FAIL("Unknown exception");
00427   }
00428 
00429   //Trying read mesh from file, if mesh name is not set, i.e. empty
00430   aRdWrDriver22->setMeshName("");
00431   aMesh_1->setName("");
00432   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
00433 
00434   //Trying write mesh to file, if mesh name is not set, i.e empty
00435   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
00436 
00437   //Trying read mesh from file with very long name
00438   aRdWrDriver22->setMeshName(longmeshname);
00439   aMesh_1->setName(longmeshname);
00440   CPPUNIT_ASSERT_THROW(aRdWrDriver22->read(), MEDEXCEPTION);
00441 
00442   //Trying write mesh to file with very long name
00443   CPPUNIT_ASSERT_THROW(aRdWrDriver22->write(), MEDEXCEPTION);
00444 
00445   //Test write() method
00446   aRdWrDriver22->setMeshName(newmeshname);
00447   aMesh_1->setName(newmeshname);
00448   try
00449   {
00450     aRdWrDriver22->write();
00451   }
00452   catch(MEDEXCEPTION &e)
00453   {
00454     CPPUNIT_FAIL(e.what());
00455   }
00456   catch( ... )
00457   {
00458     CPPUNIT_FAIL("Unknown exception");
00459   }
00460 
00461   //Check Mesh
00462   CPPUNIT_ASSERT(aMesh_1);
00463 
00464   //Default constructor
00465   MED_MESH_RDWR_DRIVER aRdWrDriver22Cpy_1;
00466 
00467   //Test (void operator =) defined in GENDRIVER class
00468   aRdWrDriver22Cpy_1= *aRdWrDriver22;
00469 
00470   //Test (bool operator ==) defined GENDRIVER class
00471   CPPUNIT_ASSERT(aRdWrDriver22Cpy_1.GENDRIVER::operator==(*aRdWrDriver22));
00472 
00473   //Test copy constructor
00474   MED_MESH_RDWR_DRIVER aRdWrDriver22Cpy_2 (aRdWrDriver22Cpy_1);
00475   // CPPUNIT_ASSERT(aRdWrDriver22Cpy_2.GENDRIVER::operator==( *aRdWrDriver22));
00476 
00477   try
00478   {
00479     aRdWrDriver22->close();
00480   }
00481   catch(MEDEXCEPTION &e)
00482   {
00483     CPPUNIT_FAIL(e.what());
00484   }
00485   catch( ... )
00486   {
00487     CPPUNIT_FAIL("Unknown exception");
00488   }
00489 
00490   //Test case: trying read/write mesh using copy closed driver
00491   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_1.read(), MEDEXCEPTION);
00492   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_2.read(), MEDEXCEPTION);
00493   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_1.write(), MEDEXCEPTION);
00494   CPPUNIT_ASSERT_THROW(aRdWrDriver22Cpy_2.write(), MEDEXCEPTION);
00495 
00496   //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00497   ostringstream rwostr1, rwostr2;
00498   rwostr1<<aRdWrDriver22Cpy_1;
00499   rwostr2<<aRdWrDriver22Cpy_2;
00500   cout << aRdWrDriver22Cpy_1;
00501   cout << aRdWrDriver22Cpy_2;
00502   CPPUNIT_ASSERT(rwostr1.str() != "");
00503   CPPUNIT_ASSERT(rwostr1.str() == rwostr2.str());
00504 
00505   //Delete all objects
00506   delete []longmeshname;
00507 
00508   delete aInvalidRdDriver22;
00509   delete aRdDriver22;
00510 
00511   delete aInvalidWrDriver22;
00512   delete aWrDriver22;
00513 
00514   delete aInvalidRdWrDriver22;
00515   delete aRdWrDriver22;
00516   delete aTmpWrDriver22;
00517 
00518   aMesh->removeReference();
00519   aMesh_1->removeReference();
00520 }