gtest_parallel.cpp
Go to the documentation of this file.
00001 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
00002 *
00003 *   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
00004 *   to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
00005 *   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:
00006 *   The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
00007 *
00008 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00009 *   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,
00010 *   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.
00011 */
00012 
00013 #include <gtest/gtest.h>
00014 #include "action_test_node.h"
00015 #include "condition_test_node.h"
00016 #include "behaviortree_cpp/behavior_tree.h"
00017 
00018 using BT::NodeStatus;
00019 using std::chrono::milliseconds;
00020 
00021 struct SimpleParallelTest : testing::Test
00022 {
00023     BT::ParallelNode root;
00024     BT::AsyncActionTest action_1;
00025     BT::ConditionTestNode condition_1;
00026 
00027     BT::AsyncActionTest action_2;
00028     BT::ConditionTestNode condition_2;
00029 
00030     SimpleParallelTest()
00031       : root("root_parallel", 4)
00032       , action_1("action_1", milliseconds(100) )
00033       , condition_1("condition_1")
00034       , action_2("action_2", milliseconds(300))
00035       , condition_2("condition_2")
00036     {
00037         root.addChild(&condition_1);
00038         root.addChild(&action_1);
00039         root.addChild(&condition_2);
00040         root.addChild(&action_2);
00041     }
00042     ~SimpleParallelTest()
00043     {
00044         haltAllActions(&root);
00045     }
00046 };
00047 
00048 struct ComplexParallelTest : testing::Test
00049 {
00050     BT::ParallelNode parallel_root;
00051     BT::ParallelNode parallel_left;
00052     BT::ParallelNode parallel_right;
00053 
00054     BT::AsyncActionTest action_L1;
00055     BT::ConditionTestNode condition_L1;
00056 
00057     BT::AsyncActionTest action_L2;
00058     BT::ConditionTestNode condition_L2;
00059 
00060     BT::AsyncActionTest action_R;
00061     BT::ConditionTestNode condition_R;
00062 
00063     ComplexParallelTest()
00064       : parallel_root("root", 2)
00065       , parallel_left("par1", 3)
00066       , parallel_right("par2", 1)
00067       , action_L1("action_1", milliseconds(100) )
00068       , condition_L1("condition_1")
00069       , action_L2("action_2", milliseconds(200) )
00070       , condition_L2("condition_2")
00071       , action_R("action_3", milliseconds(400) )
00072       , condition_R("condition_3")
00073     {
00074         parallel_root.addChild(&parallel_left);
00075         {
00076             parallel_left.addChild(&condition_L1);
00077             parallel_left.addChild(&action_L1);
00078             parallel_left.addChild(&condition_L2);
00079             parallel_left.addChild(&action_L2);
00080         }
00081         parallel_root.addChild(&parallel_right);
00082         {
00083             parallel_right.addChild(&condition_R);
00084             parallel_right.addChild(&action_R);
00085         }
00086     }
00087     ~ComplexParallelTest()
00088     {
00089         haltAllActions(&parallel_root);
00090     }
00091 };
00092 
00093 /****************TESTS START HERE***************************/
00094 
00095 TEST_F(SimpleParallelTest, ConditionsTrue)
00096 {
00097     BT::NodeStatus state = root.executeTick();
00098 
00099     ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
00100     ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status());
00101     ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00102     ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
00103     ASSERT_EQ(NodeStatus::RUNNING, state);
00104 
00105     std::this_thread::sleep_for( milliseconds(200) );
00106     state = root.executeTick();
00107 
00108     ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
00109     ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status());
00110     ASSERT_EQ(NodeStatus::SUCCESS, action_1.status());
00111     ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
00112     ASSERT_EQ(NodeStatus::RUNNING, state);
00113 
00114     std::this_thread::sleep_for( milliseconds(200) );
00115     state = root.executeTick();
00116 
00117     ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00118     ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00119     ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00120     ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00121     ASSERT_EQ(NodeStatus::SUCCESS, state);
00122 }
00123 
00124 TEST_F(SimpleParallelTest, Threshold_3)
00125 {
00126     root.setThresholdM(3);
00127     action_1.setTime( milliseconds(100) );
00128     action_2.setTime( milliseconds(500) ); // this takes a lot of time
00129 
00130     BT::NodeStatus state = root.executeTick();
00131     // first tick, zero wait
00132     ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
00133     ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status());
00134     ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00135     ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
00136     ASSERT_EQ(NodeStatus::RUNNING, state);
00137 
00138     std::this_thread::sleep_for( milliseconds(150) );
00139     state = root.executeTick();
00140     // second tick: action1 should be completed, but not action2
00141     // nevertheless it is sufficient because threshold is 3
00142     ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00143     ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00144     ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00145     ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00146     ASSERT_EQ(NodeStatus::SUCCESS, state);
00147 }
00148 
00149 TEST_F(SimpleParallelTest, Threshold_1)
00150 {
00151     root.setThresholdM(2);
00152     BT::NodeStatus state = root.executeTick();
00153 
00154     ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00155     ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00156     ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00157     ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00158     ASSERT_EQ(NodeStatus::SUCCESS, state);
00159 }
00160 
00161 TEST_F(ComplexParallelTest, ConditionsTrue)
00162 {
00163     BT::NodeStatus state = parallel_root.executeTick();
00164 
00165     ASSERT_EQ(NodeStatus::RUNNING, parallel_left.status());
00166     ASSERT_EQ(NodeStatus::SUCCESS, condition_L1.status());
00167     ASSERT_EQ(NodeStatus::SUCCESS, condition_L2.status());
00168     ASSERT_EQ(NodeStatus::RUNNING, action_L1.status());
00169     ASSERT_EQ(NodeStatus::RUNNING, action_L2.status());
00170 
00171     ASSERT_EQ(NodeStatus::SUCCESS, parallel_right.status());
00172     ASSERT_EQ(NodeStatus::IDLE, condition_R.status());
00173     ASSERT_EQ(NodeStatus::IDLE, action_R.status());
00174 
00175     ASSERT_EQ(NodeStatus::RUNNING, state);
00176     //----------------------------------------
00177     std::this_thread::sleep_for(milliseconds(200));
00178     state = parallel_root.executeTick();
00179 
00180     ASSERT_EQ(NodeStatus::IDLE, parallel_left.status());
00181     ASSERT_EQ(NodeStatus::IDLE, condition_L1.status());
00182     ASSERT_EQ(NodeStatus::IDLE, condition_L2.status());
00183     ASSERT_EQ(NodeStatus::IDLE, action_L1.status());
00184     ASSERT_EQ(NodeStatus::IDLE, action_L2.status());
00185 
00186     ASSERT_EQ(NodeStatus::IDLE, parallel_right.status());
00187     ASSERT_EQ(NodeStatus::IDLE, condition_R.status());
00188     ASSERT_EQ(NodeStatus::IDLE, action_R.status());
00189 
00190     ASSERT_EQ(NodeStatus::SUCCESS, state);
00191 }
00192 
00193 TEST_F(ComplexParallelTest, ConditionRightFalse)
00194 {
00195     condition_R.setBoolean(false);
00196     BT::NodeStatus state = parallel_root.executeTick();
00197 
00198     // All the actions are running
00199 
00200     ASSERT_EQ(NodeStatus::RUNNING, parallel_left.status());
00201     ASSERT_EQ(NodeStatus::SUCCESS, condition_L1.status());
00202     ASSERT_EQ(NodeStatus::SUCCESS, condition_L2.status());
00203     ASSERT_EQ(NodeStatus::RUNNING, action_L1.status());
00204     ASSERT_EQ(NodeStatus::RUNNING, action_L2.status());
00205 
00206     ASSERT_EQ(NodeStatus::RUNNING, parallel_right.status());
00207     ASSERT_EQ(NodeStatus::FAILURE, condition_R.status());
00208     ASSERT_EQ(NodeStatus::RUNNING, action_R.status());
00209 
00210     ASSERT_EQ(NodeStatus::RUNNING, state);
00211 
00212     //----------------------------------------
00213     std::this_thread::sleep_for(milliseconds(500));
00214     state = parallel_root.executeTick();
00215 
00216     ASSERT_EQ(NodeStatus::IDLE, parallel_left.status());
00217     ASSERT_EQ(NodeStatus::IDLE, condition_L1.status());
00218     ASSERT_EQ(NodeStatus::IDLE, condition_L2.status());
00219     ASSERT_EQ(NodeStatus::IDLE, action_L1.status());
00220     ASSERT_EQ(NodeStatus::IDLE, action_L2.status());
00221 
00222     ASSERT_EQ(NodeStatus::IDLE, parallel_right.status());
00223     ASSERT_EQ(NodeStatus::IDLE, condition_R.status());
00224     ASSERT_EQ(NodeStatus::IDLE, action_R.status());
00225 
00226     ASSERT_EQ(NodeStatus::SUCCESS, state);
00227 }
00228 
00229 TEST_F(ComplexParallelTest, ConditionRightFalseAction1Done)
00230 {
00231     condition_R.setBoolean(false);
00232 
00233     parallel_left.setThresholdM(4);
00234 
00235     BT::NodeStatus state = parallel_root.executeTick();
00236     std::this_thread::sleep_for(milliseconds(300));
00237 
00238     // parallel_1 hasn't realize (yet) that action_1 has succeeded
00239     ASSERT_EQ(NodeStatus::RUNNING, parallel_left.status());
00240     ASSERT_EQ(NodeStatus::SUCCESS, condition_L1.status());
00241     ASSERT_EQ(NodeStatus::SUCCESS, condition_L2.status());
00242     ASSERT_EQ(NodeStatus::SUCCESS, action_L1.status());
00243     ASSERT_EQ(NodeStatus::SUCCESS, action_L2.status());
00244 
00245     ASSERT_EQ(NodeStatus::RUNNING, parallel_right.status());
00246 
00247     //------------------------
00248     state = parallel_root.executeTick();
00249 
00250     ASSERT_EQ(NodeStatus::SUCCESS, parallel_left.status());
00251     ASSERT_EQ(NodeStatus::IDLE, condition_L1.status());
00252     ASSERT_EQ(NodeStatus::IDLE, condition_L2.status());
00253     ASSERT_EQ(NodeStatus::IDLE, action_L1.status());
00254     ASSERT_EQ(NodeStatus::IDLE, action_L2.status());
00255 
00256     ASSERT_EQ(NodeStatus::RUNNING, parallel_right.status());
00257     ASSERT_EQ(NodeStatus::RUNNING, action_R.status());
00258 
00259     ASSERT_EQ(NodeStatus::RUNNING, state);
00260 
00261     //----------------------------------
00262     std::this_thread::sleep_for(milliseconds(300));
00263     state = parallel_root.executeTick();
00264 
00265     ASSERT_EQ(NodeStatus::IDLE, parallel_left.status());
00266     ASSERT_EQ(NodeStatus::IDLE, action_L1.status());
00267 
00268     ASSERT_EQ(NodeStatus::IDLE, parallel_right.status());
00269     ASSERT_EQ(NodeStatus::IDLE, action_R.status());
00270 
00271     ASSERT_EQ(NodeStatus::SUCCESS, state);
00272 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Jun 8 2019 20:17:15