1 // Copyright (C) 2013-2016 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
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"
35 void FieldTest::setUp()
39 void FieldTest::tearDown()
43 void FieldTest::cleanUp()
47 Field* FieldTest::testField(XAO::Type type)
49 Field* f = Field::createField(type, XAO::FACE, 10, 3);
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());
59 CPPUNIT_ASSERT_EQUAL(std::string("field1"), f->getName());
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);
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
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());
89 void FieldTest::testBooleanField()
91 BooleanField* f = (BooleanField*)testField(XAO::BOOLEAN);
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);
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
104 void FieldTest::testIntegerField()
106 IntegerField* f = (IntegerField*)testField(XAO::INTEGER);
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);
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
118 void FieldTest::testDoubleField()
120 DoubleField* f = (DoubleField*)testField(XAO::DOUBLE);
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);
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
132 void FieldTest::testStringField()
134 StringField* f = (StringField*)testField(XAO::STRING);
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);
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
147 void FieldTest::testStep(XAO::Type type, Step* step)
149 CPPUNIT_ASSERT_EQUAL(type, step->getType());
151 CPPUNIT_ASSERT_EQUAL(0, step->getStep());
153 CPPUNIT_ASSERT_EQUAL(10, step->getStep());
155 CPPUNIT_ASSERT_EQUAL(0, step->getStamp());
157 CPPUNIT_ASSERT_EQUAL(100, step->getStamp());
159 CPPUNIT_ASSERT_EQUAL(5, step->countElements());
160 CPPUNIT_ASSERT_EQUAL(3, step->countComponents());
161 CPPUNIT_ASSERT_EQUAL(15, step->countValues());
164 void FieldTest::testBooleanStep()
166 Step* step = new BooleanStep(0, 0, 5, 3);
167 testStep(XAO::BOOLEAN, step);
169 void FieldTest::testIntegerStep()
171 Step* step = new IntegerStep(0, 0, 5, 3);
172 testStep(XAO::INTEGER, step);
174 void FieldTest::testDoubleStep()
176 Step* step = new DoubleStep(0, 0, 5, 3);
177 testStep(XAO::DOUBLE, step);
179 void FieldTest::testStringStep()
181 Step* step = new StringStep(0, 0, 5, 3);
182 testStep(XAO::STRING, step);
185 void FieldTest::testBooleanStepValues()
187 int nbComponents = 3; // > 1
188 int nbElements = 5; // > 1
190 BooleanStep* step = new BooleanStep(0, 0, nbElements, nbComponents);
191 for (int i = 0; i < step->countElements(); ++i)
193 for (int j = 0; j < step->countComponents(); ++j)
195 step->setValue(i, j, j % 2 == 0);
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);
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)
210 for (int j = 0; j < nbComponents; ++j)
211 CPPUNIT_ASSERT((j % 2 == 0) == values[i*nbComponents+j]);
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]);
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]);
229 std::vector<bool> newEltValues;
231 newEltValues.push_back(true);
232 CPPUNIT_ASSERT_THROW(step->setElement(2, newEltValues), XAO_Exception);
234 for (int i = 1; i < nbComponents; ++i)
235 newEltValues.push_back(true);
236 step->setElement(2, newEltValues);
239 std::vector<bool> newCompValues;
241 newCompValues.push_back(true);
242 CPPUNIT_ASSERT_THROW(step->setComponent(1, newCompValues), XAO_Exception);
244 for (int i = 1; i < nbElements; ++i)
245 newCompValues.push_back(true);
246 step->setComponent(1, newCompValues);
249 step->setStringValue(0, 0, "true");
250 CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
253 std::vector<bool> allValues;
255 allValues.push_back(true);
256 CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
258 for (int i = 1; i < nbElements*nbComponents; ++i)
259 allValues.push_back(true);
260 step->setValues(allValues);
263 void FieldTest::testIntegerStepValues()
265 int nbComponents = 3;
268 IntegerStep* step = new IntegerStep(0, 0, nbElements, nbComponents);
269 for (int i = 0; i < step->countElements(); ++i)
271 for (int j = 0; j < step->countComponents(); ++j)
272 step->setValue(i, j, i*10 + j);
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);
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)
285 for (int j = 0; j < nbComponents; ++j)
286 CPPUNIT_ASSERT_EQUAL(10*i+j, values[i*nbComponents+j]);
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]);
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]);
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);
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);
320 step->setStringValue(0, 0, "0");
321 CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
324 std::vector<int> allValues;
326 allValues.push_back(11);
327 CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
329 for (int i = 1; i < nbElements*nbComponents; ++i)
330 allValues.push_back(11);
331 step->setValues(allValues);
334 void FieldTest::testDoubleStepValues()
336 int nbComponents = 3;
339 DoubleStep* step = new DoubleStep(0, 0, nbElements, nbComponents);
340 for (int i = 0; i < step->countElements(); ++i)
342 for (int j = 0; j < step->countComponents(); ++j)
343 step->setValue(i, j, i*10 + j*0.1);
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);
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)
356 for (int j = 0; j < nbComponents; ++j)
357 CPPUNIT_ASSERT_EQUAL(10*i+j*0.1, values[i*nbComponents+j]);
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]);
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]);
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);
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);
391 step->setStringValue(0, 0, "0.2");
392 CPPUNIT_ASSERT_THROW(step->setStringValue(0, 0, "aa"), XAO_Exception);
394 std::vector<double> allValues;
396 allValues.push_back(1.1);
397 CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
399 for (int i = 1; i < nbElements*nbComponents; ++i)
400 allValues.push_back(1.1);
401 step->setValues(allValues);}
403 void FieldTest::testStringStepValues()
405 int nbComponents = 3;
408 StringStep* step = new StringStep(0, 0, nbElements, nbComponents);
409 for (int i = 0; i < step->countElements(); ++i)
411 for (int j = 0; j < step->countComponents(); ++j)
412 step->setValue(i, j, XaoUtils::intToString(i*10 + j));
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);
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)
425 for (int j = 0; j < nbComponents; ++j)
426 CPPUNIT_ASSERT_EQUAL(XaoUtils::intToString(10*i+j), values[i*nbComponents+j]);
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]);
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]);
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);
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);
460 step->setStringValue(0, 0, "0");
462 std::vector<std::string> allValues;
464 allValues.push_back("abc");
465 CPPUNIT_ASSERT_THROW(step->setValues(allValues), XAO_Exception);
467 for (int i = 1; i < nbElements*nbComponents; ++i)
468 allValues.push_back("abc");
469 step->setValues(allValues);
472 void FieldTest::testSetComponents()
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));
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));
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));
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));