Salome HOME
Merge from V6_main 01/04/2013
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_Family.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_define.hxx"
24 #include "MEDMEM_Family.hxx"
25 #include "MEDMEM_MedMeshDriver.hxx"
26 #include "MEDMEM_Mesh.hxx"
27 #include "MEDMEM_Support.hxx"
28
29 #include <sstream>
30 #include <cmath>
31
32 // use this define to enable lines, execution of which leads to Segmentation Fault
33 //#define ENABLE_FAULTS
34
35 // use this define to enable CPPUNIT asserts and fails, showing bugs
36 //#define ENABLE_FORCED_FAILURES
37
38 using namespace std;
39 using namespace MEDMEM;
40
41 // #13: MEDMEM_Family.hxx  }  MEDMEMTest_Family.cxx
42
43 /*!
44  *  Check methods (24), defined in MEDMEM_Family.hxx:
45  *  class FAMILY : virtual public SUPPORT {
46  *   (+)     FAMILY();
47  *   (+)     FAMILY(MESH* Mesh, int Identifier, string Name,
48  *                  int NumberOfAttribute, int *AttributeIdentifier,
49  *                  int *AttributeValue, string AttributeDescription,
50  *                  int NumberOfGroup,   string GroupName,
51  *                  int * MEDArrayNodeFamily, int ** MEDArrayCellFamily,
52  *                  int ** MEDArrayFaceFamily, int ** MEDArrayEdgeFamily);
53  *   (+)     FAMILY(const FAMILY & m);
54  *   (+)     FAMILY(const SUPPORT & s);
55  *   (+)     virtual ~FAMILY();
56  *
57  *           // Does not work (Segmentation Fault of Hang up)
58  *   (!)     FAMILY & operator=(const FAMILY &fam);
59  *
60  *   (+)     friend ostream & operator<<(ostream &os, FAMILY &my);
61  *   (+)     friend ostream & operator<<(ostream &os, const FAMILY &my);
62  *
63  *   (+)     bool build(MED_EN::medEntityMesh Entity,int **FamilyNumber);
64  *
65  *   (+)     inline void setIdentifier             (int Identifier);
66  *   (+)     inline void setNumberOfAttributes     (int NumberOfAttribute);
67  *   (+)     inline void setAttributesIdentifiers  (int * AttributeIdentifier);
68  *   (+)     inline void setAttributesValues       (int * AttributeValue);
69  *   (+)     inline void setAttributesDescriptions (string * AttributeDescription);
70  *   (+)     inline void setNumberOfGroups         (int NumberOfGroups);
71  *   (+)     inline void setGroupsNames            (string * GroupName);
72  *
73  *   (+)     inline int            getIdentifier()             const;
74  *   (+)     inline int            getNumberOfAttributes()     const;
75  *   (+)     inline const int *    getAttributesIdentifiers()  const;
76  *   (+)     inline const int *    getAttributesValues()       const;
77  *   (+)     inline const string * getAttributesDescriptions() const;
78  *   (+)     inline int            getNumberOfGroups()         const;
79  *   (+)     inline const string * getGroupsNames()            const;
80  *
81  *   (+)     inline int    getAttributeIdentifier(int i)  const;
82  *   (+)     inline int    getAttributeValue(int i)       const;
83  *   (+)     inline string getAttributeDescription(int i) const;
84  *   (+)     inline string getGroupName(int i)            const;
85  *  }
86  *
87  *  Use code of MEDMEM/test_copie_family.cxx
88  */
89 static void check_support(const SUPPORT * theSupport,
90                    string theName, string theDescr, MED_EN::medEntityMesh theEntity, int theNbTypes)
91 {
92   string aName  = theSupport->getName();
93   string aDescr = theSupport->getDescription();
94   MED_EN::medEntityMesh anEntity = theSupport->getEntity();
95   int aNbGeomTypes = theSupport->getNumberOfTypes();
96
97   CPPUNIT_ASSERT_EQUAL(theName, aName);
98   CPPUNIT_ASSERT_EQUAL(theDescr, aDescr);
99   CPPUNIT_ASSERT_EQUAL(theEntity, anEntity);
100   CPPUNIT_ASSERT_EQUAL(theNbTypes, aNbGeomTypes);
101 }
102
103 static void check_famille(const FAMILY * theFamily,
104                    string theName, string theDescr, MED_EN::medEntityMesh theEntity, int theNbTypes,
105                    int theID, int theNbAttributes, int theNbGroups)
106 {
107     check_support(theFamily, theName, theDescr, theEntity, theNbTypes);
108
109     // identifier
110     int id           = theFamily->getIdentifier();
111     int nbAttributes = theFamily->getNumberOfAttributes();
112     int nbGroups     = theFamily->getNumberOfGroups();
113
114     CPPUNIT_ASSERT_EQUAL(theID, id);
115     CPPUNIT_ASSERT_EQUAL(theNbAttributes, nbAttributes);
116     CPPUNIT_ASSERT_EQUAL(theNbGroups, nbGroups);
117 }
118
119 void MEDMEMTest::testFamily()
120 {
121   ///////////////////////////////////
122   // TEST 1: test_copie_family.cxx //
123   ///////////////////////////////////
124   string filename = getResourceFile("pointe.med");
125   string meshname = "maa1";
126
127   MESH * aMesh = new MESH;
128   aMesh->setName(meshname);
129   MED_MESH_RDONLY_DRIVER aMeshDriver (filename, aMesh);
130   aMeshDriver.setMeshName(meshname);
131   aMeshDriver.open();
132   aMeshDriver.read();
133   aMeshDriver.close();
134
135   const FAMILY * aFamily1 = aMesh->getFamily(MED_EN::MED_NODE, 1);
136
137   // SUPPORT attributes
138   string aName     = aFamily1->getName();
139   string aDescr    = aFamily1->getDescription();
140   MED_EN::medEntityMesh anEntity = aFamily1->getEntity();
141   int aNbGeomTypes = aFamily1->getNumberOfTypes();
142
143   // FAMILY attributes
144   int id           = aFamily1->getIdentifier();
145   int nbAttributes = aFamily1->getNumberOfAttributes();
146   int nbGroups     = aFamily1->getNumberOfGroups();
147
148   // Dump
149   ostringstream ostr1;
150   ostr1 << *aFamily1;
151   CPPUNIT_ASSERT(ostr1.str() != "");
152
153   check_famille(aFamily1,
154                 aName, aDescr, anEntity, aNbGeomTypes,
155                 id, nbAttributes, nbGroups);
156
157   // Copy constructor
158   FAMILY * aFamily2 = new FAMILY (* aFamily1);
159
160   // Dump
161   ostringstream ostr2;
162   ostr2 << *aFamily2;
163   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
164
165   // Compare supports
166   CPPUNIT_ASSERT(aFamily2->deepCompare(*aFamily1));
167
168   check_famille(aFamily2,
169                 aName, aDescr, anEntity, aNbGeomTypes,
170                 id, nbAttributes, nbGroups);
171
172   // One more copy
173   FAMILY * aFamily3 = new FAMILY (* aFamily2);
174   aFamily2->removeReference();
175
176   // Dump
177   ostringstream ostr3;
178   ostr3 << *aFamily3;
179   CPPUNIT_ASSERT(ostr1.str() == ostr3.str());
180
181   check_famille(aFamily3,
182                 aName, aDescr, anEntity, aNbGeomTypes,
183                 id, nbAttributes, nbGroups);
184
185
186   ////////////
187   // TEST 2 //
188   ////////////
189   {
190     // Prepare data
191
192     // attributes
193     int   newNumberOfAttribute = 3;
194     int * newAttributeIdentifier = new int[newNumberOfAttribute];
195     int * newAttributeValue = new int[newNumberOfAttribute];
196     string * newAttributeDescription = new string[newNumberOfAttribute];
197
198     char tmp [32];
199     for (int i = 0; i < newNumberOfAttribute; i++) {
200       newAttributeIdentifier[i] = i+1;
201       newAttributeValue[i] = (i+1)*10;
202       sprintf(tmp, "Attribut N° %d", i+1);
203       newAttributeDescription[i] = tmp;
204     }
205
206     // groups
207     int newNumberOfGroups = 4;
208     string * newGroups = new string[newNumberOfGroups];
209     for (int i = 0; i < newNumberOfGroups; i++) {
210       sprintf(tmp, "Group N° %d", i+1);
211       newGroups[i] = tmp;
212     }
213
214     // Change aFamily3
215
216     // attributes
217     aFamily3->setNumberOfAttributes(newNumberOfAttribute);
218     aFamily3->setAttributesIdentifiers(newAttributeIdentifier);
219     aFamily3->setAttributesValues(newAttributeValue);
220     aFamily3->setAttributesDescriptions(newAttributeDescription);
221
222     // groups
223     aFamily3->setNumberOfGroups(newNumberOfGroups);
224     aFamily3->setGroupsNames(newGroups, /*giveOwnership = */true);
225
226     // identifier
227     aFamily3->setIdentifier(77);
228
229     // check new parameters of aFamily3
230
231     // attributes
232     CPPUNIT_ASSERT_EQUAL(newNumberOfAttribute, aFamily3->getNumberOfAttributes());
233     const int *    newAttributeIdentifierBack  = aFamily3->getAttributesIdentifiers();
234     const int *    newAttributeValueBack       = aFamily3->getAttributesValues();
235     const string * newAttributeDescriptionBack = aFamily3->getAttributesDescriptions();
236     for (int i = 1; i <= newNumberOfAttribute; i++) {
237       sprintf(tmp, "Attribut N° %d", i);
238       string descr (tmp);
239
240       CPPUNIT_ASSERT_EQUAL(i    , aFamily3->getAttributeIdentifier(i));
241       CPPUNIT_ASSERT_EQUAL(i*10 , aFamily3->getAttributeValue(i));
242       CPPUNIT_ASSERT_EQUAL(descr, aFamily3->getAttributeDescription(i));
243
244       CPPUNIT_ASSERT_EQUAL(i    , newAttributeIdentifierBack[i - 1]);
245       CPPUNIT_ASSERT_EQUAL(i*10 , newAttributeValueBack[i - 1]);
246       CPPUNIT_ASSERT_EQUAL(descr, newAttributeDescriptionBack[i - 1]);
247     }
248
249     // next string is commented since this pointer is shared
250     // by Family3 and in future during deleting of Family3
251     // we recieve error
252     delete [] newAttributeDescription;
253
254     // groups
255     CPPUNIT_ASSERT_EQUAL(newNumberOfGroups, aFamily3->getNumberOfGroups());
256     const string * newGroupsBack = aFamily3->getGroupsNames();
257     for (int i = 1; i <= newNumberOfGroups; i++) {
258       sprintf(tmp, "Group N° %d", i);
259       string grp (tmp);
260
261       CPPUNIT_ASSERT_EQUAL(grp, aFamily3->getGroupName(i));
262       CPPUNIT_ASSERT_EQUAL(grp, newGroupsBack[i - 1]);
263     }
264
265     // identifier
266     CPPUNIT_ASSERT_EQUAL(77, aFamily3->getIdentifier());
267   }
268
269   /////////////////////////////////////////////////////
270   // TEST 3: check default constructor and operator= //
271   /////////////////////////////////////////////////////
272   {
273     FAMILY *aFamily4=new FAMILY;
274     *aFamily4 = (const FAMILY &)*aFamily3;
275     aFamily4->removeReference();
276   }
277
278   ///////////////////////////////////////////////////
279   // TEST 4: check construction from given support //
280   ///////////////////////////////////////////////////
281   {
282     SUPPORT *s1=new SUPPORT(*aFamily3);
283     FAMILY  *f1=new FAMILY(*s1);
284     CPPUNIT_ASSERT_EQUAL(0, f1->getIdentifier());
285     CPPUNIT_ASSERT_EQUAL(0, f1->getNumberOfAttributes());
286     CPPUNIT_ASSERT_EQUAL(0, f1->getNumberOfGroups());
287     CPPUNIT_ASSERT(s1->deepCompare(*f1));
288     s1->removeReference();
289     f1->removeReference();
290   }
291
292   aFamily3->removeReference();
293   aMesh->removeReference();
294
295   /////////////////////////////////////////////////////////////////
296   // TEST 5: check constructor, designed to use with med driver. //
297   /////////////////////////////////////////////////////////////////
298   {
299     MESH * aTestMesh = MEDMEMTest_createTestMesh();
300
301     // TestMesh families. Let's imagine that we would like to have
302     // the following nodes and elements distribution by families:
303
304     // NumberOfNodes = 19;
305     int aNodeFamily[19] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
306                            2, 2, 2, 2, 2, 2, 3, 3, 3};
307
308     // NumberOfCellTypes = 3;
309     // NumberOfCells[NumberOfCellTypes] = {12, 2, 2};
310     int aCellType1Family[12] = {4,4,4,4,4,5,5,5,5,5,5,5};
311     int aCellType2Family[ 2] = {5,6};
312     int aCellType3Family[ 2] = {6,6};
313     int * aCellFamily[3] = {aCellType1Family, aCellType2Family, aCellType3Family};
314
315     // NumberOfFaceTypes = 2;
316     // NumberOfFaces[NumberOfFaceTypes] = {4, 4};
317     int aFaceType1Family[4] = {7,7,7,7};
318     int aFaceType2Family[4] = {7,7,7,7};
319     int * aFaceFamily[3] = {aFaceType1Family, aFaceType2Family};
320
321     // No edges in aTestMesh
322     int ** anEdgeFamily = NULL;
323
324     // new families data
325     int attrId[2] = {1,2};
326     int attrVa[2] = {7,8};
327
328     string attrDescr1 ("Attribute 1 description");
329     string attrDescr2 ("Attribute 2 description");
330     string attrDescrEmpty (MED_COMMENT_SIZE - 23, ' ');
331     attrDescr1 += attrDescrEmpty;
332     attrDescr2 += attrDescrEmpty;
333     CPPUNIT_ASSERT(MED_COMMENT_SIZE == attrDescr1.length());
334     CPPUNIT_ASSERT(MED_COMMENT_SIZE == attrDescr2.length());
335     string attrDescr = attrDescr1 + attrDescr2;
336
337     string groupName1 ("Group_1");
338     string groupName2 ("Group_2");
339     string groupName3 ("Group_3");
340     string groupNameEmpty (MED_LNAME_SIZE - 7, ' ');
341     groupName1 += groupNameEmpty;
342     groupName2 += groupNameEmpty;
343     groupName3 += groupNameEmpty;
344     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName1.length());
345     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName2.length());
346     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName3.length());
347     string groupNames = groupName1 + groupName2 + groupName3;
348
349     // nodes family 1
350     FAMILY *aNodesF1=new FAMILY(aTestMesh, /*Identifier*/1, "Nodes 1", 
351                                 /*NumberOfAttribute*/2, attrId, attrVa, attrDescr,
352                                 /*NumberOfGroup*/3, groupNames,
353                                 aNodeFamily, aCellFamily, aFaceFamily, anEdgeFamily);
354
355
356     CPPUNIT_ASSERT_EQUAL(1, aNodesF1->getIdentifier());
357     CPPUNIT_ASSERT(strcmp("Nodes 1", aNodesF1->getName().c_str()) == 0);
358     CPPUNIT_ASSERT(MED_EN::MED_NODE == aNodesF1->getEntity());
359     CPPUNIT_ASSERT(!aNodesF1->isOnAllElements());
360     CPPUNIT_ASSERT_EQUAL(7, aNodesF1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
361
362     CPPUNIT_ASSERT_EQUAL(2, aNodesF1->getNumberOfAttributes());
363     CPPUNIT_ASSERT_EQUAL(1, aNodesF1->getAttributeIdentifier(1));
364     CPPUNIT_ASSERT_EQUAL(2, aNodesF1->getAttributeIdentifier(2));
365     CPPUNIT_ASSERT_EQUAL(7, aNodesF1->getAttributeValue(1));
366     CPPUNIT_ASSERT_EQUAL(8, aNodesF1->getAttributeValue(2));
367     CPPUNIT_ASSERT_EQUAL(attrDescr1, aNodesF1->getAttributeDescription(1));
368     CPPUNIT_ASSERT_EQUAL(attrDescr2, aNodesF1->getAttributeDescription(2));
369
370     CPPUNIT_ASSERT_EQUAL(3, aNodesF1->getNumberOfGroups());
371     CPPUNIT_ASSERT_EQUAL(groupName1, aNodesF1->getGroupName(1));
372     CPPUNIT_ASSERT_EQUAL(groupName2, aNodesF1->getGroupName(2));
373     CPPUNIT_ASSERT_EQUAL(groupName3, aNodesF1->getGroupName(3));
374     aNodesF1->removeReference();
375
376     // faces family 7
377     FAMILY *aFacesF7=new FAMILY(aTestMesh, /*Identifier*/7, "Faces All", 
378                                 /*NumberOfAttribute*/2, attrId, attrVa, attrDescr,
379                                 /*NumberOfGroup*/3, groupNames,
380                                 aNodeFamily, aCellFamily, aFaceFamily, anEdgeFamily);
381
382
383     CPPUNIT_ASSERT_EQUAL(7, aFacesF7->getIdentifier());
384     CPPUNIT_ASSERT(strcmp("Faces All", aFacesF7->getName().c_str()) == 0);
385     CPPUNIT_ASSERT(MED_EN::MED_FACE == aFacesF7->getEntity());
386
387     CPPUNIT_ASSERT_EQUAL(8, aTestMesh->getNumberOfElements(MED_EN::MED_FACE,
388                                                            MED_EN::MED_ALL_ELEMENTS));
389     CPPUNIT_ASSERT_EQUAL(8, aFacesF7->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
390
391     CPPUNIT_ASSERT(!aFacesF7->isOnAllElements());
392
393     CPPUNIT_ASSERT_EQUAL(2, aFacesF7->getNumberOfAttributes());
394     CPPUNIT_ASSERT_EQUAL(1, aFacesF7->getAttributeIdentifier(1));
395     CPPUNIT_ASSERT_EQUAL(2, aFacesF7->getAttributeIdentifier(2));
396     CPPUNIT_ASSERT_EQUAL(7, aFacesF7->getAttributeValue(1));
397     CPPUNIT_ASSERT_EQUAL(8, aFacesF7->getAttributeValue(2));
398     CPPUNIT_ASSERT_EQUAL(attrDescr1, aFacesF7->getAttributeDescription(1));
399     CPPUNIT_ASSERT_EQUAL(attrDescr2, aFacesF7->getAttributeDescription(2));
400
401     CPPUNIT_ASSERT_EQUAL(3, aFacesF7->getNumberOfGroups());
402     CPPUNIT_ASSERT_EQUAL(groupName1, aFacesF7->getGroupName(1));
403     CPPUNIT_ASSERT_EQUAL(groupName2, aFacesF7->getGroupName(2));
404     CPPUNIT_ASSERT_EQUAL(groupName3, aFacesF7->getGroupName(3));
405     aFacesF7->removeReference();
406     aTestMesh->removeReference();
407
408     // Method build() is not tested directly, but it is called from constructor, tested here
409   }
410 }