Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0e8680b

Browse files
author
Davide Faconti
committed
more comments
1 parent 3383e5a commit 0e8680b

File tree

1 file changed

+29
-22
lines changed

1 file changed

+29
-22
lines changed

gtest/navigation_test.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,15 @@ class FollowPath: public CoroActionNode, public TestNode
147147

148148
//-------------------------------------
149149

150+
template <typename Original, typename Casted>
151+
void TryDynamicCastPtr(Original* ptr, Casted*& destination)
152+
{
153+
if( dynamic_cast<Casted*>(ptr) )
154+
{
155+
destination = dynamic_cast<Casted*>(ptr);
156+
}
157+
}
158+
150159
/****************TESTS START HERE***************************/
151160

152161
TEST(Navigationtest, MoveBaseReocvery)
@@ -160,6 +169,8 @@ TEST(Navigationtest, MoveBaseReocvery)
160169

161170
auto tree = buildTreeFromText(factory, xml_text);
162171

172+
// Need to retrieve the node pointers with dynamic cast
173+
// In a normal application you would NEVER want to do such a thing.
163174
IsStuck *first_stuck_node = nullptr;
164175
IsStuck *second_stuck_node = nullptr;
165176
BackUpAndSpin* back_spin_node = nullptr;
@@ -169,25 +180,17 @@ TEST(Navigationtest, MoveBaseReocvery)
169180
for (auto& node: tree.nodes)
170181
{
171182
auto ptr = node.get();
172-
if( dynamic_cast<IsStuck*>(ptr) )
183+
184+
if( !first_stuck_node )
173185
{
174-
if( !first_stuck_node )
175-
{
176-
first_stuck_node = dynamic_cast<IsStuck*>(ptr);
177-
}
178-
else{
179-
second_stuck_node = dynamic_cast<IsStuck*>(ptr);
180-
}
181-
}
182-
else if( dynamic_cast<BackUpAndSpin*>(ptr) ){
183-
back_spin_node = dynamic_cast<BackUpAndSpin*>(ptr);
184-
}
185-
else if( dynamic_cast<ComputePathToPose*>(ptr) ){
186-
compute_node = dynamic_cast<ComputePathToPose*>(ptr);
186+
TryDynamicCastPtr(ptr, first_stuck_node);
187187
}
188-
else if( dynamic_cast<FollowPath*>(ptr) ){
189-
follow_node = dynamic_cast<FollowPath*>(ptr);
188+
else{
189+
TryDynamicCastPtr(ptr, second_stuck_node);
190190
}
191+
TryDynamicCastPtr(ptr, back_spin_node);
192+
TryDynamicCastPtr(ptr, follow_node);
193+
TryDynamicCastPtr(ptr, compute_node);
191194
}
192195

193196
ASSERT_TRUE( first_stuck_node );
@@ -196,15 +199,15 @@ TEST(Navigationtest, MoveBaseReocvery)
196199
ASSERT_TRUE( compute_node );
197200
ASSERT_TRUE( follow_node );
198201

202+
std::cout << "-----------------------" << std::endl;
203+
204+
// First case: not stuck, everything fine.
199205
NodeStatus status = NodeStatus::IDLE;
200206

201207
auto deadline = Now() + Milliseconds(100);
202208

203209
first_stuck_node->setExpectedResult(false);
204210

205-
std::cout << "-----------------------" << std::endl;
206-
// First case: not stuck, everything fine.
207-
208211
while( status == NodeStatus::IDLE || status == NodeStatus::RUNNING )
209212
{
210213
status = tree.root_node->executeTick();
@@ -225,19 +228,23 @@ TEST(Navigationtest, MoveBaseReocvery)
225228
ASSERT_FALSE( follow_node->wasHalted() );
226229

227230
std::cout << "-----------------------" << std::endl;
231+
232+
// Second case: get stuck after a while
233+
234+
// Initialize evrything first
228235
first_stuck_node->resetTickCount();
229236
second_stuck_node->resetTickCount();
230237
compute_node->resetTickCount();
231238
follow_node->resetTickCount();
232239
back_spin_node->resetTickCount();
233-
234240
status = NodeStatus::IDLE;
235-
236241
int cycle = 0;
237242
deadline = Now() + Milliseconds(100);
243+
238244
while( status == NodeStatus::IDLE || status == NodeStatus::RUNNING )
239245
{
240-
if( cycle++ == 5 )
246+
// At the fifth cycle get stucked
247+
if( ++cycle == 5 )
241248
{
242249
first_stuck_node->setExpectedResult(true);
243250
second_stuck_node->setExpectedResult(true);

0 commit comments

Comments
 (0)