CellModules
test.cpp
Go to the documentation of this file.
1#include "catch.hpp"
2#define protected public
3#define private public
4#include "../mecacell/mecacell.h"
5#undef protected
6#undef private
7#include <iostream>
8
9using namespace MecaCell;
10
11bool doubleEq(double a, double b) { return abs(a - b) < 0.000000001; }
12
13class VolCell : public MecaCell::ConnectableCell<VolCell, ContactSurfaceBody> {
14 public:
16 using Base::Base;
17 double getAdhesionWith(const VolCell*, const MecaCell::Vec&) const { return 0.0; }
18 template <typename W> void updateBehavior(W&) {}
19};
20
21template <typename W> void checkThatWorldssAreIdentical(W& w0, W& w1) {
22 for (size_t c = 0; c < w0.cells.size(); ++c) {
23 REQUIRE(w0.cells[c]->id == w1.cells[c]->id);
24 REQUIRE(w0.cells[c]->getPosition() == w1.cells[c]->getPosition());
25 REQUIRE(w0.cells[c]->getVelocity() == w1.cells[c]->getVelocity());
26 REQUIRE(w0.cells[c]->getForce() == w1.cells[c]->getForce());
27 REQUIRE(w0.cells[c]->getExternalForces() == w1.cells[c]->getExternalForces());
28 REQUIRE(w0.cells[c]->getConnectedCells().size() ==
29 w1.cells[c]->getConnectedCells().size());
30 auto connected = w0.cells[c]->getConnectedCells();
31 for (const auto& other : connected) {
32 bool sameConnections = false;
33 for (auto& other1 : w1.cells[c]->getConnectedCells()) {
34 if (other1->id == other->id) {
35 sameConnections = true;
36 break;
37 }
38 }
39 REQUIRE(sameConnections);
40 }
41 }
42}
43
44template <typename W> void printCells(W& w) {
45 std::cerr << " ---- (update " << w.getNbUpdates() << ") ---- " << std::endl;
46 std::cerr << " " << w.cells.size() << " cells:" << std::endl;
47 for (auto& c : w.cells) {
48 std::cerr << YELLOW << "cell " << c->id << RESET << " ( adr = " << c
49 << "):" << std::endl;
50 std::cerr << " -- connectedCells:" << std::endl;
51 for (auto& co : c->connectedCells)
52 std::cerr << " |> " << BOLDBLUE << co->id << RESET << " (" << co << ")"
53 << std::endl;
54 std::cerr << " -- cellConnections:" << std::endl;
55 for (auto& co : c->membrane.cccm.cellConnections) {
56 std::cerr << " |> Connection " << co << " btwn " << co->c0->id << " & "
57 << co->c1->id << std::endl;
58 }
59 }
60 std::cerr << BOLDYELLOW << ">>> World stored connections ("
61 << w.cellCellConnections.size() << "): " << RESET << std::endl;
62 for (auto& co : w.cellCellConnections) {
63 std::cerr << " |> (" << co.second.get() << ") between cells " << BOLDBLUE
64 << co.second->c0->id << " & " << co.second->c1->id << RESET << std::endl;
65 }
66}
67
68TEST_CASE("World creation, cell additions & deletion") {
70 REQUIRE(w.getNbUpdates() == 0);
71 w.update();
72 REQUIRE(w.cells.size() == 0);
73 REQUIRE(w.getNbUpdates() == 1);
74 w.addCell(new VolCell());
75 w.update();
76 REQUIRE(w.cells.size() == 1);
77 REQUIRE(w.cells[0]->getPosition() == MecaCell::Vector3D(0, 0, 0));
78 w.update();
79 REQUIRE(w.cells[0]->getPosition() == MecaCell::Vector3D(0, 0, 0));
80 Vector3D secondCellPos(50, 0, 0);
81 w.addCell(new VolCell(secondCellPos));
82 w.update();
83 REQUIRE(w.cells.size() == 2);
84 REQUIRE(w.cells[1]->getPosition() == secondCellPos);
85}
86
87// TEST_CASE("Cells update determinism") {
88// using Cell = VolCell;
89// using World = MecaCell::BasicWorld<Cell>;
90// for (int n = 0; n < 15; ++n) {
91// World w0, w1, w2;
92// const int nbC = 8;
93// for (int i = 0; i < nbC; ++i) {
94// auto pos = MecaCell::Vec::randomUnit() * 50.0;
95// w0.addCell(new Cell(pos));
96// w1.addCell(new Cell(pos));
97// w2.addCell(new Cell(pos));
98//}
99// REQUIRE(w0.cells.size() == nbC);
100// REQUIRE(w0.cells.size() == w1.cells.size());
101// REQUIRE(w1.cells.size() == w2.cells.size());
102// for (int l = 0; l < 300; ++l) {
103// w0.update();
104// w1.update();
105// w2.update();
106// checkThatCellsAreIdentical(w0, w1);
107// checkThatCellsAreIdentical(w1, w2);
108//}
109// w0.cells[1]->die();
110// w1.cells[1]->die();
111// w2.cells[1]->die();
112// for (int l = 0; l < 500; ++l) {
113// w0.update();
114// w1.update();
115// w2.update();
116// checkThatCellsAreIdentical(w0, w1);
117// checkThatCellsAreIdentical(w1, w2);
118//}
119// REQUIRE(w0.cells.size() == nbC - 1);
120// REQUIRE(w0.cells.size() == w1.cells.size());
121// REQUIRE(w1.cells.size() == w2.cells.size());
122//}
123//}
#define REQUIRE(expr)
Definition: catch.hpp:9333
Basis for every cell a user might want to use.
general purpose 3D vector/point class.
Definition: vector3D.h:20
Where "everything" happens.
Definition: world.hpp:25
size_t getNbUpdates() const
get the number of update since the creation of the world
Definition: world.hpp:193
void update()
main update method
Definition: world.hpp:323
void addCell(Cell *c)
adds a cell to the new cells batch (which will be added to the main cells container at the end of the...
Definition: world.hpp:219
Definition: test.cpp:13
double getAdhesionWith(const VolCell *, const MecaCell::Vec &) const
Definition: test.cpp:17
void updateBehavior(W &)
Definition: test.cpp:18
this file contains various miscellanious utility functions & helpers *
const static constexpr char YELLOW[]
Definition: logger.hpp:44
const static constexpr char BOLDYELLOW[]
Definition: logger.hpp:52
const static constexpr char BOLDBLUE[]
Definition: logger.hpp:53
const static constexpr char RESET[]
Definition: logger.hpp:40
int abs(int x)
Computes the absolute value of an integer.
Definition: std.hpp:83
iostream cerr
Standard error stream.
Definition: std.hpp:281
iostream endl
End-of-line manipulator.
Definition: std.hpp:274
void printCells(W &w)
Definition: test.cpp:44
TEST_CASE("World creation, cell additions & deletion")
Definition: test.cpp:68
bool doubleEq(double a, double b)
Definition: test.cpp:11
void checkThatWorldssAreIdentical(W &w0, W &w1)
Definition: test.cpp:21