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

Skip to content

Commit 91994f8

Browse files
committed
initiate unit test for reactive sequence
1 parent bc1f378 commit 91994f8

File tree

3 files changed

+270
-10
lines changed

3 files changed

+270
-10
lines changed

tests/CMakeLists.txt

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@ include_directories(include)
66
set(BT_TESTS
77
src/action_test_node.cpp
88
src/condition_test_node.cpp
9-
gtest_tree.cpp
10-
gtest_sequence.cpp
11-
gtest_parallel.cpp
12-
gtest_fallback.cpp
13-
gtest_factory.cpp
14-
gtest_decorator.cpp
15-
gtest_blackboard.cpp
16-
gtest_coroutines.cpp
17-
navigation_test.cpp
18-
gtest_subtree.cpp
9+
# gtest_tree.cpp
10+
# gtest_sequence.cpp
11+
# gtest_parallel.cpp
12+
# gtest_fallback.cpp
13+
# gtest_factory.cpp
14+
# gtest_decorator.cpp
15+
# gtest_blackboard.cpp
16+
# gtest_coroutines.cpp
17+
# navigation_test.cpp
18+
# gtest_subtree.cpp
19+
#gtest_reactive_sequence.cpp
20+
gtest_reactive_tree.cpp
21+
1922
)
2023

2124
if(ament_cmake_FOUND AND BUILD_TESTING)

tests/gtest_reactive_sequence.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,135 @@
11

2+
/* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
*
9+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
11+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12+
*/
13+
14+
#include <gtest/gtest.h>
15+
#include "action_test_node.h"
16+
#include "condition_test_node.h"
17+
#include "behaviortree_cpp/behavior_tree.h"
18+
19+
using BT::NodeStatus;
20+
using std::chrono::milliseconds;
21+
22+
struct ComplexReactiveSequence2ActionsTest : testing::Test
23+
{
24+
BT::ReactiveSequence root;
25+
BT::AsyncActionTest action_1;
26+
BT::AsyncActionTest action_2;
27+
BT::ReactiveSequence seq_1;
28+
BT::ReactiveSequence seq_2;
29+
30+
BT::ConditionTestNode condition_1;
31+
BT::ConditionTestNode condition_2;
32+
33+
ComplexReactiveSequence2ActionsTest()
34+
: root("root_sequence")
35+
, action_1("action_1", milliseconds(100))
36+
, action_2("action_2", milliseconds(100))
37+
, seq_1("sequence_1")
38+
, seq_2("sequence_2")
39+
, condition_1("condition_1")
40+
, condition_2("condition_2")
41+
{
42+
root.addChild(&seq_1);
43+
{
44+
seq_1.addChild(&condition_1);
45+
seq_1.addChild(&action_1);
46+
}
47+
root.addChild(&seq_2);
48+
{
49+
seq_2.addChild(&condition_2);
50+
seq_2.addChild(&action_2);
51+
}
52+
}
53+
~ComplexReactiveSequence2ActionsTest()
54+
{
55+
haltAllActions(&root);
56+
}
57+
};
58+
59+
struct ComplexReactiveTree : testing::Test
60+
{
61+
BT::ReactiveSequence root;
62+
BT::AsyncActionTest action_1;
63+
BT::AsyncActionTest action_2;
64+
BT::ReactiveFallback fal_1;
65+
BT::ReactiveFallback fal_2;
66+
67+
BT::ConditionTestNode condition_1;
68+
BT::ConditionTestNode condition_2;
69+
70+
ComplexReactiveTree()
71+
: root("root_sequence")
72+
, action_1("action_1", milliseconds(100))
73+
, action_2("action_2", milliseconds(100))
74+
, fal_1("fallback_1")
75+
, fal_2("fallback_2")
76+
, condition_1("condition_1")
77+
, condition_2("condition_2")
78+
{
79+
root.addChild(&fal_1);
80+
{
81+
fal_1.addChild(&condition_1);
82+
fal_1.addChild(&action_1);
83+
}
84+
root.addChild(&fal_2);
85+
{
86+
fal_2.addChild(&condition_2);
87+
fal_2.addChild(&action_2);
88+
}
89+
}
90+
~ComplexReactiveTree()
91+
{
92+
haltAllActions(&root);
93+
}
94+
};
95+
96+
/****************TESTS START HERE***************************/
97+
98+
99+
TEST_F(ComplexReactiveSequence2ActionsTest, ConditionsTrue)
100+
{
101+
BT::NodeStatus state = root.executeTick();
102+
103+
state = root.executeTick();
104+
105+
ASSERT_EQ(NodeStatus::RUNNING, state);
106+
ASSERT_EQ(NodeStatus::RUNNING, seq_1.status());
107+
ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
108+
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
109+
ASSERT_EQ(NodeStatus::IDLE, seq_2.status());
110+
ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
111+
ASSERT_EQ(NodeStatus::IDLE, action_2.status());
112+
113+
std::this_thread::sleep_for(milliseconds(300));
114+
state = root.executeTick();
115+
116+
ASSERT_EQ(NodeStatus::RUNNING, state);
117+
ASSERT_EQ(NodeStatus::SUCCESS, seq_1.status());
118+
ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
119+
ASSERT_EQ(NodeStatus::IDLE, action_1.status());
120+
ASSERT_EQ(NodeStatus::RUNNING, seq_2.status());
121+
ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status());
122+
ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
123+
124+
125+
std::this_thread::sleep_for(milliseconds(300));
126+
state = root.executeTick();
127+
128+
ASSERT_EQ(NodeStatus::RUNNING, state);
129+
ASSERT_EQ(NodeStatus::RUNNING, seq_1.status());
130+
ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
131+
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
132+
ASSERT_EQ(NodeStatus::IDLE, seq_2.status());
133+
ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
134+
ASSERT_EQ(NodeStatus::IDLE, action_2.status());;
135+
}

tests/gtest_reactive_tree.cpp

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,124 @@
11

2+
3+
/* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
6+
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
8+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
11+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
12+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13+
*/
14+
15+
#include <gtest/gtest.h>
16+
#include "action_test_node.h"
17+
#include "condition_test_node.h"
18+
#include "behaviortree_cpp/behavior_tree.h"
19+
20+
using BT::NodeStatus;
21+
using std::chrono::milliseconds;
22+
23+
24+
struct ComplexReactiveTree : testing::Test
25+
{
26+
BT::ReactiveSequence root;
27+
BT::AsyncActionTest action_1;
28+
BT::AsyncActionTest action_2;
29+
BT::ReactiveFallback fal_1;
30+
BT::ReactiveFallback fal_2;
31+
32+
BT::ConditionTestNode condition_1;
33+
BT::ConditionTestNode condition_2;
34+
35+
ComplexReactiveTree()
36+
: root("root_sequence")
37+
, action_1("action_1", milliseconds(5000))
38+
, action_2("action_2", milliseconds(5000))
39+
, fal_1("fallback_1")
40+
, fal_2("fallback_2")
41+
, condition_1("condition_1")
42+
, condition_2("condition_2")
43+
{
44+
root.addChild(&fal_1);
45+
{
46+
fal_1.addChild(&condition_1);
47+
fal_1.addChild(&action_1);
48+
}
49+
root.addChild(&fal_2);
50+
{
51+
fal_2.addChild(&condition_2);
52+
fal_2.addChild(&action_2);
53+
}
54+
}
55+
~ComplexReactiveTree()
56+
{
57+
haltAllActions(&root);
58+
}
59+
};
60+
61+
/****************TESTS START HERE***************************/
62+
63+
64+
TEST_F(ComplexReactiveTree, ConditionsFalse)
65+
{
66+
67+
condition_1.setBoolean(false);
68+
condition_2.setBoolean(false);
69+
70+
BT::NodeStatus state = root.executeTick();
71+
72+
state = root.executeTick();
73+
74+
ASSERT_EQ(NodeStatus::RUNNING, state);
75+
ASSERT_EQ(NodeStatus::RUNNING, fal_1.status());
76+
ASSERT_EQ(NodeStatus::FAILURE, condition_1.status());
77+
ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
78+
ASSERT_EQ(NodeStatus::IDLE, fal_2.status());
79+
ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
80+
ASSERT_EQ(NodeStatus::IDLE, action_2.status());
81+
82+
std::this_thread::sleep_for(milliseconds(300));
83+
84+
condition_1.setBoolean(true);
85+
86+
// state = root.executeTick();
87+
88+
89+
// ASSERT_EQ(NodeStatus::RUNNING, state);
90+
// ASSERT_EQ(NodeStatus::SUCCESS, fal_1.status());
91+
// ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
92+
// ASSERT_EQ(NodeStatus::IDLE, action_1.status());
93+
// ASSERT_EQ(NodeStatus::RUNNING, fal_2.status());
94+
// ASSERT_EQ(NodeStatus::FAILURE, condition_2.status());
95+
// ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
96+
97+
98+
// std::this_thread::sleep_for(milliseconds(300));
99+
100+
// state = root.executeTick();
101+
102+
// ASSERT_EQ(NodeStatus::RUNNING, state);
103+
// ASSERT_EQ(NodeStatus::SUCCESS, fal_1.status());
104+
// ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
105+
// ASSERT_EQ(NodeStatus::IDLE, action_1.status());
106+
// ASSERT_EQ(NodeStatus::RUNNING, fal_2.status());
107+
// ASSERT_EQ(NodeStatus::FAILURE, condition_2.status());
108+
// ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
109+
110+
// std::this_thread::sleep_for(milliseconds(300));
111+
112+
// condition_1.setBoolean(false);
113+
114+
// state = root.executeTick();
115+
116+
// ASSERT_EQ(NodeStatus::RUNNING, state);
117+
// ASSERT_EQ(NodeStatus::RUNNING, fal_1.status());
118+
// ASSERT_EQ(NodeStatus::FAILURE, condition_1.status());
119+
// ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
120+
// ASSERT_EQ(NodeStatus::IDLE, fal_2.status());
121+
// ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
122+
// ASSERT_EQ(NodeStatus::IDLE, action_2.status());
123+
124+
}

0 commit comments

Comments
 (0)