1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "MEDMEMTest.hxx"
21 #include <cppunit/TestAssert.h>
23 #include <MEDMEM_VtkFieldDriver.hxx>
24 #include <MEDMEM_Field.hxx>
26 // use this define to enable lines, execution of which leads to Segmentation Fault
27 //#define ENABLE_FAULTS
29 // use this define to enable CPPUNIT asserts and fails, showing bugs
30 //#define ENABLE_FORCED_FAILURES
33 using namespace MEDMEM;
34 using namespace MED_EN;
37 * Check methods (14), defined in MEDMEM_VtkFieldDriver.hxx:
38 * template <class T> class VTK_FIELD_DRIVER : public GENDRIVER {
39 * //MUST BE PRIVATE, because it is impossible to set _ptrField after this constructor.
40 * //AND cannot compile, so maybe it is specially implemented to prevent usage of it.
41 * (!) template <class INTERLACING_TAG> VTK_FIELD_DRIVER();
42 * (+) template <class INTERLACING_TAG> VTK_FIELD_DRIVER
43 * (const string & fileName, FIELD<T, INTERLACING_TAG> * ptrField)
44 * (+) VTK_FIELD_DRIVER(const VTK_FIELD_DRIVER & fieldDriver)
45 * (+) ~VTK_FIELD_DRIVER()
46 * (+) void openConst() const throw (MEDEXCEPTION)
47 * (+) void openConstAppend() const throw (MEDEXCEPTION)
48 * (+) void open() throw (MEDEXCEPTION)
49 * (+) void openAppend() throw (MEDEXCEPTION)
50 * (+) void closeConst() const throw (MEDEXCEPTION)
52 * (+) void setFieldName(const string & fieldName);
53 * (+) string getFieldName() const;
54 * (+) void read (void) throw (MEDEXCEPTION);
55 * (+) void write(void) const throw (MEDEXCEPTION);
56 * (+) void writeAppend(void) const throw (MEDEXCEPTION);
59 void MEDMEMTest::testVtkFieldDriver()
61 FIELD<int> *aField = new FIELD<int>();
62 FIELD<double> *aField_1 = new FIELD<double>();
64 string filename_rd = getResourceFile("pointe.med");
65 string emptyfilename = "";
66 string fileNotExistsName = "/path_not_exists/file_not_exists.vtk";
67 string filename_wr = makeTmpFile( "myField_pointe.vtk" );
69 string fieldname_rd_int = "fieldnodeint";
70 string fieldname_wr_int = "myintfield";
71 string fieldname_rd_double = "fieldnodedouble";
72 string fieldname_wr_double = "mydoublefield";
74 // To remove tmp files from disk
75 MEDMEMTest_TmpFilesRemover aRemover;
76 aRemover.Register(filename_wr);
78 /////////////////////////////////////
79 // TEST1: Open not existing file //
80 /////////////////////////////////////
81 //Creation Invalid VtkFieldDriver (file not exist)
82 VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_1 = new VTK_FIELD_DRIVER<int>(fileNotExistsName, aField);
83 //Trying open not existing file
84 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->open(), MEDEXCEPTION);
85 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConst(), MEDEXCEPTION);
86 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_1->openConstAppend(), MEDEXCEPTION);
88 /////////////////////////////////////////////
89 // TEST2: Open file with empty file name //
90 /////////////////////////////////////////////
91 //Creation Invalid VtkFieldDriver (empty file name)
92 VTK_FIELD_DRIVER<int> *aInvalidVtkFieldDriver_2 = new VTK_FIELD_DRIVER<int>(emptyfilename, aField);
94 //Trying file with empty name
95 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->open(), MEDEXCEPTION);
96 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConst(), MEDEXCEPTION);
97 CPPUNIT_ASSERT_THROW(aInvalidVtkFieldDriver_2->openConstAppend(), MEDEXCEPTION);
99 ////////////////////////
100 // TEST2: Main test //
101 ////////////////////////
102 //Read Fields from file
103 MED_FIELD_RDONLY_DRIVER<int> *aMedRdFieldDriver22_int = new MED_FIELD_RDONLY_DRIVER<int>(filename_rd, aField);
104 aMedRdFieldDriver22_int->open();
105 aMedRdFieldDriver22_int->setFieldName(fieldname_rd_int);
106 aMedRdFieldDriver22_int->read();
107 aMedRdFieldDriver22_int->close();
108 MESH *mesh=new MESH(MED_DRIVER,filename_rd,"maa1");
109 aField->getSupport()->setMesh(mesh);
111 MED_FIELD_RDONLY_DRIVER<double> *aMedRdFieldDriver22_double = new MED_FIELD_RDONLY_DRIVER<double>(filename_rd, aField_1);
112 aMedRdFieldDriver22_double->open();
113 aMedRdFieldDriver22_double->setFieldName(fieldname_rd_double);
114 aMedRdFieldDriver22_double->read();
115 aMedRdFieldDriver22_double->close();
116 aField_1->getSupport()->setMesh(mesh);
117 mesh->removeReference();
119 CPPUNIT_ASSERT(aField);
121 //Creation correct VtkFieldDriver
122 VTK_FIELD_DRIVER<int> *aVtkFieldDriver_int = new VTK_FIELD_DRIVER<int>(filename_wr, aField);
125 CPPUNIT_ASSERT(aVtkFieldDriver_int);
127 //Test setFieldName() and getFieldName() methods
130 aVtkFieldDriver_int->setFieldName(fieldname_wr_int);
132 catch(MEDEXCEPTION &e)
134 CPPUNIT_FAIL(e.what());
138 CPPUNIT_FAIL("Unknown exception");
140 CPPUNIT_ASSERT_EQUAL(fieldname_wr_int, aVtkFieldDriver_int->getFieldName());
145 aVtkFieldDriver_int->open();
147 catch(MEDEXCEPTION &e)
149 CPPUNIT_FAIL(e.what());
153 CPPUNIT_FAIL("Unknown exception");
156 //Test write() method
159 aVtkFieldDriver_int->write();
161 catch(MEDEXCEPTION &e)
163 CPPUNIT_FAIL(e.what());
167 CPPUNIT_FAIL("Unknown exception");
170 //Test read() method for Vtk Field Driver
171 CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
173 //Test close() method
176 aVtkFieldDriver_int->close();
178 catch(MEDEXCEPTION &e)
180 CPPUNIT_FAIL(e.what());
184 CPPUNIT_FAIL("Unknown exception");
187 //Test openAppend() method
190 aVtkFieldDriver_int->close();
192 catch(MEDEXCEPTION &e)
194 CPPUNIT_FAIL(e.what());
198 CPPUNIT_FAIL("Unknown exception");
202 /////////////////////////////////////////////////////////
203 // TEST? Test openAppend() and writeAppend() methods //
204 /////////////////////////////////////////////////////////
206 //Creation correct VtkFieldDriver
207 VTK_FIELD_DRIVER<double> *aVtkFieldDriver_double = new VTK_FIELD_DRIVER<double>(filename_wr, aField_1);
210 CPPUNIT_ASSERT(aVtkFieldDriver_double);
212 //Test openAppend() method
215 aVtkFieldDriver_double->openAppend();
217 catch(MEDEXCEPTION &e)
219 CPPUNIT_FAIL(e.what());
223 CPPUNIT_FAIL("Unknown exception");
226 aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
228 //Test writeAppend() method
231 aVtkFieldDriver_double->writeAppend();
233 catch(MEDEXCEPTION &e)
235 CPPUNIT_FAIL(e.what());
239 CPPUNIT_FAIL("Unknown exception");
244 aVtkFieldDriver_double->close();
246 catch(MEDEXCEPTION &e)
248 CPPUNIT_FAIL(e.what());
252 CPPUNIT_FAIL("Unknown exception");
255 /////////////////////////
256 // TEST BINARY FORMAT //
257 /////////////////////////
259 DRIVERFACTORY::setVtkBinaryFormatForWriting( true );
261 //BINARY: Test open() method
264 aVtkFieldDriver_int->open();
266 catch(MEDEXCEPTION &e)
268 CPPUNIT_FAIL(e.what());
272 CPPUNIT_FAIL("Unknown exception");
275 //BINARY: Test write() method
278 aVtkFieldDriver_int->write();
280 catch(MEDEXCEPTION &e)
282 CPPUNIT_FAIL(e.what());
286 CPPUNIT_FAIL("Unknown exception");
289 //BINARY: Test read() method for Vtk Field Driver
290 CPPUNIT_ASSERT_THROW(aVtkFieldDriver_int->read(),MEDEXCEPTION);
292 //BINARY: Test close() method
295 aVtkFieldDriver_int->close();
297 catch(MEDEXCEPTION &e)
299 CPPUNIT_FAIL(e.what());
303 CPPUNIT_FAIL("Unknown exception");
306 //BINARY: Test openAppend() method
309 aVtkFieldDriver_int->close();
311 catch(MEDEXCEPTION &e)
313 CPPUNIT_FAIL(e.what());
317 CPPUNIT_FAIL("Unknown exception");
321 /////////////////////////////////////////////////////////
322 // BINARY: Test openAppend() and writeAppend() methods //
323 /////////////////////////////////////////////////////////
327 aVtkFieldDriver_double->openAppend();
329 catch(MEDEXCEPTION &e)
331 CPPUNIT_FAIL(e.what());
335 CPPUNIT_FAIL("Unknown exception");
338 aVtkFieldDriver_double->setFieldName(fieldname_wr_double);
340 //BINARY: Test writeAppend() method
343 aVtkFieldDriver_double->writeAppend();
345 catch(MEDEXCEPTION &e)
347 CPPUNIT_FAIL(e.what());
351 CPPUNIT_FAIL("Unknown exception");
356 aVtkFieldDriver_double->close();
358 catch(MEDEXCEPTION &e)
360 CPPUNIT_FAIL(e.what());
364 CPPUNIT_FAIL("Unknown exception");
367 DRIVERFACTORY::setVtkBinaryFormatForWriting( false );
369 VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_1;
371 //Test copy constructor
372 VTK_FIELD_DRIVER<int> aVtkFieldDriver_intCpy_2 (*aVtkFieldDriver_int);
374 CPPUNIT_ASSERT_EQUAL(aVtkFieldDriver_intCpy_2, *aVtkFieldDriver_int);
375 //Test (bool operator ==) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
376 CPPUNIT_ASSERT(aVtkFieldDriver_intCpy_2.GENDRIVER::operator== (*aVtkFieldDriver_int));
378 //Test (friend ostream & operator <<) defined in GENDRIVER class in MEDMEM_GenDriver.hxx
379 ostringstream ostr1, ostr2;
380 ostr1<<*aVtkFieldDriver_int;
381 ostr2<<aVtkFieldDriver_intCpy_2;
383 CPPUNIT_ASSERT(ostr1.str() != "");
384 CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
387 aField->removeReference();
388 aField_1->removeReference();
389 delete aInvalidVtkFieldDriver_1;
390 delete aInvalidVtkFieldDriver_2;
391 delete aMedRdFieldDriver22_int;
392 delete aMedRdFieldDriver22_double;
393 delete aVtkFieldDriver_int;
394 delete aVtkFieldDriver_double;