Commit a6f9e70f authored by Floris Berendsen's avatar Floris Berendsen
Browse files

ENH: replaced ComponentFactoryTest by new ComponentSelectorTest

parent 42fadff5
......@@ -19,14 +19,11 @@
#include "gtest/gtest.h"
#include "selxComponentSelector.h"
#include "selxTypeList.h"
#include "selxTransformComponent1.h"
//#include "itkTransformComponent1Factory.h"
#include "selxMetricComponent1.h"
//#include "itkMetricComponent1Factory.h"
#include "selxComponentFactory.h"
#include "selxGDOptimizer3rdPartyComponent.h"
#include "selxGDOptimizer4thPartyComponent.h"
#include "selxSSDMetric3rdPartyComponent.h"
......@@ -34,11 +31,10 @@
namespace selx
{
class ComponentFactoryTest : public ::testing::Test
class ComponentSelectorTest : public ::testing::Test
{
public:
typedef std::list< itk::LightObject::Pointer > RegisteredObjectsContainerType;
typedef ComponentBase ComponentType;
//typedef ComponentBase::CriteriaType CriteriaType;
......@@ -46,9 +42,13 @@ public:
typedef ComponentBase::ParameterValueType ParameterValueType;
//typedef std::map<std::string, std::string> CriteriaType;
//typedef std::pair<std::string, std::string> CriterionType;
typedef TypeList<> ComponentList;
typedef ComponentSelector<ComponentList>::Pointer NodePointer;
typedef TypeList<> EmptyComponentList;
// each node can hold multiple components (or none). Its the NetworkBuilder's task to make it one per node.
//typedef ComponentSelector<EmptyComponentList>::Pointer NodePointer;
using SmallComponentList = TypeList<TransformComponent1, MetricComponent1>;
using BigComponentList = TypeList<TransformComponent1, MetricComponent1, GDOptimizer3rdPartyComponent, GDOptimizer4thPartyComponent, SSDMetric3rdPartyComponent, SSDMetric4thPartyComponent>;
virtual void SetUp()
{
......@@ -57,157 +57,118 @@ public:
virtual void TearDown()
{
itk::ObjectFactoryBase::UnRegisterAllFactories();
}
// each node can hold multiple components (or none). Its the NetworkBuilder's task to make it one per node.
NodePointer Node1;
NodePointer Node2;
};
TEST_F( ComponentFactoryTest, EmptyObjectFactoryBase )
TEST_F( ComponentSelectorTest, EmptyComponentList )
{
// The Component factory is inspired/based on the ImageIO factory.
// In ITK Components (transformIO and imageIO) can be preregistered: CMake\UseITK.cmake sets up the IOFactory_Register_Manager
// In Elastix (by Denis) GPU filters are preregistered using (simpleITK's) Typelists and Cmake
// In this test we manually register 2 dummy modules: itkTransformComponent1 and itkMetricComponent1.
RegisteredObjectsContainerType registeredComponents;
auto componentSelector = ComponentSelector<EmptyComponentList>::New();
// "When CMake is not used to register the Component classes, there are"
EXPECT_NO_THROW( registeredComponents = itk::ObjectFactoryBase::CreateAllInstance( "ComponentBase" ) );
// " 0 Component objects available to the NetworkBuilder."
EXPECT_EQ( registeredComponents.size(), 0 );
EXPECT_EQ(componentSelector->NumberOfComponents(), 0);
}
TEST_F( ComponentFactoryTest, FilledObjectFactoryBase )
TEST_F(ComponentSelectorTest, FilledComponentList)
{
// In this test we manually register 2 dummy modules: itkTransformComponent1 and itkMetricComponent1.
RegisteredObjectsContainerType registeredComponents;
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
//EXPECT_NO_THROW(TransformComponent1Factory::RegisterOneFactory());
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
//EXPECT_NO_THROW(MetricComponent1Factory::RegisterOneFactory());
auto componentSelector = ComponentSelector<TypeList<TransformComponent1, MetricComponent1>>::New();
// After registering the TransformComponent1 and MetricComponent1object, there are
EXPECT_NO_THROW( registeredComponents = itk::ObjectFactoryBase::CreateAllInstance( "ComponentBase" ) );
// " 2 Component objects available to the NetworkBuilder."
EXPECT_EQ( registeredComponents.size(), 2 );
EXPECT_EQ(componentSelector->NumberOfComponents(), 2);
}
TEST_F( ComponentFactoryTest, SetEmptyCriteria )
TEST_F( ComponentSelectorTest, SetEmptyCriteria )
{
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
auto componentSelector = ComponentSelector<SmallComponentList>::New();
CriterionType emptyCriterion; // = CriterionType();
ASSERT_NO_THROW(Node1 = ComponentSelector<ComponentList>::New());
EXPECT_NO_THROW( Node1->AddCriterion( emptyCriterion ) );
ComponentType::Pointer Node1Component;
EXPECT_NO_THROW( Node1Component = Node1->GetComponent() );
EXPECT_NO_THROW(componentSelector->AddCriterion(emptyCriterion));
ComponentType::Pointer component;
EXPECT_NO_THROW(component = componentSelector->GetComponent());
//Too few criteria means no Component could be selected."
EXPECT_TRUE( Node1Component.IsNull() );
EXPECT_TRUE(component.IsNull());
}
TEST_F( ComponentFactoryTest, SetSufficientCriteria )
TEST_F( ComponentSelectorTest, SetSufficientCriteria )
{
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
auto componentSelector = ComponentSelector<SmallComponentList>::New();
CriterionType criterion2 = { "ComponentInput", { "Transform" } };
ASSERT_NO_THROW(Node2 = ComponentSelector<ComponentList>::New());
CriterionType criterion = { "ComponentInput", { "Transform" } };
ASSERT_NO_THROW( Node2->AddCriterion( criterion2 ) );
ComponentType::Pointer Node2Component;
EXPECT_NO_THROW( Node2Component = Node2->GetComponent() );
ASSERT_NO_THROW(componentSelector->AddCriterion(criterion));
ComponentType::Pointer component;
EXPECT_NO_THROW(component = componentSelector->GetComponent());
//Sufficient criteria means one Component was selected."
EXPECT_FALSE( Node2Component.IsNull() );
EXPECT_FALSE(component.IsNull());
//Based on the criteria MetricComponent1 should be selected
EXPECT_STREQ( Node2Component->GetNameOfClass(), "MetricComponent1" );
EXPECT_STREQ(component->GetNameOfClass(), "MetricComponent1");
}
TEST_F( ComponentFactoryTest, AddCriteria )
TEST_F( ComponentSelectorTest, AddCriteria )
{
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
auto componentSelector = ComponentSelector<SmallComponentList>::New();
CriterionType nonSelectiveCriterion({ "ComponentProperty", { "SomeProperty" } });
CriterionType criterion1( { "ComponentOutput", { "Transform" } } );
Node1 = ComponentSelector<ComponentList>::New();
CriterionType criterion( { "ComponentOutput", { "Transform" } } );
EXPECT_NO_THROW( Node1->AddCriterion( nonSelectiveCriterion ) );
ComponentType::Pointer Node1Component;
EXPECT_NO_THROW(componentSelector->AddCriterion(nonSelectiveCriterion));
ComponentType::Pointer component;
EXPECT_TRUE( Node1->NumberOfComponents()>1 );
EXPECT_NO_THROW( Node1Component = Node1->GetComponent() );
EXPECT_TRUE(componentSelector->NumberOfComponents()>1);
EXPECT_NO_THROW(component = componentSelector->GetComponent());
//Unsufficient criteria means no Component was selected."
EXPECT_TRUE( Node1Component.IsNull() );
EXPECT_TRUE(component.IsNull());
EXPECT_NO_THROW( Node1->AddCriterion( criterion1 ) );
EXPECT_NO_THROW( Node1Component = Node1->GetComponent() );
EXPECT_NO_THROW(componentSelector->AddCriterion(criterion));
EXPECT_NO_THROW(component = componentSelector->GetComponent());
//Sufficient criteria means one Component was selected."
EXPECT_FALSE( Node1->NumberOfComponents()>1 );
EXPECT_FALSE( Node1Component.IsNull() );
EXPECT_FALSE(componentSelector->NumberOfComponents()>1);
EXPECT_FALSE(component.IsNull());
//Based on the criteria TransformComponent1 should be selected
EXPECT_STREQ( Node1Component->GetNameOfClass(), "TransformComponent1" );
EXPECT_STREQ(component->GetNameOfClass(), "TransformComponent1");
}
TEST_F( ComponentFactoryTest, InterfacedObjects )
TEST_F( ComponentSelectorTest, InterfacedObjects )
{
RegisteredObjectsContainerType registeredComponents;
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< GDOptimizer3rdPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< GDOptimizer4thPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< SSDMetric3rdPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< SSDMetric4thPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( registeredComponents = itk::ObjectFactoryBase::CreateAllInstance( "ComponentBase" ) );
auto componentSelectorA = ComponentSelector<BigComponentList>::New();
// " 6 Component objects available to the NetworkBuilder."
EXPECT_EQ( registeredComponents.size(), 6 );
EXPECT_EQ(componentSelectorA->NumberOfComponents() , 6 );
NodePointer Node3 = ComponentSelector<ComponentList>::New();
Node3->AddAcceptingInterfaceCriteria( { { "NameOfInterface", "MetricDerivativeInterface" } } );
componentSelectorA->AddAcceptingInterfaceCriteria({ { "NameOfInterface", "MetricDerivativeInterface" } });
ComponentType::Pointer Node3Component;
EXPECT_NO_THROW( Node3Component = Node3->GetComponent() );
EXPECT_STREQ( Node3Component->GetNameOfClass(), "GDOptimizer3rdPartyComponent" );
ComponentType::Pointer componentA;
EXPECT_NO_THROW(componentA = componentSelectorA->GetComponent());
EXPECT_STREQ(componentA->GetNameOfClass(), "GDOptimizer3rdPartyComponent");
NodePointer Node4 = ComponentSelector<ComponentList>::New();
Node4->AddProvidingInterfaceCriteria( { { "NameOfInterface", "MetricDerivativeInterface" } } );
ComponentType::Pointer Node4Component;
EXPECT_NO_THROW( Node4Component = Node4->GetComponent() );
EXPECT_STREQ( Node4Component->GetNameOfClass(), "SSDMetric3rdPartyComponent" );
auto componentSelectorB = ComponentSelector<BigComponentList>::New();
componentSelectorB->AddProvidingInterfaceCriteria({ { "NameOfInterface", "MetricDerivativeInterface" } });
ComponentType::Pointer componentB;
EXPECT_NO_THROW(componentB = componentSelectorB->GetComponent());
EXPECT_STREQ(componentB->GetNameOfClass(), "SSDMetric3rdPartyComponent");
}
TEST_F( ComponentFactoryTest, UnknownComponent )
TEST_F( ComponentSelectorTest, UnknownComponent )
{
// Fill our component database with some components
RegisteredObjectsContainerType registeredComponents;
EXPECT_NO_THROW( ComponentFactory< TransformComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< MetricComponent1 >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< GDOptimizer3rdPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< GDOptimizer4thPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< SSDMetric3rdPartyComponent >::RegisterOneFactory() );
EXPECT_NO_THROW( ComponentFactory< SSDMetric4thPartyComponent >::RegisterOneFactory() );
auto componentSelector = ComponentSelector<BigComponentList>::New();
// Setup the criterion for a component that does not exist in our data base
CriterionType criterion( { "NameOfClass", { "DoYouHaveThisComponent?" } } );
NodePointer Node = ComponentSelector<ComponentList>::New();
Node->AddCriterion( criterion );
ComponentType::Pointer NodeComponent;
componentSelector->AddCriterion(criterion);
ComponentType::Pointer component;
// we expect 0 components
EXPECT_TRUE( Node->NumberOfComponents() == 0 );
EXPECT_TRUE( Node->GetComponent().IsNull() );
EXPECT_TRUE(componentSelector->NumberOfComponents() == 0);
EXPECT_TRUE(componentSelector->GetComponent().IsNull());
}
} // namespace selx
......@@ -48,7 +48,7 @@ set( ${MODULE}_SOURCE_FILES
# Export tests
set( ${MODULE}_TEST_SOURCE_FILES
${${MODULE}_SOURCE_DIR}/Blueprints/test/selxBlueprintTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxComponentFactoryTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxComponentSelectorTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxComponentInterfaceTest.cxx
${${MODULE}_SOURCE_DIR}/ComponentInterface/test/selxNetworkBuilderTest.cxx
${${MODULE}_SOURCE_DIR}/ConfigurationReader/test/selxConfigurationReaderTest.cxx
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment