Back to index

salome-med  6.5.0
MEDMEMTest_PorflowMeshDriver.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_PorflowMeshDriver.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 
00074 void MEDMEMTest::testPorflowMeshDriver()
00075 {
00076   MESH *aMesh                      = new MESH;
00077   MESH *aMesh_1                    = new MESH;
00078 
00079   string filename_rd               = getResourceFile("Case1.inp");
00080   string filename_wr               = makeTmpFile("myWr_Case1.inp");
00081   string meshname                  = "Case1";
00082   string newmeshname               = "new" + meshname;
00083   string fileNotExistsName_rd      = "notExists.inp";
00084   string fileNotExistsName_wr      = "/path_not_exists/file_not_exists.inp";
00085   string filename_rdwr             = makeTmpFile("myRdWr_Case1.inp", filename_rd);
00086   string res_file1                 = makeTmpFile("Case1.xyz", getResourceFile("Case1.xyz"));
00087   string res_file2                 = makeTmpFile("Case1.cnc", getResourceFile("Case1.cnc"));
00088 
00089   MEDMEMTest_TmpFilesRemover aRemover;
00090   aRemover.Register(filename_wr);
00091   aRemover.Register(filename_rdwr);
00092   aRemover.Register(res_file1);
00093   aRemover.Register(res_file2);
00094 
00095   //-----------------------------Test READ ONLY part---------------------------------------//
00096   {
00097     {
00098       //Creation a incorrect Porflow read only driver
00099       PORFLOW_MESH_RDONLY_DRIVER *aInvalidPorflowRdDriver =
00100         new PORFLOW_MESH_RDONLY_DRIVER(fileNotExistsName_rd, aMesh);
00101 
00102       //Trying open not existing file
00103       CPPUNIT_ASSERT_THROW(aInvalidPorflowRdDriver->open(), MEDEXCEPTION);
00104 
00105       delete aInvalidPorflowRdDriver;
00106     }
00107 
00108     //Creation a correct Porflow read only driver (normal constructor)
00109     PORFLOW_MESH_RDONLY_DRIVER *aPorflowRdDriver = new PORFLOW_MESH_RDONLY_DRIVER(filename_rd, aMesh);
00110 
00111     //Check driver
00112     CPPUNIT_ASSERT(aPorflowRdDriver);
00113 
00114     //Trying read mesh from file, if file is not open
00115     CPPUNIT_ASSERT_THROW(aPorflowRdDriver->read(), MEDEXCEPTION);
00116 
00117     //Test open() method 
00118     // commented by skl - must be exception since thid file is
00119     // opened yet and such case is tested below
00120     //CPPUNIT_ASSERT_NO_THROW(aPorflowRdDriver->open());
00121 
00122     //#ifdef ENABLE_FORCED_FAILURES
00123     //Trying open file secondary
00124     //CPPUNIT_ASSERT_THROW(aPorflowRdDriver->open(), MEDEXCEPTION);
00125     CPPUNIT_ASSERT_NO_THROW(aPorflowRdDriver->open());
00126     //This case  work, but it corrypt driver
00127     //#endif
00128 
00129     //Test read() method
00130     CPPUNIT_ASSERT_NO_THROW(aPorflowRdDriver->read());
00131 
00132     //Check Mesh
00133     CPPUNIT_ASSERT(aMesh);
00134 
00135     //Trying fill not empty mesh
00136     CPPUNIT_ASSERT_THROW(aPorflowRdDriver->read(), MEDEXCEPTION);
00137 
00138     //Test write() method for Porflow RDONLY Driver
00139     CPPUNIT_ASSERT_THROW(aPorflowRdDriver->write(), MEDEXCEPTION);
00140 
00141     //Test setMeshName() and getMeshName()
00142     CPPUNIT_ASSERT_NO_THROW(aPorflowRdDriver->setMeshName(meshname));
00143     CPPUNIT_ASSERT_EQUAL(meshname, aPorflowRdDriver->getMeshName());
00144 
00145     //Test close() method
00146     CPPUNIT_ASSERT_NO_THROW(aPorflowRdDriver->close());
00147 
00148     //Default constructor
00149     PORFLOW_MESH_RDONLY_DRIVER aPorflowRdDriverCpy_1;
00150     // TO DO
00151 
00152     //Test copy constructor
00153     PORFLOW_MESH_RDONLY_DRIVER aPorflowRdDriverCpy_2 (*aPorflowRdDriver);
00154 
00155     //Test (bool operator ==) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00156     CPPUNIT_ASSERT(aPorflowRdDriverCpy_2.GENDRIVER::operator==(*aPorflowRdDriver));
00157 
00158     //Test (friend ostream & operator <<) defined GENDRIVER class in MEDMEM_GenDriver.hxx
00159     ostringstream rostr1, rostr2;
00160     rostr1 << *aPorflowRdDriver;
00161     rostr2 << aPorflowRdDriverCpy_2;
00162     CPPUNIT_ASSERT(rostr1.str() != "");
00163     CPPUNIT_ASSERT_EQUAL(rostr1.str() , rostr2.str());
00164 
00165     delete aPorflowRdDriver;
00166   }
00167 
00168   //---------------------------Test WRITE ONLY part-------------------------------------//
00169   {
00170     {
00171       //Creation a incorrect Porflow write only driver
00172       PORFLOW_MESH_WRONLY_DRIVER *aInvalidPorflowWrDriver =
00173         new PORFLOW_MESH_WRONLY_DRIVER(fileNotExistsName_wr, aMesh);
00174 
00175       //Test case: trying open non existing file
00176       CPPUNIT_ASSERT_THROW(aInvalidPorflowWrDriver->open(), MEDEXCEPTION);
00177 
00178       delete aInvalidPorflowWrDriver;
00179     }
00180 
00181     //Creation a correct write only driver
00182     PORFLOW_MESH_WRONLY_DRIVER *aPorflowWrDriver = new PORFLOW_MESH_WRONLY_DRIVER(filename_wr, aMesh);
00183 
00184     //Check driver
00185     CPPUNIT_ASSERT(aPorflowWrDriver);
00186 
00187     //Test case: trying write mesh to file, if file is not open
00188     CPPUNIT_ASSERT_THROW(aPorflowWrDriver->write(), MEDEXCEPTION);
00189 
00190     //Test open() method
00191     //#ifndef ENABLE_FORCED_FAILURES
00192     FILE *tmpFile = fopen(filename_wr.data(), "w");
00193     if ( tmpFile )
00194       fclose(tmpFile);
00195     //#endif
00196     CPPUNIT_ASSERT_NO_THROW(aPorflowWrDriver->open());
00197 
00198     //Test case: trying open file secondary.
00199     CPPUNIT_ASSERT_THROW(aPorflowWrDriver->open(), MEDEXCEPTION);
00200 
00201     //Test write() method
00202     //#ifdef ENABLE_FORCED_FAILURES
00203     //Write method is not implemented
00204     //CPPUNIT_ASSERT_NO_THROW(aPorflowWrDriver->write());
00205     CPPUNIT_ASSERT_THROW(aPorflowWrDriver->write(), MEDEXCEPTION);
00206     //#endif
00207 
00208     //Test read() method for WRITE ONLY driver
00209     CPPUNIT_ASSERT_THROW(aPorflowWrDriver->read(), MEDEXCEPTION);
00210 
00211     //Test setMeshName() and getMeshName()
00212     CPPUNIT_ASSERT_NO_THROW(aPorflowWrDriver->setMeshName(newmeshname));
00213     CPPUNIT_ASSERT_EQUAL(newmeshname, aPorflowWrDriver->getMeshName());
00214 
00215     CPPUNIT_ASSERT_NO_THROW(aPorflowWrDriver->close());
00216 
00217     //Default constructor
00218     PORFLOW_MESH_WRONLY_DRIVER aPorflowWrDriverCpy_1;
00219 
00220     //Test copy constructor
00221     PORFLOW_MESH_WRONLY_DRIVER aPorflowWrDriverCpy_2 (*aPorflowWrDriver);
00222 
00223     //Test (bool operator ==) defined in GENDRIVER class
00224     CPPUNIT_ASSERT(aPorflowWrDriverCpy_2.GENDRIVER::operator==(*aPorflowWrDriver));
00225 
00226     //Test (friend ostream & operator <<) defined in GENDRIVER class
00227     ostringstream wostr1, wostr2;
00228     wostr1 << *aPorflowWrDriver;
00229     wostr2 << aPorflowWrDriverCpy_2;
00230     CPPUNIT_ASSERT(wostr1.str() != "");
00231     CPPUNIT_ASSERT(wostr1.str() == wostr2.str());
00232 
00233     delete aPorflowWrDriver;
00234   }
00235 
00236   //------------------------Test READ / WRITE part------------------------------------//
00237   {
00238     {
00239       //Creation a incorrect read/write driver
00240       PORFLOW_MESH_RDWR_DRIVER *aInvalidPorflowRdWrDriver =
00241         new PORFLOW_MESH_RDWR_DRIVER(fileNotExistsName_wr, aMesh_1);
00242 
00243       //Test case: trying open non existing file
00244       CPPUNIT_ASSERT_THROW(aInvalidPorflowRdWrDriver->open(), MEDEXCEPTION);
00245 
00246       delete aInvalidPorflowRdWrDriver;
00247     }
00248 
00249     //Creation a correct read/write driver
00250     PORFLOW_MESH_RDWR_DRIVER *aPorflowRdWrDriver = new PORFLOW_MESH_RDWR_DRIVER(filename_rdwr, aMesh_1);
00251 
00252     //Check driver
00253     CPPUNIT_ASSERT(aPorflowRdWrDriver);
00254 
00255     //Test case: trying write mesh to file, if file is not open
00256     CPPUNIT_ASSERT_THROW(aPorflowRdWrDriver->write(), MEDEXCEPTION);
00257 
00258     //Test case: trying read mesh from file, if file is not open
00259     CPPUNIT_ASSERT_THROW(aPorflowRdWrDriver->read(), MEDEXCEPTION);
00260 
00261     //Test open() method
00262     CPPUNIT_ASSERT_NO_THROW(aPorflowRdWrDriver->open());
00263 
00264     //Test case: trying open file secondary.
00265     //#ifdef ENABLE_FORCED_FAILURES
00266     // (BUG) This case work, but corrupt driver
00267     CPPUNIT_ASSERT_THROW(aPorflowRdWrDriver->open(), MEDEXCEPTION);
00268     //#endif
00269 
00270     //Test read() method
00271     CPPUNIT_ASSERT_NO_THROW();
00272     CPPUNIT_ASSERT_NO_THROW(aPorflowRdWrDriver->read());
00273 
00274     //Test write() method
00275     //aPorflowRdWrDriver->setMeshName(newmeshname);
00276     //aMesh->setName(newmeshname);
00277     //CPPUNIT_ASSERT_NO_THROW(aPorflowRdWrDriver->write());
00278     //Write method is not implemented
00279     //#ifdef ENABLE_FORCED_FAILURES
00280     //CPPUNIT_FAIL("PORFLOW_MESH_RDWR_DRIVER::write() method not implemented");
00281     //#endif
00282 
00283     //Check Mesh
00284     CPPUNIT_ASSERT(aMesh);
00285 
00286     CPPUNIT_ASSERT_NO_THROW(aPorflowRdWrDriver->close());
00287 
00288     //Default constructor
00289     PORFLOW_MESH_RDWR_DRIVER aPorflowRdWrDriverCpy_1;
00290     // TO DO
00291 
00292     //Test copy constructor
00293     PORFLOW_MESH_RDWR_DRIVER aPorflowRdWrDriverCpy_2 (*aPorflowRdWrDriver);
00294 
00295     //Test (bool operator ==) defined in GENDRIVER class
00296     CPPUNIT_ASSERT(aPorflowRdWrDriverCpy_2.GENDRIVER::operator==(*aPorflowRdWrDriver));
00297 
00298     //Test (friend ostream & operator <<) defined in GENDRIVER class
00299     ostringstream rwostr1, rwostr2;
00300     rwostr1 << *aPorflowRdWrDriver;
00301     rwostr2 << aPorflowRdWrDriverCpy_2;
00302     CPPUNIT_ASSERT(rwostr1.str() != "");
00303     CPPUNIT_ASSERT(rwostr1.str() == rwostr2.str());
00304 
00305     delete aPorflowRdWrDriver;
00306   }
00307 
00308   //Delete all objects
00309   aMesh->removeReference();
00310   aMesh_1->removeReference();
00311 }