Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_MedFileBrowser.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include "MEDMEM_MedFileBrowser.hxx"
24
25 // #include <sstream>
26 // #include <cmath>
27
28 #ifdef WNT
29 #include <windows.h>
30 #endif
31
32 // use this define to enable lines, execution of which leads to Segmentation Fault
33 //#define ENABLE_FAULTS
34
35 // use this define to enable CPPUNIT asserts and fails, showing bugs
36 //#define ENABLE_FORCED_FAILURES
37
38 using namespace std;
39 using namespace MEDMEM;
40 using namespace MED_EN;
41
42 // #30: MEDMEM_MedFileBrowser.hxx  }  MEDMEMTest_MedFileBrowser.cxx
43
44 /*!
45  *  Check methods defined in MEDMEM_MedFileBrowser.hxx:
46  *  class MEDFILEBROWSER {
47  *   (+) MEDFILEBROWSER();
48  *   (+) MEDFILEBROWSER (const string & fileName) throw (MEDEXCEPTION);
49  *   (+) void readFileStruct(const string & fileName) throw (MEDEXCEPTION);
50  *   (+) std::string  getFileName() const;
51  *   (+) int  getNumberOfMeshes (void) const;
52  *   (+) int  getNumberOfFields (void) const;
53  *   (+) void getMeshNames (string * meshNames) const throw (MEDEXCEPTION);
54  *   (+) vector<string> getMeshNames   () const;
55  *   (+) void getFieldNames (string * fieldNames) const throw (MEDEXCEPTION);
56  *   (+) vector<string> getFieldNames () const;
57  *   (+) bool isStructuredMesh(const std::string & meshName) const throw (MEDEXCEPTION);
58  *   (+) MED_EN::med_type_champ getFieldType (const std::string & fieldName) const throw (MEDEXCEPTION) ;
59  *   (+) std::string getMeshName (const std::string & fieldName) const throw (MEDEXCEPTION) ;
60  *   (+) vector<DT_IT_> getFieldIteration (const string & fieldName) const throw (MEDEXCEPTION);
61  *  }
62  */
63 namespace
64 {
65   void check_bad_file( MEDFILEBROWSER& myMed )
66   {
67     vector<string> names(1);
68     CPPUNIT_ASSERT_EQUAL( 0, myMed.getNumberOfMeshes() );
69     CPPUNIT_ASSERT_EQUAL( 0, myMed.getNumberOfFields() );
70     CPPUNIT_ASSERT_NO_THROW( myMed.getMeshNames( & names[0] ));
71     CPPUNIT_ASSERT_NO_THROW( myMed.getFieldNames( & names[0] ));
72     CPPUNIT_ASSERT_THROW( myMed.isStructuredMesh( "meshName" ), MEDEXCEPTION);
73     CPPUNIT_ASSERT_THROW( myMed.getFieldType( "fieldName" ), MEDEXCEPTION);
74     CPPUNIT_ASSERT_THROW( myMed.getMeshName( "fieldName" ), MEDEXCEPTION);
75     CPPUNIT_ASSERT_THROW( myMed.getFieldIteration( "" ), MEDEXCEPTION);
76   }
77 }
78 // CppUnit::assertion_traits<> to use CPPUNIT_ASSERT_EQUAL for VEC_DT_IT_
79 namespace CppUnit
80 {
81   struct EQ_DT_IT_
82   {
83     bool operator() (const DT_IT_ &p1, const DT_IT_ &p2) const
84     {
85       return p1.dt == p2.dt && p1.it == p2.it;
86     }
87   };
88   template <>
89   struct assertion_traits<MEDMEM::VEC_DT_IT_>
90   {  
91     static bool equal( MEDMEM::VEC_DT_IT_ x, MEDMEM::VEC_DT_IT_ y )
92     {
93       return x.size() == y.size() && std::equal(x.begin(),x.end(), y.begin(), EQ_DT_IT_());
94     }
95
96     static std::string toString( const MEDMEM::VEC_DT_IT_& x )
97     {
98       MEDMEM::STRING ost("{ ");
99       for ( unsigned i = 0; i < x.size(); ++i )
100         ost << "{ " << x[i].dt << ", " << x[i].it << " }" << ( i+1==x.size() ? " " : ", ");
101       return ost << "}";
102     }
103   };
104 }
105
106 void MEDMEMTest::testMedFileBrowser()
107 {
108   string filename = getResourceFile("pointe.med");
109   string filename_21 = getResourceFile("pointe_V21.med");
110   string filename_inexist = "InexistentFile.med";
111   string filename_otherFormat = getResourceFile("test_2D.sauve");
112
113   vector<string> names;
114
115   // 1. constructor MEDFILEBROWSER()
116   // --------------------------------
117   MEDFILEBROWSER myMed;
118   check_bad_file( myMed );
119
120   // 2. Constructor MEDFILEBROWSER (const std::string & fileName) throw (MEDEXCEPTION)
121   // ----------------------------------------------------------------------------------
122
123   // Existent med file (pointe.med)
124   CPPUNIT_ASSERT_NO_THROW( MEDFILEBROWSER myMed( filename ));
125
126   // Inexistent med file
127   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_inexist ), MEDEXCEPTION); 
128
129   // Unsupported version med file
130   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_21 ), MEDEXCEPTION); 
131
132   // Not med file
133   CPPUNIT_ASSERT_THROW( MEDFILEBROWSER myMed( filename_otherFormat ), MEDEXCEPTION); 
134
135   // 3. void readFileStruct(const std::string & fileName) throw (MEDEXCEPTION)
136   // --------------------------------------------------------------------------
137   MEDFILEBROWSER pointe_med, empty_med, ko_med;
138   
139   // Inexistent med file
140   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_inexist ), MEDEXCEPTION);
141   check_bad_file( ko_med );
142
143   // Unsupported version med file
144   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_21 ), MEDEXCEPTION);
145   check_bad_file( ko_med );
146
147   // Not med file
148   CPPUNIT_ASSERT_THROW( ko_med.readFileStruct( filename_otherFormat ), MEDEXCEPTION);
149   check_bad_file( ko_med );
150
151   // Existent med file (pointe.med)
152   CPPUNIT_ASSERT_NO_THROW( pointe_med.readFileStruct( filename ));
153
154   // 4. std::string  getFileName() const
155   // ------------------------------------
156   CPPUNIT_ASSERT( empty_med.getFileName().empty() );
157
158   CPPUNIT_ASSERT_EQUAL( filename_otherFormat, ko_med.getFileName() );
159
160   CPPUNIT_ASSERT_EQUAL( filename, pointe_med.getFileName() );
161
162   // 5. int getNumberOfMeshes ( void ) const
163   // ----------------------------------------
164   int nbMeshes;
165   CPPUNIT_ASSERT_NO_THROW( nbMeshes = empty_med.getNumberOfMeshes());
166   CPPUNIT_ASSERT_EQUAL( 0, nbMeshes );
167   CPPUNIT_ASSERT_NO_THROW( nbMeshes = ko_med.getNumberOfMeshes());
168   CPPUNIT_ASSERT_EQUAL( 0, nbMeshes );
169   CPPUNIT_ASSERT_NO_THROW( nbMeshes = pointe_med.getNumberOfMeshes());
170   CPPUNIT_ASSERT_EQUAL( 1, nbMeshes );
171
172   // 6. int        getNumberOfFields ( void ) const;
173   // ------------------------------------------------
174   int nbFields;
175   CPPUNIT_ASSERT_NO_THROW( nbFields = empty_med.getNumberOfFields());
176   CPPUNIT_ASSERT_EQUAL( 0, nbFields );
177   CPPUNIT_ASSERT_NO_THROW( nbFields = ko_med.getNumberOfFields());
178   CPPUNIT_ASSERT_EQUAL( 0, nbFields );
179   CPPUNIT_ASSERT_NO_THROW( nbFields = pointe_med.getNumberOfFields());
180   CPPUNIT_ASSERT_EQUAL( 4, nbFields );
181
182   // 7. void       getMeshNames      ( std::string * meshNames ) const;
183   // -------------------------------------------------------------------
184   names.resize(2,"");
185   string emptyStr, maa1("maa1");
186
187   CPPUNIT_ASSERT_NO_THROW( empty_med.getMeshNames( & names[0] ));
188   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
189   CPPUNIT_ASSERT_NO_THROW( ko_med.getMeshNames( & names[0] ));
190   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
191   CPPUNIT_ASSERT_NO_THROW( pointe_med.getMeshNames( & names[0] ));
192   CPPUNIT_ASSERT_EQUAL( maa1, names[0] );
193   CPPUNIT_ASSERT_EQUAL( emptyStr, names[1] );
194   
195   //8. void       getFieldNames     ( std::string * fieldNames ) const;
196   // -------------------------------------------------------------------
197   names.clear();
198   names.resize(5,"");
199   string
200     fieldcelldoublescalar("fieldcelldoublescalar"),
201     fieldcelldoublevector("fieldcelldoublevector" ),
202     fieldnodedouble("fieldnodedouble"       ),
203     fieldnodeint("fieldnodeint"          );
204     
205   
206   CPPUNIT_ASSERT_NO_THROW( empty_med.getFieldNames( & names[0] ));
207   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
208   CPPUNIT_ASSERT_NO_THROW( ko_med.getFieldNames( & names[0] ));
209   CPPUNIT_ASSERT_EQUAL( emptyStr, names[0] );
210   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldNames( & names[0] ));
211   CPPUNIT_ASSERT_EQUAL( fieldcelldoublescalar, names[0] );
212   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevector, names[1] );
213   CPPUNIT_ASSERT_EQUAL( fieldnodedouble      , names[2] );
214   CPPUNIT_ASSERT_EQUAL( fieldnodeint         , names[3] );
215   CPPUNIT_ASSERT_EQUAL( emptyStr, names[4] );
216
217   //9. std::vector< std::string > getMeshNames () const;
218   // ----------------------------------------------------
219   CPPUNIT_ASSERT_NO_THROW( names = empty_med.getMeshNames() );
220   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
221   CPPUNIT_ASSERT_NO_THROW( names = ko_med.getMeshNames() );
222   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
223   CPPUNIT_ASSERT_NO_THROW( names = pointe_med.getMeshNames() );
224   CPPUNIT_ASSERT_EQUAL(1, int( names.size() ));
225   CPPUNIT_ASSERT_EQUAL(maa1, names[0] );
226
227   // 10. std::vector< std::string > getFieldNames() const;
228   // ------------------------------------------------------
229   CPPUNIT_ASSERT_NO_THROW( names = empty_med.getFieldNames() );
230   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
231   CPPUNIT_ASSERT_NO_THROW( names = ko_med.getFieldNames() );
232   CPPUNIT_ASSERT_EQUAL(0, int( names.size() ));
233   CPPUNIT_ASSERT_NO_THROW( names = pointe_med.getFieldNames() );
234   CPPUNIT_ASSERT_EQUAL(4, int( names.size() ));
235   CPPUNIT_ASSERT_EQUAL( fieldcelldoublescalar, names[0] );
236   CPPUNIT_ASSERT_EQUAL( fieldcelldoublevector, names[1] );
237   CPPUNIT_ASSERT_EQUAL( fieldnodedouble      , names[2] );
238   CPPUNIT_ASSERT_EQUAL( fieldnodeint         , names[3] );
239
240   // 11. bool isStructuredMesh(const std::string & meshName) const throw (MEDEXCEPTION);
241   // ------------------------------------------------------------------------------------
242
243   CPPUNIT_ASSERT_THROW( empty_med. isStructuredMesh( "meshName" ), MEDEXCEPTION);
244   CPPUNIT_ASSERT_THROW( ko_med.    isStructuredMesh( "meshName" ), MEDEXCEPTION);
245   CPPUNIT_ASSERT_THROW( pointe_med.isStructuredMesh( "meshName" ), MEDEXCEPTION);
246   CPPUNIT_ASSERT_NO_THROW( pointe_med.isStructuredMesh( "maa1" ));
247   CPPUNIT_ASSERT( !pointe_med.isStructuredMesh( "maa1" ));
248
249   // 12. med_type_champ getFieldType (const string & fieldName) const throw (MEDEXCEPTION) ;
250   // ----------------------------------------------------------------------------------------
251   CPPUNIT_ASSERT_THROW( empty_med.getFieldType( "fieldName" ), MEDEXCEPTION);
252   CPPUNIT_ASSERT_THROW( pointe_med.getFieldType( "fieldName" ), MEDEXCEPTION);
253   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldType( "fieldnodeint" ));
254   CPPUNIT_ASSERT_EQUAL( MED_REEL64, pointe_med.getFieldType( "fieldcelldoublescalar" ));
255   CPPUNIT_ASSERT_EQUAL( MED_REEL64, pointe_med.getFieldType( "fieldcelldoublevector" ));
256   CPPUNIT_ASSERT_EQUAL( MED_INT32, pointe_med.getFieldType( "fieldnodeint" ));
257
258   // 13. string getMeshName (const std::string & fieldName) const throw (MEDEXCEPTION) ;
259   // ------------------------------------------------------------------------------------
260   CPPUNIT_ASSERT_THROW( empty_med.getMeshName( "fieldnodeint" ), MEDEXCEPTION);
261   CPPUNIT_ASSERT_THROW( ko_med.getMeshName( "fieldnodeint" ), MEDEXCEPTION);
262   CPPUNIT_ASSERT_THROW( pointe_med.getMeshName( "fieldName" ), MEDEXCEPTION);
263   CPPUNIT_ASSERT_NO_THROW( pointe_med.getMeshName( "fieldnodeint" ));
264   names = pointe_med.getFieldNames();
265   for ( unsigned i = 0; i < names.size(); ++i )
266     CPPUNIT_ASSERT_EQUAL( maa1, pointe_med.getMeshName( names[i] ));
267
268   // 14. VEC_DT_IT_  getFieldIteration (const std::string & fieldName) const throw (MEDEXCEPTION)
269   // ---------------------------------------------------------------------------------------------
270   CPPUNIT_ASSERT_THROW( empty_med.getFieldIteration( "fieldnodeint" ), MEDEXCEPTION);
271   CPPUNIT_ASSERT_THROW( ko_med.getFieldIteration( "fieldnodeint" ), MEDEXCEPTION);
272   CPPUNIT_ASSERT_THROW( pointe_med.getFieldIteration( "" ), MEDEXCEPTION);
273   CPPUNIT_ASSERT_NO_THROW( pointe_med.getFieldIteration( "fieldnodeint" ));
274
275   VEC_DT_IT_ vec1dtit( 1 );
276   vec1dtit[0].dt = -1;
277   vec1dtit[0].it = -1;
278   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldcelldoublescalar" ) );
279   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldcelldoublevector" ) );
280   CPPUNIT_ASSERT_EQUAL( vec1dtit, pointe_med.getFieldIteration( "fieldnodeint" ) );
281   VEC_DT_IT_ vec3dtit( 3, vec1dtit[0] );
282   vec3dtit[1].dt = 1;
283   vec3dtit[2].dt = 2;
284   CPPUNIT_ASSERT_EQUAL( vec3dtit, pointe_med.getFieldIteration( "fieldnodedouble" ) );
285 }