diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h index 39d6f1c88e063ef63a40c3fc46b62f1f099e0dcf..bb6ce3289bc663974fe4daef438312b20ed27548 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/AsgExampleToolsDict.h @@ -1,5 +1,5 @@ // Dear emacs, this is -*- c++ -*- -// $Id: AsgExampleToolsDict.h 744643 2016-05-03 19:12:00Z krumnack $ +// $Id: AsgExampleToolsDict.h 773002 2016-09-13 13:21:21Z krumnack $ #ifndef ASGTOOLS_ASGTOOLSDICT_H #define ASGTOOLS_ASGTOOLSDICT_H @@ -7,6 +7,7 @@ # pragma GCC diagnostic ignored "-Wdeprecated-declarations" #endif +#include "AsgExampleTools/UnitTestTool3.h" #include "AsgExampleTools/UnitTestTool2.h" #include "AsgExampleTools/UnitTestTool1A.h" diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool1.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool1.h index eb1d3ce40d4ac7d3c9f2292c867bd3532f27f552..09b1ed4b54e83a4f76c2735cdb650204e6408258 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool1.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool1.h @@ -22,6 +22,10 @@ namespace asg // Declare the interface that this class provides ASG_TOOL_INTERFACE( CP::IUnitTestTool1 ) + /// \brief get the integer property + public: + virtual std::string getPropertyString () const = 0; + /// \brief get the integer property public: virtual int getPropertyInt () const = 0; diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool2.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool2.h index 3a70128c2ba3f4d31a6ca77965be62f17c9d0fed..b8b3abec6374ca8eab70a5f0c540b41670f2e510 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool2.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool2.h @@ -24,9 +24,14 @@ namespace asg // Declare the interface that this class provides ASG_TOOL_INTERFACE( CP::IUnitTestTool2 ) + /// \brief whether the given ToolHandle is empty + public: + virtual bool + toolHandleEmpty (const std::string& handleName) const = 0; + /// \brief get the tool from the regular ToolHandle public: - virtual IUnitTestTool1 * + virtual const IUnitTestTool1 * getToolHandle (const std::string& handleName) const = 0; /// \brief call retrieve on the regular ToolHandle diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool3.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool3.h new file mode 100644 index 0000000000000000000000000000000000000000..1feccafca179408f1910f28a4acf57edca05bb2b --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/IUnitTestTool3.h @@ -0,0 +1,41 @@ +#ifndef ASG_TOOLS__I_UNIT_TEST_TOOL3_H +#define ASG_TOOLS__I_UNIT_TEST_TOOL3_H + +// Copyright Iowa State University 2016. +// Author: Nils Krumnack +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Please feel free to contact me (nils.erik.krumnack@cern.ch) for bug +// reports, feature suggestions, praise and complaints. + + +#include <AsgTools/IAsgTool.h> + +namespace asg +{ + class IUnitTestTool1; + + /// \brief the interface for \ref UnitTestTool3 + + class IUnitTestTool3 : virtual public IAsgTool + { + // Declare the interface that this class provides + ASG_TOOL_INTERFACE( CP::IUnitTestTool3 ) + + /// \brief get the subtool we configured + public: + virtual const IUnitTestTool1 *getSubtool () const = 0; + + /// \brief get whether the subtool configured for our subtool is empty + public: + virtual bool subsubtoolEmpty () const = 0; + + /// \brief get the subtool configured for our subtool + public: + virtual const IUnitTestTool1 *getSubsubtool () const = 0; + }; +} + +#endif diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1.h index 21a24b310d341eaa00a62293cf05f0fee2f4ac86..c7d12afa318fc52afc9f987401914049ffa6f2a2 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1.h @@ -30,9 +30,16 @@ namespace asg public: UnitTestTool1 (const std::string& val_name); + /// \brief standard destructor + public: + ~UnitTestTool1 (); + public: StatusCode initialize () override; + public: + virtual std::string getPropertyString () const override; + public: virtual int getPropertyInt () const override; @@ -46,6 +53,10 @@ namespace asg public: bool m_isInitialized = false; + /// \brief the string property + public: + std::string m_propertyString; + /// \brief the integer property public: int m_propertyInt = 0; @@ -53,6 +64,11 @@ namespace asg /// \brief whether initialize should fail public: bool m_initializeFail = false; + + /// \brief the number of times the tool of the given name has been + /// instantiated + public: + static int& instance_counts (const std::string& name); }; } diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1A.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1A.h index b0bed5be10bd19db49e8581614b937dae99fc4f5..abf91ea880331a4e5898123589336710b4badf02 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1A.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool1A.h @@ -33,6 +33,9 @@ namespace asg public: StatusCode initialize () override; + public: + virtual std::string getPropertyString () const override; + public: virtual int getPropertyInt () const override; @@ -46,6 +49,10 @@ namespace asg public: bool m_isInitialized = false; + /// \brief the string property + public: + std::string m_propertyString; + /// \brief the integer property public: int m_propertyInt = -7; diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool2.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool2.h index f5d57f343808dbf7fb1613848a9f25e341f6e6e7..8819f94b55414afe5e91d63b6f7797b989c44cc7 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool2.h +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool2.h @@ -35,7 +35,11 @@ namespace asg StatusCode initialize () override; public: - virtual IUnitTestTool1 * + virtual bool + toolHandleEmpty (const std::string& handleName) const override; + + public: + virtual const IUnitTestTool1 * getToolHandle (const std::string& handleName) const override; public: @@ -63,6 +67,9 @@ namespace asg private: bool m_wasUserConfiguredPrivate = false; + + private: + bool m_allowEmpty = false; }; } diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool3.h b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool3.h new file mode 100644 index 0000000000000000000000000000000000000000..f6c89ba3c3ab6d03b3dc2c6d339c7b1559df952c --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/UnitTestTool3.h @@ -0,0 +1,74 @@ +#ifndef ASG_TOOLS__UNIT_TEST_TOOL3_H +#define ASG_TOOLS__UNIT_TEST_TOOL3_H + +// Copyright Iowa State University 2016. +// Author: Nils Krumnack +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Please feel free to contact me (nils.erik.krumnack@cern.ch) for bug +// reports, feature suggestions, praise and complaints. + + +#include <AsgTools/AnaToolHandle.h> +#include <AsgTools/AsgTool.h> +#include <AsgExampleTools/IUnitTestTool3.h> + +namespace asg +{ + class IUnitTestTool2; + + /// \brief a tool used to unit test AnaToolHandle + /// + /// This allows to unit test that I can use an AnaToolHandle + /// property to provide a customization point to the user and then + /// pass it on as a property to another tool it holds and creates + /// via an AnaToolHandle. + + struct UnitTestTool3 : virtual public IUnitTestTool3, + public AsgTool + { + ASG_TOOL_CLASS (UnitTestTool3, IUnitTestTool3) + + /// \brief standard constructor + public: + UnitTestTool3 (const std::string& val_name); + + public: + StatusCode initialize () override; + + /// \brief get the subtool we configured + public: + virtual const IUnitTestTool1 *getSubtool () const override; + + /// \brief get the subtool configured for our subtool + public: + virtual const IUnitTestTool1 *getSubsubtool () const override; + + public: + virtual bool subsubtoolEmpty () const override; + + /// \brief a public tool handle the tool the user configures + private: + ToolHandle<IUnitTestTool1> m_subtool0; + + /// \brief the tool the user configures + private: + AnaToolHandle<IUnitTestTool1> m_subtool1; + + /// \brief the tool we configure + private: + AnaToolHandle<IUnitTestTool2> m_subtool2; + + /// \brief the property name we set on \ref m_subsubtool + private: + std::string m_propertyName; + + /// \brief whether to use the public tool handle + private: + bool m_usePublic = false; + }; +} + +#endif diff --git a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml index afc32a8bc374a12a8b5fa72fd01379feffab7ee8..7417c49482ad13dca316240a48892a321e3220dd 100644 --- a/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml +++ b/Control/AthToolSupport/AsgExampleTools/AsgExampleTools/selection.xml @@ -1,7 +1,8 @@ -<!-- $Id: selection.xml 744643 2016-05-03 19:12:00Z krumnack $ --> +<!-- $Id: selection.xml 773002 2016-09-13 13:21:21Z krumnack $ --> <lcgdict> <!-- Unit test types: --> + <class name="asg::UnitTestTool3" /> <class name="asg::UnitTestTool2" /> <class name="asg::UnitTestTool1A" /> diff --git a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt index d74d09cc9ccf9c51ee690d28b51dead802a4c5aa..aabcfe96c64784caf2a44f9532d81b1eaea00f34 100644 --- a/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt +++ b/Control/AthToolSupport/AsgExampleTools/CMakeLists.txt @@ -1,4 +1,4 @@ -# $Id: CMakeLists.txt 765747 2016-08-01 09:00:33Z will $ +# $Id: CMakeLists.txt 779405 2016-10-20 13:44:57Z krasznaa $ ################################################################################ # Package: AsgExampleTools ################################################################################ @@ -6,44 +6,61 @@ # Declare the package name: atlas_subdir( AsgExampleTools ) -# External dependencies: -find_package( GTest ) +# Extra dependencies based on the build environment: +set( extra_deps ) +if( XAOD_STANDALONE ) + set( extra_deps Control/xAODRootAccess ) +else() + set( extra_deps Control/AthenaBaseComps GaudiKernel ) +endif() # Declare the package's dependencies: atlas_depends_on_subdirs( PUBLIC Control/AthToolSupport/AsgTools PRIVATE - Control/AthenaBaseComps - GaudiKernel ) + ${extra_deps} ) + +# External dependencies: +find_package( GTest ) -# Component(s) in the package: -atlas_add_component( AsgExampleTools - src/*.cxx - Root/AsgHelloTool.cxx - AsgTools/UnitTestTool1.h AsgTools/UnitTestTool1A.h AsgTools/UnitTestTool2.h - Root/UnitTestTool1.cxx Root/UnitTestTool1A.cxx Root/UnitTestTool2.cxx - src/components/*.cxx - LINK_LIBRARIES AsgTools AthenaBaseComps GaudiKernel ) +# Libraries in the package: +atlas_add_root_dictionary( AsgExampleToolsLib AsgExampleToolsCintDict + ROOT_HEADERS AsgExampleTools/UnitTestTool1.h Root/LinkDef.h + EXTERNAL_PACKAGES ROOT ) -# Install files from the package: -atlas_install_headers( AsgExampleTools ) +atlas_add_library( AsgExampleToolsLib + AsgExampleTools/*.h Root/*.cxx ${AsgExampleToolsCintDict} + PUBLIC_HEADERS AsgExampleTools + LINK_LIBRARIES AsgTools ) + +if( NOT XAOD_STANDALONE ) + atlas_add_component( AsgExampleTools + src/*.h src/*.cxx src/components/*.cxx + LINK_LIBRARIES AthenaBaseComps GaudiKernel AsgExampleToolsLib ) +endif() + +atlas_add_dictionary( AsgExampleToolsDict + AsgExampleTools/AsgExampleToolsDict.h + AsgExampleTools/selection.xml + LINK_LIBRARIES AsgExampleToolsLib ) + +# Executable(s) in the package: +atlas_add_executable( AsgExampleTools_hello + util/hello.cxx + LINK_LIBRARIES AsgExampleToolsLib ) # Test(s) in the package: +set( extra_libs ) +if( XAOD_STANDALONE ) + set( extra_libs xAODRootAccess ) +endif() atlas_add_test( gt_ToolHandle_test SOURCES test/gt_ToolHandle_test.cxx - Root/UnitTestTool1.cxx Root/UnitTestTool2.cxx INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} - LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools ) + LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib ${extra_libs} ) atlas_add_test( gt_AnaToolHandle_test SOURCES test/gt_AnaToolHandle_test.cxx - Root/UnitTestTool1.cxx Root/UnitTestTool2.cxx INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} - LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools ) - -#atlas_add_test( gt_UnitTest_test -# SOURCES test/gt_UnitTest_test.cxx -# INCLUDE_DIRS ${GTEST_INCLUDE_DIRS} -# LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools ) - + LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib ) diff --git a/Control/AthToolSupport/AsgExampleTools/Root/AsgHelloTool.cxx b/Control/AthToolSupport/AsgExampleTools/Root/AsgHelloTool.cxx index 1487b35e864b2e2295e06095ac44c6d9f59b08e3..58d41838f22fb8f8035753f1989aacca337b2bd8 100644 --- a/Control/AthToolSupport/AsgExampleTools/Root/AsgHelloTool.cxx +++ b/Control/AthToolSupport/AsgExampleTools/Root/AsgHelloTool.cxx @@ -27,7 +27,10 @@ int AsgHelloTool::talk() const { // Create and execute a temporary tool. if ( name() != "tmphello" ) { AsgHelloTool tmptool("tmphello"); - tmptool.setProperty("Message", "Hi from the temporary tool."); + if( ! tmptool.setProperty("Message", "Hi from the temporary tool.").isSuccess() ) { + ATH_MSG_ERROR( "Failed to set property on temporary tool" ); + return 1; + } tmptool.talk(); } return 0; diff --git a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1.cxx b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1.cxx index 1ecc6148a67022a8e9b6a0b5b80a6fc39a667b80..c7d4fdcdd49f98904555ae5bf9e21eb0e238cb36 100644 --- a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1.cxx +++ b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1.cxx @@ -14,6 +14,9 @@ #include <AsgExampleTools/UnitTestTool1.h> +#include <gtest/gtest.h> +#include <map> + // // method implementations // @@ -25,7 +28,18 @@ namespace asg : AsgTool (val_name) { declareProperty ("propertyInt", m_propertyInt, "the integer property"); + declareProperty ("propertyString", m_propertyString, "the string property"); declareProperty ("initializeFail", m_initializeFail, "whether initialize should fail"); + + ++ instance_counts (name()); + } + + + + UnitTestTool1 :: + ~UnitTestTool1 () + { + -- instance_counts (name()); } @@ -49,6 +63,14 @@ namespace asg + std::string UnitTestTool1 :: + getPropertyString () const + { + return m_propertyString; + } + + + int UnitTestTool1 :: getPropertyInt () const { @@ -70,4 +92,17 @@ namespace asg { return m_isInitialized; } + + + + int& UnitTestTool1 :: + instance_counts (const std::string& name) + { + static std::map<std::string,int> counts; + auto iter = counts.find (name); + if (iter == counts.end()) + iter = counts.insert (std::make_pair (name, 0)).first; + assert (iter != counts.end()); + return iter->second; + } } diff --git a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1A.cxx b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1A.cxx index 33c2e14b8e7345025c4c875200f62bc2e301bdc6..acbcc6f6df0d4277cc5dff7da9152963fce03640 100644 --- a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1A.cxx +++ b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool1A.cxx @@ -25,6 +25,7 @@ namespace asg : AsgTool (val_name) { declareProperty ("propertyInt", m_propertyInt, "the integer property"); + declareProperty ("propertyString", m_propertyString, "the string property"); declareProperty ("initializeFail", m_initializeFail, "whether initialize should fail"); } @@ -49,6 +50,14 @@ namespace asg + std::string UnitTestTool1A :: + getPropertyString () const + { + return m_propertyString; + } + + + int UnitTestTool1A :: getPropertyInt () const { diff --git a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool2.cxx b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool2.cxx index 52b1f80f0b673840c4775c66082e56ffe92ff3e1..fd12d8d0e5f57a5d51400a80b3cae28490b8ee9a 100644 --- a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool2.cxx +++ b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool2.cxx @@ -27,9 +27,11 @@ namespace asg : AsgTool (val_name), m_regPublicHandle ("regPublicHandle", nullptr), m_regPrivateHandle ("regPrivateHandle", this), - m_anaPublicHandle ("anaPublicHandle", nullptr), - m_anaPrivateHandle ("anaPrivateHandle", this) + m_anaPublicHandle ("asg::UnitTestTool1/anaPublicHandle", nullptr), + m_anaPrivateHandle ("asg::UnitTestTool1/anaPrivateHandle", this) { + declareProperty ("allowEmpty", m_allowEmpty); + declareProperty ("regPublicHandle", m_regPublicHandle); declareProperty ("regPrivateHandle", m_regPrivateHandle); m_anaPublicHandle.declarePropertyFor (this, "anaPublicHandle"); @@ -41,13 +43,14 @@ namespace asg StatusCode UnitTestTool2 :: initialize () { + m_anaPublicHandle.setAllowEmpty (m_allowEmpty); + m_anaPrivateHandle.setAllowEmpty (m_allowEmpty); + m_wasUserConfiguredPublic = m_anaPublicHandle.isUserConfigured(); - ANA_CHECK (m_anaPublicHandle.make ("asg::UnitTestTool1/anaPublicTool")); ANA_CHECK (m_anaPublicHandle.setProperty ("propertyInt", 111)); ANA_CHECK (m_anaPublicHandle.initialize ()); m_wasUserConfiguredPrivate = m_anaPrivateHandle.isUserConfigured(); - ANA_CHECK (m_anaPrivateHandle.make ("asg::UnitTestTool1/anaPrivateTool")); ANA_CHECK (m_anaPrivateHandle.setProperty ("propertyInt", 222)); ANA_CHECK (m_anaPrivateHandle.initialize ()); @@ -56,7 +59,23 @@ namespace asg - IUnitTestTool1 *UnitTestTool2 :: + bool UnitTestTool2 :: + toolHandleEmpty (const std::string& handleName) const + { + if (handleName == "regPublicHandle") + return m_regPublicHandle.empty(); + if (handleName == "regPrivateHandle") + return m_regPrivateHandle.empty(); + if (handleName == "anaPublicHandle") + return m_anaPublicHandle.empty(); + if (handleName == "anaPrivateHandle") + return m_anaPrivateHandle.empty(); + throw std::runtime_error ("unknown handle: " + handleName); + } + + + + const IUnitTestTool1 *UnitTestTool2 :: getToolHandle (const std::string& handleName) const { if (handleName == "regPublicHandle") diff --git a/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool3.cxx b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool3.cxx new file mode 100644 index 0000000000000000000000000000000000000000..589aa44e46e82a9c6dcad3bee0c4c98acc96f863 --- /dev/null +++ b/Control/AthToolSupport/AsgExampleTools/Root/UnitTestTool3.cxx @@ -0,0 +1,85 @@ +// Copyright Iowa State University 2016. +// Author: Nils Krumnack +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Please feel free to contact me (nils.erik.krumnack@cern.ch) for bug +// reports, feature suggestions, praise and complaints. + + +// +// includes +// + +#include <AsgExampleTools/UnitTestTool3.h> + +#include <AsgExampleTools/IUnitTestTool1.h> +#include <AsgExampleTools/IUnitTestTool2.h> + +// +// method implementations +// + +namespace asg +{ + UnitTestTool3 :: + UnitTestTool3 (const std::string& val_name) + : AsgTool (val_name), + m_subtool1 ("asg::UnitTestTool1/subtool1", this), + m_subtool2 ("asg::UnitTestTool2/subtool2", this) + { + declareProperty ("propertyName", m_propertyName = "regPublicHandle"); + declareProperty ("usePublic", m_usePublic); + declareProperty ("subtool0", m_subtool0); + + m_subtool1.setAllowEmpty (); + m_subtool1.declarePropertyFor (this, "subtool1"); + } + + + + StatusCode UnitTestTool3 :: + initialize () + { + if (!m_usePublic) + { + ANA_CHECK (m_subtool1.setProperty ("propertyInt", 42)); + // ANA_CHECK (m_subtool1.initialize ()); + ANA_CHECK (m_subtool2.setProperty (m_propertyName, m_subtool1)); + } else + { + ANA_MSG_INFO ("using public handle"); + ANA_CHECK (m_subtool2.setProperty (m_propertyName, m_subtool0)); + } + ANA_CHECK (m_subtool2.setProperty ("allowEmpty", true)); + ANA_CHECK (m_subtool2.initialize ()); + + return StatusCode::SUCCESS; + } + + + + const IUnitTestTool1 *UnitTestTool3 :: + getSubtool () const + { + if (m_usePublic) + return &*m_subtool0; + else + return m_subtool1.get(); + } + + + + const IUnitTestTool1 *UnitTestTool3 :: + getSubsubtool () const + { + return m_subtool2->getToolHandle (m_propertyName); + } + + bool UnitTestTool3 :: + subsubtoolEmpty () const + { + return m_subtool2->toolHandleEmpty (m_propertyName); + } +} diff --git a/Control/AthToolSupport/AsgExampleTools/cmt/requirements b/Control/AthToolSupport/AsgExampleTools/cmt/requirements index 8903193479dbaf941152ea8b983a155368ff7813..d70e7ad89c978ace23a4ed86f89ef6bd8aa84a90 100644 --- a/Control/AthToolSupport/AsgExampleTools/cmt/requirements +++ b/Control/AthToolSupport/AsgExampleTools/cmt/requirements @@ -11,7 +11,7 @@ use AthenaBaseComps AthenaBaseComps-* Control use GaudiInterface GaudiInterface-* External end_private -apply_pattern dual_use_library files="*.cxx ../Root/AsgHelloTool.cxx ../Root/UnitTestTool1.cxx ../Root/UnitTestTool2.cxx ../Root/UnitTestTool1A.cxx" +apply_pattern dual_use_library files="*.cxx ../Root/AsgHelloTool.cxx ../Root/UnitTestTool1.cxx ../Root/UnitTestTool2.cxx ../Root/UnitTestTool3.cxx ../Root/UnitTestTool1A.cxx" private # make the unit tests work diff --git a/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx b/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx index c8bbeb8b858b45892386fbe14a33f133ed8ec533..f5770a573b6696fc66a7f9f090ec053be7ffec93 100644 --- a/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx +++ b/Control/AthToolSupport/AsgExampleTools/src/components/AsgExampleTools_entries.cxx @@ -7,6 +7,7 @@ #include <AsgExampleTools/UnitTestTool1.h> #include <AsgExampleTools/UnitTestTool1A.h> #include <AsgExampleTools/UnitTestTool2.h> +#include <AsgExampleTools/UnitTestTool3.h> DECLARE_TOOL_FACTORY(AsgHelloTool) @@ -15,6 +16,7 @@ DECLARE_ALGORITHM_FACTORY(AsgExampleAlgorithm) DECLARE_NAMESPACE_TOOL_FACTORY( asg, UnitTestTool1 ) DECLARE_NAMESPACE_TOOL_FACTORY( asg, UnitTestTool1A ) DECLARE_NAMESPACE_TOOL_FACTORY( asg, UnitTestTool2 ) +DECLARE_NAMESPACE_TOOL_FACTORY( asg, UnitTestTool3 ) DECLARE_FACTORY_ENTRIES(AsgExampleTools) { DECLARE_TOOL(AsgHelloTool) @@ -22,5 +24,6 @@ DECLARE_FACTORY_ENTRIES(AsgExampleTools) { DECLARE_NAMESPACE_TOOL( asg, UnitTestTool1 ); DECLARE_NAMESPACE_TOOL( asg, UnitTestTool1A ); DECLARE_NAMESPACE_TOOL( asg, UnitTestTool2 ); + DECLARE_NAMESPACE_TOOL( asg, UnitTestTool3 ); } diff --git a/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx b/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx index 312fafb7fbdb79e570a63ac6bd5f77e2d1fbd369..b5d487aad741649cf957f723ce43ce3c2a5e8405 100644 --- a/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx +++ b/Control/AthToolSupport/AsgExampleTools/test/gt_AnaToolHandle_test.cxx @@ -17,6 +17,7 @@ #include <AsgTools/UnitTest.h> #include <AsgExampleTools/UnitTestTool1.h> #include <AsgExampleTools/UnitTestTool2.h> +#include <AsgExampleTools/UnitTestTool3.h> #include <cmath> #include <gtest/gtest.h> #include <sstream> @@ -33,7 +34,6 @@ namespace asg { namespace { -#ifndef ROOTCORE /// \brief make a unique tool name to be used in unit tests std::string makeUniqueName () { @@ -42,7 +42,6 @@ namespace asg str << "unique" << ++ index; return str.str(); } -#endif } @@ -55,63 +54,36 @@ namespace asg { public: AnaToolHandleMakeTest() - : tool ("asg::UnitTestTool1/test") + : name (makeUniqueName()), + tool ("asg::UnitTestTool1/" + name) {} + std::string name; AnaToolHandle<IUnitTestTool1> tool; }; - /// \brief fixture for all tests that do things before a tool is - /// initialized - /// - /// e.g. setting properties and then initializing, or checking that - /// various methods fail in this state - class AnaToolHandleSetTest : public AnaToolHandleMakeTest - { - public: - AnaToolHandleSetTest() - { - ANA_CHECK_THROW (tool.make ()); - } - }; - - /// \brief fixture for all tests that do things after a tool is /// initialized /// /// e.g. accessing the tool itself, or checking that various methods /// fail in this state - class AnaToolHandleInitTest : public AnaToolHandleSetTest + class AnaToolHandleUseTest : public AnaToolHandleMakeTest { public: - AnaToolHandleInitTest() + AnaToolHandleUseTest() { ANA_CHECK_THROW (tool.initialize()); } }; - /// \brief fixture for all tests that do things when a tool is in - /// broken state - /// - /// mostly checking that we don't do anything forbidden - class AnaToolHandleBrokenTest : public AnaToolHandleSetTest - { - public: - AnaToolHandleBrokenTest() - { - AnaToolHandle<IUnitTestTool1> tool2 = std::move (tool); - } - }; - - // check that we can create and initialize a simple tool TEST (AnaToolHandleTest, basic1) { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + makeUniqueName()); ASSERT_FALSE (tool.isUserConfigured()); ASSERT_TRUE (tool.isConfigurable()); ASSERT_SUCCESS (tool.make()); @@ -123,30 +95,144 @@ namespace asg ASSERT_TRUE (tool.get() != nullptr); } - // check that we can create and initialize a simple tool - TEST (AnaToolHandleTest, basic2) + + +#ifndef ROOTCORE + // check that tool destruction works as advertised for tools + TEST (AnaToolHandleTest, DISABLED_destruct_direct) { - AnaToolHandle<IUnitTestTool2> tool ("asg::UnitTestTool2/test"); - ASSERT_FALSE (tool.isUserConfigured()); - ASSERT_TRUE (tool.isConfigurable()); - ASSERT_SUCCESS (tool.make()); - ASSERT_SUCCESS (tool.initialize()); - ASSERT_TRUE (tool.get() != nullptr); + std::string myname; + { + ToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + makeUniqueName()); + ASSERT_SUCCESS (tool.retrieve()); + myname = tool->name(); + ASSERT_EQ (2u, tool->refCount()); + EXPECT_EQ (1, UnitTestTool1::instance_counts(myname)); + tool->release(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(myname)); + tool.release(); + EXPECT_EQ (0, UnitTestTool1::instance_counts(myname)); + } + interfaceType_t *tool = nullptr; + EXPECT_FALSE (detail::toolExists (myname, tool)); } +#endif - // check that inPremakeState is working - TEST (AnaToolHandleTest, inPremakeState) + // check that tool destruction works as advertised for public tools +#ifdef ROOTCORE + TEST (AnaToolHandleTest, destruct_public) +#else + TEST (AnaToolHandleTest, DISABLED_destruct_public) +#endif + { + std::string myname; + { + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + makeUniqueName()); + ASSERT_SUCCESS (tool.initialize()); + myname = tool->name(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(myname)); + } + EXPECT_EQ (0, UnitTestTool1::instance_counts(myname)); + } + + + // check that tool destruction works as advertised for private tools +#ifdef ROOTCORE + TEST (AnaToolHandleTest, destruct_private) +#else + TEST (AnaToolHandleTest, DISABLED_destruct_private) +#endif + { + std::string myname; + { + std::unique_ptr<UnitTestTool1> parent (new UnitTestTool1 (makeUniqueName())); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + makeUniqueName(), parent.get()); + ASSERT_SUCCESS (tool.initialize()); + myname = tool->name(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(myname)); + } + EXPECT_EQ (0, UnitTestTool1::instance_counts(myname)); + } + + // check that tool destruction works as advertised with copy constructor +#ifdef ROOTCORE + TEST (AnaToolHandleTest, copy_destruct) +#else + TEST (AnaToolHandleTest, DISABLED_copy_destruct) +#endif + { + std::string name = makeUniqueName(); + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + { + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); + ASSERT_SUCCESS (tool.initialize()); + name = tool->name(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + AnaToolHandle<IUnitTestTool1> tool2 = tool; + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + } + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + } + + // check that tool destruction works as advertised with assignment operator +#ifdef ROOTCORE + TEST (AnaToolHandleTest, assign_destruct) +#else + TEST (AnaToolHandleTest, DISABLED_assign_destruct) +#endif + { + std::string name = makeUniqueName(); + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + { + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); + ASSERT_SUCCESS (tool.initialize()); + name = tool->name(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + AnaToolHandle<IUnitTestTool1> tool2; + tool2 = tool; + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + } + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + } + + // check that tool destruction works as advertised with swap function + // + // not that this swaps twice, as there may be a difference in + // swapping to and from a valid handle (due to the specifics of the + // Athena ToolHandle) +#ifdef ROOTCORE + TEST (AnaToolHandleTest, swap_destruct) +#else + TEST (AnaToolHandleTest, DISABLED_swap_destruct) +#endif + { + std::string name = makeUniqueName(); + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + { + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); + ASSERT_SUCCESS (tool.initialize()); + name = tool->name(); + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + AnaToolHandle<IUnitTestTool1> tool2; + tool2.swap (tool); + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + tool2.swap (tool); + EXPECT_EQ (1, UnitTestTool1::instance_counts(name)); + } + EXPECT_EQ (0, UnitTestTool1::instance_counts(name)); + } + + // check that we can create and initialize a simple tool + TEST (AnaToolHandleTest, basic2) { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); - ASSERT_TRUE (tool.inPremakeState()); + AnaToolHandle<IUnitTestTool2> tool ("asg::UnitTestTool2/" + makeUniqueName()); + ASSERT_FALSE (tool.isUserConfigured()); + ASSERT_TRUE (tool.isConfigurable()); ASSERT_SUCCESS (tool.make()); - ASSERT_FALSE (tool.inPremakeState()); ASSERT_SUCCESS (tool.initialize()); - ASSERT_FALSE (tool.inPremakeState()); - AnaToolHandle<IUnitTestTool1> tool2 = std::move (tool); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_TRUE (tool.get() != nullptr); } @@ -154,7 +240,7 @@ namespace asg // check that the isInitialized member works as advertised TEST (AnaToolHandleTest, isInitialized) { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + makeUniqueName()); ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.make()); ASSERT_FALSE (tool.isInitialized()); @@ -166,207 +252,149 @@ namespace asg - // check that inBrokenState is working - TEST (AnaToolHandleTest, inBrokenState) - { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); - ASSERT_FALSE (tool.inBrokenState()); - ASSERT_SUCCESS (tool.make()); - ASSERT_FALSE (tool.inBrokenState()); - ASSERT_SUCCESS (tool.initialize()); - ASSERT_FALSE (tool.inBrokenState()); - AnaToolHandle<IUnitTestTool1> tool2 = std::move (tool); - ASSERT_TRUE (tool.inBrokenState()); - } - - - // check make() TEST_F (AnaToolHandleMakeTest, makeBasic) { ASSERT_SUCCESS (tool.make()); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.initialize()); ASSERT_EQ ("asg::UnitTestTool1", tool.type()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + tool.name() + "$", tool->name()); } // check make(), with no type TEST (AnaToolHandleTest, makeBasic_noType) { - AnaToolHandle<IUnitTestTool1> tool ("test"); -#ifdef ROOTCORE - ASSERT_FAILURE (tool.make ()); -#else - ASSERT_SUCCESS (tool.make ()); + AnaToolHandle<IUnitTestTool1> tool (makeUniqueName()); + ASSERT_FALSE (tool.isUserConfigured ()); ASSERT_FAILURE (tool.initialize ()); -#endif } // check make(), with an invalid type TEST (AnaToolHandleTest, makeBasic_undefinedType) { - AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/test"); -#ifdef ROOTCORE - ASSERT_FAILURE (tool.make ()); -#else + AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/" + makeUniqueName()); ASSERT_SUCCESS (tool.make ()); ASSERT_FAILURE (tool.initialize ()); -#endif - } - - // check make() - TEST_F (AnaToolHandleSetTest, makeBasic) - { - ASSERT_DEATH (tool.make(), ""); - } - - // check make() - TEST_F (AnaToolHandleInitTest, makeBasic) - { - ASSERT_DEATH (tool.make(), ""); - } - - // check make() - TEST_F (AnaToolHandleBrokenTest, makeBasic) - { - ASSERT_FAILURE (tool.make()); } // check make(type) - TEST_F (AnaToolHandleMakeTest, makeTyped) + TEST (AnaToolHandleTest, makeTyped) { // making separate ToolHandle with different type - AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/test"); + AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/" + makeUniqueName()); ASSERT_EQ ("UNKNOWN_TOOL_TYPE", tool.type()); ASSERT_SUCCESS (tool.make ("asg::UnitTestTool1")); ASSERT_EQ ("asg::UnitTestTool1", tool.type()); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.initialize()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + tool.name() + "$", tool->name()); } // check make(type) - TEST_F (AnaToolHandleMakeTest, makeTyped_named) + TEST (AnaToolHandleTest, makeTyped_named) { // making separate ToolHandle with different type AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/UNKNOWN_TOOL_NAME"); ASSERT_EQ ("UNKNOWN_TOOL_TYPE", tool.type()); ASSERT_EQ ("UNKNOWN_TOOL_NAME", tool.name()); - ASSERT_SUCCESS (tool.make ("asg::UnitTestTool1/test")); + std::string name = makeUniqueName(); + ASSERT_SUCCESS (tool.make ("asg::UnitTestTool1/" + name)); ASSERT_EQ ("asg::UnitTestTool1", tool.type()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool.name()); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + name + "$", tool.name()); + ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.initialize()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + name + "$", tool->name()); } // check that we can fail tocreate and initialize a simple tool when // passing an unknown typename as argument into make() TEST_F (AnaToolHandleMakeTest, makeTyped_unknownType) { -#ifdef ROOTCORE - ASSERT_FAILURE (tool.make ("UNDEFINED_TYPE_NAME")); -#else ASSERT_SUCCESS (tool.make ("UNDEFINED_TYPE_NAME")); ASSERT_FAILURE (tool.initialize ()); -#endif } +#ifndef NDEBUG // check make(type) - TEST_F (AnaToolHandleSetTest, makeTyped) + TEST_F (AnaToolHandleUseTest, makeTyped) { ASSERT_DEATH (tool.make ("asg::UnitTestTool1"), ""); } - - // check make(type) - TEST_F (AnaToolHandleInitTest, makeTyped) - { - ASSERT_DEATH (tool.make ("asg::UnitTestTool1"), ""); - } - - // check make(type) - TEST_F (AnaToolHandleBrokenTest, makeTyped) - { - ASSERT_FAILURE (tool.make ("asg::UnitTestTool1")); - } +#endif #ifdef ROOTCORE // check makeNew<type>() - TEST_F (AnaToolHandleMakeTest, makeNew) + TEST (AnaToolHandleTest, makeNew) { // making separate ToolHandle with different type - AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/test"); + std::string name = makeUniqueName(); + AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/" + name); ASSERT_EQ ("UNKNOWN_TOOL_TYPE", tool.type()); ASSERT_SUCCESS (tool.makeNew<asg::UnitTestTool1> ("asg::UnitTestTool1")); ASSERT_EQ ("asg::UnitTestTool1", tool.type()); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.initialize()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + name + "$", tool->name()); } // check makeNew<type>() - TEST_F (AnaToolHandleSetTest, makeNew) + TEST_F (AnaToolHandleUseTest, makeNew) { ASSERT_DEATH (tool.makeNew<asg::UnitTestTool1> ("asg::UnitTestTool1"), ""); } +#endif - // check makeNew<type>() - TEST_F (AnaToolHandleInitTest, makeNew) - { - ASSERT_DEATH (tool.makeNew<asg::UnitTestTool1> ("asg::UnitTestTool1"), ""); - } - // check makeNew<type>() - TEST_F (AnaToolHandleBrokenTest, makeNew) + + // check ASG_SET_ANA_TOOL_TYPE + TEST (AnaToolHandleTest, setToolTypeMacro) { - ASSERT_FAILURE (tool.makeNew<asg::UnitTestTool1> ("asg::UnitTestTool1")); + // making separate ToolHandle with different type + std::string name = makeUniqueName(); + AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/" + name); + ASSERT_EQ ("UNKNOWN_TOOL_TYPE", tool.type()); + ASG_SET_ANA_TOOL_TYPE (tool, asg::UnitTestTool1); + ASSERT_EQ ("asg::UnitTestTool1", tool.type()); + ASSERT_FALSE (tool.isInitialized()); + ASSERT_SUCCESS (tool.initialize()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + name + "$", tool->name()); } -#endif // check ASG_MAKE_ANA_TOOL - TEST_F (AnaToolHandleMakeTest, makeMacro) + TEST (AnaToolHandleTest, makeMacro) { // making separate ToolHandle with different type - AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/test"); + std::string name = makeUniqueName(); + AnaToolHandle<IUnitTestTool1> tool ("UNKNOWN_TOOL_TYPE/" + name); ASSERT_EQ ("UNKNOWN_TOOL_TYPE", tool.type()); ASSERT_SUCCESS (ASG_MAKE_ANA_TOOL (tool, asg::UnitTestTool1)); ASSERT_EQ ("asg::UnitTestTool1", tool.type()); - ASSERT_FALSE (tool.inPremakeState()); + ASSERT_FALSE (tool.isInitialized()); ASSERT_SUCCESS (tool.initialize()); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", tool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + name + "$", tool->name()); } +#ifndef NDEBUG // check ASG_MAKE_ANA_TOOL - TEST_F (AnaToolHandleSetTest, makeMacro) + TEST_F (AnaToolHandleUseTest, makeMacro) { ASSERT_DEATH (ASG_MAKE_ANA_TOOL (tool, asg::UnitTestTool1), ""); } - - // check ASG_MAKE_ANA_TOOL - TEST_F (AnaToolHandleInitTest, makeMacro) - { - ASSERT_DEATH (ASG_MAKE_ANA_TOOL (tool, asg::UnitTestTool1), ""); - } - - // check ASG_MAKE_ANA_TOOL - TEST_F (AnaToolHandleBrokenTest, makeMacro) - { - ASSERT_FAILURE (ASG_MAKE_ANA_TOOL (tool, asg::UnitTestTool1)); - } +#endif // check that we can actually change the tool type TEST (AnaToolHandleTest, changeType) { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1A/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1A/" + makeUniqueName()); ASSERT_SUCCESS (tool.make()); ASSERT_SUCCESS (tool.initialize()); ASSERT_EQ (-7, tool->getPropertyInt()); @@ -376,7 +404,7 @@ namespace asg // check setProperty<int>() - TEST_F (AnaToolHandleSetTest, setPropertyInt) + TEST_F (AnaToolHandleMakeTest, setPropertyInt) { ASSERT_SUCCESS (tool.setProperty<int> ("propertyInt", 42)); ASSERT_SUCCESS (tool.initialize()); @@ -384,36 +412,49 @@ namespace asg } // check setProperty<int>() - TEST_F (AnaToolHandleSetTest, setPropertyInt_failure) + TEST_F (AnaToolHandleMakeTest, setPropertyInt_failure) { ASSERT_SUCCESS (tool.setProperty<int> ("UNKNOWN_PROPERTY", 42)); ASSERT_FAILURE (tool.initialize ()); } +#ifndef NDEBUG // check setProperty<int>() - TEST_F (AnaToolHandleMakeTest, setPropertyInt) + TEST_F (AnaToolHandleUseTest, setPropertyInt) { - ASSERT_SUCCESS (tool.setProperty<int> ("propertyInt", 42)); + ASSERT_DEATH (tool.setProperty<int> ("propertyInt", 42), ""); + } +#endif + + + + // check setProperty(const char*) + TEST_F (AnaToolHandleMakeTest, setPropertyString) + { + ASSERT_SUCCESS (tool.setProperty ("propertyString", "42")); ASSERT_SUCCESS (tool.initialize()); - ASSERT_EQ (42, tool->getPropertyInt()); + ASSERT_EQ ("42", tool->getPropertyString()); } - // check setProperty<int>() - TEST_F (AnaToolHandleInitTest, setPropertyInt) + // check setProperty(const char*) + TEST_F (AnaToolHandleMakeTest, setPropertyString_failure) { - ASSERT_DEATH (tool.setProperty<int> ("propertyInt", 42), ""); + ASSERT_SUCCESS (tool.setProperty ("UNKNOWN_PROPERTY", "42")); + ASSERT_FAILURE (tool.initialize ()); } - // check setProperty<int>() - TEST_F (AnaToolHandleBrokenTest, setPropertyInt) +#ifndef NDEBUG + // check setProperty(const char*) + TEST_F (AnaToolHandleUseTest, setPropertyString) { - ASSERT_FAILURE (tool.setProperty<int> ("propertyInt", 42)); + ASSERT_DEATH (tool.setProperty ("propertyString", "42"), ""); } +#endif // check initialize() - TEST_F (AnaToolHandleSetTest, initialize) + TEST_F (AnaToolHandleMakeTest, initialize) { ASSERT_SUCCESS (tool.initialize()); ASSERT_TRUE (tool.isInitialized()); @@ -421,61 +462,38 @@ namespace asg } // check initialize() - TEST_F (AnaToolHandleSetTest, initialize_failure) + TEST_F (AnaToolHandleMakeTest, initialize_failure) { ASSERT_SUCCESS (tool.setProperty<bool> ("initializeFail", true)); ASSERT_FAILURE (tool.initialize()); ASSERT_FALSE (tool.isInitialized()); - ASSERT_TRUE (tool.inBrokenState()); } +#ifndef NDEBUG // check initialize() - TEST_F (AnaToolHandleMakeTest, initialize) - { - ASSERT_SUCCESS (tool.initialize()); - ASSERT_TRUE (tool.isInitialized()); - ASSERT_TRUE (tool->isInitialized()); - } - - // check initialize() - TEST_F (AnaToolHandleInitTest, initialize) + TEST_F (AnaToolHandleUseTest, initialize) { ASSERT_DEATH (tool.initialize(), ""); } - - // check initialize() - TEST_F (AnaToolHandleBrokenTest, initialize) - { - ASSERT_FAILURE (tool.initialize ()); - } +#endif // check get() (and by implication * and ->) - TEST_F (AnaToolHandleInitTest, get) + TEST_F (AnaToolHandleUseTest, get) { IUnitTestTool1 *mytool = tool.get(); ASSERT_TRUE (mytool != nullptr); - ASSERT_MATCH_REGEX ("^(ToolSvc.)?test$", mytool->name()); + ASSERT_MATCH_REGEX ("^(ToolSvc.)?" + tool.name() + "$", mytool->name()); } +#ifndef NDEBUG // check get() (and by implication * and ->) TEST_F (AnaToolHandleMakeTest, get) { ASSERT_DEATH (tool.get(), ""); } - - // check get() (and by implication * and ->) - TEST_F (AnaToolHandleSetTest, get) - { - ASSERT_DEATH (tool.get(), ""); - } - - // check get() (and by implication * and ->) - TEST_F (AnaToolHandleBrokenTest, get) - { - ASSERT_DEATH (tool.get(), ""); - } +#endif @@ -483,10 +501,15 @@ namespace asg // different parameters after we are done with the first one. this // is routinely done as part of unit tests, so this absolutely has // to stay +#ifdef ROOTCORE TEST (AnaToolHandleTest, duplicate_series) +#else + TEST (AnaToolHandleTest, DISABLED_duplicate_series) +#endif { + std::string name = makeUniqueName(); { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); ASSERT_FALSE (tool.isUserConfigured()); ASSERT_TRUE (tool.isConfigurable()); ASSERT_SUCCESS (tool.setProperty<int> ("propertyInt", 42)); @@ -494,7 +517,7 @@ namespace asg ASSERT_EQ (42, tool->getPropertyInt()); } { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); ASSERT_FALSE (tool.isUserConfigured()); ASSERT_TRUE (tool.isConfigurable()); ASSERT_SUCCESS (tool.setProperty<int> ("propertyInt", 4)); @@ -502,7 +525,7 @@ namespace asg ASSERT_EQ (4, tool->getPropertyInt()); } { - AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool ("asg::UnitTestTool1/" + name); ASSERT_FALSE (tool.isUserConfigured()); ASSERT_TRUE (tool.isConfigurable()); ASSERT_SUCCESS (tool.setProperty<int> ("propertyInt", 17)); @@ -517,8 +540,9 @@ namespace asg // if they are given the same name and have no parents. TEST (AnaToolHandleTest, shared_tool) { + std::string name = makeUniqueName(); { - AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/" + name); EXPECT_FALSE (tool1.isUserConfigured()); EXPECT_TRUE (tool1.isConfigurable()); ASSERT_SUCCESS (tool1.setProperty<int> ("propertyInt", 42)); @@ -528,7 +552,7 @@ namespace asg EXPECT_FALSE (tool1.isUserConfigured()); EXPECT_TRUE (tool1.isConfigurable()); - AnaToolHandle<IUnitTestTool1> tool2 ("asg::UnitTestTool1/test"); + AnaToolHandle<IUnitTestTool1> tool2 ("asg::UnitTestTool1/" + name); EXPECT_TRUE (tool2.isUserConfigured()); EXPECT_FALSE (tool2.isConfigurable()); ASSERT_SUCCESS (tool2.setProperty<int> ("propertyInt", 7)); @@ -541,35 +565,49 @@ namespace asg ASSERT_EQ (tool1.get(), tool2.get()); ASSERT_EQ (42, tool1->getPropertyInt()); } - AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/test"); - EXPECT_FALSE (tool1.isUserConfigured()); - EXPECT_TRUE (tool1.isConfigurable()); - ASSERT_SUCCESS (tool1.setProperty<int> ("propertyInt", 17)); - ASSERT_SUCCESS (tool1.initialize()); } - // check that in Athena we pick up whether set job options cause us - // to flag a tool as user configured -#ifndef ROOTCORE - TEST (AnaToolHandleTest, usesJobOptions) + // check that a tool gets shared between two AnaToolHandle objects + // if they are given the same name and have no parents. +#ifdef ROOTCORE + TEST (AnaToolHandleTest, shared_tool_cleanup) +#else + TEST (AnaToolHandleTest, DISABLED_shared_tool_cleanup) +#endif { - // this is a dummy tool that is only used to fill the job options - // service. it is purposely not initialized to make sure that it - // does nothing but set the job options - AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/test"); - ASSERT_SUCCESS (tool1.setProperty<int> ("propertyInt", 42)); + std::string name = makeUniqueName(); + { + AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/" + name); + EXPECT_FALSE (tool1.isUserConfigured()); + EXPECT_TRUE (tool1.isConfigurable()); + ASSERT_SUCCESS (tool1.setProperty<int> ("propertyInt", 42)); + EXPECT_FALSE (tool1.isUserConfigured()); + EXPECT_TRUE (tool1.isConfigurable()); + ASSERT_SUCCESS (tool1.initialize()); + EXPECT_FALSE (tool1.isUserConfigured()); + EXPECT_TRUE (tool1.isConfigurable()); - AnaToolHandle<IUnitTestTool1> tool2 ("asg::UnitTestTool1/test"); - EXPECT_TRUE (tool2.isUserConfigured()); - EXPECT_FALSE (tool2.isConfigurable()); - ASSERT_SUCCESS (tool2.setProperty<int> ("propertyInt", 7)); - ASSERT_SUCCESS (tool2.initialize()); + AnaToolHandle<IUnitTestTool1> tool2 ("asg::UnitTestTool1/" + name); + EXPECT_TRUE (tool2.isUserConfigured()); + EXPECT_FALSE (tool2.isConfigurable()); + ASSERT_SUCCESS (tool2.setProperty<int> ("propertyInt", 7)); + EXPECT_TRUE (tool2.isUserConfigured()); + EXPECT_FALSE (tool2.isConfigurable()); + ASSERT_SUCCESS (tool2.initialize()); + EXPECT_TRUE (tool2.isUserConfigured()); + EXPECT_FALSE (tool2.isConfigurable()); - ASSERT_EQ (42, tool2->getPropertyInt()); + ASSERT_EQ (tool1.get(), tool2.get()); + ASSERT_EQ (42, tool1->getPropertyInt()); + } + AnaToolHandle<IUnitTestTool1> tool1 ("asg::UnitTestTool1/" + name); + EXPECT_FALSE (tool1.isUserConfigured()); + EXPECT_TRUE (tool1.isConfigurable()); + ASSERT_SUCCESS (tool1.setProperty<int> ("propertyInt", 17)); + ASSERT_SUCCESS (tool1.initialize()); } -#endif @@ -582,19 +620,18 @@ namespace asg SCOPED_TRACE (handleName); TH par; - AnaToolHandle<IUnitTestTool2> mainTool ("asg::UnitTestTool2/mainTool"); + AnaToolHandle<IUnitTestTool2> mainTool ("asg::UnitTestTool2/" + makeUniqueName()); -#ifdef ROOTCORE if (!par.isSettable()) -#else - if (!par.isSettable() || !par.isGettable()) -#endif { - ASSERT_FAILURE (mainTool.setProperty (handleName, par.handle)); - return; + ASSERT_FAILURE (mainTool.setProperty (handleName, par.handle)); + return; } ASSERT_SUCCESS (mainTool.setProperty (handleName, par.handle)); + // for AnaToolHandle members we try to get the tool in + // initialize(), so if we can't get a tool from the tool handle, + // we will fail if (!isRegular && !par.isGettable()) { ASSERT_FAILURE (mainTool.initialize ()); @@ -602,6 +639,19 @@ namespace asg } ASSERT_SUCCESS (mainTool.initialize ()); + // a regular ToolHandle will throw when trying to get an empty + // handle, an AnaToolHandle will return a nullptr + if (par.handle.empty()) + { + if (isRegular) + ASSERT_ANY_THROW (mainTool->getToolHandle (handleName)); + else + ASSERT_EQ (nullptr, mainTool->getToolHandle (handleName)); + return; + } + + // for old-fashioned ToolHandle members we don't try to get the + // tool until we access it, making it fail at that point. if (!par.isGettable()) { ASSERT_ANY_THROW (mainTool->getToolHandle (handleName)); @@ -609,7 +659,7 @@ namespace asg } auto *const subtool = mainTool->getToolHandle (handleName); #ifdef ROOTCORE - EXPECT_EQ (&*par.handle, subtool); + EXPECT_EQ (par.getTool(), subtool); #else EXPECT_EQ (par.handle->getPropertyInt(), subtool->getPropertyInt()); #endif @@ -654,22 +704,25 @@ namespace asg { PublicAnaSubTool () { - ANA_CHECK_THROW (handle.make ("asg::UnitTestTool1/subtool")); + ANA_CHECK_THROW (handle.make ("asg::UnitTestTool1/" + makeUniqueName())); ANA_CHECK_THROW (handle.initialize ()); } bool isSettable () const {return true;} bool isGettable () const {return true;} + const IUnitTestTool1 *getTool () const {return &*handle;} AnaToolHandle<IUnitTestTool1> handle; }; +#ifdef ROOTCORE INSTANTIATE_TYPED_TEST_CASE_P (PublicAnaSubToolTest, SetToolHandlePropertyTest, PublicAnaSubTool); +#endif struct PrivateAnaSubTool { PrivateAnaSubTool () - : parent (new UnitTestTool1 ("parentTool")), - handle ("asg::UnitTestTool1/subtool", parent.get()) + : parent (new UnitTestTool1 (makeUniqueName())), + handle ("asg::UnitTestTool1/" + makeUniqueName(), parent.get()) { ANA_CHECK_THROW (parent->initialize ()); ANA_CHECK_THROW (handle.initialize ()); @@ -677,11 +730,14 @@ namespace asg bool isSettable () const {return true;} bool isGettable () const {return true;} + const IUnitTestTool1 *getTool () const {return &*handle;} std::unique_ptr<UnitTestTool1> parent; AnaToolHandle<IUnitTestTool1> handle; }; +#ifdef ROOTCORE INSTANTIATE_TYPED_TEST_CASE_P (PrivateAnaSubToolTest, SetToolHandlePropertyTest, PrivateAnaSubTool); +#endif struct EmptyRegSubTool { @@ -689,8 +745,9 @@ namespace asg { } - bool isSettable () const {return false;} + bool isSettable () const {return true;} bool isGettable () const {return false;} + IUnitTestTool1 *getTool () const {return nullptr;} ToolHandle<IUnitTestTool1> handle; }; @@ -705,6 +762,7 @@ namespace asg bool isSettable () const {return true;} bool isGettable () const {return false;} + const IUnitTestTool1 *getTool () const {return &*handle;} ToolHandle<IUnitTestTool1> handle; }; @@ -729,6 +787,7 @@ namespace asg bool isSettable () const {return true;} bool isGettable () const {return true;} + const IUnitTestTool1 *getTool () const {return &*handle;} #ifdef ROOTCORE std::unique_ptr<UnitTestTool1> mytool; @@ -742,13 +801,14 @@ namespace asg { PointerRegSubTool () { - ANA_CHECK_THROW (tool.make ("asg::UnitTestTool1/subtool")); + ANA_CHECK_THROW (tool.make ("asg::UnitTestTool1/" + makeUniqueName())); ANA_CHECK_THROW (tool.initialize ()); handle = tool.get (); } bool isSettable () const {return true;} bool isGettable () const {return true;} + const IUnitTestTool1 *getTool () const {return &*handle;} AnaToolHandle<IUnitTestTool1> tool; ToolHandle<IUnitTestTool1> handle; @@ -818,30 +878,143 @@ namespace asg TEST (AnaToolHandleTest, swap_test) { - AnaToolHandle<IAsgTool> tool1("asg::UnitTestTool1/myTool"); + std::string name = makeUniqueName(); + AnaToolHandle<IAsgTool> tool1("asg::UnitTestTool1/" + name); ASSERT_SUCCESS (tool1.initialize()); -#ifndef ROOTCORE - EXPECT_EQ (2u, tool1->refCount()); -#endif asg::AnaToolHandle<asg::IAsgTool> tool3; { - asg::AnaToolHandle<asg::IAsgTool> tool2("asg::UnitTestTool1/myTool"); + asg::AnaToolHandle<asg::IAsgTool> tool2("asg::UnitTestTool1/" + name); ASSERT_SUCCESS (tool2.initialize()); -#ifndef ROOTCORE - EXPECT_EQ (3u, tool2->refCount()); -#endif tool3 = std::move(tool2); + } + } + + + + struct SubtoolTest : public testing::TestWithParam<std::tuple<std::string,std::string,std::string> > + { + }; + + + + TEST_P (SubtoolTest, set) + { + std::vector<std::shared_ptr<void> > cleanup; + int value = 7; + + std::string propertyName; + if (std::get<1>(GetParam()) == "public") + propertyName = "subtool0"; + else if (std::get<1>(GetParam()) == "private") + propertyName = "subtool1"; + else + { + ADD_FAILURE () << "unknown parameter " << std::get<1>(GetParam()); + return; + } + + AnaToolHandle<IUnitTestTool3> th3 ("asg::UnitTestTool3/" + makeUniqueName()); + ASSERT_SUCCESS (th3.setProperty ("propertyName", std::get<0>(GetParam ()))); + if (std::get<2>(GetParam()) == "empty") + { + value = -1; + ToolHandle<asg::IUnitTestTool1> th (""); + ASSERT_TRUE (th.empty()); + ASSERT_SUCCESS (th3.setProperty (propertyName, th)); + } else if (std::get<2>(GetParam()) == "none") + { + value = 42; + } else if (std::get<2>(GetParam()) == "ATH" || + std::get<2>(GetParam()) == "TH" || + std::get<2>(GetParam()) == "NOINIT") + { + std::shared_ptr<AnaToolHandle<IUnitTestTool1> > th1; + if (std::get<1>(GetParam()) == "public") + { + th1 = + std::make_shared<AnaToolHandle<IUnitTestTool1> > + ("asg::UnitTestTool1/" + makeUniqueName()); + cleanup.push_back (th1); + } else + { + auto th0 = + std::make_shared<AnaToolHandle<IUnitTestTool1> > + ("asg::UnitTestTool1/" + makeUniqueName()); + cleanup.push_back (th0); + ASSERT_SUCCESS (th0->initialize ()); + th1 = + std::make_shared<AnaToolHandle<IUnitTestTool1> > + ("asg::UnitTestTool1/" + makeUniqueName(), + dynamic_cast<AsgTool*>(th0->get())); + cleanup.push_back (th1); + } + ASSERT_SUCCESS (th1->setProperty ("propertyInt", value)); + if (std::get<2>(GetParam()) != "NOINIT") + { #ifndef ROOTCORE - EXPECT_EQ (3u, tool3->refCount()); + ASSERT_EQ (std::get<1>(GetParam()) == "public", th1->getHandle().isPublic()); #endif - } + ASSERT_SUCCESS (th1->initialize ()); #ifndef ROOTCORE - EXPECT_EQ (3u, tool3->refCount()); - EXPECT_EQ (3u, tool1->refCount()); + ASSERT_EQ (std::get<1>(GetParam()) == "public", th1->getHandle().isPublic()); #endif + } + + if (std::get<2>(GetParam()) == "ATH") + { + ASSERT_SUCCESS (th3.setProperty (propertyName, *th1)); + } else if (std::get<2>(GetParam()) == "TH") + { + ASSERT_SUCCESS (th3.setProperty (propertyName, th1->getHandle())); + } else if (std::get<2>(GetParam()) == "NOINIT") + { + ASSERT_SUCCESS (th3.setProperty (propertyName, *th1)); + } else + { + ADD_FAILURE () << "unknown parameter " << std::get<2>(GetParam()); + return; + } + } else + { + ADD_FAILURE () << "unknown parameter " << std::get<1>(GetParam()); + return; + } + if (std::get<1>(GetParam()) == "public") + ASSERT_SUCCESS (th3.setProperty ("usePublic", true)); + + ASSERT_SUCCESS (th3.initialize ()); + if (value == -1) + { + ASSERT_TRUE (th3->subsubtoolEmpty()); + } else + { + ASSERT_FALSE (th3->subsubtoolEmpty()); + ASSERT_EQ (value, th3->getSubsubtool()->getPropertyInt()); + } } + + INSTANTIATE_TEST_CASE_P + (MySubtoolTest1, SubtoolTest, ::testing::Values + (std::make_tuple ("regPublicHandle", "public", "ATH"), + std::make_tuple ("anaPublicHandle", "public", "ATH"), + std::make_tuple ("regPublicHandle", "public", "TH"), + std::make_tuple ("anaPublicHandle", "public", "TH"), + std::make_tuple ("regPublicHandle", "public", "NOINIT"), + std::make_tuple ("anaPublicHandle", "public", "NOINIT"), + std::make_tuple ("regPublicHandle", "public", "empty"), + std::make_tuple ("anaPublicHandle", "public", "empty"), + std::make_tuple ("regPrivateHandle", "private", "ATH"), + std::make_tuple ("anaPrivateHandle", "private", "ATH"), + std::make_tuple ("regPrivateHandle", "private", "TH"), + std::make_tuple ("anaPrivateHandle", "private", "TH"), + std::make_tuple ("regPrivateHandle", "private", "NOINIT"), + std::make_tuple ("anaPrivateHandle", "private", "NOINIT"), + std::make_tuple ("regPrivateHandle", "private", "empty"), + std::make_tuple ("anaPrivateHandle", "private", "empty"), + std::make_tuple ("regPrivateHandle", "private", "none"), + std::make_tuple ("anaPrivateHandle", "private", "none")),); } ATLAS_GOOGLE_TEST_MAIN diff --git a/Control/AthToolSupport/AsgExampleTools/util/hello.cxx b/Control/AthToolSupport/AsgExampleTools/util/hello.cxx index 2fd0e903ad9e47f1bae45ec7b6749680adb468b7..2cc0ec5692dcf8da052be612752f03545da9e2be 100644 --- a/Control/AthToolSupport/AsgExampleTools/util/hello.cxx +++ b/Control/AthToolSupport/AsgExampleTools/util/hello.cxx @@ -9,42 +9,47 @@ #include <string> #include <iostream> #include "AsgExampleTools/AsgHelloTool.h" +#include <AsgTools/MessageCheck.h> using std::string; using std::cout; using std::endl; int main() { + using namespace asg::msgUserCode; + ANA_CHECK_SET_TYPE (int); + + const string myname = "hello: "; cout << myname << "Begin." << endl; AsgHelloTool htool("myhello"); - ASG_CHECK_SA( "hello", htool.setProperty("Message", "Hello from ASG.") ); - ASG_CHECK_SA( "hello", htool.setProperty("OutputLevel", MSG::DEBUG) ); + ANA_CHECK( htool.setProperty("Message", "Hello from ASG.") ); + ANA_CHECK( htool.setProperty("OutputLevel", MSG::DEBUG) ); cout << myname << "Initialize" << endl; - ASG_CHECK_SA( "hello", htool.initialize()); + ANA_CHECK( htool.initialize()); cout << myname << "Show properties" << endl; htool.print(); cout << myname << "Extract property" << endl; - const string* msg = htool.getProperty< string >( "Message" ); - if( ! msg ) { + const string* message = htool.getProperty< string >( "Message" ); + if( ! message ) { cout << myname << "Couldn't extract property from the tool" << endl; return 1; } htool.getProperty< string >( "UnknownProperty" ); htool.getProperty< int >( "Message" ); - cout << myname << "The \"Message\" property of the tool: " << *msg << endl; + cout << myname << "The \"Message\" property of the tool: " << *message << endl; cout << myname << "Run 10 times" << endl; string line = "---------------------------------------------------"; cout << line << endl; for ( int i=0; i<10; ++i ) { if ( i == 3 ) { - ASG_CHECK_SA( "hello", htool.setProperty("OutputLevel", MSG::INFO) ); + ANA_CHECK( htool.setProperty("OutputLevel", MSG::INFO) ); } htool.talk(); } cout << line << endl; cout << myname << "Check failure:" << endl; - ASG_CHECK_SA( "hello", StatusCode::FAILURE); + ANA_CHECK( StatusCode (StatusCode::FAILURE)); cout << myname << "End of failure check" << endl; cout << myname << "End." << endl; return 0;