Salome HOME
c9d7c20d10808c92ffe7de3aac87782ddbd91440
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_Support_fault.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_Mesh.hxx"
24 #include "MEDMEM_STRING.hxx"
25 #include "MEDMEM_Support.hxx"
26
27 #include <sstream>
28 #include <cmath>
29
30 // use this define to enable lines, execution of which leads to Segmentation Fault
31 //#define ENABLE_FAULTS
32
33 // use this define to enable CPPUNIT asserts and fails, showing bugs
34 //#define ENABLE_FORCED_FAILURES
35
36 using namespace std;
37 using namespace MEDMEM;
38
39 // #52: MEDMEM_Support.hxx  }  MEDMEMTest_Support.cxx
40
41 /*!
42  *  Check methods (48), defined in MEDMEM_Support.hxx:
43  *  class SUPPORT : public RCBASE 
44 {
45  *   (+)     SUPPORT();
46  *   (+)     SUPPORT(MESH* Mesh, string Name="", MED_EN::medEntityMesh Entity=MED_EN::MED_CELL);
47  *   (+)     SUPPORT(const SUPPORT & m);
48  *   (+)     virtual ~SUPPORT();
49  *
50  *   (+)     friend ostream & operator<<(ostream &os,const SUPPORT &my);
51  *   (+)     SUPPORT& operator=(const SUPPORT &support);
52  *   (+)     bool operator == (const SUPPORT &support) const;
53  *   (+)     bool deepCompare(const SUPPORT &support) const;
54  *
55  *   (+)     void update();
56  *
57  *   (+)     inline void setName(string Name);
58  *   (+)     inline void setDescription(string Description);
59  *   (+)     void setMesh(MESH *Mesh) const;
60  *   (+)     inline void setMeshName(const string & meshName);
61  *   (+)     inline void setAll(bool All);
62  *   (+)     inline void setEntity(MED_EN::medEntityMesh Entity);
63  *
64  *   (+)     inline void setNumberOfGeometricType(int NumberOfGeometricType);
65  *   (+)     inline void setGeometricType(const MED_EN::medGeometryElement *GeometricType);
66  *   (+)     inline void setNumberOfElements(const int *NumberOfElements);
67  *   (+)     inline void setTotalNumberOfElements(int TotalNumberOfElements);
68  *   (+)     inline void setNumber(MEDSKYLINEARRAY * Number);
69  *   (+)     inline void setNumber(const int * index, const int* value, bool shallowCopy=false);
70  *
71  *   (+)     inline string getName() const;
72  *   (+)     inline string getDescription() const;
73  *   (+)     virtual inline MESH * getMesh() const;
74  *   (+)     string getMeshName() const;
75  *   (+)     inline MED_EN::medEntityMesh getEntity() const;
76  *   (+)     inline bool   isOnAllElements() const;
77  *
78  *   (+)     inline int getNumberOfTypes() const;
79  *   (+)     inline const MED_EN::medGeometryElement* getTypes() const;
80  *   (+)     inline int getNumberOfElements
81  *                      (MED_EN::medGeometryElement GeometricType) const throw (MEDEXCEPTION);
82  *   (+)     inline const int * getNumberOfElements() const throw (MEDEXCEPTION);
83  *   (+)     virtual inline MEDSKYLINEARRAY * getnumber() const throw (MEDEXCEPTION);
84  *   (NO METHOD IN THIS BRANCH) virtual inline MEDSKYLINEARRAY * getnumberFromFile() const throw (MEDEXCEPTION);
85  *   (+)     virtual inline const int * getNumber
86  *                   (MED_EN::medGeometryElement GeometricType) const throw (MEDEXCEPTION);
87  *   (NO METHOD IN THIS BRANCH) virtual inline const int * getNumberFromFile
88  *                   (MED_EN::medGeometryElement GeometricType) const throw (MEDEXCEPTION);
89  *   (+)     virtual inline const int * getNumberIndex() const throw (MEDEXCEPTION);
90  *   (+)     virtual int getValIndFromGlobalNumber(const int number) const throw (MEDEXCEPTION);
91  *
92  *   (+)     void blending(SUPPORT * mySupport) throw (MEDEXCEPTION);
93  *
94  *   (+)     void setpartial(string Description, int NumberOfGeometricType,
95  *                           int TotalNumberOfEntity, MED_EN::medGeometryElement *GeometricType,
96  *                           int *NumberOfEntity, int *NumberValue);
97  *   (+)     void setpartial(MEDSKYLINEARRAY * number, bool shallowCopy=false) throw (MEDEXCEPTION);
98  *   (NO METHOD IN THIS BRANCH) void setpartial_fromfile(MEDSKYLINEARRAY * number, bool shallowCopy=false) throw (MEDEXCEPTION);
99  *
100  *   (+)     void   setProfilNames(vector<string> profilNames) throw (MEDEXCEPTION);
101  *   (+)     vector<string> getProfilNames() const throw (MEDEXCEPTION);
102  *
103  *   (+)     void getBoundaryElements() throw (MEDEXCEPTION);
104  *
105  *   (+)     void changeElementsNbs(MED_EN::medEntityMesh entity, const int *renumberingFromOldToNew);
106  *   (+)     void intersecting(SUPPORT * mySupport) throw (MEDEXCEPTION);
107  *   (+)     bool belongsTo(const SUPPORT& other, bool deepCompare=false) const;
108  *
109  *   (+)     SUPPORT *getComplement() const;
110  *   (+)     SUPPORT *substract(const SUPPORT& other) const throw (MEDEXCEPTION);
111  *
112  *   (+)     SUPPORT *getBoundaryElements(MED_EN::medEntityMesh Entity) const throw (MEDEXCEPTION);
113  *
114  *   (+)     void fillFromNodeList(const list<int>& listOfNode) throw (MEDEXCEPTION);
115  *   (+)     void fillFromElementList(const list<int>& listOfElt) throw (MEDEXCEPTION);
116  *
117  *   (+)     void clearDataOnNumbers();
118  *
119  *   (reference counter presently disconnected in C++) virtual void addReference() const;
120  *   (reference counter presently disconnected in C++) virtual void removeReference() const;
121  *  
122 }
123  */
124 static void MEDMEMTest_testSupport()
125 {
126   MESH * aMesh = MEDMEMTest_createTestMesh();
127   MESH * aMeshOneMore = MEDMEMTest_createTestMesh();
128
129   ////////////
130   // TEST 1 //
131   ////////////
132
133   SUPPORT *aSupportOnFaces1 = new SUPPORT;
134   aSupportOnFaces1->setMesh( aMesh );
135   aSupportOnFaces1->setName( "Support On Faces 1" );
136   aSupportOnFaces1->setEntity( MED_EN::MED_FACE );
137   aSupportOnFaces1->setAll( true );
138   aSupportOnFaces1->update();
139
140   SUPPORT *aSupportOnCells1 = new SUPPORT;
141   aSupportOnCells1->setMesh( aMesh );
142   aSupportOnCells1->setName( "Support On Cells 1" );
143   aSupportOnFaces1->setAll( true );
144   aSupportOnFaces1->update();
145
146   SUPPORT *aSupportOnFaces2=new SUPPORT;
147   SUPPORT *aSupportOnCells2=new SUPPORT;
148
149   SUPPORT *aSupportOnFaces3 = new SUPPORT;
150   aSupportOnFaces3->setMesh( aMeshOneMore );
151   aSupportOnFaces3->setName( "Support On Faces 3" );
152   aSupportOnFaces3->setEntity( MED_EN::MED_FACE );
153   aSupportOnFaces1->setAll( true );
154   aSupportOnFaces1->update();
155
156   // entity
157   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aSupportOnFaces1->getEntity());
158   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_CELL, aSupportOnCells1->getEntity());
159
160   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_CELL, aSupportOnFaces2->getEntity());
161   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_CELL, aSupportOnCells2->getEntity());
162
163   aSupportOnFaces2->setEntity(MED_EN::MED_FACE);
164   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aSupportOnFaces2->getEntity());
165
166   // isOnAllElements
167   CPPUNIT_ASSERT(aSupportOnFaces1->isOnAllElements());
168   CPPUNIT_ASSERT(aSupportOnCells1->isOnAllElements());
169
170   CPPUNIT_ASSERT(!aSupportOnFaces2->isOnAllElements());
171   CPPUNIT_ASSERT(!aSupportOnCells2->isOnAllElements());
172
173   // mesh and mesh name
174   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->setMeshName("Mesh 1"), MEDEXCEPTION);
175
176   aSupportOnFaces2->setMeshName("Mesh 1");
177   aSupportOnCells2->setMeshName("Mesh 1");
178   CPPUNIT_ASSERT(aSupportOnFaces2->getMeshName() == "Mesh 1");
179   CPPUNIT_ASSERT(aSupportOnCells2->getMeshName() == "Mesh 1");
180
181   aSupportOnFaces2->setMesh(aMesh);
182   CPPUNIT_ASSERT_THROW(aSupportOnFaces2->setMeshName("Mesh 1"), MEDEXCEPTION);
183
184   aSupportOnCells1->setMesh(NULL);
185   aSupportOnCells1->setMeshName("Mesh 1");
186
187   CPPUNIT_ASSERT(aSupportOnFaces1->getMeshName() == "meshing");
188   CPPUNIT_ASSERT(aSupportOnFaces2->getMeshName() == "meshing");
189   CPPUNIT_ASSERT(aSupportOnCells1->getMeshName() == "Mesh 1");
190   CPPUNIT_ASSERT(aSupportOnCells2->getMeshName() == "Mesh 1");
191
192   CPPUNIT_ASSERT(aSupportOnFaces1->getMesh() == aMesh);
193   CPPUNIT_ASSERT(aSupportOnFaces2->getMesh() == aMesh);
194   CPPUNIT_ASSERT(aSupportOnCells1->getMesh() == NULL);
195   CPPUNIT_ASSERT(aSupportOnCells2->getMesh() == NULL);
196
197   // operator ==
198   aSupportOnFaces2->setName("Support On Faces 2");
199   aSupportOnCells2->setName("Support On Cells 2");
200
201   // because aSupportOnFaces1 is on all elements, but aSupportOnFaces2 not
202   CPPUNIT_ASSERT(!(aSupportOnFaces1 == aSupportOnFaces2));
203
204   aSupportOnCells1->setAll(false);
205   CPPUNIT_ASSERT(!aSupportOnCells1->isOnAllElements());
206   // this is only sets flag _isOnAllElts to false, but not clear elements and types
207   // information, so aSupportOnCells1 is not equal to aSupportOnCells2 at this moment
208
209   //#ifdef ENABLE_FAULTS
210   // (BUG) SUPPORT::operator= gives "Segmentation Fault" on supports with NULL meshes!!!
211   CPPUNIT_ASSERT(!(aSupportOnCells1 == aSupportOnCells2));
212   //#endif
213   //#ifdef ENABLE_FORCED_FAILURES
214   //  CPPUNIT_FAIL("SUPPORT::operator= gives Segmentation Fault on supports with NULL meshes!!!");
215   //#endif
216
217   aSupportOnFaces2->setAll(true);
218   CPPUNIT_ASSERT(aSupportOnFaces2->isOnAllElements());
219   // flag _isOnAllElts now is set to true, but we need also to call update()
220   // to set elements and types information from mesh to this support
221   CPPUNIT_ASSERT(!(aSupportOnFaces1 == aSupportOnFaces2));
222   aSupportOnFaces2->update();
223   CPPUNIT_ASSERT(aSupportOnFaces1 == aSupportOnFaces2);
224
225   // deep compare
226   CPPUNIT_ASSERT(aSupportOnFaces1->deepCompare(*aSupportOnFaces2));
227
228   CPPUNIT_ASSERT(!(aSupportOnFaces1 == aSupportOnFaces3)); // different mesh pointers
229   CPPUNIT_ASSERT(aSupportOnFaces1->deepCompare(*aSupportOnFaces3)); // meshes are really equal
230
231   // description
232   aSupportOnFaces1->setDescription("Support on all faces with mesh pointer");
233   aSupportOnFaces2->setDescription("Support on all faces with mesh pointer");
234
235   aSupportOnCells1->setDescription("Support on a part of cells with mesh name");
236   aSupportOnCells2->setDescription("Support on a part of cells with mesh name");
237
238   CPPUNIT_ASSERT(aSupportOnFaces1->getDescription() == "Support on all faces with mesh pointer");
239   CPPUNIT_ASSERT(aSupportOnFaces2->getDescription() == "Support on all faces with mesh pointer");
240   CPPUNIT_ASSERT(aSupportOnCells1->getDescription() == "Support on a part of cells with mesh name");
241   CPPUNIT_ASSERT(aSupportOnCells2->getDescription() == "Support on a part of cells with mesh name");
242
243   // operator<<
244   ostringstream ostr1;
245   ostr1 << aSupportOnFaces1;
246   CPPUNIT_ASSERT(ostr1.str() != "");
247
248   ostringstream ostr2;
249   ostr2 << aSupportOnCells2;
250   CPPUNIT_ASSERT(ostr2.str() != "");
251
252   // set/get elements and types information
253
254   // 1. aSupportOnCells1 is not on all elements, but now it contains information
255   //    about all cells of aMesh, because it was created on mesh and with parameters.
256   CPPUNIT_ASSERT_EQUAL(3, aSupportOnCells1->getNumberOfTypes());
257   CPPUNIT_ASSERT(aSupportOnCells1->getTypes() != NULL);
258
259   aSupportOnCells1->setNumberOfGeometricType(0);
260   CPPUNIT_ASSERT_EQUAL(0, aSupportOnCells1->getNumberOfTypes());
261   CPPUNIT_ASSERT(aSupportOnCells1->getTypes() == NULL);
262
263   MED_EN::medGeometryElement aSCTypes[2] = 
264     {
265       MED_EN::MED_PYRA5, MED_EN::MED_PENTA15
266     };
267   aSupportOnCells1->setNumberOfGeometricType(2);
268   aSupportOnCells1->setGeometricType(aSCTypes);
269
270   CPPUNIT_ASSERT_EQUAL(2, aSupportOnCells1->getNumberOfTypes());
271   CPPUNIT_ASSERT(aSupportOnCells1->getTypes() != NULL);
272   CPPUNIT_ASSERT(aSupportOnCells1->getNumberOfElements() == NULL); // reset by setNumberOfGeometricType
273
274   int nbEltsSC[2] = 
275     {
276       2, 1
277     };
278   aSupportOnCells1->setNumberOfElements(nbEltsSC);
279
280   const int * nbEltsSCBack = aSupportOnCells1->getNumberOfElements();
281   CPPUNIT_ASSERT_EQUAL(2, aSupportOnCells1->getNumberOfElements(MED_EN::MED_PYRA5));
282   CPPUNIT_ASSERT_EQUAL(2, nbEltsSCBack[0]);
283   CPPUNIT_ASSERT_EQUAL(1, aSupportOnCells1->getNumberOfElements(MED_EN::MED_PENTA15));
284   CPPUNIT_ASSERT_EQUAL(1, nbEltsSCBack[1]);
285   CPPUNIT_ASSERT_EQUAL(3, aSupportOnCells1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
286
287   // old types
288   CPPUNIT_ASSERT_THROW(aSupportOnCells1->getNumberOfElements(MED_EN::MED_TETRA4), MEDEXCEPTION);
289   CPPUNIT_ASSERT_THROW(aSupportOnCells1->getNumberOfElements(MED_EN::MED_HEXA8), MEDEXCEPTION);
290
291   //_number= new MEDSKYLINEARRAY(_numberOfGeometricType,_totalNumberOfElements,index,value,shallowCopy);
292   int indexSC[3] = 
293     {
294       1,3,4
295     }; // length = nb.types + 1
296   int valueSC[3] = 
297     {
298       21,22,47
299     }; // length = total nb. of elements
300   aSupportOnCells1->setNumber(indexSC, valueSC);
301
302   // check number
303   MEDSKYLINEARRAY * aNumberSCBack = aSupportOnCells1->getnumber();
304
305   CPPUNIT_ASSERT_EQUAL(2, aNumberSCBack->getNumberOf());
306   CPPUNIT_ASSERT_EQUAL(3, aNumberSCBack->getLength());
307   const int * ind = aNumberSCBack->getIndex();
308   const int * val = aNumberSCBack->getValue();
309   CPPUNIT_ASSERT_EQUAL(1, ind[0]);
310   CPPUNIT_ASSERT_EQUAL(3, ind[1]);
311   CPPUNIT_ASSERT_EQUAL(4, ind[2]);
312   CPPUNIT_ASSERT_EQUAL(21, val[0]);
313   CPPUNIT_ASSERT_EQUAL(22, val[1]);
314   CPPUNIT_ASSERT_EQUAL(47, val[2]);
315
316   // 2. aSupportOnCells2 is not on all elements, and now it contains
317   //    no information about elements of a mesh
318   CPPUNIT_ASSERT_EQUAL(0, aSupportOnCells2->getNumberOfTypes());
319   CPPUNIT_ASSERT(aSupportOnCells2->getTypes() == NULL);
320
321   aSupportOnCells2->setNumberOfGeometricType(2);
322   aSupportOnCells2->setGeometricType(aSCTypes);
323
324   CPPUNIT_ASSERT_EQUAL(2, aSupportOnCells2->getNumberOfTypes());
325   CPPUNIT_ASSERT(aSupportOnCells2->getTypes() != NULL);
326
327   aSupportOnCells2->setNumberOfElements(nbEltsSC);
328
329   // we create MEDSKYLINEARRAY as pointer, because aSupportOnCells2 will
330   // become an owner of it, and we must not delete it.
331   MEDSKYLINEARRAY * aNumberSC = new MEDSKYLINEARRAY(2, 3, indexSC, valueSC);
332   aSupportOnCells2->setNumber(aNumberSC);
333
334   // check number
335   const int * aNumberIndexSC = aSupportOnCells2->getNumberIndex();
336   CPPUNIT_ASSERT_EQUAL(1, aNumberIndexSC[0]);
337   CPPUNIT_ASSERT_EQUAL(3, aNumberIndexSC[1]);
338   CPPUNIT_ASSERT_EQUAL(4, aNumberIndexSC[2]);
339
340   const int * aNbPYRA5 = aSupportOnCells2->getNumber(MED_EN::MED_PYRA5);
341   const int * aNbPENTA15 = aSupportOnCells2->getNumber(MED_EN::MED_PENTA15);
342   CPPUNIT_ASSERT_THROW(aSupportOnCells2->getNumber(MED_EN::MED_TETRA4), MEDEXCEPTION);
343
344   const int * aNbC = aSupportOnCells2->getNumber(MED_EN::MED_ALL_ELEMENTS);
345
346   CPPUNIT_ASSERT_EQUAL(21, aNbPYRA5[0]);
347   CPPUNIT_ASSERT_EQUAL(22, aNbPYRA5[1]);
348   CPPUNIT_ASSERT_EQUAL(47, aNbPENTA15[0]);
349
350   CPPUNIT_ASSERT_EQUAL(21, aNbC[0]);
351   CPPUNIT_ASSERT_EQUAL(22, aNbC[1]);
352   CPPUNIT_ASSERT_EQUAL(47, aNbC[2]);
353
354   // getValIndFromGlobalNumber
355   CPPUNIT_ASSERT_EQUAL(1, aSupportOnCells2->getValIndFromGlobalNumber(21));
356   CPPUNIT_ASSERT_EQUAL(2, aSupportOnCells2->getValIndFromGlobalNumber(22));
357   CPPUNIT_ASSERT_EQUAL(3, aSupportOnCells2->getValIndFromGlobalNumber(47));
358   // not existing element
359   CPPUNIT_ASSERT_THROW(aSupportOnCells2->getValIndFromGlobalNumber(10), MEDEXCEPTION);
360
361   // compare aSupportOnCells1 and aSupportOnCells2
362
363   // temporary set mesh (because of bug with operator==)
364   aSupportOnCells1->setMesh(aMesh);
365   aSupportOnCells2->setMesh(aMesh);
366
367   CPPUNIT_ASSERT_EQUAL(aSupportOnCells1, aSupportOnCells2);
368
369   // restore mesh name
370   aSupportOnCells1->setMesh(NULL);
371   aSupportOnCells2->setMesh(NULL);
372   aSupportOnCells1->setMeshName("Mesh 1");
373   aSupportOnCells2->setMeshName("Mesh 1");
374
375   // 3. aSupportOnFacesN is on all elements, and it contains
376   //    information about all faces of aMesh
377   CPPUNIT_ASSERT_EQUAL(2, aSupportOnFaces1->getNumberOfTypes());
378   const MED_EN::medGeometryElement* aSF1Types = aSupportOnFaces1->getTypes();
379   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_TRIA3, aSF1Types[0]);
380   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_QUAD4, aSF1Types[1]);
381   //#ifdef ENABLE_FORCED_FAILURES
382   // (BUG) Comment to method SUPPORT::getTypes() says:
383   // "If isOnAllElements is false, returns an array of %medGeometryElement types used by the support."
384   // But in fact it returns types in any case (comment to SUPPORT::setAll() gives more information).
385   //  CPPUNIT_FAIL("Comment and implementation does not match for SUPPORT::getTypes()");
386   // This concerns also method SUPPORT::update(), being called from constructor,
387   // which fills internal SUPPORT structures with information about mesh elements
388   // in case of isOnAllElements == true.
389   // Also comments and implementation does not match for SUPPORT::getNumberOfElements().
390   // It seems that in case "isOnAllElements == true" calls like getNumberOfElements()
391   // must be redirected to mesh, but what if mesh is not set (mesh name used instead)?.
392   //#endif
393   const int * nbEltsSF1 = aSupportOnFaces1->getNumberOfElements();
394   CPPUNIT_ASSERT_EQUAL(4, aSupportOnFaces1->getNumberOfElements(MED_EN::MED_TRIA3));
395   CPPUNIT_ASSERT_EQUAL(4, nbEltsSF1[0]);
396   CPPUNIT_ASSERT_EQUAL(4, aSupportOnFaces1->getNumberOfElements(MED_EN::MED_QUAD4));
397   CPPUNIT_ASSERT_EQUAL(4, nbEltsSF1[1]);
398   CPPUNIT_ASSERT_EQUAL(8, aSupportOnFaces1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
399   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumberOfElements(MED_EN::MED_TRIA6), MEDEXCEPTION);
400   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumberOfElements(MED_EN::MED_QUAD8), MEDEXCEPTION);
401
402   // check number
403   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumberIndex(), MEDEXCEPTION);
404   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumber(MED_EN::MED_TRIA3), MEDEXCEPTION);
405   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumber(MED_EN::MED_QUAD4), MEDEXCEPTION);
406   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getNumber(MED_EN::MED_ALL_ELEMENTS), MEDEXCEPTION);
407   CPPUNIT_ASSERT_THROW(aSupportOnFaces1->getnumber(), MEDEXCEPTION);
408
409   // getValIndFromGlobalNumber
410   CPPUNIT_ASSERT_EQUAL(8, aMesh->getNumberOfElements(MED_EN::MED_FACE, MED_EN::MED_ALL_ELEMENTS));
411   for (int i = 1; i <= 8; i++) 
412     {
413       CPPUNIT_ASSERT_EQUAL(i, aSupportOnFaces1->getValIndFromGlobalNumber(i));
414     }
415
416   //#ifdef ENABLE_FORCED_FAILURES
417   // Why methods setNumber do not throw exception in case "isOnAllElements == true",
418   // like methods getNumber do?
419   //CPPUNIT_ASSERT_THROW(aSupportOnFaces1->setNumber(indexSF1, valueSF1));
420   //#endif
421
422   // skl: setTotalNumberOfElements() is removed.
423   //#ifdef ENABLE_FORCED_FAILURES
424   // It seems to be dangerous and useless to have method SUPPORT::setTotalNumberOfElements()
425   //CPPUNIT_FAIL("It seems to be dangerous and useless to have method SUPPORT::setTotalNumberOfElements()");
426   //#endif
427
428   // following test is commented since method
429   // setTotalNumberOfElements() is removed.
430   /*
431   // setTotalNumberOfElements
432   aSupportOnFaces1->setTotalNumberOfElements(1000);
433   CPPUNIT_ASSERT_EQUAL(1000, aSupportOnFaces1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
434   */
435
436   // clearDataOnNumbers
437   aSupportOnCells1->clearDataOnNumbers();
438   CPPUNIT_ASSERT_EQUAL(0, aSupportOnCells1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
439   CPPUNIT_ASSERT_EQUAL(0, aSupportOnCells1->getNumberOfTypes());
440   CPPUNIT_ASSERT_THROW(aSupportOnCells1->getnumber(), MEDEXCEPTION);
441
442   ////////////
443   // TEST 2 //
444   ////////////
445
446   list<int> nodes137;
447   nodes137.push_back(1);
448   nodes137.push_back(3);
449   nodes137.push_back(7);
450
451   list<int> nodes248;
452   nodes248.push_back(2);
453   nodes248.push_back(4);
454   nodes248.push_back(8);
455
456   list<int> nodes27;
457   nodes27.push_back(2);
458   nodes27.push_back(7);
459
460   SUPPORT *aNodes137 = new SUPPORT;
461   SUPPORT *aNodes248 = new SUPPORT;
462   SUPPORT *aNodes27 = new SUPPORT;
463   aNodes137->setMesh( aMesh );
464   aNodes248->setMesh( aMesh );
465   aNodes27->setMesh( aMesh );
466   aNodes137->setName( "Support On Nodes 1,3,7" );
467   aNodes248->setName( "Support On Nodes 2,4,8" );
468   aNodes27->setName( "Support On Nodes 2,7" );
469
470   aNodes137->fillFromNodeList(nodes137);
471   aNodes248->fillFromNodeList(nodes248);
472   aNodes27->fillFromNodeList(nodes27);
473
474   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_NODE, aNodes137->getEntity());
475   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_NODE, aNodes248->getEntity());
476
477   list<int>::const_iterator iter137 = nodes137.begin();
478   list<int>::const_iterator iter248 = nodes248.begin();
479   for (int i = 1; i <= 3; i++, iter137++, iter248++) 
480     {
481       CPPUNIT_ASSERT_EQUAL(i, aNodes137->getValIndFromGlobalNumber(*iter137));
482       CPPUNIT_ASSERT_EQUAL(i, aNodes248->getValIndFromGlobalNumber(*iter248));
483     }
484
485   ////////////
486
487   list<int> faces123;
488   faces123.push_back(1);
489   faces123.push_back(2);
490   faces123.push_back(3);
491
492   list<int> faces135;
493   faces135.push_back(1);
494   faces135.push_back(3);
495   faces135.push_back(5);
496
497   SUPPORT *aFaces123 = new SUPPORT;
498   aFaces123->setMesh( aMesh );
499   aFaces123->setName( "Support On Faces 1,2,3" );
500   aFaces123->setEntity( MED_EN::MED_FACE );
501   SUPPORT *aFaces135 = new SUPPORT;
502   aFaces135->setMesh( aMesh );
503   aFaces135->setName( "Support On Faces 1,3,5" );
504   aFaces135->setEntity( MED_EN::MED_FACE );
505
506   aFaces123->fillFromElementList(faces123);
507   aFaces135->fillFromElementList(faces135);
508
509   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aFaces123->getEntity());
510   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aFaces135->getEntity());
511
512   list<int>::const_iterator iter123 = faces123.begin();
513   list<int>::const_iterator iter135 = faces135.begin();
514   for (int i = 1; i <= 3; i++, iter123++, iter135++) 
515     {
516       CPPUNIT_ASSERT_EQUAL(i, aFaces123->getValIndFromGlobalNumber(*iter123));
517       CPPUNIT_ASSERT_EQUAL(i, aFaces135->getValIndFromGlobalNumber(*iter135));
518     }
519
520   // substract
521   SUPPORT * aFaces2 = aFaces123->substract(*aFaces135); // => 2
522   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aFaces2->getEntity());
523   CPPUNIT_ASSERT_EQUAL(1, aFaces2->getValIndFromGlobalNumber(2));
524   CPPUNIT_ASSERT_EQUAL(1, aFaces2->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
525
526   CPPUNIT_ASSERT_THROW(aFaces123->substract(*aNodes137), MEDEXCEPTION);
527
528   //#ifdef ENABLE_FORCED_FAILURES
529   // (BUG) SUPPORT::substract() does not work with supports on nodes,
530   //       because it uses method MESH::buildSupportOnNodeFromElementList(),
531   //       which does not work if entity = MED_NODE
532   //SUPPORT * aNodes137mesh = aMesh->buildSupportOnNodeFromElementList(nodes137, MED_EN::MED_NODE);
533   //delete aNodes137mesh;
534   SUPPORT * aNodes137sub = aNodes137->substract(*aNodes248); // => 1,3,7
535   CPPUNIT_ASSERT_EQUAL(*aNodes137sub, *aNodes137);
536   aNodes137sub->removeReference();
537   //#endif
538
539   // belongsTo
540   CPPUNIT_ASSERT(aFaces2->belongsTo(*aFaces123));
541   CPPUNIT_ASSERT(!aFaces2->belongsTo(*aFaces135));
542
543   CPPUNIT_ASSERT(!aFaces2->belongsTo(*aSupportOnFaces3));
544   CPPUNIT_ASSERT(aFaces2->belongsTo(*aSupportOnFaces3, /*deepCompare*/true));
545
546   aFaces2->removeReference();
547
548   // getComplement
549   SUPPORT * aFaces_135 = aFaces135->getComplement(); // => 2,4,6,7,8
550   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_FACE, aFaces_135->getEntity());
551   CPPUNIT_ASSERT_EQUAL(5, aFaces_135->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
552
553   //#ifdef ENABLE_FORCED_FAILURES
554   // (BUG) Memory problem in SUPPORT::sub()
555   // Invalid read of size 4
556   //    at 0x34902492: MEDMEM::SUPPORT::sub(int, int, int const*, int) (MEDMEM_Support.cxx:852)
557   //    by 0x3490296B: MEDMEM::SUPPORT::getComplement() const (MEDMEM_Support.cxx:916)
558   //    by 0x34398F1E: MEDMEMTest::testSupport() (MEDMEMTest_Support.cxx:514)
559   //  Address 0x352DB91C is 0 bytes after a block of size 12 alloc'd
560   //    at 0x3414C8E2: operator new[](unsigned) (vg_replace_malloc.c:138)
561   //    by 0x3490243A: MEDMEM::SUPPORT::sub(int, int, int const*, int) (MEDMEM_Support.cxx:847)
562   //    by 0x3490296B: MEDMEM::SUPPORT::getComplement() const (MEDMEM_Support.cxx:916)
563   //    by 0x34398F1E: MEDMEMTest::testSupport() (MEDMEMTest_Support.cxx:514)
564   //  CPPUNIT_FAIL("Memory problem in SUPPORT::sub(): Invalid read of size 4 from array <temp>");
565   //#endif
566
567   //#ifdef ENABLE_FORCED_FAILURES
568   // (BUG) SUPPORT::getComplement() does not work with support on nodes,
569   //       because it uses method MESH::buildSupportOnNodeFromElementList(),
570   //       which does not work if entity = MED_NODE
571   SUPPORT * aNodes_137 = aNodes137->getComplement(); // => 2,4,5,6,8,9,10-19
572   CPPUNIT_ASSERT_EQUAL(MED_EN::MED_NODE, aNodes_137->getEntity());
573   CPPUNIT_ASSERT_EQUAL(16, aNodes_137->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
574   //#endif
575
576   // intersecting
577   aFaces_135->intersecting(aFaces123); // => 2
578   CPPUNIT_ASSERT_EQUAL(1, aFaces_135->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
579   CPPUNIT_ASSERT_EQUAL(1, aFaces_135->getValIndFromGlobalNumber(2));
580
581   //aNodes_137->intersecting(&aNodes248); // => 2,4,8
582   //CPPUNIT_ASSERT_EQUAL(3, aNodes_137->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
583   //CPPUNIT_ASSERT_EQUAL(*aNodes_137, aNodes248);
584   aNodes137->intersecting(aNodes248); // => 
585   {
586   }
587   CPPUNIT_ASSERT_EQUAL(0, aNodes137->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
588
589   // blending
590   aFaces_135->blending(aFaces135); // => 1,2,3,5
591   CPPUNIT_ASSERT_EQUAL(4, aFaces_135->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
592
593   aNodes248->blending(aNodes27); // => 2,4,7,8
594   CPPUNIT_ASSERT_EQUAL(4, aNodes248->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
595
596   // operator=
597   SUPPORT *aCopy1=new SUPPORT;
598   *aCopy1 = *aNodes248;
599   CPPUNIT_ASSERT_EQUAL(*aNodes248, *aCopy1);
600
601   // copy constructor
602   SUPPORT *aCopy2=new SUPPORT(*aFaces135);
603   CPPUNIT_ASSERT_EQUAL(*aFaces135, *aCopy2);
604
605   aFaces_135->removeReference();
606   //#ifdef ENABLE_FORCED_FAILURES
607   aNodes_137->removeReference();
608   //#endif
609
610   // profil names
611   vector<string> aPrN2 (2); // nb. types
612   aPrN2[0] = "Profil 1";
613   aPrN2[1] = "Profil 2";
614   aFaces135->setProfilNames(aPrN2);
615
616   // because on all elements
617   CPPUNIT_ASSERT_THROW(aSupportOnFaces3->setProfilNames(aPrN2), MEDEXCEPTION);
618
619   // because length of vector is not equal to nb.types
620   vector<string> aPrN3 (3);
621   CPPUNIT_ASSERT_THROW(aFaces135->setProfilNames(aPrN3), MEDEXCEPTION);
622
623   vector<string> aPrN2Back = aFaces135->getProfilNames();
624   CPPUNIT_ASSERT_EQUAL(aPrN2[0], aPrN2Back[0]);
625   CPPUNIT_ASSERT_EQUAL(aPrN2[1], aPrN2Back[1]);
626
627   //#ifdef ENABLE_FORCED_FAILURES
628   // (BUG) profil names are not copied by copy constructor and operator=,
629   // not compared by operator==, not dumped in stream by operator<<
630   //CPPUNIT_FAIL("Not complete implementation for Profil Names");
631   //#endif
632
633   ////////////
634   // TEST 3 //
635   ////////////
636
637   SUPPORT *aPartialCells = new SUPPORT;
638   aPartialCells->setMesh( aMesh );
639   aPartialCells->setName( "Support On Cells 2,3,4" );
640   aPartialCells->setEntity( MED_EN::MED_CELL );
641
642   SUPPORT *aPartialNodes = new SUPPORT;
643   aPartialNodes->setMesh( aMesh );
644   aPartialNodes->setName( "Support On Nodes 2,3,4" );
645   aPartialNodes->setEntity( MED_EN::MED_NODE );
646
647   // setpartial
648   MED_EN::medGeometryElement gtCells[1] = 
649     {
650       MED_EN::MED_TETRA4
651     };
652   int nbCells[1] = 
653     {
654       3
655     };
656   int cells[3] = 
657     {
658       2,3,4
659     };
660   aPartialCells->setpartial("with description", 1, 3, gtCells, nbCells, cells);
661   CPPUNIT_ASSERT_EQUAL(1, aPartialCells->getValIndFromGlobalNumber(2));
662   CPPUNIT_ASSERT_EQUAL(2, aPartialCells->getValIndFromGlobalNumber(3));
663   CPPUNIT_ASSERT_EQUAL(3, aPartialCells->getValIndFromGlobalNumber(4));
664
665   MED_EN::medGeometryElement gtNodes[1] = 
666     {
667       MED_EN::MED_NONE
668     };
669   aPartialNodes->setNumberOfGeometricType(1);
670   aPartialNodes->setGeometricType(gtNodes);
671   int indexSN[2] = 
672     {
673       1,4
674     };
675   int valueSN[3] = 
676     {
677       2,3,4
678     };
679   MEDSKYLINEARRAY * aNumberSN = new MEDSKYLINEARRAY(1, 3, indexSN, valueSN);
680   aPartialNodes->setpartial(aNumberSN, /*shallowCopy*/false);
681   delete aNumberSN;
682   CPPUNIT_ASSERT_EQUAL(1, aPartialNodes->getValIndFromGlobalNumber(2));
683   CPPUNIT_ASSERT_EQUAL(2, aPartialNodes->getValIndFromGlobalNumber(3));
684   CPPUNIT_ASSERT_EQUAL(3, aPartialNodes->getValIndFromGlobalNumber(4));
685
686   // changeElementsNbs
687
688   // {1,2,3,4,5,6,7,8,9,10} -> {10,9,8,7,6,5,4,3,2,1}
689   int renumberingFromOldToNew[10] = 
690     {
691       10,9,8,7,6,5,4,3,2,1
692     };
693
694   CPPUNIT_ASSERT_THROW(aPartialCells->changeElementsNbs
695                        (MED_EN::MED_NODE, renumberingFromOldToNew), MEDEXCEPTION);
696
697   aPartialCells->changeElementsNbs(MED_EN::MED_CELL, renumberingFromOldToNew);
698   CPPUNIT_ASSERT_EQUAL(3, aPartialCells->getValIndFromGlobalNumber(9));
699   CPPUNIT_ASSERT_EQUAL(2, aPartialCells->getValIndFromGlobalNumber(8));
700   CPPUNIT_ASSERT_EQUAL(1, aPartialCells->getValIndFromGlobalNumber(7));
701
702   // {1,2,3,4,5,6,7} -> {1,3,5,7,2,4,6}, {8,9,10,11} -> {8,10,9,11}
703   int renumberingFromOldToNewP[11] = 
704     {
705       1,3,5,7,2,4,6,8,10,9,11
706     };
707   aPartialCells->changeElementsNbs(MED_EN::MED_CELL, renumberingFromOldToNewP);
708   CPPUNIT_ASSERT_EQUAL(3, aPartialCells->getValIndFromGlobalNumber(10));
709   CPPUNIT_ASSERT_EQUAL(2, aPartialCells->getValIndFromGlobalNumber(8));
710   CPPUNIT_ASSERT_EQUAL(1, aPartialCells->getValIndFromGlobalNumber(6));
711
712   // this method does not suit for nodes renumbering!
713   // {1,2,3} -> {3,2,1}, {4,5,6,7} -> {5,6,7,4}
714   //int renumberingFromOldToNewN[3] = {3,2,1};
715   //int renumberingFromOldToNewPolyN[4] = {5,6,7,4};
716   //aPartialNodes.changeElementsNbs(MED_EN::MED_NODE, renumberingFromOldToNewN,
717   //                                3, renumberingFromOldToNewPolyN);
718
719   // getBoundaryElements
720   //#ifdef ENABLE_FAULTS
721   // (BUG) Segmentation Fault during SUPPORT::getBoundaryElements()
722   // ??? May be mesh is not complete ???
723   SUPPORT *aBoundaryFaces = new SUPPORT;
724   aBoundaryFaces->setMesh( aMesh );
725   aBoundaryFaces->setName( "meshing boundary faces" );
726   aBoundaryFaces->setEntity( MED_EN::MED_FACE );
727   //cout << "aBoundaryFaces:" << endl;
728   //cout << aBoundaryFaces << endl;
729   aBoundaryFaces->removeReference();
730
731   SUPPORT * aBoundaryNodes = aSupportOnFaces3->getBoundaryElements(MED_EN::MED_NODE);
732   //cout << "aSupportOnFaces3:" << endl;
733   //cout << aSupportOnFaces3 << endl;
734   //cout << "aBoundaryNodes:" << endl;
735   //cout << aBoundaryNodes << endl;
736   aBoundaryNodes->removeReference();
737   //#endif
738   //#ifdef ENABLE_FORCED_FAILURES
739   //  CPPUNIT_FAIL("Segmentation Fault during SUPPORT::getBoundaryElements()");
740   // Invalid write of size 4
741   //   at 0x3439DFCE: void std::_Construct<int, int>(int*, int const&) (stl_construct.h:81)
742   //   by 0x3476E4D1: std::vector<int, std::allocator<int> >::push_back(int const&) (stl_vector.h:560)
743   //   by 0x34763DDB: MEDMEM::CONNECTIVITY::calculateReverseNodalConnectivity() (MEDMEM_Connectivity.cxx:1155)
744   //   by 0x347655FF: MEDMEM::CONNECTIVITY::calculateDescendingConnectivity() (MEDMEM_Connectivity.cxx:1333)
745   //   by 0x3476363E: MEDMEM::CONNECTIVITY::getReverseDescendingConnectivity() (MEDMEM_Connectivity.cxx:1090)
746   //   by 0x34772AB6: MEDMEM::CONNECTIVITY::getReverseConnectivity(long, long) (MEDMEM_Connectivity.hxx:503)
747   //   by 0x348F4F4B: MEDMEM::MESH::getReverseConnectivity(long, long) const (MEDMEM_Mesh.hxx:848)
748   //   by 0x348FF559: MEDMEM::SUPPORT::getBoundaryElements() (MEDMEM_Support.cxx:483)
749   //   by 0x3439C8E5: MEDMEMTest::testSupport() (MEDMEMTest_Support.cxx:641)
750   //  Address 0x61666A2F is not stack'd, malloc'd or (recently) free'd
751   //#endif
752
753   delete aMesh;
754   delete aMeshOneMore;
755 }
756
757 int main (int argc, char** argv)
758 {
759   MEDMEMTest_testSupport();
760 }