Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/med.git] / src / MEDMEM / Test / MEDMEMTest_TopLevel.cxx
1 // Copyright (C) 2006  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
8 //
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
22
23 #include <MEDMEM_TopLevel.hxx>
24 #include <MEDMEM_MedFieldDriver22.hxx>
25 #include <MEDMEM_Field.hxx>
26 #include <MEDMEM_Med.hxx>
27
28 // use this define to enable lines, execution of which leads to Segmentation Fault
29 //#define ENABLE_FAULTS
30
31 // use this define to enable CPPUNIT asserts and fails, showing bugs
32 #define ENABLE_FORCED_FAILURES
33
34 using namespace std;
35 using namespace MEDMEM;
36 using namespace MED_EN;
37
38 /*!
39  *  Check methods (6), defined in MEDMEM_TopLevel.hxx:
40  *  (+) MED *readMedInFile(const std::string& fileName) throw(MEDEXCEPTION);
41  *  (+) MESH *readMeshInFile(const std::string& fileName, const std::string& meshName);
42  *  (+) template<class T> FIELD<T> *readFieldInFile
43  *                            (const std::string& fileName, const std::string& fieldName);
44  *  (+) void writeMedToFile(const MED *medObj, const std::string& fileName);
45  *  (+) void writeMeshToFile(const MESH *meshObj, const std::string& fileName);
46  *  (-) template<class T> void writeFieldToFile(const FIELD<T> *fieldObj, const std::string& fileName);
47  */
48 void MEDMEMTest::testTopLevel()
49 {
50   MED * aMed = NULL;
51
52   string data_dir                   = getenv("DATA_DIR");
53   string tmp_dir                    = getenv("TMP");
54   if (tmp_dir == "")
55     tmp_dir = "/tmp";
56
57   string filename_rd                = data_dir + "/MedFiles/pointe.med";
58   string filename22_rd              = data_dir + "/MedFiles/pointe_import22.med";
59   string filenameMesh_wr            = tmp_dir + "/myMesh.med";
60   string filenameMed_wr             = tmp_dir + "/myMed.med";
61   string filenameField_wr           = tmp_dir + "/myField.med";
62   string fileNotExist_rd            = "/notExist.med";
63   string fileNotExist_wr            = "/path_not_exist/file_not_exist.med";
64   string meshname                   = "maa1";
65   string meshname_not_exist         = "anymesh";
66   string fieldname_not_exist        = "anyfield";
67   string fieldname                  = "fieldnodedouble";
68
69   // To remove tmp files from disk
70   MEDMEMTest_TmpFilesRemover aRemover;
71   aRemover.Register(filenameMesh_wr);
72   aRemover.Register(filenameMed_wr);
73   aRemover.Register(filenameField_wr);
74
75   ///////////////////
76   //Test Read Part //
77   ///////////////////
78   {
79     //Trying read from not existing file
80 #ifdef ENABLE_FORCED_FAILURES
81     CPPUNIT_ASSERT_THROW(readMedInFile(fileNotExist_rd), MEDEXCEPTION);
82     // (BUG) No exception in this case
83 #endif
84     CPPUNIT_ASSERT_THROW(readMeshInFile(fileNotExist_rd, meshname), MEDEXCEPTION);
85     CPPUNIT_ASSERT_THROW(readFieldInFile<double>(fileNotExist_rd, fieldname), MEDEXCEPTION);
86
87     //Trying read not existing mesh from file
88     CPPUNIT_ASSERT_THROW(readMeshInFile(filename_rd, meshname_not_exist), MEDEXCEPTION);
89
90     //Trying read not existing field from file
91     CPPUNIT_ASSERT_THROW(readFieldInFile<double>(filename22_rd, fieldname_not_exist), MEDEXCEPTION);
92
93     //Test readMeshInFile() method
94 #ifdef ENABLE_FORCED_FAILURES
95     {
96       MESH * aMesh = NULL;
97       //MEDEXCEPTION: can not open file, but file exist
98       CPPUNIT_ASSERT_NO_THROW(aMesh = readMeshInFile(filename_rd, meshname));
99       CPPUNIT_ASSERT(aMesh);
100       delete aMesh;
101     }
102 #endif
103
104     //Test readMedInFile() method
105     {
106       CPPUNIT_ASSERT_NO_THROW(aMed = readMedInFile(filename_rd));
107       CPPUNIT_ASSERT(aMed);
108     }
109
110     //Test readFieldInFile() method
111 #ifdef ENABLE_FORCED_FAILURES
112     {
113       FIELD<double> * aField = NULL;
114       //MEDEXCEPTION: can not open file, but file exist
115       CPPUNIT_ASSERT_NO_THROW(aField = readFieldInFile<double>(filename22_rd, fieldname));
116       CPPUNIT_ASSERT(aField);
117       delete aField;
118     }
119 #endif
120   }
121
122   ////////////////////
123   //Test Write Part //
124   ////////////////////
125   {
126     //Create a FIELD
127     FIELD<double> *aField_1 = new FIELD<double> ();
128
129     MED_FIELD_RDONLY_DRIVER22<double> *aMedRdFieldDriver22 =
130       new MED_FIELD_RDONLY_DRIVER22<double>(filename22_rd, aField_1);
131     aMedRdFieldDriver22->setFieldName(fieldname);
132     aMedRdFieldDriver22->open();
133     aMedRdFieldDriver22->read();
134     aMedRdFieldDriver22->close();
135
136     //Trying write objects in the not existing file
137 #ifdef ENABLE_FORCED_FAILURES
138     // (BUG) No exception in this case
139     CPPUNIT_ASSERT_THROW(writeMedToFile(aMed, fileNotExist_wr), MEDEXCEPTION);
140 #endif
141
142     //Create a MESH
143     MESH * aMesh = MEDMEMTest_createTestMesh();
144     CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, fileNotExist_wr), MEDEXCEPTION);
145
146 #ifdef ENABLE_FORCED_FAILURES
147     // (BUG) Error during compilation
148     //MED_SRC/src/MEDMEM/MEDMEM_TopLevel.hxx:66: error: passing `const MEDMEM::FIELD<double, MEDMEM::FullInterlace>' as `this' argument of `int MEDMEM::FIELD<T, INTERLACING_TAG>::addDriver(MEDMEM::driverTypes, const std::string&, const std::string&, MED_EN::med_mode_acces) [with T = double, INTERLACING_TAG = MEDMEM::FullInterlace]' discards qualifiers  CPPUNIT_ASSERT_THROW(writeFieldToFile<double>(aField_1, fileNotExist_wr),MEDEXCEPTION);
149     CPPUNIT_FAIL("writeFieldToFile(): compilation error");
150 #endif
151
152     //Trying write mesh in the file with empty name
153     aMesh->setName("");
154     CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, filenameField_wr), MEDEXCEPTION);
155
156     //Test writeMedToFile() and writeMeshToFile() methods
157     aMesh->setName(meshname);
158     CPPUNIT_ASSERT_NO_THROW(writeMeshToFile(aMesh, filenameMesh_wr));
159     CPPUNIT_ASSERT_NO_THROW(writeMedToFile(aMed, filenameMed_wr));
160
161     delete aField_1;
162     delete aMesh;
163     delete aMed;
164   }
165 }