Back to index

salome-med  6.5.0
MEDMEMTest_Family.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "MEDMEMTest.hxx"
00021 #include <cppunit/TestAssert.h>
00022 
00023 #include "MEDMEM_define.hxx"
00024 #include "MEDMEM_Family.hxx"
00025 #include "MEDMEM_MedMeshDriver.hxx"
00026 #include "MEDMEM_Mesh.hxx"
00027 #include "MEDMEM_Support.hxx"
00028 
00029 #include <sstream>
00030 #include <cmath>
00031 
00032 // use this define to enable lines, execution of which leads to Segmentation Fault
00033 //#define ENABLE_FAULTS
00034 
00035 // use this define to enable CPPUNIT asserts and fails, showing bugs
00036 //#define ENABLE_FORCED_FAILURES
00037 
00038 using namespace std;
00039 using namespace MEDMEM;
00040 
00041 // #13: MEDMEM_Family.hxx  }  MEDMEMTest_Family.cxx
00042 
00089 static void check_support(const SUPPORT * theSupport,
00090                    string theName, string theDescr, MED_EN::medEntityMesh theEntity, int theNbTypes)
00091 {
00092   string aName  = theSupport->getName();
00093   string aDescr = theSupport->getDescription();
00094   MED_EN::medEntityMesh anEntity = theSupport->getEntity();
00095   int aNbGeomTypes = theSupport->getNumberOfTypes();
00096 
00097   CPPUNIT_ASSERT_EQUAL(theName, aName);
00098   CPPUNIT_ASSERT_EQUAL(theDescr, aDescr);
00099   CPPUNIT_ASSERT_EQUAL(theEntity, anEntity);
00100   CPPUNIT_ASSERT_EQUAL(theNbTypes, aNbGeomTypes);
00101 }
00102 
00103 static void check_famille(const FAMILY * theFamily,
00104                    string theName, string theDescr, MED_EN::medEntityMesh theEntity, int theNbTypes,
00105                    int theID, int theNbAttributes, int theNbGroups)
00106 {
00107     check_support(theFamily, theName, theDescr, theEntity, theNbTypes);
00108 
00109     // identifier
00110     int id           = theFamily->getIdentifier();
00111     int nbAttributes = theFamily->getNumberOfAttributes();
00112     int nbGroups     = theFamily->getNumberOfGroups();
00113 
00114     CPPUNIT_ASSERT_EQUAL(theID, id);
00115     CPPUNIT_ASSERT_EQUAL(theNbAttributes, nbAttributes);
00116     CPPUNIT_ASSERT_EQUAL(theNbGroups, nbGroups);
00117 }
00118 
00119 void MEDMEMTest::testFamily()
00120 {
00122   // TEST 1: test_copie_family.cxx //
00124   string filename = getResourceFile("pointe.med");
00125   string meshname = "maa1";
00126 
00127   MESH * aMesh = new MESH;
00128   aMesh->setName(meshname);
00129   MED_MESH_RDONLY_DRIVER aMeshDriver (filename, aMesh);
00130   aMeshDriver.setMeshName(meshname);
00131   aMeshDriver.open();
00132   aMeshDriver.read();
00133   aMeshDriver.close();
00134 
00135   const FAMILY * aFamily1 = aMesh->getFamily(MED_EN::MED_NODE, 1);
00136 
00137   // SUPPORT attributes
00138   string aName     = aFamily1->getName();
00139   string aDescr    = aFamily1->getDescription();
00140   MED_EN::medEntityMesh anEntity = aFamily1->getEntity();
00141   int aNbGeomTypes = aFamily1->getNumberOfTypes();
00142 
00143   // FAMILY attributes
00144   int id           = aFamily1->getIdentifier();
00145   int nbAttributes = aFamily1->getNumberOfAttributes();
00146   int nbGroups     = aFamily1->getNumberOfGroups();
00147 
00148   // Dump
00149   ostringstream ostr1;
00150   ostr1 << *aFamily1;
00151   CPPUNIT_ASSERT(ostr1.str() != "");
00152 
00153   check_famille(aFamily1,
00154                 aName, aDescr, anEntity, aNbGeomTypes,
00155                 id, nbAttributes, nbGroups);
00156 
00157   // Copy constructor
00158   FAMILY * aFamily2 = new FAMILY (* aFamily1);
00159 
00160   // Dump
00161   ostringstream ostr2;
00162   ostr2 << *aFamily2;
00163   CPPUNIT_ASSERT(ostr1.str() == ostr2.str());
00164 
00165   // Compare supports
00166   CPPUNIT_ASSERT(aFamily2->deepCompare(*aFamily1));
00167 
00168   check_famille(aFamily2,
00169                 aName, aDescr, anEntity, aNbGeomTypes,
00170                 id, nbAttributes, nbGroups);
00171 
00172   // One more copy
00173   FAMILY * aFamily3 = new FAMILY (* aFamily2);
00174   aFamily2->removeReference();
00175 
00176   // Dump
00177   ostringstream ostr3;
00178   ostr3 << *aFamily3;
00179   CPPUNIT_ASSERT(ostr1.str() == ostr3.str());
00180 
00181   check_famille(aFamily3,
00182                 aName, aDescr, anEntity, aNbGeomTypes,
00183                 id, nbAttributes, nbGroups);
00184 
00185 
00187   // TEST 2 //
00189   {
00190     // Prepare data
00191 
00192     // attributes
00193     int   newNumberOfAttribute = 3;
00194     int * newAttributeIdentifier = new int[newNumberOfAttribute];
00195     int * newAttributeValue = new int[newNumberOfAttribute];
00196     string * newAttributeDescription = new string[newNumberOfAttribute];
00197 
00198     char tmp [32];
00199     for (int i = 0; i < newNumberOfAttribute; i++) {
00200       newAttributeIdentifier[i] = i+1;
00201       newAttributeValue[i] = (i+1)*10;
00202       sprintf(tmp, "Attribut N %d", i+1);
00203       newAttributeDescription[i] = tmp;
00204     }
00205 
00206     // groups
00207     int newNumberOfGroups = 4;
00208     string * newGroups = new string[newNumberOfGroups];
00209     for (int i = 0; i < newNumberOfGroups; i++) {
00210       sprintf(tmp, "Group N %d", i+1);
00211       newGroups[i] = tmp;
00212     }
00213 
00214     // Change aFamily3
00215 
00216     // attributes
00217     aFamily3->setNumberOfAttributes(newNumberOfAttribute);
00218     aFamily3->setAttributesIdentifiers(newAttributeIdentifier);
00219     aFamily3->setAttributesValues(newAttributeValue);
00220     aFamily3->setAttributesDescriptions(newAttributeDescription);
00221 
00222     // groups
00223     aFamily3->setNumberOfGroups(newNumberOfGroups);
00224     aFamily3->setGroupsNames(newGroups, /*giveOwnership = */true);
00225 
00226     // identifier
00227     aFamily3->setIdentifier(77);
00228 
00229     // check new parameters of aFamily3
00230 
00231     // attributes
00232     CPPUNIT_ASSERT_EQUAL(newNumberOfAttribute, aFamily3->getNumberOfAttributes());
00233     const int *    newAttributeIdentifierBack  = aFamily3->getAttributesIdentifiers();
00234     const int *    newAttributeValueBack       = aFamily3->getAttributesValues();
00235     const string * newAttributeDescriptionBack = aFamily3->getAttributesDescriptions();
00236     for (int i = 1; i <= newNumberOfAttribute; i++) {
00237       sprintf(tmp, "Attribut N %d", i);
00238       string descr (tmp);
00239 
00240       CPPUNIT_ASSERT_EQUAL(i    , aFamily3->getAttributeIdentifier(i));
00241       CPPUNIT_ASSERT_EQUAL(i*10 , aFamily3->getAttributeValue(i));
00242       CPPUNIT_ASSERT_EQUAL(descr, aFamily3->getAttributeDescription(i));
00243 
00244       CPPUNIT_ASSERT_EQUAL(i    , newAttributeIdentifierBack[i - 1]);
00245       CPPUNIT_ASSERT_EQUAL(i*10 , newAttributeValueBack[i - 1]);
00246       CPPUNIT_ASSERT_EQUAL(descr, newAttributeDescriptionBack[i - 1]);
00247     }
00248 
00249     // next string is commented since this pointer is shared
00250     // by Family3 and in future during deleting of Family3
00251     // we recieve error
00252     delete [] newAttributeDescription;
00253 
00254     // groups
00255     CPPUNIT_ASSERT_EQUAL(newNumberOfGroups, aFamily3->getNumberOfGroups());
00256     const string * newGroupsBack = aFamily3->getGroupsNames();
00257     for (int i = 1; i <= newNumberOfGroups; i++) {
00258       sprintf(tmp, "Group N %d", i);
00259       string grp (tmp);
00260 
00261       CPPUNIT_ASSERT_EQUAL(grp, aFamily3->getGroupName(i));
00262       CPPUNIT_ASSERT_EQUAL(grp, newGroupsBack[i - 1]);
00263     }
00264 
00265     // identifier
00266     CPPUNIT_ASSERT_EQUAL(77, aFamily3->getIdentifier());
00267   }
00268 
00270   // TEST 3: check default constructor and operator= //
00272   {
00273     FAMILY *aFamily4=new FAMILY;
00274     *aFamily4 = (const FAMILY &)*aFamily3;
00275     aFamily4->removeReference();
00276   }
00277 
00279   // TEST 4: check construction from given support //
00281   {
00282     SUPPORT *s1=new SUPPORT(*aFamily3);
00283     FAMILY  *f1=new FAMILY(*s1);
00284     CPPUNIT_ASSERT_EQUAL(0, f1->getIdentifier());
00285     CPPUNIT_ASSERT_EQUAL(0, f1->getNumberOfAttributes());
00286     CPPUNIT_ASSERT_EQUAL(0, f1->getNumberOfGroups());
00287     CPPUNIT_ASSERT(s1->deepCompare(*f1));
00288     s1->removeReference();
00289     f1->removeReference();
00290   }
00291 
00292   aFamily3->removeReference();
00293   aMesh->removeReference();
00294 
00296   // TEST 5: check constructor, designed to use with med driver. //
00298   {
00299     MESH * aTestMesh = MEDMEMTest_createTestMesh();
00300 
00301     // TestMesh families. Let's imagine that we would like to have
00302     // the following nodes and elements distribution by families:
00303 
00304     // NumberOfNodes = 19;
00305     int aNodeFamily[19] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
00306                            2, 2, 2, 2, 2, 2, 3, 3, 3};
00307 
00308     // NumberOfCellTypes = 3;
00309     // NumberOfCells[NumberOfCellTypes] = {12, 2, 2};
00310     int aCellType1Family[12] = {4,4,4,4,4,5,5,5,5,5,5,5};
00311     int aCellType2Family[ 2] = {5,6};
00312     int aCellType3Family[ 2] = {6,6};
00313     int * aCellFamily[3] = {aCellType1Family, aCellType2Family, aCellType3Family};
00314 
00315     // NumberOfFaceTypes = 2;
00316     // NumberOfFaces[NumberOfFaceTypes] = {4, 4};
00317     int aFaceType1Family[4] = {7,7,7,7};
00318     int aFaceType2Family[4] = {7,7,7,7};
00319     int * aFaceFamily[3] = {aFaceType1Family, aFaceType2Family};
00320 
00321     // No edges in aTestMesh
00322     int ** anEdgeFamily = NULL;
00323 
00324     // new families data
00325     int attrId[2] = {1,2};
00326     int attrVa[2] = {7,8};
00327 
00328     string attrDescr1 ("Attribute 1 description");
00329     string attrDescr2 ("Attribute 2 description");
00330     string attrDescrEmpty (MED_COMMENT_SIZE - 23, ' ');
00331     attrDescr1 += attrDescrEmpty;
00332     attrDescr2 += attrDescrEmpty;
00333     CPPUNIT_ASSERT(MED_COMMENT_SIZE == attrDescr1.length());
00334     CPPUNIT_ASSERT(MED_COMMENT_SIZE == attrDescr2.length());
00335     string attrDescr = attrDescr1 + attrDescr2;
00336 
00337     string groupName1 ("Group_1");
00338     string groupName2 ("Group_2");
00339     string groupName3 ("Group_3");
00340     string groupNameEmpty (MED_LNAME_SIZE - 7, ' ');
00341     groupName1 += groupNameEmpty;
00342     groupName2 += groupNameEmpty;
00343     groupName3 += groupNameEmpty;
00344     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName1.length());
00345     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName2.length());
00346     CPPUNIT_ASSERT(MED_LNAME_SIZE == groupName3.length());
00347     string groupNames = groupName1 + groupName2 + groupName3;
00348 
00349     // nodes family 1
00350     FAMILY *aNodesF1=new FAMILY(aTestMesh, /*Identifier*/1, "Nodes 1", 
00351                                 /*NumberOfAttribute*/2, attrId, attrVa, attrDescr,
00352                                 /*NumberOfGroup*/3, groupNames,
00353                                 aNodeFamily, aCellFamily, aFaceFamily, anEdgeFamily);
00354 
00355 
00356     CPPUNIT_ASSERT_EQUAL(1, aNodesF1->getIdentifier());
00357     CPPUNIT_ASSERT(strcmp("Nodes 1", aNodesF1->getName().c_str()) == 0);
00358     CPPUNIT_ASSERT(MED_EN::MED_NODE == aNodesF1->getEntity());
00359     CPPUNIT_ASSERT(!aNodesF1->isOnAllElements());
00360     CPPUNIT_ASSERT_EQUAL(7, aNodesF1->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
00361 
00362     CPPUNIT_ASSERT_EQUAL(2, aNodesF1->getNumberOfAttributes());
00363     CPPUNIT_ASSERT_EQUAL(1, aNodesF1->getAttributeIdentifier(1));
00364     CPPUNIT_ASSERT_EQUAL(2, aNodesF1->getAttributeIdentifier(2));
00365     CPPUNIT_ASSERT_EQUAL(7, aNodesF1->getAttributeValue(1));
00366     CPPUNIT_ASSERT_EQUAL(8, aNodesF1->getAttributeValue(2));
00367     CPPUNIT_ASSERT_EQUAL(attrDescr1, aNodesF1->getAttributeDescription(1));
00368     CPPUNIT_ASSERT_EQUAL(attrDescr2, aNodesF1->getAttributeDescription(2));
00369 
00370     CPPUNIT_ASSERT_EQUAL(3, aNodesF1->getNumberOfGroups());
00371     CPPUNIT_ASSERT_EQUAL(groupName1, aNodesF1->getGroupName(1));
00372     CPPUNIT_ASSERT_EQUAL(groupName2, aNodesF1->getGroupName(2));
00373     CPPUNIT_ASSERT_EQUAL(groupName3, aNodesF1->getGroupName(3));
00374     aNodesF1->removeReference();
00375 
00376     // faces family 7
00377     FAMILY *aFacesF7=new FAMILY(aTestMesh, /*Identifier*/7, "Faces All", 
00378                                 /*NumberOfAttribute*/2, attrId, attrVa, attrDescr,
00379                                 /*NumberOfGroup*/3, groupNames,
00380                                 aNodeFamily, aCellFamily, aFaceFamily, anEdgeFamily);
00381 
00382 
00383     CPPUNIT_ASSERT_EQUAL(7, aFacesF7->getIdentifier());
00384     CPPUNIT_ASSERT(strcmp("Faces All", aFacesF7->getName().c_str()) == 0);
00385     CPPUNIT_ASSERT(MED_EN::MED_FACE == aFacesF7->getEntity());
00386 
00387     CPPUNIT_ASSERT_EQUAL(8, aTestMesh->getNumberOfElements(MED_EN::MED_FACE,
00388                                                            MED_EN::MED_ALL_ELEMENTS));
00389     CPPUNIT_ASSERT_EQUAL(8, aFacesF7->getNumberOfElements(MED_EN::MED_ALL_ELEMENTS));
00390 
00391     CPPUNIT_ASSERT(!aFacesF7->isOnAllElements());
00392 
00393     CPPUNIT_ASSERT_EQUAL(2, aFacesF7->getNumberOfAttributes());
00394     CPPUNIT_ASSERT_EQUAL(1, aFacesF7->getAttributeIdentifier(1));
00395     CPPUNIT_ASSERT_EQUAL(2, aFacesF7->getAttributeIdentifier(2));
00396     CPPUNIT_ASSERT_EQUAL(7, aFacesF7->getAttributeValue(1));
00397     CPPUNIT_ASSERT_EQUAL(8, aFacesF7->getAttributeValue(2));
00398     CPPUNIT_ASSERT_EQUAL(attrDescr1, aFacesF7->getAttributeDescription(1));
00399     CPPUNIT_ASSERT_EQUAL(attrDescr2, aFacesF7->getAttributeDescription(2));
00400 
00401     CPPUNIT_ASSERT_EQUAL(3, aFacesF7->getNumberOfGroups());
00402     CPPUNIT_ASSERT_EQUAL(groupName1, aFacesF7->getGroupName(1));
00403     CPPUNIT_ASSERT_EQUAL(groupName2, aFacesF7->getGroupName(2));
00404     CPPUNIT_ASSERT_EQUAL(groupName3, aFacesF7->getGroupName(3));
00405     aFacesF7->removeReference();
00406     aTestMesh->removeReference();
00407 
00408     // Method build() is not tested directly, but it is called from constructor, tested here
00409   }
00410 }