1 // Copyright (C) 2006 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
23 #include <MEDMEM_TopLevel.hxx>
24 #include <MEDMEM_MedFieldDriver22.hxx>
25 #include <MEDMEM_Field.hxx>
26 #include <MEDMEM_Med.hxx>
28 // use this define to enable lines, execution of which leads to Segmentation Fault
29 //#define ENABLE_FAULTS
31 // use this define to enable CPPUNIT asserts and fails, showing bugs
32 #define ENABLE_FORCED_FAILURES
35 using namespace MEDMEM;
36 using namespace MED_EN;
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);
48 void MEDMEMTest::testTopLevel()
52 string data_dir = getenv("DATA_DIR");
53 string tmp_dir = getenv("TMP");
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";
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);
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
84 CPPUNIT_ASSERT_THROW(readMeshInFile(fileNotExist_rd, meshname), MEDEXCEPTION);
85 CPPUNIT_ASSERT_THROW(readFieldInFile<double>(fileNotExist_rd, fieldname), MEDEXCEPTION);
87 //Trying read not existing mesh from file
88 CPPUNIT_ASSERT_THROW(readMeshInFile(filename_rd, meshname_not_exist), MEDEXCEPTION);
90 //Trying read not existing field from file
91 CPPUNIT_ASSERT_THROW(readFieldInFile<double>(filename22_rd, fieldname_not_exist), MEDEXCEPTION);
93 //Test readMeshInFile() method
94 #ifdef ENABLE_FORCED_FAILURES
97 //MEDEXCEPTION: can not open file, but file exist
98 CPPUNIT_ASSERT_NO_THROW(aMesh = readMeshInFile(filename_rd, meshname));
99 CPPUNIT_ASSERT(aMesh);
104 //Test readMedInFile() method
106 CPPUNIT_ASSERT_NO_THROW(aMed = readMedInFile(filename_rd));
107 CPPUNIT_ASSERT(aMed);
110 //Test readFieldInFile() method
111 #ifdef ENABLE_FORCED_FAILURES
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);
127 FIELD<double> *aField_1 = new FIELD<double> ();
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();
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);
143 MESH * aMesh = MEDMEMTest_createTestMesh();
144 CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, fileNotExist_wr), MEDEXCEPTION);
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");
152 //Trying write mesh in the file with empty name
154 CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, filenameField_wr), MEDEXCEPTION);
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));