Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_TopLevel.cxx
1 // Copyright (C) 2007-2013  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_TopLevel.hxx>
24 #include <MEDMEM_MedFieldDriver.hxx>
25 #include <MEDMEM_Field.hxx>
26
27 // use this define to enable lines, execution of which leads to Segmentation Fault
28 //#define ENABLE_FAULTS
29
30 // use this define to enable CPPUNIT asserts and fails, showing bugs
31 //#define ENABLE_FORCED_FAILURES
32
33 using namespace std;
34 using namespace MEDMEM;
35 using namespace MED_EN;
36
37 /*!
38  *  Check methods (6), defined in MEDMEM_TopLevel.hxx:
39  *  (+) MESH *readMeshInFile(const std::string& fileName, const std::string& meshName);
40  *  (+) template<class T> FIELD<T> *readFieldInFile
41  *                            (const std::string& fileName, const std::string& fieldName);
42  *  (+) void writeMeshToFile(const MESH *meshObj, const std::string& fileName);
43  *  (-) template<class T> void writeFieldToFile(const FIELD<T> *fieldObj, const std::string& fileName);
44  */
45 void MEDMEMTest::testTopLevel()
46 {
47   string filename_rd                = getResourceFile("pointe.med");
48   string filename22_rd              = getResourceFile("pointe.med");
49   string filenameMesh_wr            = makeTmpFile("myMesh.med");
50   string filenameMed_wr             = makeTmpFile("myMed.med");
51   string filenameField_wr           = makeTmpFile("myField.med");
52   string fileNotExist_rd            = "/notExist.med";
53   string fileNotExist_wr            = "/path_not_exist/file_not_exist.med";
54   string meshname                   = "maa1";
55   string meshname_not_exist         = "anymesh";
56   string fieldname_not_exist        = "anyfield";
57   string fieldname                  = "fieldnodedouble";
58
59   // To remove tmp files from disk
60   MEDMEMTest_TmpFilesRemover aRemover;
61   aRemover.Register(filenameMesh_wr);
62   aRemover.Register(filenameMed_wr);
63   aRemover.Register(filenameField_wr);
64
65   ///////////////////
66   //Test Read Part //
67   ///////////////////
68   {
69     CPPUNIT_ASSERT_THROW(readMeshInFile(fileNotExist_rd, meshname), MEDEXCEPTION);
70     CPPUNIT_ASSERT_THROW(readFieldInFile<double>(fileNotExist_rd, fieldname), MEDEXCEPTION);
71
72     //Trying read not existing mesh from file
73     CPPUNIT_ASSERT_THROW(readMeshInFile(filename_rd, meshname_not_exist), MEDEXCEPTION);
74
75     //Trying read not existing field from file
76     CPPUNIT_ASSERT_THROW(readFieldInFile<double>(filename_rd, fieldname_not_exist), MEDEXCEPTION);
77
78     //Test readMeshInFile() method
79     {
80       MESH * aMesh = NULL;
81       CPPUNIT_ASSERT_NO_THROW(aMesh = readMeshInFile(filename22_rd, meshname));
82       CPPUNIT_ASSERT(aMesh);
83       aMesh->removeReference();
84     }
85
86     //Test readFieldInFile() method
87     {
88       FIELD<double> * aField = NULL;
89       CPPUNIT_ASSERT_NO_THROW(aField = readFieldInFile<double>(filename22_rd, fieldname));
90       CPPUNIT_ASSERT(aField);
91       aField->removeReference();
92     }
93   }
94
95   ////////////////////
96   //Test Write Part //
97   ////////////////////
98   {
99     //Create a FIELD
100     FIELD<double> *aField_1 = new FIELD<double>();
101
102     MED_FIELD_RDONLY_DRIVER<double> *aMedRdFieldDriver22 =
103       new MED_FIELD_RDONLY_DRIVER<double>(filename22_rd, aField_1);
104     aMedRdFieldDriver22->setFieldName(fieldname);
105     aMedRdFieldDriver22->open();
106     aMedRdFieldDriver22->read();
107     aMedRdFieldDriver22->close();
108
109     //Create a MESH
110     MESH * aMesh = MEDMEMTest_createTestMesh();
111     CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, fileNotExist_wr), MEDEXCEPTION);
112
113     //Trying write mesh in the file with empty name
114     aMesh->setName("");
115     CPPUNIT_ASSERT_THROW(writeMeshToFile(aMesh, filenameField_wr), MEDEXCEPTION);
116
117     //Test writeMeshToFile() method
118     aMesh->setName(meshname);
119     CPPUNIT_ASSERT_NO_THROW(writeMeshToFile(aMesh, filenameMesh_wr));
120
121     aField_1->removeReference();
122     aMesh->removeReference();
123   }
124 }