1 // Copyright (C) 2007-2013 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_AsciiFieldDriver.hxx"
24 #include "MEDMEM_STRING.hxx"
32 // use this define to enable lines, execution of which leads to Segmentation Fault
33 //#define ENABLE_FAULTS
35 // use this define to enable CPPUNIT asserts and fails, showing bugs
36 //#define ENABLE_FORCED_FAILURES
39 using namespace MEDMEM;
41 // #4: MEDMEM_AsciiFieldDriver.hxx } MEDMEMTest_AsciiFieldDriver.cxx
44 * Check methods (8), defined in MEDMEM_AsciiFieldDriver.hxx:
46 * (+) template<int N,unsigned int CODE> void fill(double *a, const double *b)
47 * (+) template<int N> bool compare(const double* a, const double* b)
48 * (+) template<> void fill<-1,0x3>(double *a, const double *b);
49 * (+) template<> bool compare<-1>(const double *a, const double *b);
51 * template <class T, int SPACEDIMENSION, unsigned int SORTSTRATEGY>
52 * class SDForSorting {
53 * (+) SDForSorting(const double *coords, const T* comp, int nbComponents);
54 * (+) SDForSorting(const SDForSorting& other);
55 * (+) ~SDForSorting();
56 * (+) bool operator< (const SDForSorting<T,SPACEDIMENSION,SORTSTRATEGY>& other) const;
57 * (+) void writeLine(ofstream& file) const;
61 * class ASCII_FIELD_DRIVER : public GENDRIVER {
63 * //MUST BE PRIVATE as there is no possibility to set _ptrField after this constructor usage
64 * (-) template <class INTERLACING_TAG> ASCII_FIELD_DRIVER();
66 * (+) template <class INTERLACING_TAG>
67 * ASCII_FIELD_DRIVER(const string & fileName, FIELD<T,INTERLACING_TAG> * ptrField,
68 * MED_EN::med_sort_direc direction=MED_EN::ASCENDING, const char *priority="");
69 * (+) ASCII_FIELD_DRIVER(const ASCII_FIELD_DRIVER<T>& other);
70 * (+) void open() throw (MEDEXCEPTION);
72 * (+) void read (void) throw (MEDEXCEPTION);
73 * (+) void write(void) const throw (MEDEXCEPTION);
74 * (+) GENDRIVER* copy() const;
77 void MEDMEMTest::testAsciiFieldDriver()
79 // read a mesh from a MED file
80 string filename = getResourceFile("pointe.med");
81 string meshname = "maa1";
82 string fieldname = "fieldcelldoublescalar";
84 string anyfile1 = makeTmpFile( "anyfile1" );
85 string SDFfilename = makeTmpFile( "myfile" );
86 ofstream aFile(SDFfilename.c_str());
88 // To remove tmp files from disk
89 MEDMEMTest_TmpFilesRemover aRemover;
90 aRemover.Register(anyfile1);
91 aRemover.Register(SDFfilename);
93 //Test SDForSorting class
95 double coord_1[10] = { 1.0, 2.0,
101 int comp_1[5] = {1, 3, 5, 7, 9};
102 SDForSorting<int, 2, 48> aSDF_1(coord_1, comp_1, 5);
104 SDForSorting<int, 2, 48> aSDFCpy_1 = SDForSorting<int, 2, 48>(aSDF_1);
105 CPPUNIT_ASSERT_EQUAL(aSDFCpy_1 < aSDF_1, false);
106 CPPUNIT_ASSERT_NO_THROW(aSDF_1.writeLine(aFile));
110 // template<> void MEDMEM::fill<-1,0x3>(double *a, const double *b)
112 // template<> bool MEDMEM::compare<-1>(const double *a, const double *b)
113 // declared in MEDMEM_AsciiFieldDriver.hxx,
114 // are implemented in MEDMEM_DriverFactory.cxx?
116 // template<int N,unsigned int CODE> void fill(double *a, const double *b)
120 double bb[3] = {1,2,3};
122 fill<2,198>(aa, bb); // ZYX // 11000110 // 012
123 //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with ZYX");
124 CPPUNIT_ASSERT_EQUAL(aa[0], bb[0]);
125 CPPUNIT_ASSERT_EQUAL(aa[1], bb[1]);
126 CPPUNIT_ASSERT_EQUAL(aa[2], bb[2]);
128 fill<2,210>(aa, bb); // ZXY // 11010010 // 102
129 //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with ZXY");
130 CPPUNIT_ASSERT_EQUAL(aa[0], bb[1]);
131 CPPUNIT_ASSERT_EQUAL(aa[1], bb[0]);
132 CPPUNIT_ASSERT_EQUAL(aa[2], bb[2]);
134 fill<2,228>(aa, bb); // XYZ // 11100100 // 210
135 //MEDMEMTest_DumpArray<double>(cout, aa, 3, "aa filled with XYZ");
136 CPPUNIT_ASSERT_EQUAL(aa[0], bb[2]);
137 CPPUNIT_ASSERT_EQUAL(aa[1], bb[1]);
138 CPPUNIT_ASSERT_EQUAL(aa[2], bb[0]);
141 // template<int N> bool compare(const double* a, const double* b)
145 double aa[8] = {1,1,1,1,1,1,1,1};
146 double bb[8] = {1,1,1,1,1,1,1,2};
147 CPPUNIT_ASSERT(compare<7>(aa, bb));
152 double aa[8] = {1,1,1,1,1,1,1,1};
153 double bb[8] = {2,1,1,1,1,1,1,1};
154 CPPUNIT_ASSERT(compare<7>(aa, bb));
159 double aa[8] = {2,1,1,1,1,1,1,1};
160 double bb[8] = {1,1,1,1,1,1,1,1};
161 CPPUNIT_ASSERT(!compare<7>(aa, bb));
166 double aa[8] = {1,1,1,1,1,1,1,1};
167 double bb[8] = {1,1,1,1,1,1,1,1};
168 CPPUNIT_ASSERT(!compare<7>(aa, bb));
173 double aa[8] = {2,1,1,1,1,1,1,1};
174 double bb[8] = {1,1,1,1,1,1,1,1};
175 CPPUNIT_ASSERT(!compare<-1>(aa, bb));
179 // Test ASCII_FIELD_DRIVER
180 FIELD<double> * aField1 = new FIELD<double> (MED_DRIVER, filename, fieldname);
181 const SUPPORT * aSupport = aField1->getSupport();
182 MESH * aMesh = new MESH(MED_DRIVER, filename, aSupport->getMeshName());
183 aSupport->setMesh(aMesh);
185 // create an ASCII driver for a field
186 ASCII_FIELD_DRIVER<double> * aDriver1 =
187 new ASCII_FIELD_DRIVER<double> (anyfile1, aField1, MED_EN::ASCENDING, "");
188 CPPUNIT_ASSERT(aDriver1);
190 CPPUNIT_ASSERT(aDriver1->getFileName() == anyfile1);
191 CPPUNIT_ASSERT(aDriver1->getAccessMode() == MED_EN::WRONLY);
193 // and write the field on disk
195 // must throw because the file is not opened
197 CPPUNIT_ASSERT_THROW(aDriver1->write(), MEDEXCEPTION);
201 // must throw because the file is opened
202 CPPUNIT_ASSERT_THROW(aDriver1->setFileName("anyfile2"), MEDEXCEPTION);
203 CPPUNIT_ASSERT_THROW(aDriver1->setFileName(anyfile1), MEDEXCEPTION);
205 CPPUNIT_ASSERT_THROW(aDriver1->open(), MEDEXCEPTION);
208 // must throw because it is a writeonly driver
209 CPPUNIT_ASSERT_THROW(aDriver1->read(), MEDEXCEPTION);
214 // must throw because the file is not opened
216 CPPUNIT_ASSERT_THROW(aDriver1->write(), MEDEXCEPTION);
218 //CPPUNIT_ASSERT_THROW(aDriver1->close(), MEDEXCEPTION);
219 CPPUNIT_ASSERT_NO_THROW(aDriver1->close()); // do not make troubles to the user
221 // check priority definition
222 int spaceDimension = aMesh->getSpaceDimension();
223 if (spaceDimension == 3) {
225 CPPUNIT_ASSERT_NO_THROW(ASCII_FIELD_DRIVER<double> aDriver2
226 ("anyfile2", aField1, MED_EN::ASCENDING, "XYZ"));
228 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver3
229 ("anyfile3", aField1, MED_EN::ASCENDING, "XYZX"), MEDEXCEPTION);
231 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver4
232 ("anyfile4", aField1, MED_EN::ASCENDING, "XY"), MEDEXCEPTION);
234 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver5
235 ("anyfile5", aField1, MED_EN::ASCENDING, "ABC"), MEDEXCEPTION);
237 else if (spaceDimension == 2) {
239 CPPUNIT_ASSERT_NO_THROW(ASCII_FIELD_DRIVER<double> aDriver2
240 ("anyfile2", aField1, MED_EN::ASCENDING, "XY"));
242 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver3
243 ("anyfile3", aField1, MED_EN::ASCENDING, "XYZ"), MEDEXCEPTION);
245 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver4
246 ("anyfile4", aField1, MED_EN::ASCENDING, "X"), MEDEXCEPTION);
248 CPPUNIT_ASSERT_THROW(ASCII_FIELD_DRIVER<double> aDriver5
249 ("anyfile5", aField1, MED_EN::ASCENDING, "AB"), MEDEXCEPTION);
252 CPPUNIT_FAIL("Cannot test ASCII_FIELD_DRIVER because file pointe.med"
253 " contains mesh of wrong dimension: must be 2 or 3");
257 ASCII_FIELD_DRIVER<double> aDriver1_Cpy1 = ASCII_FIELD_DRIVER<double> (*aDriver1);
259 //Test copy() function
260 ASCII_FIELD_DRIVER<double> *aDriver1_Cpy2 = (ASCII_FIELD_DRIVER<double>*)aDriver1->copy();
261 CPPUNIT_ASSERT(aDriver1_Cpy2);
262 delete aDriver1_Cpy2;
265 aField1->removeReference();
266 aMesh->removeReference();