5 #include <gtest/gtest.h> 8 #include <eigen3/Eigen/Eigen> 13 using namespace Eigen;
17 TEST(TestStateMap, CopyAssignmentOperatorArray)
22 NestedArrayLvl1 nState0, nState1;
24 for (
int i = 0; i < nState0.data().size(); ++i)
26 nState0.
data()(i) = i + 1;
29 for (
int i = 0; i < nState1.data().size(); ++i)
31 nState1.data()(i) = 0;
33 for (
int i = 0; i < nState0.data().size(); ++i)
35 EXPECT_EQ(i + 1, nState0.data()(i));
38 for (
int i = 0; i < nState0.data().size(); ++i)
40 nState0.data()(i) = i + 1;
42 nState1.sub(0) = nState0.sub(0);
43 for (
int i = 0; i < nState1.sub(0).data().size(); ++i)
45 nState1.sub(0).data()(i) = 0;
47 for (
int i = 0; i < nState0.sub(0).data().size(); ++i)
49 EXPECT_EQ(i + 1, nState0.sub(0).data()(i));
54 nState1 = NestedArrayLvl1(nState0);
55 FAIL() <<
"Expected std::runtime_error";
57 catch (std::runtime_error
const &err)
59 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
63 FAIL() <<
"Expected std::runtime_error";
68 nState1.sub(0) = NestedArrayLvl0(nState0.sub(0));
69 FAIL() <<
"Expected std::runtime_error";
71 catch (std::runtime_error
const &err)
73 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
77 FAIL() <<
"Expected std::runtime_error";
80 EXPECT_EQ(24, nState1.data().size());
81 for (
int i = 0; i < nState1.data().size(); ++i)
83 EXPECT_EQ(0, nState1.data()(i));
86 TEST(TestStateMap, CopyAssignmentOperatorVector)
91 NestedVectorLvl1 nState0, nState1;
93 nState0.sub(0).subResize(2);
94 for (
int i = 0; i < nState0.data().size(); ++i)
96 nState0.data()(i) = i + 1;
100 for (
int i = 0; i < nState1.data().size(); ++i)
102 nState1.data()(i) = 0;
104 for (
int i = 0; i < nState0.data().size(); ++i)
106 EXPECT_EQ(i + 1, nState0.data()(i));
109 nState1.subResize(5);
112 for (
int i = 0; i < nState1.data().size(); ++i)
114 nState1.data()(i) = 0;
116 for (
int i = 0; i < nState0.data().size(); ++i)
118 EXPECT_EQ(i + 1, nState0.data()(i));
121 nState1.sub(0).subResize(7);
124 for (
int i = 0; i < nState1.data().size(); ++i)
126 nState1.data()(i) = 0;
128 for (
int i = 0; i < nState0.data().size(); ++i)
130 EXPECT_EQ(i + 1, nState0.data()(i));
134 for (
int i = 0; i < nState1.data().size(); ++i)
136 nState1.data()(i) = 0;
138 for (
int i = 0; i < nState0.data().size(); ++i)
140 EXPECT_EQ(i + 1, nState0.data()(i));
143 for (
int i = 0; i < nState0.data().size(); ++i)
145 nState0.data()(i) = i + 1;
148 nState1.sub(0) = nState0.sub(0);
149 for (
int i = 0; i < nState1.data().size(); ++i)
151 nState1.data()(i) = 0;
153 for (
int i = 0; i < nState0.data().size(); ++i)
155 EXPECT_EQ(i + 1, nState0.data()(i));
160 nState1 = NestedVectorLvl1(nState0);
161 FAIL() <<
"Expected std::runtime_error";
163 catch (std::runtime_error
const &err)
165 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
169 FAIL() <<
"Expected std::runtime_error";
174 nState1.sub(0) = NestedVectorLvl0(nState0.sub(0));
175 FAIL() <<
"Expected std::runtime_error";
177 catch (std::runtime_error
const &err)
179 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
183 FAIL() <<
"Expected std::runtime_error";
186 EXPECT_EQ(2, nState1.data().size());
187 for (
int i = 0; i < nState1.data().size(); ++i)
189 EXPECT_EQ(0, nState1.data()(i));
192 TEST(TestStateMap, CopyAssignmentOperatorTuple)
199 NestedTupleLvl2 nState0, nState1;
200 nState0.
sub<0>().sub<1>().subResize(2);
201 for (
int i = 0; i < nState0.data().size(); ++i)
203 nState0.data()(i) = i + 1;
207 for (
int i = 0; i < nState1.data().size(); ++i)
209 nState1.data()(i) = 0;
211 for (
int i = 0; i < nState0.data().size(); ++i)
213 EXPECT_EQ(i + 1, nState0.data()(i));
216 nState1.sub<0>().sub<1>().subResize(7);
219 for (
int i = 0; i < nState1.data().size(); ++i)
221 nState1.data()(i) = 0;
223 for (
int i = 0; i < nState0.data().size(); ++i)
225 EXPECT_EQ(i + 1, nState0.data()(i));
228 for (
int i = 0; i < nState0.data().size(); ++i)
230 nState0.data()(i) = i + 1;
232 nState1.sub<0>() = nState0.sub<0>();
233 for (
int i = 0; i < nState1.sub<0>().data().size(); ++i)
235 nState1.sub<0>().data()(i) = 0;
237 for (
int i = 0; i < nState0.sub<0>().data().size(); ++i)
239 EXPECT_EQ(i + 1, nState0.sub<0>().data()(i));
242 for (
int i = 0; i < nState0.data().size(); ++i)
244 nState0.data()(i) = i + 1;
246 nState1.sub<0>().sub<0>() = nState0.sub<0>().sub<0>();
247 for (
int i = 0; i < nState1.sub<0>().sub<0>().data().size(); ++i)
249 nState1.sub<0>().sub<0>().data()(i) = 0;
251 for (
int i = 0; i < nState0.sub<0>().sub<0>().data().size(); ++i)
253 EXPECT_EQ(i + 1, nState0.sub<0>().sub<0>().data()(i));
258 nState1 = NestedTupleLvl2(nState0);
259 FAIL() <<
"Expected std::runtime_error";
261 catch (std::runtime_error
const &err)
263 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
267 FAIL() <<
"Expected std::runtime_error";
272 nState1.sub<0>() = NestedTupleLvl1(nState0.sub<0>());
273 FAIL() <<
"Expected std::runtime_error";
275 catch (std::runtime_error
const &err)
277 EXPECT_EQ(std::string(
"Copy-constructor not allowed"), err.what());
281 FAIL() <<
"Expected std::runtime_error";
284 EXPECT_EQ(10, nState1.data().size());
285 for (
int i = 0; i < nState1.data().size(); ++i)
287 EXPECT_EQ(0, nState1.data()(i));
294 using StateMapArray::StateMapArray;
300 TEST(TestStateMap, ExtendedClassInStateMap)
308 TEST(TestStateMap, NestedArrayVectorOneLvl)
310 constexpr
const size_t sizeLvl0 = 4;
314 NestedArrayLvl0 nArrLvl0;
315 NestedVectorLvl0 nVecLvl0;
318 TEST(TestStateMap, NestedArraysCompileTimeSize)
320 constexpr
const size_t sizeLvl0 = 4;
321 constexpr
const size_t sizeLvl1 = 3;
326 EXPECT_EQ(sizeLvl0, nArrLvl0.MapSize);
328 EXPECT_EQ(sizeLvl1 * sizeLvl0, nArrLvl1.MapSize);
331 TEST(TestStateMap, NestedVectorsCompileTimeSize)
338 EXPECT_EQ(Eigen::Dynamic, nLvl0.MapSize);
339 EXPECT_EQ(Eigen::Dynamic, nLvl1.MapSize);
342 TEST(TestStateMap, NestedArraysVectorsCompileTimeSize)
349 NestedArray6Vector nA6V;
350 EXPECT_EQ(Eigen::Dynamic, nA6V.MapSize);
351 EXPECT_EQ(Eigen::Dynamic, nA6V.sub(0).MapSize);
352 NestedVectorArray4 nVA5;
353 EXPECT_EQ(Eigen::Dynamic, nVA5.MapSize);
355 EXPECT_EQ(Eigen::Dynamic, nVA5.MapSize);
356 EXPECT_EQ(4, nVA5.sub(1).MapSize);
359 TEST(TestStateMap, NestedTupleCompileTimeSize)
369 using NestedTupleDynInLvl2 =
372 NestedTupleConst nConst;
373 EXPECT_EQ(24 + 4 + 24, nConst.MapSize);
374 EXPECT_EQ(24, nConst.sub<0>().MapSize);
375 EXPECT_EQ(4, nConst.sub<1>().MapSize);
376 EXPECT_EQ(24, nConst.sub<2>().MapSize);
377 EXPECT_EQ(4, nConst.sub<0>().sub(0).MapSize);
378 EXPECT_EQ(4, nConst.sub<2>().sub(0).MapSize);
380 NestedTupleDynInLvl1 nDynLvl1;
381 nDynLvl1.sub<2>().subResize(2);
382 EXPECT_EQ(Eigen::Dynamic, nDynLvl1.MapSize);
383 EXPECT_EQ(24 + 4 + 24, nDynLvl1.sub<0>().MapSize);
384 EXPECT_EQ(Eigen::Dynamic, nDynLvl1.sub<1>().MapSize);
385 EXPECT_EQ(Eigen::Dynamic, nDynLvl1.sub<2>().MapSize);
386 EXPECT_EQ(4, nDynLvl1.sub<2>().sub(0).MapSize);
388 NestedArray6Array6Vector nDynArArVec;
389 nDynArArVec.sub(0).sub(0).subResize(2);
390 EXPECT_EQ(Eigen::Dynamic, nDynArArVec.MapSize);
391 EXPECT_EQ(Eigen::Dynamic, nDynArArVec.sub(0).MapSize);
392 EXPECT_EQ(Eigen::Dynamic, nDynArArVec.sub(0).sub(0).MapSize);
394 NestedTupleDynInLvl2 nDynLvl2;
395 nDynLvl2.sub<1>().sub(0).sub(0).subResize(2);
396 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.MapSize);
397 EXPECT_EQ(24 + 4 + 24, nDynLvl2.sub<0>().MapSize);
398 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<1>().MapSize);
399 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<2>().MapSize);
400 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<1>().sub(0).MapSize);
401 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<1>().sub(0).sub(0).MapSize);
402 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<2>().sub(0).MapSize);
403 EXPECT_EQ(Eigen::Dynamic, nDynLvl2.sub<2>().sub(0).sub(0).MapSize);
406 TEST(TestStateMap, NestedStateResize)
418 NestedTupleLvl2 nState;
419 EXPECT_EQ(4, nState.data().size());
420 for (
int i = 0; i < nState.data().size(); ++i)
422 nState.
data()(i) = 1;
425 nState.sub<2>().subResize(2);
426 EXPECT_EQ(4 + 2 * 4, nState.data().size());
428 for (
int i = 0; i < nState.sub<2>().sub(0).data().size(); ++i)
430 nState.sub<2>().sub(0).data()(i) = 2;
432 for (
int i = 0; i < nState.sub<2>().sub(1).data().size(); ++i)
434 nState.sub<2>().sub(1).data()(i) = 3;
437 nState.sub<1>().sub(0).sub(0).subResize(1);
438 for (
int i = 0; i < nState.sub<1>().sub(0).sub(0).data().size(); ++i)
440 nState.sub<1>().sub(0).sub(0).data()(i) = 4;
442 EXPECT_EQ(4 + 2 * 4 + 1, nState.data().size());
443 nState.sub<1>().sub(0).sub(1).subResize(2);
444 for (
int i = 0; i < nState.sub<1>().sub(0).sub(1).data().size(); ++i)
446 nState.sub<1>().sub(0).sub(1).data()(i) = 5;
448 EXPECT_EQ(4 + 2 * 4 + 1 + 2, nState.data().size());
449 nState.sub<1>().sub(1).sub(0).subResize(3);
450 for (
int i = 0; i < nState.sub<1>().sub(1).sub(0).data().size(); ++i)
452 nState.sub<1>().sub(1).sub(0).data()(i) = 6;
454 EXPECT_EQ(4 + 2 * 4 + 1 + 2 + 3, nState.data().size());
456 nState.sub<0>().sub<0>().subResize(3);
457 for (
int i = 0; i < nState.sub<0>().sub<0>().data().size(); ++i)
459 nState.sub<0>().sub<0>().data()(i) = 7;
461 EXPECT_EQ(4 + 2 * 4 + 1 + 2 + 3 + 3, nState.data().size());
462 nState.sub<0>().sub<2>().subResize(2);
463 for (
int i = 0; i < nState.sub<0>().sub<2>().data().size(); ++i)
465 nState.sub<0>().sub<2>().data()(i) = 8;
467 EXPECT_EQ(4 + 2 * 4 + 1 + 2 + 3 + 3 + 2 * 6, nState.data().size());
469 std::array<double, 4 + 2 * 4 + 1 + 2 + 3 + 3 + 2 * 6> numRes = { 7, 7, 7, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
470 8, 8, 4, 5, 5, 6, 6, 6, 2, 2, 2, 2, 3, 3, 3, 3 };
471 for (
size_t i = 0; i < numRes.size(); ++i)
473 EXPECT_EQ(numRes[i], nState.data()(i));
476 nState.sub<0>().sub<0>().subResize(1);
477 nState.sub<2>().subResize(1);
478 std::array<double, 4 + 1 * 4 + 1 + 2 + 3 + 1 + 2 * 6> numRes2 = { 7, 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 8, 8, 8,
479 8, 8, 8, 4, 5, 5, 6, 6, 6, 2, 2, 2, 2 };
480 for (
size_t i = 0; i < numRes2.size(); ++i)
482 EXPECT_EQ(numRes2[i], nState.data()(i));
485 nState.sub<0>().sub<0>().subResize(0);
486 nState.sub<2>().subResize(0);
487 std::array<double, 4 + 0 * 4 + 1 + 2 + 3 + 0 + 2 * 6> numRes3 = { 1, 1, 1, 1, 8, 8, 8, 8, 8, 8, 8,
488 8, 8, 8, 8, 8, 4, 5, 5, 6, 6, 6 };
489 for (
size_t i = 0; i < numRes3.size(); ++i)
491 EXPECT_EQ(numRes3[i], nState.data()(i));
495 TEST(TestStateMap, ResizeWithEmptyVectors)
497 constexpr
const size_t sizeLvl0 = 4;
504 nArr.
sub(0).subResize(2);
507 nState.sub(0).sub(0).subResize(1);
509 for (
int i = 0; i < nState.data().size(); ++i)
511 nState.data()(i) = 1;
514 nState.sub(0).sub(0).subResize(3);
515 std::array<double, 3> numRes0 = { 1, 0, 0 };
516 for (
int i = 0; i < nState.data().size(); ++i)
518 EXPECT_EQ(numRes0[i], nState.data()(i));
524 for (
int i = 0; i < nState.data().size(); ++i)
526 EXPECT_EQ(numRes0[i], nState.data()(i));
530 TEST(TestStateMap, NestedArraysDataBuffer)
532 constexpr
const size_t sizeLvl0 = 4;
533 constexpr
const size_t sizeLvl1 = 3;
537 Eigen::Vector4d inc4(1., 2., 3., 4.);
538 Eigen::Vector3d dec3(9., 8., 7.);
541 nArrLvl1.data().block(0, 0, 3, 1) = dec3;
542 nArrLvl1.data().block(3, 0, 3, 1) = dec3;
543 nArrLvl1.data().block(6, 0, 3, 1) = dec3;
544 nArrLvl1.data().block(9, 0, 3, 1) = dec3;
545 for (
int i = 0; i < nArrLvl1.data().size(); ++i)
547 EXPECT_EQ(9 - i % 3, nArrLvl1.data()(i));
549 nArrLvl1.data().block(7, 0, 4, 1) = inc4;
550 for (
int i = 7; i < 7 + 4; ++i)
552 EXPECT_EQ(i - 7 + 1, nArrLvl1.data()(i));
554 nArrLvl1.sub(0).data() = inc4;
555 nArrLvl1.sub(2).data() = inc4;
556 for (
int i = 0; i < nArrLvl1.sub(0).data().rows(); ++i)
558 EXPECT_EQ(i + 1, nArrLvl1.data()(i));
560 for (
int i = 2 * (
int)sizeLvl0; i < 2 * (int)sizeLvl0 + nArrLvl1.sub(3).data().rows(); ++i)
562 EXPECT_EQ(i + 1 - 2 * sizeLvl0, nArrLvl1.data()(i));
566 TEST(TestStateMap, VirtualBase)
572 std::shared_ptr<StateMapBaseVirt<double>> nStateBaseVirt = std::shared_ptr<NestedArray4VectorArray6>(
new NestedArray4VectorArray6);
574 nStateBaseVirt->
sub(0).subResize(2);
575 for (
int i = 0; i < nStateBaseVirt->data().size(); ++i)
577 nStateBaseVirt->data()(i) = i;
580 for (
int i = 0; i < nStateBaseVirt->data().size(); ++i)
582 EXPECT_EQ(i, nStateBaseVirt->data()(i));
587 nStateBaseVirt->subResize(2);
588 FAIL() <<
"Expected std::runtime_error";
590 catch (std::runtime_error
const &err)
592 EXPECT_EQ(std::string(
"Cannot resize an array"), err.what());
596 FAIL() <<
"Expected std::runtime_error";
601 nStateBaseVirt->sub(0).sub(0).sub(0);
602 FAIL() <<
"Expected std::runtime_error";
604 catch (std::runtime_error
const &err)
606 EXPECT_EQ(std::string(
"Access of numeric Leaf using virtual \"sub\" function not allowed"), err.what());
610 FAIL() <<
"Expected std::runtime_error";
615 std::shared_ptr<StateMapBaseVirt<double>> nStateTupBaseVirt = std::shared_ptr<NestedTupleArray4VectorArray6>(
new NestedTupleArray4VectorArray6);
617 nStateTupBaseVirt->
sub(1).subResize(2);
618 for (
int i = 0; i < nStateTupBaseVirt->data().size(); ++i)
620 nStateTupBaseVirt->data()(i) = i;
624 nStateTupBaseVirt->subResize(2);
625 FAIL() <<
"Expected std::runtime_error";
627 catch (std::runtime_error
const &err)
629 EXPECT_EQ(std::string(
"Cannot resize a tuple"), err.what());
633 FAIL() <<
"Expected std::runtime_error";
636 std::shared_ptr<NestedTupleArray4VectorArray6> nStateTupExt =
637 dynamic_pointer_cast<NestedTupleArray4VectorArray6>(nStateTupBaseVirt);
638 std::shared_ptr<StateMapBaseCRTP<NestedTupleArray4VectorArray6>> nStateTupBaseCRTP = nStateTupExt;
640 for (
int i = 0; i < nStateTupBaseVirt->sub(1).data().size(); ++i)
642 EXPECT_EQ(i + 6, nStateTupBaseVirt->sub(1).data()(i));
644 for (
int i = 0; i < nStateTupExt->sub<1>().data().size(); ++i)
646 EXPECT_EQ(i + 6, nStateTupExt->sub<1>().data()(i));
648 for (
int i = 0; i < nStateTupBaseCRTP->sub<1>().data().size(); ++i)
650 EXPECT_EQ(i + 6, nStateTupBaseCRTP->sub<1>().data()(i));
656 int main(
int argc,
char **argv)
658 testing::InitGoogleTest(&argc, argv);
660 return RUN_ALL_TESTS();
Helper function needed to upgrade c++ 2011.
void subResize(const size_t &_size)
LeafType & sub(const size_t &_i)
int main(int argc, char **argv)
TEST(TestStateMap, VirtualBase)