Back to index

salome-med  6.5.0
MEDMEMTest_MedFileBrowser.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_MedFileBrowser.hxx"
00024 
00025 // #include <sstream>
00026 // #include <cmath>
00027 
00028 #ifdef WNT
00029 #include <windows.h>
00030 #endif
00031 
00032 // use this define to enable lines, execution of which leads to Segmentation Fault
00033 //#define ENABLE_FAULTS
00034 
00035 // use this define to enable CPPUNIT asserts and fails, showing bugs
00036 //#define ENABLE_FORCED_FAILURES
00037 
00038 using namespace std;
00039 using namespace MEDMEM;
00040 using namespace MED_EN;
00041 
00042 // #30: MEDMEM_MedFileBrowser.hxx  }  MEDMEMTest_MedFileBrowser.cxx
00043 
00063 namespace
00064 {
00065   void check_bad_file( MEDFILEBROWSER& myMed )
00066   {
00067     vector<string> names(1);
00068     CPPUNIT_ASSERT_EQUAL( 0, myMed.getNumberOfMeshes() );
00069     CPPUNIT_ASSERT_EQUAL( 0, myMed.getNumberOfFields() );
00070     CPPUNIT_ASSERT_NO_THROW( myMed.getMeshNames( & names[0] ));
00071     CPPUNIT_ASSERT_NO_THROW( myMed.getFieldNames( & names[0] ));
00072     CPPUNIT_ASSERT_THROW( myMed.isStructuredMesh( "meshName" ), MEDEXCEPTION);
00073     CPPUNIT_ASSERT_THROW( myMed.getFieldType( "fieldName" ), MEDEXCEPTION);
00074     CPPUNIT_ASSERT_THROW( myMed.getMeshName( "fieldName" ), MEDEXCEPTION);
00075     CPPUNIT_ASSERT_THROW( myMed.getFieldIteration( "" ), MEDEXCEPTION);
00076   }
00077 }
00078 // CppUnit::assertion_traits<> to use CPPUNIT_ASSERT_EQUAL for VEC_DT_IT_
00079 namespace CppUnit
00080 {
00081   struct EQ_DT_IT_
00082   {
00083     bool operator() (const DT_IT_ &p1, const DT_IT_ &p2) const
00084     {
00085       return p1.dt == p2.dt && p1.it == p2.it;
00086     }
00087   };
00088   template <>
00089   struct assertion_traits<MEDMEM::VEC_DT_IT_>
00090   {  
00091     static bool equal( MEDMEM::VEC_DT_IT_ x, MEDMEM::VEC_DT_IT_ y )
00092     {
00093       return x.size() == y.size() && std::equal(x.begin(),x.end(), y.begin(), EQ_DT_IT_());
00094     }
00095 
00096     static std::string toString( const MEDMEM::VEC_DT_IT_& x )
00097     {
00098       MEDMEM::STRING ost("{ ");
00099       for ( unsigned i = 0; i < x.size(); ++i )
00100         ost << "{ " << x[i].dt << ", " << x[i].it << " }" << ( i+1==x.size() ? " " : ", ");
00101       return ost << "}";
00102     }
00103   };
00104 }
00105 
00106 void MEDMEMTest::testMedFileBrowser()
00107 {
00108   string filename = getResourceFile("pointe.med");
00109   string filename_21 = getResourceFile("pointe_V21.med");
00110   string filename_inexist = "InexistentFile.med";
00111   string filename_otherFormat = getResourceFile("test_2D.sauve");
00112 
00113   vector<string> names;
00114 
00115   // 1. constructor MEDFILEBROWSER()
00116   // --------------------------------
00117   MEDFILEBROWSER myMed;
00118   check_bad_file( myMed );
00119 
00120   // 2. Constructor MEDFILEBROWSER (const std::string & fileName) throw (MEDEXCEPTION)
00121   // ----------------------------------------------------------------------------------
00122 
00123   // Existent med file (pointe.med)
00124   CPPUNIT_ASSERT_NO_THROW( MEDFILEBROWSER myMed( filename ));
00125 
00126   // Inexistent med file
00127   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_inexist ), MEDEXCEPTION); 
00128 
00129   // Unsupported version med file
00130   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_21 ), MEDEXCEPTION); 
00131 
00132   // Not med file
00133   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_otherFormat ), MEDEXCEPTION); 
00134 
00135   // 3. void readFileStruct(const std::string & fileName) throw (MEDEXCEPTION)
00136   // --------------------------------------------------------------------------
00137   MEDFILEBROWSER pointe_med, empty_med, ko_med;
00138   
00139   // Inexistent med file
00140   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_inexist ), MEDEXCEPTION);
00141   check_bad_file( ko_med );
00142 
00143   // Unsupported version med file
00144   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_21 ), MEDEXCEPTION);
00145   check_bad_file( ko_med );
00146 
00147   // Not med file
00148   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_otherFormat ), MEDEXCEPTION);
00149   check_bad_file( ko_med );
00150 
00151   // Existent med file (pointe.med)
00152   CPPUNIT_ASSERT_NO_THROW( pointe_med.readFileStruct( filename ));
00153 
00154   // 4. std::string  getFileName() const
00155   // ------------------------------------
00156   CPPUNIT_ASSERT( empty_med.getFileName().empty() );
00157 
00158   CPPUNIT_ASSERT_EQUAL( filename_otherFormat, ko_med.getFileName() );
00159 
00160   CPPUNIT_ASSERT_EQUAL( filename, pointe_med.getFileName() );
00161 
00162   // 5. int getNumberOfMeshes ( void ) const
00163   // ----------------------------------------
00164   int nbMeshes;
00165   CPPUNIT_ASSERT_NO_THROW( nbMeshes = empty_med.getNumberOfMeshes());
00166   CPPUNIT_ASSERT_EQUAL( 0, nbMeshes );
00167   CPPUNIT_ASSERT_NO_THROW( nbMeshes = ko_med.getNumberOfMeshes());
00168   CPPUNIT_ASSERT_EQUAL( 0, nbMeshes );
00169   CPPUNIT_ASSERT_NO_THROW( nbMeshes = pointe_med.getNumberOfMeshes());
00170   CPPUNIT_ASSERT_EQUAL( 1, nbMeshes );
00171 
00172   // 6. int        getNumberOfFields ( void ) const;
00173   // ------------------------------------------------
00174   int nbFields;
00175   CPPUNIT_ASSERT_NO_THROW( nbFields = empty_med.getNumberOfFields());
00176   CPPUNIT_ASSERT_EQUAL( 0, nbFields );
00177   CPPUNIT_ASSERT_NO_THROW( nbFields = ko_med.getNumberOfFields());
00178   CPPUNIT_ASSERT_EQUAL( 0, nbFields );
00179   CPPUNIT_ASSERT_NO_THROW( nbFields = pointe_med.getNumberOfFields());
00180   CPPUNIT_ASSERT_EQUAL( 4, nbFields );
00181 
00182   // 7. void       getMeshNames      ( std::string * meshNames ) const;
00183   // -------------------------------------------------------------------
00184   names.resize(2,"");
00185   string emptyStr, maa1("maa1");
00186 
00187   CPPUNIT_ASSERT_NO_THROW( empty_med.getMeshNames( & names[0] ));
00188   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
00189   CPPUNIT_ASSERT_NO_THROW( ko_med.getMeshNames( & names[0] ));
00190   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
00191   CPPUNIT_ASSERT_NO_THROW( pointe_med.getMeshNames( & names[0] ));
00192   CPPUNIT_ASSERT_EQUAL( maa1, names[0] );
00193   CPPUNIT_ASSERT_EQUAL( emptyStr, names[1] );
00194   
00195   //8. void       getFieldNames     ( std::string * fieldNames ) const;
00196   // -------------------------------------------------------------------
00197   names.clear();
00198   names.resize(5,"");
00199   string
00200     fieldcelldoublescalar("fieldcelldoublescalar"),
00201     fieldcelldoublevector("fieldcelldoublevector" ),
00202     fieldnodedouble("fieldnodedouble"       ),
00203     fieldnodeint("fieldnodeint"          );
00204     
00205   
00206   CPPUNIT_ASSERT_NO_THROW( empty_med.getFieldNames( & names[0] ));
00207   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
00208   CPPUNIT_ASSERT_NO_THROW( ko_med.getFieldNames( & names[0] ));
00209   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
00210   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldNames( & names[0] ));
00211   CPPUNIT_ASSERT_EQUAL( fieldcelldoublescalar, names[0] );
00212   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevector, names[1] );
00213   CPPUNIT_ASSERT_EQUAL( fieldnodedouble      , names[2] );
00214   CPPUNIT_ASSERT_EQUAL( fieldnodeint         , names[3] );
00215   CPPUNIT_ASSERT_EQUAL( emptyStr, names[4] );
00216 
00217   //9. std::vector< std::string > getMeshNames () const;
00218   // ----------------------------------------------------
00219   CPPUNIT_ASSERT_NO_THROW( names = empty_med.getMeshNames() );
00220   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
00221   CPPUNIT_ASSERT_NO_THROW( names = ko_med.getMeshNames() );
00222   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
00223   CPPUNIT_ASSERT_NO_THROW( names = pointe_med.getMeshNames() );
00224   CPPUNIT_ASSERT_EQUAL(1, int( names.size() ));
00225   CPPUNIT_ASSERT_EQUAL(maa1, names[0] );
00226 
00227   // 10. std::vector< std::string > getFieldNames() const;
00228   // ------------------------------------------------------
00229   CPPUNIT_ASSERT_NO_THROW( names = empty_med.getFieldNames() );
00230   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
00231   CPPUNIT_ASSERT_NO_THROW( names = ko_med.getFieldNames() );
00232   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
00233   CPPUNIT_ASSERT_NO_THROW( names = pointe_med.getFieldNames() );
00234   CPPUNIT_ASSERT_EQUAL(4, int( names.size() ));
00235   CPPUNIT_ASSERT_EQUAL( fieldcelldoublescalar, names[0] );
00236   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevector, names[1] );
00237   CPPUNIT_ASSERT_EQUAL( fieldnodedouble      , names[2] );
00238   CPPUNIT_ASSERT_EQUAL( fieldnodeint         , names[3] );
00239 
00240   // 11. bool isStructuredMesh(const std::string & meshName) const throw (MEDEXCEPTION);
00241   // ------------------------------------------------------------------------------------
00242 
00243   CPPUNIT_ASSERT_THROW( empty_med. isStructuredMesh( "meshName" ), MEDEXCEPTION);
00244   CPPUNIT_ASSERT_THROW( ko_med.    isStructuredMesh( "meshName" ), MEDEXCEPTION);
00245   CPPUNIT_ASSERT_THROW( pointe_med.isStructuredMesh( "meshName" ), MEDEXCEPTION);
00246   CPPUNIT_ASSERT_NO_THROW( pointe_med.isStructuredMesh( "maa1" ));
00247   CPPUNIT_ASSERT( !pointe_med.isStructuredMesh( "maa1" ));
00248 
00249   // 12. med_type_champ getFieldType (const string & fieldName) const throw (MEDEXCEPTION) ;
00250   // ----------------------------------------------------------------------------------------
00251   CPPUNIT_ASSERT_THROW( empty_med.getFieldType( "fieldName" ), MEDEXCEPTION);
00252   CPPUNIT_ASSERT_THROW( pointe_med.getFieldType( "fieldName" ), MEDEXCEPTION);
00253   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldType( "fieldnodeint" ));
00254   CPPUNIT_ASSERT_EQUAL( MED_REEL64, pointe_med.getFieldType( "fieldcelldoublescalar" ));
00255   CPPUNIT_ASSERT_EQUAL( MED_REEL64, pointe_med.getFieldType( "fieldcelldoublevector" ));
00256   CPPUNIT_ASSERT_EQUAL( MED_INT32, pointe_med.getFieldType( "fieldnodeint" ));
00257 
00258   // 13. string getMeshName (const std::string & fieldName) const throw (MEDEXCEPTION) ;
00259   // ------------------------------------------------------------------------------------
00260   CPPUNIT_ASSERT_THROW( empty_med.getMeshName( "fieldnodeint" ), MEDEXCEPTION);
00261   CPPUNIT_ASSERT_THROW( ko_med.getMeshName( "fieldnodeint" ), MEDEXCEPTION);
00262   CPPUNIT_ASSERT_THROW( pointe_med.getMeshName( "fieldName" ), MEDEXCEPTION);
00263   CPPUNIT_ASSERT_NO_THROW( pointe_med.getMeshName( "fieldnodeint" ));
00264   names = pointe_med.getFieldNames();
00265   for ( unsigned i = 0; i < names.size(); ++i )
00266     CPPUNIT_ASSERT_EQUAL( maa1, pointe_med.getMeshName( names[i] ));
00267 
00268   // 14. VEC_DT_IT_  getFieldIteration (const std::string & fieldName) const throw (MEDEXCEPTION)
00269   // ---------------------------------------------------------------------------------------------
00270   CPPUNIT_ASSERT_THROW( empty_med.getFieldIteration( "fieldnodeint" ), MEDEXCEPTION);
00271   CPPUNIT_ASSERT_THROW( ko_med.getFieldIteration( "fieldnodeint" ), MEDEXCEPTION);
00272   CPPUNIT_ASSERT_THROW( pointe_med.getFieldIteration( "" ), MEDEXCEPTION);
00273   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldIteration( "fieldnodeint" ));
00274 
00275   VEC_DT_IT_ vec1dtit( 1 );
00276   vec1dtit[0].dt = -1;
00277   vec1dtit[0].it = -1;
00278   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldcelldoublescalar" ) );
00279   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldcelldoublevector" ) );
00280   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldnodeint" ) );
00281   VEC_DT_IT_ vec3dtit( 3, vec1dtit[0] );
00282   vec3dtit[1].dt = 1;
00283   vec3dtit[2].dt = 2;
00284   CPPUNIT_ASSERT_EQUAL( vec3dtit, pointe_med.getFieldIteration( "fieldnodedouble" ) );
00285 }