Back to index

salome-med  6.5.0
MEDMEMTest_VtkFieldDriver_fault.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_VtkFieldDriver.hxx>
00024 #include <MEDMEM_Field.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 
00059 static void MEDMEMTest_testVtkFieldDriver()
00060 {
00061   FIELD<int> *aField                = new FIELD<int>();
00062   FIELD<double> *aField_1           = new FIELD<double>();
00063 
00064   string filename_rd                = getResourceFile("pointe.med");
00065   string emptyfilename              = "";
00066   string fileNotExistsName          = "/path_not_exists/file_not_exists.vtk";
00067   string filename_wr                = makeTmpFile( "myField_pointe.vtk" );
00068 
00069   string fieldname_rd_int           = "fieldnodeint";
00070   string fieldname_wr_int           = "myintfield";
00071   string fieldname_rd_double        = "fieldnodedouble";
00072   string fieldname_wr_double        = "mydoublefield";
00073 
00074   // To remove tmp files from disk
00075   MEDMEMTest_TmpFilesRemover aRemover;
00076   aRemover.Register(filename_wr);
00077 
00079   //  TEST1: Open not existing file  //
00081   //Creation Invalid VtkFieldDriver (file not exist)
00082   VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_1 = new VTK_FIELD_DRIVER<int>(fileNotExistsName, aField);
00083   //Trying open not existing file
00084   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->open(), MEDEXCEPTION);
00085   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConst(), MEDEXCEPTION);
00086   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConstAppend(), MEDEXCEPTION);
00087 
00089   //  TEST2: Open file with empty file name  //
00091   //Creation Invalid VtkFieldDriver (empty file name)
00092   VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_2 = new VTK_FIELD_DRIVER<int>(emptyfilename, aField);
00093 
00094   //Trying file with empty name
00095   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->open(), MEDEXCEPTION);
00096   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConst(), MEDEXCEPTION);
00097   CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConstAppend(), MEDEXCEPTION);
00098 
00100   //  TEST2: Main test  //
00102   //Read Fields from file
00103   MED_FIELD_RDONLY_DRIVER<int> *aMedRdFieldDriver22_int = new MED_FIELD_RDONLY_DRIVER<int>(filename_rd, aField);
00104   aMedRdFieldDriver22_int->open();
00105   aMedRdFieldDriver22_int->setFieldName(fieldname_rd_int);
00106   aMedRdFieldDriver22_int->read();
00107   aMedRdFieldDriver22_int->close();
00108   MESH *mesh=new MESH(MED_DRIVER,filename_rd,"maa1");
00109   aField->getSupport()->setMesh(mesh);
00110 
00111   MED_FIELD_RDONLY_DRIVER<double> *aMedRdFieldDriver22_double = new MED_FIELD_RDONLY_DRIVER<double>(filename_rd, aField_1);
00112   aMedRdFieldDriver22_double->open();
00113   aMedRdFieldDriver22_double->setFieldName(fieldname_rd_double);
00114   aMedRdFieldDriver22_double->read();
00115   aMedRdFieldDriver22_double->close();
00116   aField_1->getSupport()->setMesh(mesh);
00117   mesh->removeReference();
00118   //Check fields
00119   CPPUNIT_ASSERT(aField);
00120 
00121   //Creation correct VtkFieldDriver
00122   VTK_FIELD_DRIVER<int> *aVtkFieldDriver_int = new VTK_FIELD_DRIVER<int>(filename_wr, aField);
00123 
00124   //Check driver
00125   CPPUNIT_ASSERT(aVtkFieldDriver_int);
00126 
00127   //Test setFieldName() and getFieldName() methods
00128   try
00129   {
00130     aVtkFieldDriver_int->setFieldName(fieldname_wr_int);
00131   }
00132   catch(MEDEXCEPTION &e)
00133   {
00134     CPPUNIT_FAIL(e.what());
00135   }
00136   catch( ... )
00137   {
00138     CPPUNIT_FAIL("Unknown exception");
00139   }
00140   CPPUNIT_ASSERT_EQUAL(fieldname_wr_int, aVtkFieldDriver_int->getFieldName());
00141 
00142   //Test open() method
00143   try
00144   {
00145     aVtkFieldDriver_int->open();
00146   }
00147   catch(MEDEXCEPTION &e)
00148   {
00149     CPPUNIT_FAIL(e.what());
00150   }
00151   catch( ... )
00152   {
00153     CPPUNIT_FAIL("Unknown exception");
00154   }
00155 
00156 //#ifdef ENABLE_FAULTS
00157   //Test write() method
00158   try
00159   {
00160     aVtkFieldDriver_int->write();
00161   }
00162   catch(MEDEXCEPTION &e)
00163   {
00164     CPPUNIT_FAIL(e.what());
00165   }
00166   catch( ... )
00167   {
00168     CPPUNIT_FAIL("Unknown exception");
00169   }
00170     // => Segmentation fault
00171 //#endif
00172 
00173   //Test read() method for Vtk Field Driver
00174   CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
00175 
00176   //Test close() method
00177   try
00178   {
00179     aVtkFieldDriver_int->close();
00180   }
00181   catch(MEDEXCEPTION &e)
00182   {
00183     CPPUNIT_FAIL(e.what());
00184   }
00185   catch( ... )
00186   {
00187     CPPUNIT_FAIL("Unknown exception");
00188   }
00189 
00190   //Test openAppend() method
00191   try
00192   {
00193     aVtkFieldDriver_int->close();
00194   }
00195   catch(MEDEXCEPTION &e)
00196   {
00197     CPPUNIT_FAIL(e.what());
00198   }
00199   catch( ... )
00200   {
00201     CPPUNIT_FAIL("Unknown exception");
00202   }
00203 
00204 
00206   //  TEST? Test openAppend() and writeAppend() methods  //
00208 
00209   //Creation correct VtkFieldDriver
00210   VTK_FIELD_DRIVER<double> *aVtkFieldDriver_double = new VTK_FIELD_DRIVER<double>(filename_wr, aField_1);
00211 
00212   //Check driver
00213   CPPUNIT_ASSERT(aVtkFieldDriver_double);
00214 
00215   //Test openAppend() method
00216   try
00217   {
00218     aVtkFieldDriver_double->openAppend();
00219   }
00220   catch(MEDEXCEPTION &e)
00221   {
00222     CPPUNIT_FAIL(e.what());
00223   }
00224   catch( ... )
00225   {
00226     CPPUNIT_FAIL("Unknown exception");
00227   }
00228 
00229   aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
00230 
00231 //#ifdef ENABLE_FAULTS
00232   //Test writeAppend() method
00233   try
00234   {
00235     aVtkFieldDriver_double->writeAppend();
00236   }
00237   catch(MEDEXCEPTION &e)
00238   {
00239     CPPUNIT_FAIL(e.what());
00240   }
00241   catch( ... )
00242   {
00243     CPPUNIT_FAIL("Unknown exception");
00244   }
00245   // => Segmentation fault
00246 //#endif
00247 
00248   try
00249   {
00250     aVtkFieldDriver_double->close();
00251   }
00252   catch(MEDEXCEPTION &e)
00253   {
00254     CPPUNIT_FAIL(e.what());
00255   }
00256   catch( ... )
00257   {
00258     CPPUNIT_FAIL("Unknown exception");
00259   }
00260 
00261   //#ifdef ENABLE_FORCED_FAILURES
00262   //VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_1;
00263   //VTK_FIELD_DRIVER<int>.template VTK_FIELD_DRIVER<FullInterlace> aVtkFieldDriver_intCpy_1;
00264   //CPPUNIT_FAIL("Compilation error: no matching function for call to 'MEDMEM::VTK_FIELD_DRIVER<int>::VTK_FIELD_DRIVER()");
00265   //#endif
00266 
00267 //#ifdef ENABLE_FAULTS
00268   //Test copy constructor
00269   VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_2 (*aVtkFieldDriver_int);
00270   // => Segmentation fault after call Copy Constructor
00271 
00272   CPPUNIT_ASSERT_EQUAL(aVtkFieldDriver_intCpy_2, *aVtkFieldDriver_int);
00273   //Test (bool operator ==) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
00274   CPPUNIT_ASSERT(aVtkFieldDriver_intCpy_2.GENDRIVER::operator== (*aVtkFieldDriver_int));
00275 
00276   //Test (friend ostream & operator <<) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
00277   ostringstream ostr1, ostr2;
00278   ostr1<<*aVtkFieldDriver_int;
00279   ostr2<<aVtkFieldDriver_intCpy_2;
00280 
00281   CPPUNIT_ASSERT(ostr1.str() != "");
00282   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
00283 //#endif
00284 
00285   //Delete all objects
00286   aField->removeReference();
00287   aField_1->removeReference();
00288 //#ifdef ENABLE_FORCED_FAILURES
00289   // (BUG) Exception in the destructor after trying close not existing file.
00290   delete aInvalidVtkFieldDriver_1;
00291 //#endif
00292   delete aInvalidVtkFieldDriver_2;
00293   delete aMedRdFieldDriver22_int;
00294   delete aMedRdFieldDriver22_double;
00295   delete aVtkFieldDriver_int;
00296   delete aVtkFieldDriver_double;
00297 }
00298 
00299 int main (int argc, char** argv)
00300 {
00301   MEDMEMTest_testVtkFieldDriver();
00302 }