Salome HOME
4586c3907e6f43e39a79e1c84f34b0d7a25089b6
[modules/geom.git] / src / XAO / tests / GeometryTest.cxx
1 // Copyright (C) 2013-2023  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, or (at your option) any later version.
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 <vector>
21
22 #include "TestUtils.hxx"
23 #include "GeometryTest.hxx"
24 #include "../XAO_XaoUtils.hxx"
25 #include "../XAO_Geometry.hxx"
26 #include "../XAO_GeometricElement.hxx"
27
28 using namespace XAO;
29
30 void GeometryTest::setUp()
31 {
32 }
33
34 void GeometryTest::tearDown()
35 {
36 }
37
38 void GeometryTest::cleanUp()
39 {
40 }
41
42 void GeometryTest::testGeometryElement()
43 {
44     GeometricElement elt;
45     CPPUNIT_ASSERT_EQUAL(false, elt.hasName());
46     CPPUNIT_ASSERT_EQUAL(std::string(""), elt.getName());
47     CPPUNIT_ASSERT_EQUAL(std::string(""), elt.getReference());
48
49     elt.setName("test");
50     CPPUNIT_ASSERT_EQUAL(true, elt.hasName());
51     CPPUNIT_ASSERT_EQUAL(std::string("test"), elt.getName());
52
53     elt.setReference("abc");
54     CPPUNIT_ASSERT_EQUAL(std::string("abc"), elt.getReference());
55
56     GeometricElement elt2("aa", "bb");
57     CPPUNIT_ASSERT_EQUAL(std::string("aa"), elt2.getName());
58     CPPUNIT_ASSERT_EQUAL(std::string("bb"), elt2.getReference());
59 }
60
61 void GeometryTest::testGeometryElementList()
62 {
63     GeometricElementList lst;
64     CPPUNIT_ASSERT_EQUAL(0, lst.getSize());
65     lst.setSize(10);
66     CPPUNIT_ASSERT_EQUAL(10, lst.getSize());
67
68     GeometricElementList otherLst(15);
69     CPPUNIT_ASSERT_EQUAL(15, otherLst.getSize());
70     CPPUNIT_ASSERT_EQUAL(std::string(""), otherLst.getName(0));
71     CPPUNIT_ASSERT_EQUAL(std::string(""), otherLst.getReference(0));
72
73     CPPUNIT_ASSERT_THROW(otherLst.getName(20), XAO_Exception);
74     CPPUNIT_ASSERT_THROW(otherLst.getReference(20), XAO_Exception);
75     CPPUNIT_ASSERT_THROW(otherLst.hasName(20), XAO_Exception);
76
77     otherLst.setName(0, "aa");
78     CPPUNIT_ASSERT_EQUAL(std::string("aa"), otherLst.getName(0));
79     CPPUNIT_ASSERT_THROW(otherLst.setName(20, "aa"), XAO_Exception);
80     otherLst.setReference(0, "bb");
81     CPPUNIT_ASSERT_EQUAL(std::string("bb"), otherLst.getReference(0));
82     CPPUNIT_ASSERT_THROW(otherLst.setReference(20, "aa"), XAO_Exception);
83
84     otherLst.setSize(10);
85     CPPUNIT_ASSERT_EQUAL(std::string(""), otherLst.getName(0));
86     CPPUNIT_ASSERT_EQUAL(std::string(""), otherLst.getReference(0));
87     CPPUNIT_ASSERT_EQUAL(false, otherLst.hasName(0));
88
89     otherLst.setElement(3, "name", "ref");
90     CPPUNIT_ASSERT_EQUAL(std::string("name"), otherLst.getName(3));
91     CPPUNIT_ASSERT_EQUAL(std::string("ref"), otherLst.getReference(3));
92     CPPUNIT_ASSERT_EQUAL(true, otherLst.hasName(3));
93     CPPUNIT_ASSERT_THROW(otherLst.setElement(30, "name", "ref"), XAO_Exception);
94
95     // ---- create elements "name i", "Ri"
96     for (int i = 0; i < otherLst.getSize(); ++i)
97     {
98         std::ostringstream name;
99         name << "name " << i;
100         std::ostringstream ref;
101         ref << "R" << i;
102
103         otherLst.setElement(i, name.str(), ref.str());
104     }
105
106     CPPUNIT_ASSERT_EQUAL(8, otherLst.getIndexByReference("R8"));
107     CPPUNIT_ASSERT_THROW(otherLst.getIndexByReference("ZZ"), XAO_Exception);
108
109     GeometricElementList::iterator first = otherLst.begin();
110     GeometricElement firstElt = first->second;
111     CPPUNIT_ASSERT_EQUAL(std::string("R0"),  firstElt.getReference());
112 }
113
114 void GeometryTest::testGeometry()
115 {
116     Geometry* geom = Geometry::createGeometry(XAO::BREP, "cube");
117
118     CPPUNIT_ASSERT_EQUAL(std::string("cube"), geom->getName());
119     CPPUNIT_ASSERT_EQUAL(XAO::BREP, geom->getFormat());
120
121     geom->setName("sphere");
122     CPPUNIT_ASSERT_EQUAL(std::string("sphere"), geom->getName());
123
124     delete geom;
125 }
126
127 void GeometryTest::testGeometryErrors()
128 {
129     CPPUNIT_ASSERT_THROW(Geometry::createGeometry(XAO::STEP), XAO_Exception);
130 }
131
132 void GeometryTest::testSetElement()
133 {
134     Geometry* geom = Geometry::createGeometry(XAO::BREP, "cube");
135
136     CPPUNIT_ASSERT_THROW(geom->setVertexName(0, "aa"), XAO_Exception);
137
138     char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath("Box_1.brep"));
139     geom->setShapeString(txt);
140
141     CPPUNIT_ASSERT_EQUAL(false, geom->hasVertexName(0));
142     geom->setVertexName(0, "va");
143     CPPUNIT_ASSERT_EQUAL(true, geom->hasVertexName(0));
144     CPPUNIT_ASSERT_EQUAL(std::string("va"), geom->getVertexName(0));
145     CPPUNIT_ASSERT_THROW(geom->getVertexName(100), XAO_Exception);
146     CPPUNIT_ASSERT_THROW(geom->hasVertexName(100), XAO_Exception);
147
148     geom->setEdgeName(0, "ea");
149     CPPUNIT_ASSERT_EQUAL(std::string("ea"), geom->getEdgeName(0));
150     CPPUNIT_ASSERT_THROW(geom->getEdgeName(100), XAO_Exception);
151
152     geom->setFaceName(0, "fa");
153     CPPUNIT_ASSERT_EQUAL(std::string("fa"), geom->getFaceName(0));
154     CPPUNIT_ASSERT_THROW(geom->getFaceName(100), XAO_Exception);
155
156     geom->setSolidName(0, "sa");
157     CPPUNIT_ASSERT_EQUAL(std::string("sa"), geom->getSolidName(0));
158     CPPUNIT_ASSERT_THROW(geom->getSolidName(100), XAO_Exception);
159
160     delete geom;
161 }