Salome HOME
2e2913c2cb8989a17296c27e75d763c29b90d505
[modules/geom.git] / src / XAO / tests / ImportExportTest.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 // Author : Frederic Pons (OpenCascade)
20
21 #include "TestUtils.hxx"
22 #include "ImportExportTest.hxx"
23 #include "../XAO_XaoUtils.hxx"
24 #include "../XAO_Geometry.hxx"
25 #include "../XAO_Group.hxx"
26 #include "../XAO_Field.hxx"
27 #include "../XAO_IntegerField.hxx"
28 #include "../XAO_IntegerStep.hxx"
29
30 using namespace XAO;
31
32
33 void ImportExportTest::setUp()
34 {
35 }
36
37 void ImportExportTest::tearDown()
38 {
39 }
40
41 void ImportExportTest::cleanUp()
42 {
43 }
44
45 void ImportExportTest::testExportNoGeometry()
46 {
47     Xao xao("me", "1.0");
48
49     bool res = xao.exportXAO("empty.xao", "");
50     CPPUNIT_ASSERT(res);
51 }
52
53 void ImportExportTest::testExportGeometry()
54 {
55     Xao xao("me", "1.0");
56     Geometry* geom = Geometry::createGeometry(XAO::BREP);
57     geom->setName("mygeom");
58     CPPUNIT_ASSERT_EQUAL(false, geom->isReadOnly());
59
60     // add elements
61     geom->setCountVertices(4);
62     geom->setVertex(0, "v1", "1");
63     geom->setVertex(1, "", "2");
64     geom->setVertex(2, "v3", "3");
65     geom->setVertex(3, "", "4");
66
67     geom->setCountEdges(3);
68     geom->setEdge(0, "e1", "5");
69     geom->setEdge(1, "", "6");
70     geom->setEdge(2, "e3", "7");
71
72     geom->setCountFaces(2);
73     geom->setFace(0, "f1", "8");
74     geom->setFace(1, "", "9");
75
76     geom->setCountSolids(1);
77     geom->setSolid(0, "s1", "10");
78
79     xao.setGeometry(geom);
80     CPPUNIT_ASSERT_EQUAL(true, geom->isReadOnly());
81
82     // groups
83     Group* group = xao.addGroup(XAO::SOLID);
84     group->setName("boite1");
85     group->add(0);
86
87     group = xao.addGroup(XAO::FACE);
88     group->setName("faces");
89     group->add(0);
90     group->add(1);
91
92     // fields
93     IntegerField* field = (IntegerField*)xao.addField(XAO::INTEGER, XAO::FACE, 2, "color");
94     for (int stepIndex = 0; stepIndex < 10; ++stepIndex)
95     {
96         IntegerStep* istep = field->addStep(stepIndex, 100*stepIndex);
97         for (int eltIndex = 0; eltIndex < istep->countElements(); ++eltIndex)
98         {
99             for (int compIndex = 0; compIndex < istep->countComponents(); ++compIndex)
100             {
101                 istep->setValue(eltIndex, compIndex, istep->getStamp() + eltIndex*10 + compIndex);
102             }
103         }
104     }
105
106     bool res = xao.exportXAO("mygeom.xao", "");
107     CPPUNIT_ASSERT(res);
108
109     std::string xml = xao.getXML();
110     //CPPUNIT_ASSERT_EQUAL(strlen(xml) == 1007);
111 }
112
113 void ImportExportTest::testGeometryError()
114 {
115     Geometry* geom = Geometry::createGeometry(XAO::BREP);
116     geom->setName("mygeom");
117     geom->setCountVertices(2);
118     CPPUNIT_ASSERT_THROW(geom->setVertex(3, "v4", "4"), XAO_Exception);
119     delete geom;
120 }
121
122 void ImportExportTest::testImportXao()
123 {
124     Xao xao;
125     xao.importXAO(TestUtils::getTestFilePath("test.xao"));
126     checkImport(xao);
127 }
128
129 void ImportExportTest::testImportXaoFromText()
130 {
131     char* txt = TestUtils::readTextFile(TestUtils::getTestFilePath("test.xao"));
132
133     Xao xao;
134     xao.setXML(txt);
135     checkImport(xao);
136 }
137
138 void ImportExportTest::checkImport(Xao& xao)
139 {
140     CPPUNIT_ASSERT_EQUAL(std::string("me"), xao.getAuthor());
141     CPPUNIT_ASSERT_EQUAL(std::string("1.0"), xao.getVersion());
142
143     Geometry* geom = xao.getGeometry();
144     CPPUNIT_ASSERT(geom != NULL);
145     CPPUNIT_ASSERT_EQUAL(std::string("mygeom"), geom->getName());
146
147     CPPUNIT_ASSERT_EQUAL(8, geom->countVertices());
148     CPPUNIT_ASSERT_EQUAL(std::string("v1"), geom->getVertexName(0));
149     CPPUNIT_ASSERT_EQUAL(std::string("6"), geom->getVertexReference(0));
150     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getVertexName(1));
151     CPPUNIT_ASSERT_EQUAL(std::string("7"), geom->getVertexReference(1));
152     CPPUNIT_ASSERT_EQUAL(std::string("v3"), geom->getVertexName(2));
153     CPPUNIT_ASSERT_EQUAL(std::string("9"), geom->getVertexReference(2));
154     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getVertexName(3));
155     CPPUNIT_ASSERT_EQUAL(std::string("11"), geom->getVertexReference(3));
156     CPPUNIT_ASSERT_EQUAL(std::string("v5"), geom->getVertexName(4));
157     CPPUNIT_ASSERT_EQUAL(std::string("16"), geom->getVertexReference(4));
158     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getVertexName(5));
159     CPPUNIT_ASSERT_EQUAL(std::string("17"), geom->getVertexReference(5));
160     CPPUNIT_ASSERT_EQUAL(std::string("v7"), geom->getVertexName(6));
161     CPPUNIT_ASSERT_EQUAL(std::string("19"), geom->getVertexReference(6));
162     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getVertexName(7));
163     CPPUNIT_ASSERT_EQUAL(std::string("21"), geom->getVertexReference(7));
164
165     CPPUNIT_ASSERT_EQUAL(12, geom->countEdges());
166     CPPUNIT_ASSERT_EQUAL(std::string("e1"), geom->getEdgeName(0));
167     CPPUNIT_ASSERT_EQUAL(std::string("5"), geom->getEdgeReference(0));
168     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(1));
169     CPPUNIT_ASSERT_EQUAL(std::string("8"), geom->getEdgeReference(1));
170     CPPUNIT_ASSERT_EQUAL(std::string("e3"), geom->getEdgeName(2));
171     CPPUNIT_ASSERT_EQUAL(std::string("10"), geom->getEdgeReference(2));
172     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(3));
173     CPPUNIT_ASSERT_EQUAL(std::string("12"), geom->getEdgeReference(3));
174     CPPUNIT_ASSERT_EQUAL(std::string("e5"), geom->getEdgeName(4));
175     CPPUNIT_ASSERT_EQUAL(std::string("15"), geom->getEdgeReference(4));
176     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(5));
177     CPPUNIT_ASSERT_EQUAL(std::string("18"), geom->getEdgeReference(5));
178     CPPUNIT_ASSERT_EQUAL(std::string("e7"), geom->getEdgeName(6));
179     CPPUNIT_ASSERT_EQUAL(std::string("20"), geom->getEdgeReference(6));
180     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(7));
181     CPPUNIT_ASSERT_EQUAL(std::string("22"), geom->getEdgeReference(7));
182     CPPUNIT_ASSERT_EQUAL(std::string("e9"), geom->getEdgeName(8));
183     CPPUNIT_ASSERT_EQUAL(std::string("25"), geom->getEdgeReference(8));
184     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(9));
185     CPPUNIT_ASSERT_EQUAL(std::string("26"), geom->getEdgeReference(9));
186     CPPUNIT_ASSERT_EQUAL(std::string("e11"), geom->getEdgeName(10));
187     CPPUNIT_ASSERT_EQUAL(std::string("29"), geom->getEdgeReference(10));
188     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getEdgeName(11));
189     CPPUNIT_ASSERT_EQUAL(std::string("30"), geom->getEdgeReference(11));
190
191     CPPUNIT_ASSERT_EQUAL(6, geom->countFaces());
192     CPPUNIT_ASSERT_EQUAL(std::string("f1"), geom->getFaceName(0));
193     CPPUNIT_ASSERT_EQUAL(std::string("3"), geom->getFaceReference(0));
194     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getFaceName(1));
195     CPPUNIT_ASSERT_EQUAL(std::string("13"), geom->getFaceReference(1));
196     CPPUNIT_ASSERT_EQUAL(std::string("f3"), geom->getFaceName(2));
197     CPPUNIT_ASSERT_EQUAL(std::string("23"), geom->getFaceReference(2));
198     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getFaceName(3));
199     CPPUNIT_ASSERT_EQUAL(std::string("27"), geom->getFaceReference(3));
200     CPPUNIT_ASSERT_EQUAL(std::string("f5"), geom->getFaceName(4));
201     CPPUNIT_ASSERT_EQUAL(std::string("31"), geom->getFaceReference(4));
202     CPPUNIT_ASSERT_EQUAL(std::string(""), geom->getFaceName(5));
203     CPPUNIT_ASSERT_EQUAL(std::string("33"), geom->getFaceReference(5));
204
205     CPPUNIT_ASSERT_EQUAL(1, geom->countSolids());
206     CPPUNIT_ASSERT_EQUAL(std::string("s1"), geom->getSolidName(0));
207     CPPUNIT_ASSERT_EQUAL(std::string("1"), geom->getSolidReference(0));
208
209     CPPUNIT_ASSERT_EQUAL(2, xao.countGroups());
210     Group* group = xao.getGroup(0);
211     CPPUNIT_ASSERT_EQUAL(1, group->count());
212     CPPUNIT_ASSERT_EQUAL(std::string("boite_1"), group->getName());
213     CPPUNIT_ASSERT_EQUAL(XAO::SOLID, group->getDimension());
214     CPPUNIT_ASSERT_EQUAL(0, group->get(0));
215     group = xao.getGroup(1);
216     CPPUNIT_ASSERT_EQUAL(2, group->count());
217     CPPUNIT_ASSERT_EQUAL(std::string(""), group->getName());
218     CPPUNIT_ASSERT_EQUAL(XAO::FACE, group->getDimension());
219     CPPUNIT_ASSERT_EQUAL(0, group->get(0));
220     CPPUNIT_ASSERT_EQUAL(1, group->get(1));
221
222     CPPUNIT_ASSERT_EQUAL(1, xao.countFields());
223     Field* field= xao.getField(0);
224     CPPUNIT_ASSERT_EQUAL(std::string("color"), field->getName());
225     CPPUNIT_ASSERT_EQUAL(XAO::INTEGER, field->getType());
226     CPPUNIT_ASSERT_EQUAL(XAO::SOLID, field->getDimension());
227
228 }