Salome HOME
Updated copyright comment
[modules/geom.git] / src / XAO / tests / FieldTest.cxx
1 // Copyright (C) 2013-2024  CEA, EDF, 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 "FieldTest.hxx"
23 #include "../XAO_Xao.hxx"
24 #include "../XAO_XaoUtils.hxx"
25 #include "../XAO_Field.hxx"
26 #include "../XAO_Step.hxx"
27 #include "../XAO_BooleanField.hxx"
28 #include "../XAO_IntegerField.hxx"
29 #include "../XAO_DoubleField.hxx"
30 #include "../XAO_StringField.hxx"
31
32 using namespace XAO;
33
34
35 void FieldTest::setUp()
36 {
37 }
38
39 void FieldTest::tearDown()
40 {
41 }
42
43 void FieldTest::cleanUp()
44 {
45 }
46
47 Field* FieldTest::testField(XAO::Type type)
48 {
49     Field* f = Field::createField(type, XAO::FACE, 10, 3);
50
51     CPPUNIT_ASSERT_EQUAL(0, (int)f->getName().size());
52     CPPUNIT_ASSERT_EQUAL(type, f->getType());
53     CPPUNIT_ASSERT_EQUAL(XAO::FACE, f->getDimension());
54     CPPUNIT_ASSERT_EQUAL(3, f->countComponents());
55     CPPUNIT_ASSERT_EQUAL(10, f->countElements());
56     CPPUNIT_ASSERT_EQUAL(30, f->countValues());
57
58     f->setName("field1");
59     CPPUNIT_ASSERT_EQUAL(std::string("field1"), f->getName());
60
61     CPPUNIT_ASSERT_EQUAL(0, (int)f->getComponentName(0).size());
62     f->setComponentName(0, "x");
63     f->setComponentName(1, "y");
64     f->setComponentName(2, "z");
65     CPPUNIT_ASSERT_EQUAL(3, f->countComponents());
66     CPPUNIT_ASSERT_EQUAL(std::string("x"), f->getComponentName(0));
67     CPPUNIT_ASSERT_EQUAL(std::string("y"), f->getComponentName(1));
68     CPPUNIT_ASSERT_EQUAL(std::string("z"), f->getComponentName(2));
69     CPPUNIT_ASSERT_THROW(f->setComponentName(3, "a"), XAO_Exception);
70     CPPUNIT_ASSERT_THROW(f->getComponentName(3), XAO_Exception);
71
72     CPPUNIT_ASSERT_EQUAL(0, f->countSteps());
73     Step* step = f->addNewStep(0);
74     CPPUNIT_ASSERT_EQUAL(type, step->getType());
75     CPPUNIT_ASSERT_EQUAL(1, f->countSteps());
76     step = f->addNewStep(1);
77     step = f->addNewStep(2);
78     CPPUNIT_ASSERT_EQUAL(3, f->countSteps());
79     CPPUNIT_ASSERT_THROW(f->addNewStep(2), XAO_Exception); // step already exists
80
81     CPPUNIT_ASSERT_EQUAL(true, f->removeStep(step));
82     CPPUNIT_ASSERT_EQUAL(2, f->countSteps());
83     CPPUNIT_ASSERT_EQUAL(false, f->removeStep(step)); // remove same
84     CPPUNIT_ASSERT_EQUAL(2, f->countSteps());
85
86     return f;
87 }
88
89 void FieldTest::testBooleanField()
90 {
91     BooleanField* f = (BooleanField*)testField(XAO::BOOLEAN);
92
93     BooleanStep* step = f->getStep(0);
94     CPPUNIT_ASSERT_EQUAL(XAO::BOOLEAN, step->getType());
95     CPPUNIT_ASSERT_MESSAGE("step is NULL", step != NULL);
96     CPPUNIT_ASSERT_THROW(f->getStep(2), XAO_Exception);
97
98     step = f->addStep(10);
99     CPPUNIT_ASSERT_EQUAL(XAO::BOOLEAN, step->getType());
100     CPPUNIT_ASSERT_EQUAL(3, f->countSteps());
101     CPPUNIT_ASSERT_THROW(f->addStep(10), XAO_Exception); // step already exists
102 }
103
104 void FieldTest::testIntegerField()
105 {
106     IntegerField* f = (IntegerField*)testField(XAO::INTEGER);
107
108     IntegerStep* step = f->getStep(0);
109     CPPUNIT_ASSERT_EQUAL(XAO::INTEGER, step->getType());
110     CPPUNIT_ASSERT_MESSAGE("step is NULL", step != NULL);
111     CPPUNIT_ASSERT_THROW(f->getStep(2), XAO_Exception);
112
113     step = f->addStep(10);
114     CPPUNIT_ASSERT_EQUAL(XAO::INTEGER, step->getType());
115     CPPUNIT_ASSERT_EQUAL(3, f->countSteps());
116     CPPUNIT_ASSERT_THROW(f->addStep(10), XAO_Exception); // step already exists
117 }
118 void FieldTest::testDoubleField()
119 {
120     DoubleField* f = (DoubleField*)testField(XAO::DOUBLE);
121
122     DoubleStep* step = f->getStep(0);
123     CPPUNIT_ASSERT_EQUAL(XAO::DOUBLE, step->getType());
124     CPPUNIT_ASSERT_MESSAGE("step is NULL", step != NULL);
125     CPPUNIT_ASSERT_THROW(f->getStep(2), XAO_Exception);
126
127     step = f->addStep(10);
128     CPPUNIT_ASSERT_EQUAL(XAO::DOUBLE, step->getType());
129     CPPUNIT_ASSERT_EQUAL(3, f->countSteps());
130     CPPUNIT_ASSERT_THROW(f->addStep(10), XAO_Exception); // step already exists
131 }
132 void FieldTest::testStringField()
133 {
134     StringField* f = (StringField*)testField(XAO::STRING);
135
136     StringStep* step = f->getStep(0);
137     CPPUNIT_ASSERT_EQUAL(XAO::STRING, step->getType());
138     CPPUNIT_ASSERT_MESSAGE("step is NULL", step != NULL);
139     CPPUNIT_ASSERT_THROW(f->getStep(2), XAO_Exception);
140
141     step = f->addStep(10);
142     CPPUNIT_ASSERT_EQUAL(XAO::STRING, step->getType());
143     CPPUNIT_ASSERT_EQUAL(3, f->countSteps());
144     CPPUNIT_ASSERT_THROW(f->addStep(10), XAO_Exception); // step already exists
145 }
146
147 void FieldTest::testStep(XAO::Type type, Step* step)
148 {
149     CPPUNIT_ASSERT_EQUAL(type, step->getType());
150
151     CPPUNIT_ASSERT_EQUAL(0, step->getStep());
152     step->setStep(10);
153     CPPUNIT_ASSERT_EQUAL(10, step->getStep());
154
155     CPPUNIT_ASSERT_EQUAL(0, step->getStamp());
156     step->setStamp(100);
157     CPPUNIT_ASSERT_EQUAL(100, step->getStamp());
158
159     CPPUNIT_ASSERT_EQUAL(5, step->countElements());
160     CPPUNIT_ASSERT_EQUAL(3, step->countComponents());
161     CPPUNIT_ASSERT_EQUAL(15, step->countValues());
162 }
163
164 void FieldTest::testBooleanStep()
165 {
166     Step* step = new BooleanStep(0, 0, 5, 3);
167     testStep(XAO::BOOLEAN, step);
168 }
169 void FieldTest::testIntegerStep()
170 {
171     Step* step = new IntegerStep(0, 0, 5, 3);
172     testStep(XAO::INTEGER, step);
173 }
174 void FieldTest::testDoubleStep()
175 {
176     Step* step = new DoubleStep(0, 0, 5, 3);
177     testStep(XAO::DOUBLE, step);
178 }
179 void FieldTest::testStringStep()
180 {
181     Step* step = new StringStep(0, 0, 5, 3);
182     testStep(XAO::STRING, step);
183 }
184
185 void FieldTest::testBooleanStepValues()
186 {
187     int nbComponents = 3; // > 1
188     int nbElements = 5;   // > 1
189
190     BooleanStep* step = new BooleanStep(0, 0, nbElements, nbComponents);
191     for (int i = 0; i < step->countElements(); ++i)
192     {
193         for (int j = 0; j < step->countComponents(); ++j)
194         {
195             step->setValue(i, j, j % 2 == 0);
196         }
197     }
198
199     CPPUNIT_ASSERT_EQUAL(true, step->getValue(1, 2));
200     CPPUNIT_ASSERT_EQUAL(std::string("true"), step->getStringValue(1, 2));
201     CPPUNIT_ASSERT_THROW(step->getValue(nbElements, 2), XAO_Exception);
202     CPPUNIT_ASSERT_THROW(step->getValue(1, nbComponents), XAO_Exception);
203
204     // get all values
205     std::vector<bool> values;
206     values = step->getValues();
207     CPPUNIT_ASSERT_EQUAL(nbElements*nbComponents, (int)values.size());
208     for (int i = 0; i < nbElements; ++i)
209     {
210         for (int j = 0; j < nbComponents; ++j)
211             CPPUNIT_ASSERT((j % 2 == 0) == values[i*nbComponents+j]);
212     }
213
214     // get one element
215     values = step->getElement(2);
216     CPPUNIT_ASSERT_THROW(step->getElement(nbElements), XAO_Exception);
217     CPPUNIT_ASSERT_EQUAL(nbComponents, (int)values.size());
218     for (int i = 0; i < nbComponents; ++i)
219         CPPUNIT_ASSERT((i % 2 == 0) == values[i]);
220
221     // get one component
222     values = step->getComponent(1);
223     CPPUNIT_ASSERT_THROW(step->getComponent(nbComponents), XAO_Exception);
224     CPPUNIT_ASSERT_EQUAL(nbElements, (int)values.size());
225     for (int i = 0; i < nbElements; ++i)
226         CPPUNIT_ASSERT(false == values[i]);
227
228     // set one element
229     std::vector<bool> newEltValues;
230     // only one value
231     newEltValues.push_back(true);
232     CPPUNIT_ASSERT_THROW(step->setElement(2, newEltValues), XAO_Exception);
233     // all values
234     for (int i = 1; i < nbComponents; ++i)
235         newEltValues.push_back(true);
236     step->setElement(2, newEltValues);
237
238     // set one component
239     std::vector<bool> newCompValues;
240     // only one value
241     newCompValues.push_back(true);
242     CPPUNIT_ASSERT_THROW(step->setComponent(1, newCompValues), XAO_Exception);
243     // all values
244     for (int i = 1; i < nbElements; ++i)
245         newCompValues.push_back(true);
246     step->setComponent(1, newCompValues);
247
248     // set string value
249     step->setStringValue(0, 0, "true");
250     CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
251
252     // set all values
253     std::vector<bool> allValues;
254     // only one value
255     allValues.push_back(true);
256     CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
257     // all values
258     for (int i = 1; i < nbElements*nbComponents; ++i)
259         allValues.push_back(true);
260     step->setValues(allValues);
261 }
262
263 void FieldTest::testIntegerStepValues()
264 {
265     int nbComponents = 3;
266     int nbElements = 5;
267
268     IntegerStep* step = new IntegerStep(0, 0, nbElements, nbComponents);
269     for (int i = 0; i < step->countElements(); ++i)
270     {
271         for (int j = 0; j < step->countComponents(); ++j)
272             step->setValue(i, j, i*10 + j);
273     }
274
275     CPPUNIT_ASSERT_EQUAL(12, step->getValue(1, 2));
276     CPPUNIT_ASSERT_THROW(step->getValue(nbElements, 2), XAO_Exception);
277     CPPUNIT_ASSERT_THROW(step->getValue(1, nbComponents), XAO_Exception);
278
279     // get all values
280     std::vector<int> values;
281     values = step->getValues();
282     CPPUNIT_ASSERT_EQUAL(nbElements*nbComponents, (int)values.size());
283     for (int i = 0; i < nbElements; ++i)
284     {
285         for (int j = 0; j < nbComponents; ++j)
286             CPPUNIT_ASSERT_EQUAL(10*i+j, values[i*nbComponents+j]);
287     }
288
289     // get one element
290     values = step->getElement(2);
291     CPPUNIT_ASSERT_THROW(step->getElement(nbElements), XAO_Exception);
292     CPPUNIT_ASSERT_EQUAL(nbComponents, (int)values.size());
293     for (int i = 0; i < nbComponents; ++i)
294         CPPUNIT_ASSERT_EQUAL(20+i, values[i]);
295
296     // get one component
297     values = step->getComponent(1);
298     CPPUNIT_ASSERT_THROW(step->getComponent(nbComponents), XAO_Exception);
299     CPPUNIT_ASSERT_EQUAL(nbElements, (int)values.size());
300     for (int i = 0; i < nbElements; ++i)
301         CPPUNIT_ASSERT_EQUAL(10*i+1, values[i]);
302
303     // set one element
304     std::vector<int> newEltValues;
305     newEltValues.push_back(1);
306     CPPUNIT_ASSERT_THROW(step->setElement(2, newEltValues), XAO_Exception);
307     for (int i = 1; i < nbComponents; ++i)
308         newEltValues.push_back(1);
309     step->setElement(2, newEltValues);
310
311     // set one component
312     std::vector<int> newCompValues;
313     newCompValues.push_back(100);
314     CPPUNIT_ASSERT_THROW(step->setComponent(1, newCompValues), XAO_Exception);
315     for (int i = 1; i < nbElements; ++i)
316         newCompValues.push_back(100);
317     step->setComponent(1, newCompValues);
318
319     // set string value
320     step->setStringValue(0, 0, "0");
321     CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
322
323     // set all values
324     std::vector<int> allValues;
325     // only one value
326     allValues.push_back(11);
327     CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
328     // all values
329     for (int i = 1; i < nbElements*nbComponents; ++i)
330         allValues.push_back(11);
331     step->setValues(allValues);
332 }
333
334 void FieldTest::testDoubleStepValues()
335 {
336     int nbComponents = 3;
337     int nbElements = 5;
338
339     DoubleStep* step = new DoubleStep(0, 0, nbElements, nbComponents);
340     for (int i = 0; i < step->countElements(); ++i)
341     {
342         for (int j = 0; j < step->countComponents(); ++j)
343             step->setValue(i, j, i*10 + j*0.1);
344     }
345
346     CPPUNIT_ASSERT_EQUAL(10.2, step->getValue(1, 2));
347     CPPUNIT_ASSERT_THROW(step->getValue(nbElements, 2), XAO_Exception);
348     CPPUNIT_ASSERT_THROW(step->getValue(1, nbComponents), XAO_Exception);
349
350     // get all values
351     std::vector<double> values;
352     values = step->getValues();
353     CPPUNIT_ASSERT_EQUAL(nbElements*nbComponents, (int)values.size());
354     for (int i = 0; i < nbElements; ++i)
355     {
356         for (int j = 0; j < nbComponents; ++j)
357             CPPUNIT_ASSERT_EQUAL(10*i+j*0.1, values[i*nbComponents+j]);
358     }
359
360     // get one element
361     values = step->getElement(2);
362     CPPUNIT_ASSERT_THROW(step->getElement(nbElements), XAO_Exception);
363     CPPUNIT_ASSERT_EQUAL(nbComponents, (int)values.size());
364     for (int i = 0; i < nbComponents; ++i)
365         CPPUNIT_ASSERT_EQUAL(20+i*0.1, values[i]);
366
367     // get one component
368     values = step->getComponent(1);
369     CPPUNIT_ASSERT_THROW(step->getComponent(nbComponents), XAO_Exception);
370     CPPUNIT_ASSERT_EQUAL(nbElements, (int)values.size());
371     for (int i = 0; i < nbElements; ++i)
372         CPPUNIT_ASSERT_EQUAL(10*i+0.1, values[i]);
373
374     // set one element
375     std::vector<double> newEltValues;
376     newEltValues.push_back(1.);
377     CPPUNIT_ASSERT_THROW(step->setElement(2, newEltValues), XAO_Exception);
378     for (int i = 1; i < nbComponents; ++i)
379         newEltValues.push_back(1.);
380     step->setElement(2, newEltValues);
381
382     // set one component
383     std::vector<double> newCompValues;
384     newCompValues.push_back(100.0);
385     CPPUNIT_ASSERT_THROW(step->setComponent(1, newCompValues), XAO_Exception);
386     for (int i = 1; i < nbElements; ++i)
387         newCompValues.push_back(100.0);
388     step->setComponent(1, newCompValues);
389
390     // set string value
391     step->setStringValue(0, 0, "0.2");
392     CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
393
394     std::vector<double> allValues;
395     // only one value
396     allValues.push_back(1.1);
397     CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
398     // all values
399     for (int i = 1; i < nbElements*nbComponents; ++i)
400         allValues.push_back(1.1);
401     step->setValues(allValues);}
402
403 void FieldTest::testStringStepValues()
404 {
405     int nbComponents = 3;
406     int nbElements = 5;
407
408     StringStep* step = new StringStep(0, 0, nbElements, nbComponents);
409     for (int i = 0; i < step->countElements(); ++i)
410     {
411         for (int j = 0; j < step->countComponents(); ++j)
412             step->setValue(i, j, XaoUtils::intToString(i*10 + j));
413     }
414
415     CPPUNIT_ASSERT_EQUAL(std::string("12"), step->getValue(1, 2));
416     CPPUNIT_ASSERT_THROW(step->getValue(nbElements, 2), XAO_Exception);
417     CPPUNIT_ASSERT_THROW(step->getValue(1, nbComponents), XAO_Exception);
418
419     // get all values
420     std::vector<std::string> values;
421     values = step->getValues();
422     CPPUNIT_ASSERT_EQUAL(nbElements*nbComponents, (int)values.size());
423     for (int i = 0; i < nbElements; ++i)
424     {
425         for (int j = 0; j < nbComponents; ++j)
426             CPPUNIT_ASSERT_EQUAL(XaoUtils::intToString(10*i+j), values[i*nbComponents+j]);
427     }
428
429     // get one element
430     values = step->getElement(2);
431     CPPUNIT_ASSERT_THROW(step->getElement(nbElements), XAO_Exception);
432     CPPUNIT_ASSERT_EQUAL(nbComponents, (int)values.size());
433     for (int i = 0; i < nbComponents; ++i)
434         CPPUNIT_ASSERT_EQUAL(XaoUtils::intToString(20+i), values[i]);
435
436     // get one component
437     values = step->getComponent(1);
438     CPPUNIT_ASSERT_THROW(step->getComponent(nbComponents), XAO_Exception);
439     CPPUNIT_ASSERT_EQUAL(nbElements, (int)values.size());
440     for (int i = 0; i < nbElements; ++i)
441         CPPUNIT_ASSERT_EQUAL(XaoUtils::intToString(10*i+1), values[i]);
442
443     // set one element
444     std::vector<std::string> newEltValues;
445     newEltValues.push_back("1");
446     CPPUNIT_ASSERT_THROW(step->setElement(2, newEltValues), XAO_Exception);
447     for (int i = 1; i < nbComponents; ++i)
448         newEltValues.push_back("1");
449     step->setElement(2, newEltValues);
450
451     // set one component
452     std::vector<std::string> newCompValues;
453     newCompValues.push_back("100");
454     CPPUNIT_ASSERT_THROW(step->setComponent(1, newCompValues), XAO_Exception);
455     for (int i = 1; i < nbElements; ++i)
456         newCompValues.push_back("100");
457     step->setComponent(1, newCompValues);
458
459     // set string value
460     step->setStringValue(0, 0, "0");
461
462     std::vector<std::string> allValues;
463     // only one value
464     allValues.push_back("abc");
465     CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
466     // all values
467     for (int i = 1; i < nbElements*nbComponents; ++i)
468         allValues.push_back("abc");
469     step->setValues(allValues);
470 }
471
472 void FieldTest::testSetComponents()
473 {
474     // field with 3 components
475     Field* f = Field::createField(XAO::INTEGER, XAO::FACE, 6, 3);
476     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(0));
477     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(1));
478     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(2));
479
480     std::vector<std::string> names;
481     names.push_back("vx");
482     f->setComponentsNames(names);
483     CPPUNIT_ASSERT_EQUAL(std::string("vx"), f->getComponentName(0));
484     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(1));
485     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(2));
486
487     names.push_back("vy");
488     f->setComponentsNames(names);
489     CPPUNIT_ASSERT_EQUAL(std::string("vx"), f->getComponentName(0));
490     CPPUNIT_ASSERT_EQUAL(std::string("vy"), f->getComponentName(1));
491     CPPUNIT_ASSERT_EQUAL(std::string(""), f->getComponentName(2));
492
493     names.push_back("vz");
494     names.push_back("t");
495     f->setComponentsNames(names);
496     CPPUNIT_ASSERT_EQUAL(std::string("vx"), f->getComponentName(0));
497     CPPUNIT_ASSERT_EQUAL(std::string("vy"), f->getComponentName(1));
498     CPPUNIT_ASSERT_EQUAL(std::string("vz"), f->getComponentName(2));
499 }