gtest_blackboard.cpp
Go to the documentation of this file.
00001 /* Copyright (C) 2018 Davide Faconti - 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 #include "behaviortree_cpp/blackboard/blackboard_local.h"
00018 
00019 using namespace BT;
00020 
00021 class InitTestNode: public SyncActionNode
00022 {
00023   public:
00024     InitTestNode(bool try_to_access_bb, const std::string& name):
00025       SyncActionNode(name),
00026       _value(0)
00027     {
00028         if( try_to_access_bb )
00029         {
00030             // this should throw
00031             blackboard()->set(KEY(), 33);
00032         }
00033     }
00034 
00035     void onInit() {
00036         blackboard()->get(KEY(), _value);
00037     }
00038 
00039     NodeStatus tick()
00040     {
00041         _value *= 2;
00042         blackboard()->set(KEY(), _value);
00043         return NodeStatus::SUCCESS;
00044     }
00045 
00046     static const char* KEY() { return "my_entry"; }
00047 
00048   private:
00049     int _value;
00050 };
00051 
00052 
00053 
00054 
00055 /****************TESTS START HERE***************************/
00056 
00057 TEST(BlackboardTest, CheckOInit)
00058 {
00059     auto bb = Blackboard::create<BlackboardLocal>();
00060     const auto KEY = InitTestNode::KEY();
00061 
00062     EXPECT_THROW( InitTestNode(true,"init_test"), std::logic_error );
00063 
00064     InitTestNode node(false,"init_test");
00065     node.setBlackboard(bb);
00066 
00067     bb->set(KEY, 11 );
00068 
00069     // this should read and write "my_entry", respectively in onInit() and tick()
00070     node.executeTick();
00071 
00072     ASSERT_EQ( bb->get<int>(KEY), 22 );
00073 
00074     // check that onInit is executed only once
00075     bb->set(KEY, 1 );
00076 
00077     // since this value is read in OnInit(), the node will not notice the change in bb
00078     node.setStatus( NodeStatus::IDLE );
00079     node.executeTick();
00080     ASSERT_EQ( bb->get<int>(KEY), 44 );
00081 }


behaviortree_cpp
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Sat Feb 2 2019 03:50:10