Skip to content
Snippets Groups Projects
Commit a9fd9cfa authored by Vakhtang Tsulaia's avatar Vakhtang Tsulaia
Browse files

Merge branch 'asg_data_handles_test' into 'master'

add a unit test for AsgDataHandles

See merge request atlas/athena!35077
parents bc9f77e8 1af39479
No related branches found
No related tags found
No related merge requests found
Showing
with 295 additions and 13 deletions
......@@ -140,6 +140,11 @@ private:
* Throws ExcNullReadHandle on failure.
*/
const_pointer_type checkedCPtr();
/**
* @brief Helper: dereference the pointer.
*/
const_pointer_type getCPtr() const;
};
......
......@@ -109,10 +109,7 @@ inline
typename ReadHandle<T>::const_pointer_type
ReadHandle<T>::cptr()
{
const T *result = nullptr;
if (!xAOD::TActiveEvent::event()->retrieve (result, key(), true))
throw std::runtime_error ("failed to retrieve " + key());
return result;
return getCPtr();
}
......@@ -161,7 +158,7 @@ inline
typename ReadHandle<T>::const_pointer_type
ReadHandle<T>::get() const
{
return cptr();
return getCPtr();
}
......@@ -195,6 +192,21 @@ ReadHandle<T>::checkedCPtr()
}
/**
* @brief Helper: dereference the pointer.
* Throws ExcNullReadHandle on failure.
*/
template <class T>
inline
typename ReadHandle<T>::const_pointer_type
ReadHandle<T>::getCPtr() const
{
const T *result = nullptr;
(void) xAOD::TActiveEvent::event()->retrieve (result, key(), true);
return result;
}
/**
* @brief Return a @c ReadHandle referencing @c key.
* @param key The key object holding the clid/key/store.
......
......@@ -7,6 +7,7 @@
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#include "AsgExampleTools/DataHandleTestTool.h"
#include "AsgExampleTools/UnitTestTool3.h"
#include "AsgExampleTools/UnitTestTool2.h"
#include "AsgExampleTools/UnitTestTool1A.h"
......
/*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
/// @author Nils Krumnack
#ifndef ASG_TOOLS__DATA_HANDLES_TEST_TOOL_H
#define ASG_TOOLS__DATA_HANDLES_TEST_TOOL_H
#include <AsgTools/AsgTool.h>
#include <AsgExampleTools/IDataHandleTestTool.h>
#include <AsgDataHandles/ReadHandleKey.h>
// AthSimulation doesn't contain the muon-container, so we can't
// really build the tool, but it is simpler to build an empty tool
// than to exclude the tool completely from the AthSimulation build.
#ifndef SIMULATIONBASE
#include <xAODMuon/MuonContainer.h>
#endif
namespace asg
{
/// \brief a tool used to unit test AnaToolHandle
///
/// This allows to unit test the various capabilities of
/// stand-alone data handles in a controlled fashion.
struct DataHandleTestTool : virtual public IDataHandleTestTool,
public AsgTool
{
ASG_TOOL_CLASS (DataHandleTestTool, IDataHandleTestTool)
/// \brief standard constructor
public:
DataHandleTestTool (const std::string& val_name);
/// \brief standard destructor
public:
~DataHandleTestTool ();
public:
StatusCode initialize () override;
public:
void runTest () override;
public:
#ifndef SIMULATIONBASE
SG::ReadHandleKey<xAOD::MuonContainer> m_readKey {this, "readKey", "Muons", "regular read key"};
#endif
bool m_readFailure {false};
};
}
#endif
/*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
/// @author Nils Krumnack
#ifndef ASG_TOOLS__I_DATA_HANDLE_TEST_TOOL_H
#define ASG_TOOLS__I_DATA_HANDLE_TEST_TOOL_H
#include <AsgTools/IAsgTool.h>
namespace asg
{
/// \brief the interface for \ref DataHandleTestTool
class IDataHandleTestTool : virtual public IAsgTool
{
// Declare the interface that this class provides
ASG_TOOL_INTERFACE( CP::IDataHandleTestTool )
/// \brief run the test
public:
virtual void runTest () = 0;
};
}
#endif
......@@ -2,6 +2,7 @@
<lcgdict>
<!-- Unit test types: -->
<class name="asg::DataHandleTestTool" />
<class name="asg::UnitTestTool3" />
<class name="asg::UnitTestTool2" />
<class name="asg::UnitTestTool1A" />
......
......@@ -5,21 +5,21 @@
# Declare the package name:
atlas_subdir( AsgExampleTools )
# Extra dependencies based on the build environment:
set( extra_deps )
if( XAOD_STANDALONE )
set( extra_deps Control/xAODRootAccess )
if (SIMULATIONBASE)
set (extra_dep )
set (extra_lib )
else()
set( extra_deps Control/AthenaBaseComps GaudiKernel )
set (extra_dep Event/xAOD/xAODMuon)
set (extra_lib xAODMuon)
endif()
# Declare the package's dependencies:
atlas_depends_on_subdirs(
PUBLIC
Control/AthToolSupport/AsgDataHandles
Control/AthToolSupport/AsgTools
PRIVATE
Control/AthToolSupport/AsgTesting
${extra_deps} )
${extra_dep})
# External dependencies:
find_package( GTest )
......@@ -33,7 +33,7 @@ atlas_add_root_dictionary( AsgExampleToolsLib AsgExampleToolsCintDict
atlas_add_library( AsgExampleToolsLib
AsgExampleTools/*.h Root/*.cxx ${AsgExampleToolsCintDict}
PUBLIC_HEADERS AsgExampleTools
LINK_LIBRARIES AsgTools AsgTestingLib )
LINK_LIBRARIES AsgTools AsgDataHandlesLib AsgTestingLib ${extra_lib} )
if( NOT XAOD_STANDALONE )
atlas_add_component( AsgExampleTools
......@@ -101,6 +101,12 @@ if( XAOD_STANDALONE )
LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib )
set_tests_properties (AsgExampleTools_gt_asgtools_toolhandle_test_ctest PROPERTIES LABELS "AsgTools;AsgExampleTools" )
atlas_add_test( gt_DataHandlesTest
SOURCES test/gt_DataHandlesTest.cxx
INCLUDE_DIRS ${GTEST_INCLUDE_DIRS}
LINK_LIBRARIES ${GTEST_LIBRARIES} AsgTools AsgExampleToolsLib AsgTestingLib )
set_tests_properties (AsgExampleTools_gt_DataHandlesTest_ctest PROPERTIES LABELS "AsgDataHandles;AsgExampleTools" )
endif()
......
/*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
/// @author Nils Krumnack
//
// includes
//
#include <AsgExampleTools/DataHandleTestTool.h>
#include <AsgDataHandles/ReadHandle.h>
#include <AsgTesting/UnitTest.h>
#include <gtest/gtest.h>
#include <map>
//
// method implementations
//
namespace asg
{
DataHandleTestTool ::
DataHandleTestTool (const std::string& val_name)
: AsgTool (val_name)
{
declareProperty ("readFailure", m_readFailure, "whether to expect a read failure");
}
DataHandleTestTool ::
~DataHandleTestTool ()
{
}
StatusCode DataHandleTestTool ::
initialize ()
{
#ifndef SIMULATIONBASE
ANA_CHECK (m_readKey.initialize ());
#endif
return StatusCode::SUCCESS;
}
void DataHandleTestTool ::
runTest ()
{
#ifndef SIMULATIONBASE
const xAOD::MuonContainer *muonsStore {nullptr};
ASSERT_SUCCESS (evtStore()->retrieve (muonsStore, "Muons"));
auto readHandle = makeHandle (m_readKey);
if (m_readFailure == false)
EXPECT_EQ (muonsStore, readHandle.get());
else
EXPECT_EQ (nullptr, readHandle.get());
#endif
}
}
......@@ -3,6 +3,7 @@
#include "../AsgExampleAlgorithm.h"
#include "AsgExampleTools/AsgHelloTool.h"
#include <AsgExampleTools/DataHandleTestTool.h>
#include <AsgExampleTools/UnitTestTool1.h>
#include <AsgExampleTools/UnitTestTool1A.h>
#include <AsgExampleTools/UnitTestTool2.h>
......@@ -12,6 +13,7 @@ DECLARE_COMPONENT( AsgHelloTool )
DECLARE_COMPONENT( AsgExampleAlgorithm )
DECLARE_COMPONENT( asg::DataHandleTestTool )
DECLARE_COMPONENT( asg::UnitTestTool1 )
DECLARE_COMPONENT( asg::UnitTestTool1A )
DECLARE_COMPONENT( asg::UnitTestTool2 )
......
/*
Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
*/
/// @author Nils Krumnack
//
// includes
//
#include <AsgTools/AsgToolConfig.h>
#include <AsgMessaging/MessageCheck.h>
#include <AsgTesting/UnitTest.h>
#include <AsgExampleTools/IDataHandleTestTool.h>
#include <xAODRootAccess/TEvent.h>
#include <xAODRootAccess/TStore.h>
#include <TFile.h>
#include <cmath>
#include <gtest/gtest.h>
#include <sstream>
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
//
// method implementations
//
using namespace asg::msgUserCode;
namespace asg
{
struct DataHandlesTest : public ::testing::Test
{
static void SetUpTestCase ()
{
const char *test_file = getenv ("ASG_TEST_FILE_MC");
ASSERT_NE (nullptr, test_file);
file.reset (TFile::Open (test_file, "READ"));
ASSERT_NE (nullptr, file);
}
static void TearDownTestCase ()
{
file.reset ();
}
virtual void SetUp () override
{
ASSERT_NE (nullptr, file);
ASSERT_SUCCESS (event.readFrom (file.get()));
ASSERT_TRUE (event.getEntry (0) >= 0);
}
/// \brief make a unique tool name to be used in unit tests
std::string makeUniqueName ()
{
static unsigned index = 0;
std::ostringstream str;
str << "unique" << ++ index;
return str.str();
}
static inline std::unique_ptr<TFile> file;
xAOD::TEvent event;
xAOD::TStore store;
AsgToolConfig config {"asg::DataHandleTestTool/" + makeUniqueName()};
std::shared_ptr<void> cleanup;
ToolHandle<IDataHandleTestTool> tool;
};
// just test that a basic read handle access works
TEST_F (DataHandlesTest, base_test)
{
ASSERT_SUCCESS (config.makeTool (tool, cleanup));
tool->runTest ();
}
// just test that reading unknown objects fails as it should
TEST_F (DataHandlesTest, read_failure)
{
config.setPropertyFromString ("readFailure", "1");
config.setPropertyFromString ("readKey", "MuonsFailure");
ASSERT_SUCCESS (config.makeTool (tool, cleanup));
tool->runTest ();
}
}
ATLAS_GOOGLE_TEST_MAIN
......@@ -229,6 +229,13 @@ namespace asg
ASSERT_EQ (0, value);
}
TEST (SetStringHelperTest, set_bool)
{
bool value = true;
ASSERT_SUCCESS (SetStringHelper<bool>::set ("1", value));
ASSERT_EQ (true, value);
}
TEST (SetStringHelperTest, set_enum)
{
MSG::Level value = MSG::Level::INFO;
......
......@@ -13,6 +13,7 @@
#include <AsgTools/MessageCheckAsgTools.h>
#ifdef XAOD_STANDALONE
#include <AsgTools/AsgComponent.h>
#include <AsgTools/TProperty.h>
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment