Back to index

salome-med  6.5.0
MEDMEMTest_VtkFieldDriver.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 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   //Test write() method
00157   try
00158   {
00159     aVtkFieldDriver_int->write();
00160   }
00161   catch(MEDEXCEPTION &e)
00162   {
00163     CPPUNIT_FAIL(e.what());
00164   }
00165   catch( ... )
00166   {
00167     CPPUNIT_FAIL("Unknown exception");
00168   }
00169 
00170   //Test read() method for Vtk Field Driver
00171   CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
00172 
00173   //Test close() method
00174   try
00175   {
00176     aVtkFieldDriver_int->close();
00177   }
00178   catch(MEDEXCEPTION &e)
00179   {
00180     CPPUNIT_FAIL(e.what());
00181   }
00182   catch( ... )
00183   {
00184     CPPUNIT_FAIL("Unknown exception");
00185   }
00186 
00187   //Test openAppend() method
00188   try
00189   {
00190     aVtkFieldDriver_int->close();
00191   }
00192   catch(MEDEXCEPTION &e)
00193   {
00194     CPPUNIT_FAIL(e.what());
00195   }
00196   catch( ... )
00197   {
00198     CPPUNIT_FAIL("Unknown exception");
00199   }
00200 
00201 
00203   //  TEST? Test openAppend() and writeAppend() methods  //
00205 
00206   //Creation correct VtkFieldDriver
00207   VTK_FIELD_DRIVER<double> *aVtkFieldDriver_double = new VTK_FIELD_DRIVER<double>(filename_wr, aField_1);
00208 
00209   //Check driver
00210   CPPUNIT_ASSERT(aVtkFieldDriver_double);
00211 
00212   //Test openAppend() method
00213   try
00214   {
00215     aVtkFieldDriver_double->openAppend();
00216   }
00217   catch(MEDEXCEPTION &e)
00218   {
00219     CPPUNIT_FAIL(e.what());
00220   }
00221   catch( ... )
00222   {
00223     CPPUNIT_FAIL("Unknown exception");
00224   }
00225 
00226   aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
00227 
00228   //Test writeAppend() method
00229   try
00230   {
00231     aVtkFieldDriver_double->writeAppend();
00232   }
00233   catch(MEDEXCEPTION &e)
00234   {
00235     CPPUNIT_FAIL(e.what());
00236   }
00237   catch( ... )
00238   {
00239     CPPUNIT_FAIL("Unknown exception");
00240   }
00241 
00242   try
00243   {
00244     aVtkFieldDriver_double->close();
00245   }
00246   catch(MEDEXCEPTION &e)
00247   {
00248     CPPUNIT_FAIL(e.what());
00249   }
00250   catch( ... )
00251   {
00252     CPPUNIT_FAIL("Unknown exception");
00253   }
00254 
00256   //  TEST BINARY FORMAT //
00258 
00259   DRIVERFACTORY::setVtkBinaryFormatForWriting( true );
00260 
00261   //BINARY: Test open() method
00262   try
00263   {
00264     aVtkFieldDriver_int->open();
00265   }
00266   catch(MEDEXCEPTION &e)
00267   {
00268     CPPUNIT_FAIL(e.what());
00269   }
00270   catch( ... )
00271   {
00272     CPPUNIT_FAIL("Unknown exception");
00273   }
00274 
00275   //BINARY: Test write() method
00276   try
00277   {
00278     aVtkFieldDriver_int->write();
00279   }
00280   catch(MEDEXCEPTION &e)
00281   {
00282     CPPUNIT_FAIL(e.what());
00283   }
00284   catch( ... )
00285   {
00286     CPPUNIT_FAIL("Unknown exception");
00287   }
00288 
00289   //BINARY: Test read() method for Vtk Field Driver
00290   CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
00291 
00292   //BINARY: Test close() method
00293   try
00294   {
00295     aVtkFieldDriver_int->close();
00296   }
00297   catch(MEDEXCEPTION &e)
00298   {
00299     CPPUNIT_FAIL(e.what());
00300   }
00301   catch( ... )
00302   {
00303     CPPUNIT_FAIL("Unknown exception");
00304   }
00305 
00306   //BINARY: Test openAppend() method
00307   try
00308   {
00309     aVtkFieldDriver_int->close();
00310   }
00311   catch(MEDEXCEPTION &e)
00312   {
00313     CPPUNIT_FAIL(e.what());
00314   }
00315   catch( ... )
00316   {
00317     CPPUNIT_FAIL("Unknown exception");
00318   }
00319 
00320 
00322   //  BINARY: Test openAppend() and writeAppend() methods  //
00324 
00325   try
00326   {
00327     aVtkFieldDriver_double->openAppend();
00328   }
00329   catch(MEDEXCEPTION &e)
00330   {
00331     CPPUNIT_FAIL(e.what());
00332   }
00333   catch( ... )
00334   {
00335     CPPUNIT_FAIL("Unknown exception");
00336   }
00337 
00338   aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
00339 
00340   //BINARY: Test writeAppend() method
00341   try
00342   {
00343     aVtkFieldDriver_double->writeAppend();
00344   }
00345   catch(MEDEXCEPTION &e)
00346   {
00347     CPPUNIT_FAIL(e.what());
00348   }
00349   catch( ... )
00350   {
00351     CPPUNIT_FAIL("Unknown exception");
00352   }
00353 
00354   try
00355   {
00356     aVtkFieldDriver_double->close();
00357   }
00358   catch(MEDEXCEPTION &e)
00359   {
00360     CPPUNIT_FAIL(e.what());
00361   }
00362   catch( ... )
00363   {
00364     CPPUNIT_FAIL("Unknown exception");
00365   }
00366 
00367   DRIVERFACTORY::setVtkBinaryFormatForWriting( false );
00368 
00369   VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_1;
00370 
00371   //Test copy constructor
00372   VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_2 (*aVtkFieldDriver_int);
00373 
00374   CPPUNIT_ASSERT_EQUAL(aVtkFieldDriver_intCpy_2, *aVtkFieldDriver_int);
00375   //Test (bool operator ==) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
00376   CPPUNIT_ASSERT(aVtkFieldDriver_intCpy_2.GENDRIVER::operator== (*aVtkFieldDriver_int));
00377 
00378   //Test (friend ostream & operator <<) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
00379   ostringstream ostr1, ostr2;
00380   ostr1<<*aVtkFieldDriver_int;
00381   ostr2<<aVtkFieldDriver_intCpy_2;
00382 
00383   CPPUNIT_ASSERT(ostr1.str() != "");
00384   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
00385 
00386   //Delete all objects
00387   aField->removeReference();
00388   aField_1->removeReference();
00389   delete aInvalidVtkFieldDriver_1;
00390   delete aInvalidVtkFieldDriver_2;
00391   delete aMedRdFieldDriver22_int;
00392   delete aMedRdFieldDriver22_double;
00393   delete aVtkFieldDriver_int;
00394   delete aVtkFieldDriver_double;
00395 }