Skip to content

Commit

Permalink
Re #37 Written unit test for instrumentGeometryAbstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljturner committed Sep 14, 2017
1 parent 7fa9fbd commit 8b2a797
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 20 deletions.
1 change: 1 addition & 0 deletions Framework/NexusGeometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set ( INC_FILES

set ( TEST_FILES
InstrumentGeometryAbstractionTest.h
NexusGeometryParserTest.h
)

if (COVERALLS)
Expand Down
12 changes: 12 additions & 0 deletions Framework/NexusGeometry/inc/MantidNexusGeometry/DllConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef MANTID_NEXUSGEOMETRY_DLLCONFIG_H_
#define MANTID_NEXUSGEOMETRY_DLLCONFIG_H_

#include "MantidKernel/System.h"

#ifdef IN_MANTID_NEXUSGEOMETRY
#define MANTID_DATAHANDLING_DLL DLLExport
#else
#define MANTID_DATAHANDLING_DLL DLLImport
#endif

#endif // MANTID_NEXUSGEOMETRY_DLLCONFIG_H_
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
namespace Mantid{
namespace NexusGeometry{

class InstrumentGeometryAbstraction : public NexusGeometry::InstrumentAbstractBuilder<InstrumentGeometryAbstraction>{
class DLLExport InstrumentGeometryAbstraction : public NexusGeometry::InstrumentAbstractBuilder<InstrumentGeometryAbstraction>{
public:
///Constructor creates the instrument
InstrumentGeometryAbstraction(std::string &instrumentName);
InstrumentGeometryAbstraction(const std::string &instrumentName);
///Adds component to instrument
Geometry::IComponent *addComponent(std::string &compName, Eigen::Vector3d &position);
///Adds detector to instrument
Expand All @@ -29,6 +29,8 @@ class InstrumentGeometryAbstraction : public NexusGeometry::InstrumentAbstractBu
void addSample(std::string &sampleName, Eigen::Vector3d &position);
///Add source
void addSource(std::string &sourceName, Eigen::Vector3d &position);
///Returns underlying instrument
Geometry::Instrument_sptr _unAbstractInstrument(){return this->instrument_sptr;}
private:
Geometry::Instrument_sptr instrument_sptr;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ namespace Mantid {
namespace NexusGeometry {

//Choose which derived instrumentAbstraction to use
typedef std::weak_ptr<InstrumentGeometryAbstraction> iAbstractBuilder_wkpr;
typedef InstrumentGeometryAbstraction iAbstractBuilder;
typedef std::shared_ptr<iAbstractBuilder> iAbstractBuilder_sptr;

class NexusGeometryParser
class DLLExport NexusGeometryParser
{
public:
/// Constructor // , std::weak_ptr<InstrumentHandler> iHandler
explicit NexusGeometryParser(const H5std_string &fileName);
explicit NexusGeometryParser(const H5std_string &fileName, iAbstractBuilder_sptr iAbsBuilder_sptr);

/// Destructor
~NexusGeometryParser() = default;
Expand All @@ -43,7 +44,7 @@ class NexusGeometryParser
H5::Group rootGroup;
ParsingErrors exitStatus = NO_ERROR;
///Instrument abstraction builder
iAbstractBuilder_wkpr iBuilder_wkpr;
iAbstractBuilder_sptr iBuilder_sptr;
/// Opens sub groups of current group
std::vector<H5::Group> openSubGroups(H5::Group &parentGroup, H5std_string CLASS_TYPE);
/// Opens all detector groups in a file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Mantid{
namespace NexusGeometry{

///Constructor
InstrumentGeometryAbstraction::InstrumentGeometryAbstraction(std::string &instrumentName){
InstrumentGeometryAbstraction::InstrumentGeometryAbstraction(const std::string &instrumentName){
Geometry::Instrument_sptr inst_sptr(new Geometry::Instrument(instrumentName));
this->instrument_sptr = inst_sptr;
}
Expand Down
7 changes: 4 additions & 3 deletions Framework/NexusGeometry/src/NexusGeometryParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace {

/// Constructor opens the nexus file
NexusGeometryParser::NexusGeometryParser(
const H5std_string &fileName) {
const H5std_string &fileName, iAbstractBuilder_sptr iAbsBuilder_sptr ) {

// Disable automatic printing, so Load algorithm can deal with errors
// appropriately
Expand All @@ -53,7 +53,8 @@ NexusGeometryParser::NexusGeometryParser(
} catch (GroupIException e) {
this->exitStatus = OPENING_ROOT_GROUP_ERROR;
}
//this->iHandler = iHandler;
//Initialize the instrumentAbstractBuilder
this->iBuilder_sptr = iAbsBuilder_sptr;
}

/// OFF NEXUS GEOMETRY PARSER
Expand All @@ -68,7 +69,7 @@ ParsingErrors NexusGeometryParser::ParseNexusGeometry()
}

/// Open subgroups of parent group
std::vector<Group> openSubGroups(Group &parentGroup, H5std_string CLASS_TYPE){
std::vector<Group> NexusGeometryParser::openSubGroups(Group &parentGroup, H5std_string CLASS_TYPE){
std::vector<Group> subGroups;

//Iterate over children, and determine if a group
Expand Down
15 changes: 7 additions & 8 deletions Framework/NexusGeometry/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ if ( CXXTEST_FOUND )
include_directories ( SYSTEM ${CXXTEST_INCLUDE_DIR} ${GMOCK_INCLUDE_DIR} ${GTEST_INCLUDE_DIR} )

cxxtest_add_test ( NexusGeometryTest ${TEST_FILES})
target_link_libraries( NexusGeometryTest LINK_PRIVATE ${TCMALLOC_LIBRARIES_LINKTIME}

target_include_directories( NexusGeometryTest SYSTEM PRIVATE ${HDF5_INCLUDE_DIRS} )
target_link_libraries( NexusGeometryTest LINK_PRIVATE ${TCMALLOC_LIBRARIES_LINKTIME} ${MANTIDLIBS}
NexusGeometry
Geometry
Beamline
Nexus
${NEXUS_LIBRARIES}
${GSL_LIBRARIES}
${Boost_LIBRARIES}
${POCO_LIBRARIES}
${GMOCK_LIBRARIES}
${GTEST_LIBRARIES} )

${HDF5_LIBRARIES}
${HDF5_HL_LIBRARIES} )
add_dependencies ( NexusGeometryTest Geometry)
add_dependencies ( FrameworkTests NexusGeometryTest )
# Add to the 'FrameworkTests' group in VS
set_property ( TARGET NexusGeometryTest PROPERTY FOLDER "UnitTests" )
Expand Down
49 changes: 47 additions & 2 deletions Framework/NexusGeometry/test/InstrumentGeometryAbstractionTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,56 @@ using namespace NexusGeometry;

class InstrumentGeometryAbstractionTest : public CxxTest::TestSuite {
public:
void testConstructor(){
TS_ASSERT_THROWS_NOTHING(InstrumentGeometryAbstraction iGeoAbstract(this->iTestName));
void testConstructor_and_test_unAbstractInstrument(){
InstrumentGeometryAbstraction iGeoAbstract(this->iTestName);
TS_ASSERT(iGeoAbstract._unAbstractInstrument()->getName() == this->iTestName);
}
void testAddComponent(){
InstrumentGeometryAbstraction iGeoAbstract(this->iTestName);
TS_ASSERT_THROWS_NOTHING(auto comp = iGeoAbstract.addComponent(this->cTestName, this->testPos1));
auto iVisitor = Geometry::InstrumentVisitor(iGeoAbstract._unAbstractInstrument());
iVisitor.walkInstrument();
auto iCompInfo = iVisitor.componentInfo();
TS_ASSERT(iCompInfo->position(0) == this->testPos1);
}

void testAddDetector_and_testSortDetectors(){
InstrumentGeometryAbstraction iGeoAbstract(this->iTestName);
TS_ASSERT_THROWS_NOTHING(iGeoAbstract.addDetector(this->dTestName, 2, this->testPos2));
iGeoAbstract.addDetector(this->dTestName, 1, this->testPos1);
for (int i = 0; i < 2; ++i){
auto iVisitor = Geometry::InstrumentVisitor(iGeoAbstract._unAbstractInstrument());
iVisitor.walkInstrument();
auto iDetInfo = iVisitor.detectorInfo();
if( i == 0){
TS_ASSERT(iDetInfo->position(0) == this->testPos2);
} else {
TS_ASSERT(iDetInfo->position(0) == this->testPos1);
}
TS_ASSERT_THROWS_NOTHING(iGeoAbstract.sortDetectors());
}
}

void testAddSample_and_testAddSource(){
InstrumentGeometryAbstraction iGeoAbstract(this->iTestName);
TS_ASSERT_THROWS_NOTHING(iGeoAbstract.addSample(this->sampleName, this->testPos1));
TS_ASSERT_THROWS_NOTHING(iGeoAbstract.addSource(this->sourceName, this->testPos2));
auto iVisitor = Geometry::InstrumentVisitor(iGeoAbstract._unAbstractInstrument());
iVisitor.walkInstrument();
auto iCompInfo = iVisitor.componentInfo();
TS_ASSERT(iCompInfo->hasSample());
TS_ASSERT(iCompInfo->samplePosition() == this->testPos1);
TS_ASSERT(iCompInfo->hasSource());
TS_ASSERT(iCompInfo->sourcePosition() == this->testPos2);
}
private:
std::string iTestName = "testInstrument";
std::string cTestName = "testComponent";
std::string dTestName = "testDetector";
std::string sourceName = "testSource";
std::string sampleName = "testSample";
Eigen::Vector3d testPos1 = Eigen::Vector3d(1.0,-0.5,2.9);
Eigen::Vector3d testPos2 = Eigen::Vector3d(-1.2,0.5,1.9);
};

#endif// INSTRUMENT_GEOMETRY_ABSTRACTION_TEST_H_
13 changes: 13 additions & 0 deletions Framework/NexusGeometry/test/NexusGeometryParserTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//----------------
// Includes
//----------------

#include <cxxtest/TestSuite.h>

#include "MantidNexusGeometry/NexusGeometryParser.h"

class NexusGeometryParserTest : public CxxTest::TestSuite {
public:
void TestFiller(){TS_ASSERT(true);}
private:
};

0 comments on commit 8b2a797

Please sign in to comment.