Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / MEDMEMCppTest / MEDMEMTest_Connectivity_fault.cxx
1 // Copyright (C) 2007-2012  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_Connectivity.hxx"
24 #include "MEDMEM_define.hxx"
25 #include "MEDMEM_MedMeshDriver.hxx"
26 #include "MEDMEM_Mesh.hxx"
27 #include "MEDMEM_Family.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 using namespace std;
36 using namespace MEDMEM;
37
38 static void createOrCheck (CONNECTIVITY * theC, string msg, bool create = false)
39 {
40   // Preconditions: Entity and NumberOfTypes
41   CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, MED_EN::MED_CELL, theC->getEntity());
42   CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, 2, theC->getNumberOfTypes(MED_EN::MED_CELL));
43
44   // EntityDimension
45   if (create)
46     theC->setEntityDimension(3);
47
48   // NumberOfNodes
49   int nbNodes = 20;
50
51   if (create) {
52     theC->setNumberOfNodes(nbNodes);
53   }
54
55   // GeometricTypes
56   MED_EN::medGeometryElement aCellTypes[2] = {MED_EN::MED_PYRA5, MED_EN::MED_HEXA8};
57
58   // this variable is needed in check mode (!create)
59   // because of bug with getGlobalNumberingIndex() method (see below)
60
61   if (create) {
62     theC->setGeometricTypes(aCellTypes, MED_EN::MED_CELL);
63     CPPUNIT_ASSERT_THROW(theC->setGeometricTypes(aCellTypes, MED_EN::MED_NODE), MEDEXCEPTION);
64     CPPUNIT_ASSERT_THROW(theC->setGeometricTypes(aCellTypes, MED_EN::MED_FACE), MEDEXCEPTION);
65     CPPUNIT_ASSERT_THROW(theC->setGeometricTypes(aCellTypes, MED_EN::MED_EDGE), MEDEXCEPTION);
66   }
67
68   // Nodal Connectivity for standard types
69   int countCells[3] = {1, 3, 4}; // 2 PYRA5 and 1 HEXA8
70   int nodesCells_PYRA5[10] = {2,3,4,5,1, 6,7,8,9,10};
71   int nodesCells_HEXA8[8] = {2,3,4,5, 6,7,8,9};
72
73   if (create) {
74     theC->setCount(countCells, MED_EN::MED_CELL);
75     theC->setNodal(nodesCells_PYRA5, MED_EN::MED_CELL, MED_EN::MED_PYRA5);
76     theC->setNodal(nodesCells_HEXA8, MED_EN::MED_CELL, MED_EN::MED_HEXA8);
77
78     // Invalid cases
79     CPPUNIT_ASSERT_THROW(theC->setCount(countCells, MED_EN::MED_NODE), MEDEXCEPTION);
80     CPPUNIT_ASSERT_THROW(theC->setCount(countCells, MED_EN::MED_EDGE), MEDEXCEPTION);
81     CPPUNIT_ASSERT_THROW(theC->setCount(countCells, MED_EN::MED_FACE), MEDEXCEPTION);
82
83     CPPUNIT_ASSERT_THROW(theC->setNodal(nodesCells_PYRA5, MED_EN::MED_FACE, MED_EN::MED_PYRA5), MEDEXCEPTION);
84   }
85
86   // 2 POLYHEDRA
87   const int nbPolyhedron = 2;
88   const int nbPolyNodes = 52 + 14 - 2; // = 64
89
90   int aPolyhedronIndex[nbPolyhedron + 1] = {1,33, 65};
91
92   int aPolyhedronNodalConnectivity[nbPolyNodes] = {
93     11,15,19,20,17,13,-1, 11,13,14,-1,  14,13,17,18,-1,  18,17,20,-1,  11,14,15,-1,  15,14,18,19,-1,  19,18,20,
94     11,13,17,20,19,15,-1, 11,12,13,-1,  13,12,16,17,-1,  17,16,20,-1,  11,15,12,-1,  12,15,19,16,-1,  16,19,20};
95
96   if (create) {
97     theC->setNodal(aPolyhedronNodalConnectivity,
98                    MED_EN::MED_CELL, MED_EN::MED_POLYHEDRA, aPolyhedronIndex);
99   }
100   else {
101     // CELLS(3D): theC
102     {
103       // Polyhedron-specific methods
104       {
105         // invalid polyhedron Id
106 //#ifdef ENABLE_FAULTS
107         int lenPolyh3nodes;
108         int nbFaces3;
109         int *nbNodes3;
110         // (BUG) Segmentation fault instead of MEDEXCEPTION
111         CPPUNIT_ASSERT_THROW(theC->getNodesOfPolyhedron(1, lenPolyh3nodes), MEDEXCEPTION);
112         CPPUNIT_ASSERT_THROW(theC->getNodesOfPolyhedron(3+3, lenPolyh3nodes), MEDEXCEPTION);
113         CPPUNIT_ASSERT_THROW(theC->getNodesPerFaceOfPolyhedron
114                              (/*polyhedronId*/1, nbFaces3, nbNodes3), MEDEXCEPTION);
115         CPPUNIT_ASSERT_THROW(theC->getNodesPerFaceOfPolyhedron
116                              (/*polyhedronId*/3+3, nbFaces3, nbNodes3), MEDEXCEPTION);
117 //#endif
118       } // Polyhedron-specific methods
119     } // CELLS: theC
120   }
121
122   if (create) {
123     // force _constituent computation
124     CPPUNIT_ASSERT_EQUAL_MESSAGE(msg, 2, theC->getNumberOfTypes(MED_EN::MED_FACE));
125   }
126 }
127
128 int main (int argc, char** argv)
129 {
130   CONNECTIVITY * c1 = new CONNECTIVITY(/*numberOfTypes*/2, MED_EN::MED_CELL);
131   createOrCheck(c1, "Creation", /*create*/true);
132   createOrCheck(c1, "Check just created", /*create*/false);
133 }