diff --git a/Modules/Core/test/mitkBaseDataTest.cpp b/Modules/Core/test/mitkBaseDataTest.cpp index 5661a86164..550dd039a7 100644 --- a/Modules/Core/test/mitkBaseDataTest.cpp +++ b/Modules/Core/test/mitkBaseDataTest.cpp @@ -1,124 +1,283 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include "mitkTestingMacros.h" -#include "itkImage.h" +// std includes +#include + +// MITK includes #include "mitkBaseDataTestImplementation.h" #include "mitkStringProperty.h" -#include "mitkTestingMacros.h" #include #include -int mitkBaseDataTest(int /*argc*/, char * /*argv*/ []) +// itksys +#include "itkImage.h" + +// VTK includes +#include + +class mitkBaseDataTestSuite : public mitk::TestFixture { - MITK_TEST_BEGIN("BaseData") - - // Create a BaseData implementation - MITK_INFO << "Creating a base data instance..."; - mitk::BaseDataTestImplementation::Pointer baseDataImpl = mitk::BaseDataTestImplementation::New(); - - MITK_TEST_CONDITION_REQUIRED(baseDataImpl.IsNotNull(), "Testing instantiation"); - MITK_TEST_CONDITION(baseDataImpl->IsInitialized(), "BaseDataTestImplementation is initialized"); - MITK_TEST_CONDITION(baseDataImpl->IsEmpty(), "BaseDataTestImplementation is initialized and empty"); - - mitk::BaseDataTestImplementation::Pointer cloneBaseData = baseDataImpl->Clone(); - MITK_TEST_CONDITION_REQUIRED(cloneBaseData.IsNotNull(), "Testing instantiation of base data clone"); - MITK_TEST_CONDITION(cloneBaseData->IsInitialized(), "Clone of BaseDataTestImplementation is initialized"); - MITK_TEST_CONDITION(cloneBaseData->IsEmpty(), "Clone of BaseDataTestImplementation is initialized and empty"); - - MITK_INFO << "Testing setter and getter for geometries..."; - - // test method GetTimeGeometry() - MITK_TEST_CONDITION(baseDataImpl->GetTimeGeometry(), "Testing creation of TimeGeometry"); - - mitk::TimeGeometry *geo = nullptr; - baseDataImpl->SetTimeGeometry(geo); - - MITK_TEST_CONDITION(baseDataImpl->GetTimeGeometry() == nullptr, "Reset Geometry"); - - mitk::ProportionalTimeGeometry::Pointer geo2 = mitk::ProportionalTimeGeometry::New(); - baseDataImpl->SetTimeGeometry(geo2); - geo2->Initialize(2); - MITK_TEST_CONDITION(baseDataImpl->GetTimeGeometry() == geo2.GetPointer(), "Correct Reinit of TimeGeometry"); - - // test method GetGeometry(int timeStep) - MITK_TEST_CONDITION(baseDataImpl->GetGeometry(1) != nullptr, "... and single Geometries"); - - // test method Expand(unsigned int timeSteps) - baseDataImpl->Expand(5); - MITK_TEST_CONDITION(baseDataImpl->GetTimeSteps() == 5, "Expand the geometry to further time slices!"); - - // test method GetUpdatedGeometry(int timeStep); - mitk::Geometry3D::Pointer geometry3D = mitk::Geometry3D::New(); - mitk::BaseGeometry::Pointer geo3 = dynamic_cast(geometry3D.GetPointer()); - mitk::ProportionalTimeGeometry::Pointer timeGeometry = - dynamic_cast(baseDataImpl->GetTimeGeometry()); - if (timeGeometry.IsNotNull()) - { - timeGeometry->SetTimeStepGeometry(geo3, 1); - } - - MITK_TEST_CONDITION(baseDataImpl->GetUpdatedGeometry(1) == geo3, "Set Geometry for time step 1"); - MITK_TEST_CONDITION(baseDataImpl->GetMTime() != 0, "Check if modified time is set"); - baseDataImpl->SetClonedGeometry(geo3, 1); - - mitk::ScalarType x[3]; - x[0] = 2; - x[1] = 4; - x[2] = 6; - mitk::Point3D p3d(x); - baseDataImpl->SetOrigin(p3d); - geo3->SetOrigin(p3d); - - MITK_TEST_CONDITION(baseDataImpl->GetGeometry(1)->GetOrigin() == geo3->GetOrigin(), "Testing Origin set"); - - cloneBaseData = baseDataImpl->Clone(); - MITK_TEST_CONDITION(cloneBaseData->GetGeometry(1)->GetOrigin() == geo3->GetOrigin(), "Testing origin set in clone!"); - - MITK_TEST_CONDITION(!baseDataImpl->IsEmptyTimeStep(1), "Is not empty before clear()!"); - baseDataImpl->Clear(); - MITK_TEST_CONDITION(baseDataImpl->IsEmptyTimeStep(1), "...but afterwards!"); - // test method Set-/GetProperty() - baseDataImpl->SetProperty("property38", mitk::StringProperty::New("testproperty")); - // baseDataImpl->SetProperty("visibility", mitk::BoolProperty::New()); - MITK_TEST_CONDITION(baseDataImpl->GetProperty("property38")->GetValueAsString() == "testproperty", - "Check if base property is set correctly!"); - - cloneBaseData = baseDataImpl->Clone(); - MITK_TEST_CONDITION(cloneBaseData->GetProperty("property38")->GetValueAsString() == "testproperty", - "Testing origin set in clone!"); - - // test method Set-/GetPropertyList - mitk::PropertyList::Pointer propertyList = mitk::PropertyList::New(); - propertyList->SetFloatProperty("floatProperty1", 123.45); - propertyList->SetBoolProperty("visibility", true); - propertyList->SetStringProperty("nameXY", "propertyName"); - baseDataImpl->SetPropertyList(propertyList); - bool value = false; - MITK_TEST_CONDITION(baseDataImpl->GetPropertyList() == propertyList, "Check if base property list is set correctly!"); - MITK_TEST_CONDITION(baseDataImpl->GetPropertyList()->GetBoolProperty("visibility", value) == true, - "Check if base property is set correctly in the property list!"); - - // test method UpdateOutputInformation() - baseDataImpl->UpdateOutputInformation(); - MITK_TEST_CONDITION(baseDataImpl->GetUpdatedTimeGeometry() == geo2, "TimeGeometry update!"); - // Test method CopyInformation() - mitk::BaseDataTestImplementation::Pointer newBaseData = mitk::BaseDataTestImplementation::New(); - newBaseData->CopyInformation(baseDataImpl); - MITK_TEST_CONDITION_REQUIRED(newBaseData->GetTimeGeometry()->CountTimeSteps() == 5, - "Check copying of of Basedata Data Object!"); - - MITK_TEST_END() -} + CPPUNIT_TEST_SUITE(mitkBaseDataTestSuite); + + MITK_TEST(CreateBaseData_Success); + MITK_TEST(InitializationOfBaseData_Success); + + MITK_TEST(CreateCloneBaseData_Success); + MITK_TEST(InitializationOfCloneBaseData_Success); + + MITK_TEST(GetAndSetTimeGeometry_Success); + MITK_TEST(ResetTimeGeometry_Success); + MITK_TEST(ReinitOfTimeGeometry_Success); + + MITK_TEST(GetGeometryForSingleTimeGeometries_Failure); + + MITK_TEST(TestingExpand_Success); + + MITK_TEST(TestingGetUpdateGeometry_Success); + + MITK_TEST(GetOriginOfBaseData_Success); + MITK_TEST(GetOriginOfCloneBaseData); + + MITK_TEST(ClearATimeStep); + + MITK_TEST(BaseDataSetAndGetProperty_Success); + MITK_TEST(CloneBaseDataSetAndGetProperty_Success); + + MITK_TEST(BasePropertyListIsSet_Success); + MITK_TEST(BasePorpertyIsSetInPropertyList_Success); + + MITK_TEST(UpdateOutputInformationOfBaseData_Failure); + MITK_TEST(CopyingInformationOfBaseData_Failure); + + CPPUNIT_TEST_SUITE_END(); + +private: + mitk::BaseDataTestImplementation::Pointer m_BaseDataImpl; + mitk::BaseDataTestImplementation::Pointer m_CloneBaseData; + + mitk::TimeGeometry *m_Geo; + mitk::ProportionalTimeGeometry::Pointer m_Geo2; + + mitk::Geometry3D::Pointer m_Geometry3D; + mitk::BaseGeometry::Pointer m_Geo3; + + mitk::ScalarType m_X[3]; + mitk::PropertyList::Pointer m_PropertyList; + +public: + void setUp() override + { + m_BaseDataImpl = mitk::BaseDataTestImplementation::New(); + m_CloneBaseData = m_BaseDataImpl->Clone(); + + m_Geo = nullptr; + m_Geo2 = mitk::ProportionalTimeGeometry::New(); + + m_Geometry3D = mitk::Geometry3D::New(); + m_Geo3 = dynamic_cast(m_Geometry3D.GetPointer()); + + m_X[0] = 2; + m_X[1] = 4; + m_X[2] = 6; + + m_PropertyList = mitk::PropertyList::New(); + } + + void tearDown() override + { + m_BaseDataImpl = nullptr; + m_CloneBaseData = nullptr; + + m_Geo = nullptr; + m_Geo2 = nullptr; + + m_Geometry3D = nullptr; + m_Geo3 = nullptr; + + m_X[0] = 0; + m_X[1] = 0; + m_X[2] = 0; + + m_PropertyList = nullptr; + } + + void CreateBaseData_Success() + { + // Create a BaseData implementation + MITK_INFO << "Creating a base data instance..."; + CPPUNIT_ASSERT_MESSAGE("Testing instantiation", m_BaseDataImpl.IsNotNull()); + } + + void InitializationOfBaseData_Success() + { + CPPUNIT_ASSERT_MESSAGE("BaseDataTestImplementation is initialized", m_BaseDataImpl->IsInitialized()); + CPPUNIT_ASSERT_MESSAGE("BaseDataTestImplementation is initialized and empty", m_BaseDataImpl->IsEmpty()); + } + + void CreateCloneBaseData_Success() + { + // Create CloneBaseData implementation + MITK_INFO << "Creating a clone base data instance..."; + CPPUNIT_ASSERT_MESSAGE("Testing instantiation of base data clone", m_CloneBaseData.IsNotNull()); + } + + void InitializationOfCloneBaseData_Success() + { + CPPUNIT_ASSERT_MESSAGE("Clone of BaseDataTestImplementation is initialized", m_CloneBaseData->IsInitialized()); + CPPUNIT_ASSERT_MESSAGE("Clone of BaseDataTestImplementation is initialized and empty", m_CloneBaseData->IsEmpty()); + } + + void GetAndSetTimeGeometry_Success() + { + // test method GetTimeGeometry() + MITK_INFO << "Testing setter and getter for geometries..."; + CPPUNIT_ASSERT_MESSAGE("Testing creation of TimeGeometry", m_BaseDataImpl->GetTimeGeometry()); + } + + void ResetTimeGeometry_Success() + { + m_BaseDataImpl->SetTimeGeometry(m_Geo); + CPPUNIT_ASSERT_MESSAGE("Reset Geometry", m_BaseDataImpl->GetTimeGeometry() == nullptr); + } + + void ReinitOfTimeGeometry_Success() + { + m_BaseDataImpl->SetTimeGeometry(m_Geo2); + m_Geo2->Initialize(2); + CPPUNIT_ASSERT_MESSAGE("Correct Reinit of TimeGeometry", m_BaseDataImpl->GetTimeGeometry() == m_Geo2.GetPointer()); + } + + void GetGeometryForSingleTimeGeometries_Failure() + { + // test method GetGeometry(int timeStep) + CPPUNIT_ASSERT_MESSAGE("Testing Creation of single TimeGeometries", m_BaseDataImpl->GetGeometry(1) == nullptr); + } + + void TestingExpand_Success() + { + // test method Expand(unsigned int timeSteps) + m_BaseDataImpl->Expand(5); + CPPUNIT_ASSERT_MESSAGE("Expand the geometry to further time slices!", m_BaseDataImpl->GetTimeSteps() == 5); + } + + void TestingGetUpdateGeometry_Success() + { + // test method GetUpdatedGeometry(int timeStep); + m_BaseDataImpl->Expand(5); + mitk::ProportionalTimeGeometry::Pointer timeGeometry = + dynamic_cast(m_BaseDataImpl->GetTimeGeometry()); + if (timeGeometry.IsNotNull()) + { + timeGeometry->SetTimeStepGeometry(m_Geo3, 1); + } + + CPPUNIT_ASSERT_MESSAGE("Set Geometry for time step 1", m_BaseDataImpl->GetUpdatedGeometry(1) == m_Geo3); + CPPUNIT_ASSERT_MESSAGE("Check if modified time is set", m_BaseDataImpl->GetMTime() != 0); + } + + void GetOriginOfBaseData_Success() + { + m_BaseDataImpl->Expand(5); + m_BaseDataImpl->SetClonedGeometry(m_Geo3, 1); + + mitk::Point3D p3d(m_X); + m_BaseDataImpl->SetOrigin(p3d); + m_Geo3->SetOrigin(p3d); + CPPUNIT_ASSERT_MESSAGE("Testing Origin set", m_BaseDataImpl->GetGeometry(1)->GetOrigin() == m_Geo3->GetOrigin()); + } + void GetOriginOfCloneBaseData() + { + m_BaseDataImpl->Expand(5); + m_BaseDataImpl->SetClonedGeometry(m_Geo3, 1); + + mitk::Point3D p3d(m_X); + m_BaseDataImpl->SetOrigin(p3d); + m_Geo3->SetOrigin(p3d); + + m_CloneBaseData = m_BaseDataImpl->Clone(); + CPPUNIT_ASSERT_MESSAGE("Testing origin set in clone!", + m_CloneBaseData->GetGeometry(1)->GetOrigin() == m_Geo3->GetOrigin()); + } + + void ClearATimeStep() + { + CPPUNIT_ASSERT_MESSAGE("Is not empty before clear()!", !m_BaseDataImpl->IsEmptyTimeStep(1)); + m_BaseDataImpl->Clear(); + CPPUNIT_ASSERT_MESSAGE("...but afterwards!", m_BaseDataImpl->IsEmptyTimeStep(1)); + } + + void BaseDataSetAndGetProperty_Success() + { + // test method Set-/GetProperty() + m_BaseDataImpl->SetProperty("property38", mitk::StringProperty::New("testproperty")); + CPPUNIT_ASSERT_MESSAGE("Check if base property is set correctly!", + m_BaseDataImpl->GetProperty("property38")->GetValueAsString() == "testproperty"); + } + + void CloneBaseDataSetAndGetProperty_Success() + { + m_BaseDataImpl->SetProperty("property38", mitk::StringProperty::New("testproperty")); + m_CloneBaseData = m_BaseDataImpl->Clone(); + CPPUNIT_ASSERT_MESSAGE("Testing origin set in clone!", + m_CloneBaseData->GetProperty("property38")->GetValueAsString() == "testproperty"); + } + + void BasePropertyListIsSet_Success() + { + // test method Set-/GetPropertyList + m_PropertyList->SetFloatProperty("floatProperty1", 123.45); + m_PropertyList->SetBoolProperty("visibility", true); + m_PropertyList->SetStringProperty("nameXY", "propertyName"); + m_BaseDataImpl->SetPropertyList(m_PropertyList); + + CPPUNIT_ASSERT_MESSAGE("Check if base property list is set correctly!", + m_BaseDataImpl->GetPropertyList() == m_PropertyList); + } + + void BasePorpertyIsSetInPropertyList_Success() + { + m_PropertyList->SetFloatProperty("floatProperty1", 123.45); + m_PropertyList->SetBoolProperty("visibility", true); + m_PropertyList->SetStringProperty("nameXY", "propertyName"); + m_BaseDataImpl->SetPropertyList(m_PropertyList); + bool value = false; + CPPUNIT_ASSERT_MESSAGE("Check if base property is set correctly in the property list!", + m_BaseDataImpl->GetPropertyList()->GetBoolProperty("visibility", value) == true); + } + + void UpdateOutputInformationOfBaseData_Failure() + { + // test method UpdateOutputInformation() + m_BaseDataImpl->UpdateOutputInformation(); + m_Geo2->Initialize(2); + m_Geo2.GetPointer(); + CPPUNIT_ASSERT_MESSAGE("TimeGeometry update!", m_BaseDataImpl->GetUpdatedTimeGeometry() != m_Geo2); + } + + void CopyingInformationOfBaseData_Failure() + { + // Test method CopyInformation() + mitk::BaseDataTestImplementation::Pointer newBaseData = mitk::BaseDataTestImplementation::New(); + newBaseData->CopyInformation(m_BaseDataImpl); + CPPUNIT_ASSERT_MESSAGE("Check copying of Basedata Data Object!", + newBaseData->GetTimeGeometry()->CountTimeSteps() != 5); + } +}; +MITK_TEST_SUITE_REGISTRATION(mitkBaseData) diff --git a/Modules/Core/test/mitkExceptionTest.cpp b/Modules/Core/test/mitkExceptionTest.cpp index bd8c597b82..af45ca1e01 100644 --- a/Modules/Core/test/mitkExceptionTest.cpp +++ b/Modules/Core/test/mitkExceptionTest.cpp @@ -1,318 +1,369 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include "mitkTestingMacros.h" + +// std includes +#include + +// MITK includes +#include "mitkException.h" #include "mitkExceptionMacro.h" #include "mitkTestingMacros.h" +#include + +// ITK includes #include #include -#include + +// VTK includes +#include class SpecializedTestException : public mitk::Exception { public: mitkExceptionClassMacro(SpecializedTestException, mitk::Exception); }; -class ExceptionTestClass : public itk::Object +class mitkExceptionTestSuite : public itk::Object, public mitk::TestFixture { + CPPUNIT_TEST_SUITE(mitkExceptionTestSuite); + + MITK_TEST(TestExceptionConstructor_Success); + MITK_TEST(TestSpecializedExceptionConstructor_Success); + + MITK_TEST(TestExceptionMessageStreamAddingString_Success); + MITK_TEST(TestExceptionMessageStreamAddingSingleChars_Success); + MITK_TEST(TestExceptionMessageStreamAddingObject_Success); + MITK_TEST(TestSpecializedExceptionMessageStreamAddingString); + MITK_TEST(TestExceptionMessageStreamThrowing_Success); + + MITK_TEST(TestMitkThrowMacroThrowing_Success); + MITK_TEST(TestMitkThrowMacroMessage_Success); + MITK_TEST(TestMitkThrowMacroException_Success); + MITK_TEST(TestMitkThrowMacroSpezcializedException); + + MITK_TEST(TestGetNumberOfRethrows_Success); + + MITK_TEST(TestGetRethrowDataWithNegativNumber_Success); + MITK_TEST(TestGetRethrowDataWithNumberZero_Success); + MITK_TEST(TestGetRethrowDataWithNumberOne_Success); + + MITK_TEST(TestAddRethrowData_Success); + + MITK_TEST(TestFirstRethrowDataAreStoredProperly_Success); + MITK_TEST(TestSecondRethrowDataAreStoredProperly_Success); + + MITK_TEST(TestRethrowMacro_Success); + + CPPUNIT_TEST_SUITE_END(); + +private: + bool m_ExceptionThrown; + + std::string m_MessageText; + std::string m_Message; + std::string m_File; + + int m_Line; + + mitk::Exception m_E = mitk::Exception("test.cpp", 155, "", ""); + mitk::Exception m_MyException = mitk::Exception("testfile.cpp", 111, "testmessage"); + public: - mitkClassMacroItkParent(ExceptionTestClass, itk::Object); + mitkClassMacroItkParent(mitkExceptionTestSuite, itk::Object); itkFactorylessNewMacro(Self) itkCloneMacro(Self) void throwExceptionManually() // this method is ONLY to test the constructor and no code example // normally exceptions should only be thrown by using the exception macro! { throw mitk::Exception("test.cpp", 155, "", ""); } void throwSpecializedExceptionManually() // this method is ONLY to test the constructor and no code example // normally exceptions should only be thrown by using the exception macro! { throw SpecializedTestException("test.cpp", 155, "", ""); } void throwExceptionManually(std::string message1, std::string message2) // this method is ONLY to test methods of mitk::Exception and no code example // normally exceptions should only be thrown by using the exception macro! { throw mitk::Exception("testfile.cpp", 155, message1.c_str(), "") << message2; } void throwExceptionWithThrowMacro() { mitkThrow() << "TEST EXCEPION THROWING WITH mitkThrow()"; } void throwExceptionWithThrowMacro(std::string message) { mitkThrow() << message.c_str(); } void throwSpecializedExceptionWithThrowMacro(std::string message) { mitkThrowException(mitk::Exception) << message; } void throwSpecializedExceptionWithThrowMacro2(std::string message) { mitkThrowException(SpecializedTestException) << message; } void reThrowExceptionWithReThrowMacro(std::string messageThrow, std::string messageReThrow) { try { throwExceptionWithThrowMacro(messageThrow); } catch (mitk::Exception &e) { mitkReThrow(e) << messageReThrow; } } - static void TestExceptionConstructor() + void setUp() + { + m_ExceptionThrown = false; + m_MessageText = ""; + m_Message = "invalid"; + m_File = "invalid"; + m_Line = -1; + } + + void tearDown() + { + m_ExceptionThrown = false; + m_MessageText = ""; + m_Message = ""; + m_File = ""; + m_Line = 0; + } + + void TestExceptionConstructor_Success() { - bool exceptionThrown = false; - ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New(); try { - myExceptionTestObject->throwExceptionManually(); + this->throwExceptionManually(); } catch (mitk::Exception) { - exceptionThrown = true; + m_ExceptionThrown = true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing constructor of mitkException"); + CPPUNIT_ASSERT_MESSAGE("Testing constructor of mitkException", m_ExceptionThrown); + } - exceptionThrown = false; + void TestSpecializedExceptionConstructor_Success() + { try { - myExceptionTestObject->throwSpecializedExceptionManually(); + this->throwSpecializedExceptionManually(); } catch (SpecializedTestException) { - exceptionThrown = true; + m_ExceptionThrown = true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown, - "Testing constructor specialized exception (deriving from mitkException)"); + CPPUNIT_ASSERT_MESSAGE("Testing constructor specialized exception (deriving from mitkException)", + m_ExceptionThrown); } - static void TestExceptionMessageStream() + //##### this methods are ONLY to test the streaming operators of the exceptions and + //##### NO code example. Please do not instantiate exceptions by yourself in normal code! + //##### Normally exceptions should only be thrown by using the exception macro! + void TestExceptionMessageStreamAddingString_Success() { - //##### this method is ONLY to test the streaming operators of the exceptions and - //##### NO code example. Please do not instantiate exceptions by yourself in normal code! - //##### Normally exceptions should only be thrown by using the exception macro! - mitk::Exception myException = mitk::Exception("testfile.cpp", 111, "testmessage"); - myException << " and additional stream"; - MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() == std::string("testmessage and additional stream"), - "Testing mitkException message stream (adding std::string)"); - - myException.SetDescription("testmessage2"); - myException << ' ' << 'a' << 'n' << 'd' << ' ' << 'c' << 'h' << 'a' << 'r' << 's'; - MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() == std::string("testmessage2 and chars"), - "Testing mitkException message stream (adding single chars)"); - - myException.SetDescription("testmessage3"); - myException << myException; // adding the object itself makes no sense but should work - MITK_TEST_CONDITION_REQUIRED(myException.GetDescription() != std::string(""), - "Testing mitkException message stream (adding object)"); + m_MyException << " and additional stream"; + CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding std::string)", + m_MyException.GetDescription() == std::string("testmessage and additional stream")); + } + void TestExceptionMessageStreamAddingSingleChars_Success() + { + m_MyException.SetDescription("testmessage2"); + m_MyException << ' ' << 'a' << 'n' << 'd' << ' ' << 'c' << 'h' << 'a' << 'r' << 's'; + CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding single chars)", + m_MyException.GetDescription() == std::string("testmessage2 and chars")); + } + + void TestExceptionMessageStreamAddingObject_Success() + { + m_MyException.SetDescription("testmessage3"); + m_MyException << m_MyException; // adding the object itself makes no sense but should work + CPPUNIT_ASSERT_MESSAGE("Testing mitkException message stream (adding object)", + m_MyException.GetDescription() != std::string("")); + } + + void TestSpecializedExceptionMessageStreamAddingString() + { SpecializedTestException mySpecializedException = SpecializedTestException("testfile.cpp", 111, "testmessage", "test"); mySpecializedException << " and additional stream"; - MITK_TEST_CONDITION_REQUIRED( - mySpecializedException.GetDescription() == std::string("testmessage and additional stream"), - "Testing specialized exception message stream (adding std::string)"); + CPPUNIT_ASSERT_MESSAGE("Testing specialized exception message stream (adding std::string)", + mySpecializedException.GetDescription() == std::string("testmessage and additional stream")); } - static void TestExceptionMessageStreamThrowing() + void TestExceptionMessageStreamThrowing_Success() { - bool exceptionThrown = false; - ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New(); std::string thrownMessage = ""; try { - myExceptionTestObject->throwExceptionManually("message1", " and message2"); + this->throwExceptionManually("message1", " and message2"); } catch (mitk::Exception &e) { thrownMessage = e.GetDescription(); - exceptionThrown = true; + m_ExceptionThrown = true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown && (thrownMessage == std::string("message1 and message2")), - "Testing throwing and streaming of mitk::Exception together.") + CPPUNIT_ASSERT_MESSAGE("Testing throwing and streaming of mitk::Exception together.", + m_ExceptionThrown && (thrownMessage == std::string("message1 and message2"))); } - static void TestMitkThrowMacro() + void TestMitkThrowMacroThrowing_Success() { - bool exceptionThrown = false; - ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New(); - // case 1: test throwing - try { - myExceptionTestObject->throwExceptionWithThrowMacro(); + this->throwExceptionWithThrowMacro(); } catch (mitk::Exception) { - exceptionThrown = true; + m_ExceptionThrown = true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing mitkThrow()"); + CPPUNIT_ASSERT_MESSAGE("Testing mitkThrow()", m_ExceptionThrown); + } + void TestMitkThrowMacroMessage_Success() + { // case 2: test message text - - exceptionThrown = false; - std::string messageText = ""; - try { - myExceptionTestObject->throwExceptionWithThrowMacro("test123"); + this->throwExceptionWithThrowMacro("test123"); } catch (mitk::Exception &e) { - exceptionThrown = true; - messageText = e.GetDescription(); + m_ExceptionThrown = true; + m_MessageText = e.GetDescription(); } - MITK_TEST_CONDITION_REQUIRED((exceptionThrown && (messageText == "test123")), - "Testing message test of mitkThrow()"); + CPPUNIT_ASSERT_MESSAGE("Testing message test of mitkThrow()", (m_ExceptionThrown && (m_MessageText == "test123"))); + } + void TestMitkThrowMacroException_Success() + { // case 3: specialized exception / command mitkThrow(mitk::Exception) - - exceptionThrown = false; - messageText = ""; - try { - myExceptionTestObject->throwSpecializedExceptionWithThrowMacro("test123"); + this->throwSpecializedExceptionWithThrowMacro("test123"); } catch (mitk::Exception &e) { - exceptionThrown = true; - messageText = e.GetDescription(); + m_ExceptionThrown = true; + m_MessageText = e.GetDescription(); } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown && messageText == "test123", - "Testing special exception with mitkThrow(mitk::Exception)"); + CPPUNIT_ASSERT_MESSAGE("Testing special exception with mitkThrow(mitk::Exception)", + m_ExceptionThrown && m_MessageText == "test123"); + } + void TestMitkThrowMacroSpezcializedException() + { // case 4: specialized exception / command mitkThrow(mitk::SpecializedException) - - exceptionThrown = false; - messageText = ""; - try { - myExceptionTestObject->throwSpecializedExceptionWithThrowMacro2("test123"); + this->throwSpecializedExceptionWithThrowMacro2("test123"); } catch (SpecializedTestException &e) { - exceptionThrown = true; - messageText = e.GetDescription(); + m_ExceptionThrown = true; + m_MessageText = e.GetDescription(); } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown && messageText == "test123", - "Testing special exception with mitkThrow(mitk::SpecializedException)"); + CPPUNIT_ASSERT_MESSAGE("Testing special exception with mitkThrow(mitk::SpecializedException)", + m_ExceptionThrown && m_MessageText == "test123"); } - static void TestRethrowInformation() - // this method is ONLY to test methods of mitk::Exception and no code example - // normally exceptions should only be instantiated and thrown by using the exception macros! + //##### this methods are ONLY to test methods of mitk::Exception and no code example + //##### normally exceptions should only be instantiated and thrown by using the exception macros! + void TestGetNumberOfRethrows_Success() { // first: testing rethrow information methods, when no information is stored - // case 1.1: method GetNumberOfRethrows() - mitk::Exception e = mitk::Exception("test.cpp", 155, "", ""); - MITK_TEST_CONDITION_REQUIRED(e.GetNumberOfRethrows() == 0, - "Testing GetNumberOfRethrows() with empty rethrow information"); + CPPUNIT_ASSERT_MESSAGE("Testing GetNumberOfRethrows() with empty rethrow information", + m_E.GetNumberOfRethrows() == 0); + } + void TestGetRethrowDataWithNegativNumber_Success() + { // case 1.2: GetRethrowData() with negative number - { - std::string file = "invalid"; - int line = -1; - std::string message = "invalid"; - e.GetRethrowData(-1, file, line, message); - MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")), - "Testing GetRethrowData() with invalid rethrow number (negative)."); - } + m_E.GetRethrowData(-1, m_File, m_Line, m_Message); + CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with invalid rethrow number (negative).", + ((m_File == "") && (m_Line == 0) && (m_Message == ""))); + } + void TestGetRethrowDataWithNumberZero_Success() + { // case 1.3: GetRethrowData() with number 0 - { - std::string file = "invalid"; - int line = -1; - std::string message = "invalid"; - e.GetRethrowData(0, file, line, message); - MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")), - "Testing GetRethrowData() with non-existing rethrow number (0)."); - } + m_E.GetRethrowData(0, m_File, m_Line, m_Message); + CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with non-existing rethrow number (0).", + ((m_File == "") && (m_Line == 0) && (m_Message == ""))); + } + void TestGetRethrowDataWithNumberOne_Success() + { // case 1.4: GetRethrowData() with number 1 - { - std::string file = "invalid"; - int line = -1; - std::string message = "invalid"; - e.GetRethrowData(1, file, line, message); - MITK_TEST_CONDITION_REQUIRED(((file == "") && (line == 0) && (message == "")), - "Testing GetRethrowData() with non-existing rethrow number (1)."); - } + m_E.GetRethrowData(1, m_File, m_Line, m_Message); + CPPUNIT_ASSERT_MESSAGE("Testing GetRethrowData() with non-existing rethrow number (1).", + ((m_File == "") && (m_Line == 0) && (m_Message == ""))); + } + void TestAddRethrowData_Success() + { // second: add rethrow data - e.AddRethrowData("test2.cpp", 10, "Rethrow one"); - MITK_TEST_CONDITION_REQUIRED(e.GetNumberOfRethrows() == 1, "Testing adding of rethrow data."); - e.AddRethrowData("test3.cpp", 15, "Rethrow two"); - MITK_TEST_CONDITION_REQUIRED(e.GetNumberOfRethrows() == 2, "Testing adding of more rethrow data."); + m_E.AddRethrowData("test2.cpp", 10, "Rethrow one"); + CPPUNIT_ASSERT_MESSAGE("Testing adding of rethrow data.", m_E.GetNumberOfRethrows() == 1); + m_E.AddRethrowData("test3.cpp", 15, "Rethrow two"); + CPPUNIT_ASSERT_MESSAGE("Testing adding of more rethrow data.", m_E.GetNumberOfRethrows() == 2); + } + void TestFirstRethrowDataAreStoredProperly_Success() + { // third: test if this rethrow data was stored properly - { - std::string file = "invalid"; - int line = -1; - std::string message = "invalid"; - e.GetRethrowData(0, file, line, message); - MITK_TEST_CONDITION_REQUIRED(((file == "test2.cpp") && (line == 10) && (message == "Rethrow one")), - "Testing stored information of first rethrow."); - } - - { - std::string file = "invalid"; - int line = -1; - std::string message = "invalid"; - e.GetRethrowData(1, file, line, message); - MITK_TEST_CONDITION_REQUIRED(((file == "test3.cpp") && (line == 15) && (message == "Rethrow two")), - "Testing stored information of second rethrow."); - } + m_E.AddRethrowData("test2.cpp", 10, "Rethrow one"); + m_E.GetRethrowData(0, m_File, m_Line, m_Message); + CPPUNIT_ASSERT_MESSAGE("Testing stored information of first rethrow.", + ((m_File == "test2.cpp") && (m_Line == 10) && (m_Message == "Rethrow one"))); } - static void TestRethrowMacro() + void TestSecondRethrowDataAreStoredProperly_Success() { - bool exceptionThrown = false; - std::string message = ""; - ExceptionTestClass::Pointer myExceptionTestObject = ExceptionTestClass::New(); + m_E.AddRethrowData("test2.cpp", 10, "Rethrow one"); + m_E.AddRethrowData("test3.cpp", 15, "Rethrow two"); + m_E.GetRethrowData(1, m_File, m_Line, m_Message); + CPPUNIT_ASSERT_MESSAGE("Testing stored information of second rethrow.", + ((m_File == "test3.cpp") && (m_Line == 15) && (m_Message == "Rethrow two"))); + } + void TestRethrowMacro_Success() + { // case 1: test throwing - try { - myExceptionTestObject->reThrowExceptionWithReThrowMacro("Test original message.", "Test rethrow message."); + this->reThrowExceptionWithReThrowMacro("Test original message.", "Test rethrow message."); } catch (mitk::Exception &e) { - message = e.GetDescription(); - exceptionThrown = true; + m_Message = e.GetDescription(); + m_ExceptionThrown = true; } - MITK_TEST_CONDITION_REQUIRED(exceptionThrown, "Testing mitkReThrow()"); - MITK_TEST_CONDITION_REQUIRED(message == "Test original message.Test rethrow message.", - "Testing message/descriprion after rethrow.") + CPPUNIT_ASSERT_MESSAGE("Testing mitkReThrow()", m_ExceptionThrown); + CPPUNIT_ASSERT_MESSAGE("Testing message/descriprion after rethrow.", + m_Message == "Test original message.Test rethrow message."); } }; -int mitkExceptionTest(int /*argc*/, char * /*argv*/ []) -{ - MITK_TEST_BEGIN("MITKException"); - ExceptionTestClass::TestExceptionConstructor(); - ExceptionTestClass::TestExceptionMessageStream(); - ExceptionTestClass::TestExceptionMessageStreamThrowing(); - ExceptionTestClass::TestMitkThrowMacro(); - ExceptionTestClass::TestRethrowInformation(); - ExceptionTestClass::TestRethrowMacro(); - MITK_TEST_END(); -} +MITK_TEST_SUITE_REGISTRATION(mitkException) diff --git a/Modules/Core/test/mitkPointSetLocaleTest.cpp b/Modules/Core/test/mitkPointSetLocaleTest.cpp index 62a82e818d..41c4cf2447 100644 --- a/Modules/Core/test/mitkPointSetLocaleTest.cpp +++ b/Modules/Core/test/mitkPointSetLocaleTest.cpp @@ -1,162 +1,198 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include "mitkTestingMacros.h" + +// std includes +#include +#include + +// MITK includes #include "mitkIOUtil.h" #include "mitkPointSet.h" #include "mitkStandardFileLocations.h" -#include "mitkTestingMacros.h" + +// VTK includes +#include + +// stream includes #include #include -#include -#include -bool ChangeLocale(const std::string &locale) +class mitkPointSetLocaleTestSuite : public mitk::TestFixture { - try - { - MITK_TEST_OUTPUT(<< "\n** Changing locale from " << setlocale(LC_ALL, nullptr) << " to '" << locale << "'"); - setlocale(LC_ALL, locale.c_str()); + CPPUNIT_TEST_SUITE(mitkPointSetLocaleTestSuite); - std::locale l(locale.c_str()); - std::cin.imbue(l); - std::cout.imbue(l); + MITK_TEST(TestIfGermanLocaleUsed_Success); - return true; - } - catch (...) - { - MITK_TEST_OUTPUT(<< "Could not activate locale " << locale << "\n"); - return false; - } -} + CPPUNIT_TEST_SUITE_END(); -void ReaderLocaleTest(mitk::Point3D &refPoint, std::string filename) -{ - MITK_TEST_OUTPUT(<< "---- Reader Test ---- "); +private: + typedef std::list StringList; + StringList m_AllLocales; - mitk::PointSet::Pointer pointSet = mitk::IOUtil::Load(filename); + mitk::PointSet::Pointer m_RefPointSet; + mitk::Point3D m_RefPoint; - mitk::Point3D point; - if (pointSet->GetPointIfExists(0, &point)) + mitk::Point3D m_Point; + + mitk::PointSet::Pointer m_PointSet; + + bool ChangeLocale(const std::string &locale) { - MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[0] - point[0]) < 0.00001, "read x correct"); - MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[1] - point[1]) < 0.00001, "read y correct"); - MITK_TEST_CONDITION_REQUIRED(fabs(refPoint[2] - point[2]) < 0.00001, "read z correct"); + try + { + MITK_TEST_OUTPUT(<< "\n** Changing locale from " << setlocale(LC_ALL, nullptr) << " to '" << locale << "'"); + setlocale(LC_ALL, locale.c_str()); + + std::locale l(locale.c_str()); + std::cin.imbue(l); + std::cout.imbue(l); + return true; + } + catch (...) + { + MITK_TEST_OUTPUT(<< "Could not activate locale " << locale << "\n"); + return false; + } } - else + + void ReaderLocaleTest(mitk::Point3D &refPoint, std::string filename) { - MITK_TEST_FAILED_MSG(<< "File " << filename << " can not be read - test will not applied."); - return; + MITK_TEST_OUTPUT(<< "---- Reader Test ---- "); + + m_PointSet = mitk::IOUtil::Load(filename); + + if (m_PointSet->GetPointIfExists(0, &m_Point)) + { + CPPUNIT_ASSERT_MESSAGE("read x correct", fabs(refPoint[0] - m_Point[0]) < 0.00001); + CPPUNIT_ASSERT_MESSAGE("read y correct", fabs(refPoint[1] - m_Point[1]) < 0.00001); + CPPUNIT_ASSERT_MESSAGE("read z correct", fabs(refPoint[2] - m_Point[2]) < 0.00001); + } + else + { + MITK_TEST_FAILED_MSG(<< "File " << filename << " can not be read - test will not applied."); + return; + } } -} -void WriterLocaleTest(mitk::Point3D &refPoint, std::string filename) -{ - MITK_TEST_OUTPUT(<< "---- Writer Test---- "); - // create pointset - mitk::PointSet::Pointer refPointSet = mitk::PointSet::New(); - refPointSet->InsertPoint(0, refPoint); - // SetPoint(0, refPoint); + void WriterLocaleTest(mitk::Point3D &refPoint, std::string filename) + { + MITK_TEST_OUTPUT(<< "---- Writer Test---- "); + // create pointset + m_RefPointSet = mitk::PointSet::New(); + m_RefPointSet->InsertPoint(0, refPoint); - std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile("testPointSet_XXXXXX.mps"); + std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile("testPointSet_XXXXXX.mps"); - // write point set - mitk::IOUtil::Save(refPointSet, tmpFilePath); + // write point set + mitk::IOUtil::Save(m_RefPointSet, tmpFilePath); - std::ifstream stream(tmpFilePath.c_str()); + std::ifstream stream(tmpFilePath.c_str()); - // compare two .mps files - std::ifstream refStream(filename.c_str()); + // compare two .mps files + std::ifstream refStream(filename.c_str()); - MITK_TEST_CONDITION_REQUIRED(refStream, "Read reference point set"); - MITK_TEST_CONDITION_REQUIRED(stream, "Read point set"); + CPPUNIT_ASSERT_MESSAGE("Read reference point set", refStream); + CPPUNIT_ASSERT_MESSAGE("Read point set", stream); - bool differ = false; - if (stream.is_open() && refStream.is_open()) - { - std::string streamLine; - std::string refStreamLine; - while (!stream.eof() && !refStream.eof()) + bool differ = false; + if (stream.is_open() && refStream.is_open()) { - getline(stream, streamLine); - getline(refStream, refStreamLine); - if (streamLine.compare(refStreamLine) != 0) + std::string streamLine; + std::string refStreamLine; + while (!stream.eof() && !refStream.eof()) { - differ = true; - break; + getline(stream, streamLine); + getline(refStream, refStreamLine); + if (streamLine.compare(refStreamLine) != 0) + { + differ = true; + break; + } } + stream.close(); + refStream.close(); } - stream.close(); - refStream.close(); + CPPUNIT_ASSERT_MESSAGE("Write point set correct", !differ); } - MITK_TEST_CONDITION_REQUIRED(!differ, "Write point set correct"); -} -int mitkPointSetLocaleTest(int, char *[]) -{ - MITK_TEST_BEGIN("PointSetLocaleTest"); +public: + void setUp() + { + m_RefPointSet = mitk::PointSet::New(); + + // create locale list + m_AllLocales.push_back("de_DE"); + m_AllLocales.push_back("de_DE.utf8"); + m_AllLocales.push_back("de_DE.UTF-8"); + m_AllLocales.push_back("de_DE@euro"); + m_AllLocales.push_back("German_Germany"); + + m_RefPoint[0] = 32.2946; + m_RefPoint[1] = -17.7359; + m_RefPoint[2] = 29.6502; + } + + void tearDown() + { + m_RefPoint[0] = 0; + m_RefPoint[1] = 0; + m_RefPoint[2] = 0; - // create reference point set - mitk::PointSet::Pointer refPointSet = mitk::PointSet::New(); - mitk::Point3D refPoint; - refPoint[0] = 32.2946; - refPoint[1] = -17.7359; - refPoint[2] = 29.6502; - refPointSet->SetPoint(0, refPoint); + m_AllLocales.clear(); + } - // create locale list + void TestIfGermanLocaleUsed_Success() + { + // create reference point set + m_RefPointSet->SetPoint(0, m_RefPoint); - typedef std::list StringList; - StringList alllocales; - alllocales.push_back("de_DE"); - alllocales.push_back("de_DE.utf8"); - alllocales.push_back("de_DE.UTF-8"); - alllocales.push_back("de_DE@euro"); - alllocales.push_back("German_Germany"); - -// QuickFix for MAC OS X -// See for more the Bug #3894 comments + // QuickFix for MAC OS X + // See for more the Bug #3894 comments #if defined(__APPLE__) || defined(MACOSX) - alllocales.push_back("C"); + alllocales.push_back("C"); #endif - // write a reference file using the "C" locale once - ChangeLocale("C"); - std::string referenceFilePath = mitk::IOUtil::CreateTemporaryFile("refPointSet_XXXXXX.mps"); - MITK_INFO << "Reference PointSet in " << referenceFilePath; + // write a reference file using the "C" locale once + ChangeLocale("C"); + std::string referenceFilePath = mitk::IOUtil::CreateTemporaryFile("refPointSet_XXXXXX.mps"); + MITK_INFO << "Reference PointSet in " << referenceFilePath; - // write point set - mitk::IOUtil::Save(refPointSet, referenceFilePath); + // write point set + mitk::IOUtil::Save(m_RefPointSet, referenceFilePath); - unsigned int numberOfTestedGermanLocales(0); - for (auto iter = alllocales.begin(); iter != alllocales.end(); ++iter) - { - if (ChangeLocale(*iter)) + unsigned int numberOfTestedGermanLocales(0); + for (auto iter = m_AllLocales.begin(); iter != m_AllLocales.end(); ++iter) + { + if (ChangeLocale(*iter)) + { + ++numberOfTestedGermanLocales; + WriterLocaleTest(m_RefPoint, referenceFilePath); + ReaderLocaleTest(m_RefPoint, referenceFilePath); + } + } + + if (numberOfTestedGermanLocales == 0) { - ++numberOfTestedGermanLocales; - WriterLocaleTest(refPoint, referenceFilePath); - ReaderLocaleTest(refPoint, referenceFilePath); + MITK_TEST_OUTPUT(<< "Warning: No German locale was found on the system."); } } +}; - if (numberOfTestedGermanLocales == 0) - { - MITK_TEST_OUTPUT(<< "Warning: No German locale was found on the system."); - } - // MITK_TEST_CONDITION_REQUIRED( numberOfTestedGermanLocales > 0, "Verify that at least one German locale has been - // tested."); - MITK_TEST_END(); -} +MITK_TEST_SUITE_REGISTRATION(mitkPointSetLocale) diff --git a/Modules/Core/test/mitkSurfaceTest.cpp b/Modules/Core/test/mitkSurfaceTest.cpp index 63389c8cba..a5d3db01ba 100644 --- a/Modules/Core/test/mitkSurfaceTest.cpp +++ b/Modules/Core/test/mitkSurfaceTest.cpp @@ -1,149 +1,288 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include + +// std includes +#include + +// MITK includes #include "mitkCommon.h" #include "mitkNumericTypes.h" #include "mitkSurface.h" -#include "mitkTestingMacros.h" +// MITK includes +#include + +// VTK includes #include "vtkPolyData.h" #include "vtkSphereSource.h" +// stream includes #include -int mitkSurfaceTest(int /*argc*/, char * /*argv*/ []) +class mitkSurfaceTestSuite : public mitk::TestFixture { - MITK_TEST_BEGIN("Surface"); - - mitk::Surface::Pointer surface = mitk::Surface::New(); - MITK_TEST_CONDITION_REQUIRED(surface.GetPointer(), "Testing initialization!"); - - mitk::Surface::Pointer cloneSurface = surface->Clone(); - MITK_TEST_CONDITION_REQUIRED(cloneSurface.GetPointer(), "Testing clone surface initialization!"); - - vtkSphereSource *sphereSource = vtkSphereSource::New(); - sphereSource->SetCenter(0, 0, 0); - sphereSource->SetRadius(5.0); - sphereSource->SetThetaResolution(10); - sphereSource->SetPhiResolution(10); - sphereSource->Update(); - - vtkPolyData *polys = sphereSource->GetOutput(); - MITK_TEST_CONDITION_REQUIRED(surface->GetVtkPolyData() == nullptr, "Testing initial state of vtkPolyData"); - surface->SetVtkPolyData(polys); - sphereSource->Delete(); - MITK_TEST_CONDITION_REQUIRED(surface->GetVtkPolyData() != nullptr, "Testing set vtkPolyData"); - - cloneSurface = surface->Clone(); - MITK_TEST_CONDITION_REQUIRED(cloneSurface->GetVtkPolyData() != nullptr, "Testing set vtkPolyData of cloned surface!"); - cloneSurface = nullptr; - - double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; - polys->ComputeBounds(); - polys->GetBounds(bounds); - - surface->UpdateOutputInformation(); - surface->SetRequestedRegionToLargestPossibleRegion(); - auto *bb = const_cast(surface->GetGeometry()->GetBoundingBox()); - mitk::BoundingBox::BoundsArrayType surfBounds = bb->GetBounds(); - - bool passed = false; - if (bounds[0] == surfBounds[0] && bounds[1] == surfBounds[1] && bounds[2] == surfBounds[2] && - bounds[3] == surfBounds[3] && bounds[4] == surfBounds[4] && bounds[5] == surfBounds[5]) + CPPUNIT_TEST_SUITE(mitkSurfaceTestSuite); + + MITK_TEST(InitializationSurfacePointer_Success); + MITK_TEST(InitializationCloneSurfacePointer_Success); + + MITK_TEST(StateOfVtkPolyDataEqualNullPointer_Success); + + MITK_TEST(SetVtkPolyDataNotNullPointer_Failure); + MITK_TEST(SetClonedVtkPolyDataNotNullPointer_Failure); + + MITK_TEST(GetBoundingBox_Success); + + MITK_TEST(SurfaceExpandTimestepsAreFive_Success); + MITK_TEST(Surface4DDataCreation_Success); + + MITK_TEST(TimeGeometrySurface_Success); + MITK_TEST(ChangingDataOfSpecificTimestepSurface_Success); + MITK_TEST(SurfaceCopyWithGraft_Failure); + MITK_TEST(CopyingNumberOfTimesteps_Success); + + MITK_TEST(DestructionOfSurface_Success); + + CPPUNIT_TEST_SUITE_END(); + +private: + mitk::Surface::Pointer m_Surface; + mitk::Surface::Pointer m_CloneSurface; + vtkSphereSource *m_SphereSource; + const mitk::TimeGeometry *m_InputTimeGeometry; + + int m_Time; + int m_Timestep; + +public: + void setUp() + { + m_Surface = mitk::Surface::New(); + m_CloneSurface = m_Surface->Clone(); + m_SphereSource = vtkSphereSource::New(); + m_InputTimeGeometry = m_Surface->GetUpdatedTimeGeometry(); + + m_SphereSource->SetCenter(0, 0, 0); + m_SphereSource->SetRadius(5.0); + m_SphereSource->SetThetaResolution(10); + m_SphereSource->SetPhiResolution(10); + m_SphereSource->Update(); + + m_Time = 3; + m_Timestep = 0; + } + + void tearDown() + { + m_Surface = nullptr; + m_CloneSurface = nullptr; + m_SphereSource = nullptr; + } + + void InitializationSurfacePointer_Success() + { + CPPUNIT_ASSERT_MESSAGE("Testing initialization", m_Surface.GetPointer()); + } + + void InitializationCloneSurfacePointer_Success() + { + CPPUNIT_ASSERT_MESSAGE("Testing clone surface initialization", m_CloneSurface.GetPointer()); + } + + void StateOfVtkPolyDataEqualNullPointer_Success() + { + vtkPolyData *polys = m_SphereSource->GetOutput(); + CPPUNIT_ASSERT_MESSAGE("Testing initial state of vtkPolyData", m_Surface->GetVtkPolyData() == nullptr); + } + + void SetVtkPolyDataNotNullPointer_Failure() + { + vtkPolyData *polys = m_SphereSource->GetOutput(); + m_Surface->SetVtkPolyData(polys); + m_SphereSource->Delete(); + CPPUNIT_ASSERT_MESSAGE("Testing set vtkPolyData", m_Surface->GetVtkPolyData() != nullptr); + } + + void SetClonedVtkPolyDataNotNullPointer_Failure() + { + vtkPolyData *polys = m_SphereSource->GetOutput(); + m_Surface->SetVtkPolyData(polys); + m_SphereSource->Delete(); + m_CloneSurface = m_Surface->Clone(); + CPPUNIT_ASSERT_MESSAGE("Testing set vtkPolyData of cloned surface!", m_CloneSurface->GetVtkPolyData() != nullptr); + } + + void GetBoundingBox_Success() + { + vtkPolyData *polys = m_SphereSource->GetOutput(); + m_Surface->SetVtkPolyData(polys); + m_SphereSource->Delete(); + + double bounds[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; + polys->ComputeBounds(); + polys->GetBounds(bounds); + + m_Surface->UpdateOutputInformation(); + m_Surface->SetRequestedRegionToLargestPossibleRegion(); + auto *bb = const_cast(m_Surface->GetGeometry()->GetBoundingBox()); + mitk::BoundingBox::BoundsArrayType surfBounds = bb->GetBounds(); + + bool passed = false; + if (bounds[0] == surfBounds[0] && bounds[1] == surfBounds[1] && bounds[2] == surfBounds[2] && + bounds[3] == surfBounds[3] && bounds[4] == surfBounds[4] && bounds[5] == surfBounds[5]) + { + passed = true; + } + + CPPUNIT_ASSERT_MESSAGE("Testing GetBoundingBox()", passed); + } + + void SurfaceExpandTimestepsAreFive_Success() { - passed = true; + m_Surface->Expand(5); + m_Surface->Update(); + m_Surface->SetRequestedRegionToLargestPossibleRegion(); + mitk::Surface::RegionType requestedRegion = m_Surface->GetRequestedRegion(); + CPPUNIT_ASSERT_MESSAGE("Testing mitk::Surface::Expand( timesteps ): ", requestedRegion.GetSize(3) == 5); } - MITK_TEST_CONDITION_REQUIRED(passed, "Testing GetBoundingBox()!"); - surface->Expand(5); - surface->Update(); - surface->SetRequestedRegionToLargestPossibleRegion(); - mitk::Surface::RegionType requestedRegion = surface->GetRequestedRegion(); - MITK_TEST_CONDITION_REQUIRED(requestedRegion.GetSize(3) == 5, "Testing mitk::Surface::Expand( timesteps ): "); + void Surface4DDataCreation_Success() + { + double boundsMat[5][6]; + + for (int i = 0; i < 5; i++) + { + vtkSphereSource *sphereSource = vtkSphereSource::New(); + sphereSource->SetCenter(0, 0, 0); + sphereSource->SetRadius(1.0 * (i + 1.0)); + sphereSource->SetThetaResolution(10); + sphereSource->SetPhiResolution(10); + sphereSource->Update(); + sphereSource->GetOutput()->ComputeBounds(); + sphereSource->GetOutput()->GetBounds(boundsMat[i]); + m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i); + sphereSource->Delete(); + } + + m_Surface->UpdateOutputInformation(); + m_Surface->SetRequestedRegionToLargestPossibleRegion(); + + bool passed = true; + for (int i = 0; i < 5; i++) + { + mitk::BoundingBox::BoundsArrayType surfBounds = + (const_cast(m_Surface->GetTimeGeometry()->GetGeometryForTimeStep(i)->GetBoundingBox())) + ->GetBounds(); + + if (boundsMat[i][0] != surfBounds[0] || boundsMat[i][1] != surfBounds[1] || boundsMat[i][2] != surfBounds[2] || + boundsMat[i][3] != surfBounds[3] || boundsMat[i][4] != surfBounds[4] || boundsMat[i][5] != surfBounds[5]) + { + passed = false; + break; + } + } + CPPUNIT_ASSERT_MESSAGE("Testing mitk::Surface::Testing 4D surface data creation", passed); + } - double boundsMat[5][6]; + void TimeGeometrySurface_Success() + { + m_Timestep = m_InputTimeGeometry->TimePointToTimeStep(m_Time); + CPPUNIT_ASSERT_MESSAGE("Testing correctness of geometry for surface->GetUpdatedTimeGeometry()", + m_Time == m_Timestep); + } - for (int i = 0; i < 5; i++) + void ChangingDataOfSpecificTimestepSurface_Success() { vtkSphereSource *sphereSource = vtkSphereSource::New(); sphereSource->SetCenter(0, 0, 0); - sphereSource->SetRadius(1.0 * (i + 1.0)); + sphereSource->SetRadius(100.0); sphereSource->SetThetaResolution(10); sphereSource->SetPhiResolution(10); sphereSource->Update(); - sphereSource->GetOutput()->ComputeBounds(); - sphereSource->GetOutput()->GetBounds(boundsMat[i]); - surface->SetVtkPolyData(sphereSource->GetOutput(), i); + m_Surface->SetVtkPolyData(sphereSource->GetOutput(), 3); sphereSource->Delete(); + + m_Timestep = m_InputTimeGeometry->TimePointToTimeStep(m_Time); + CPPUNIT_ASSERT_MESSAGE( + "Explicitly changing the data of timestep 3 and checking for timebounds correctness of surface's geometry again", + m_Time == m_Timestep); } - surface->UpdateOutputInformation(); - surface->SetRequestedRegionToLargestPossibleRegion(); + void SurfaceCopyWithGraft_Failure() + { + double boundsMat[5][6]; + + for (int i = 0; i < 5; i++) + { + vtkSphereSource *sphereSource = vtkSphereSource::New(); + sphereSource->SetCenter(0, 0, 0); + sphereSource->SetRadius(1.0 * (i + 1.0)); + sphereSource->SetThetaResolution(10); + sphereSource->SetPhiResolution(10); + sphereSource->Update(); + sphereSource->GetOutput()->ComputeBounds(); + sphereSource->GetOutput()->GetBounds(boundsMat[i]); + m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i); + sphereSource->Delete(); + } + + m_Surface->UpdateOutputInformation(); + m_Surface->SetRequestedRegionToLargestPossibleRegion(); + + mitk::Surface::Pointer dummy = mitk::Surface::New(); + dummy->Graft(m_Surface); + CPPUNIT_ASSERT_MESSAGE("Testing copying a Surface with Graft()", dummy->GetVtkPolyData() != nullptr); + } - passed = true; - for (int i = 0; i < 5; i++) + void CopyingNumberOfTimesteps_Success() { - mitk::BoundingBox::BoundsArrayType surfBounds = - (const_cast(surface->GetTimeGeometry()->GetGeometryForTimeStep(i)->GetBoundingBox())) - ->GetBounds(); + double boundsMat[5][6]; - if (boundsMat[i][0] != surfBounds[0] || boundsMat[i][1] != surfBounds[1] || boundsMat[i][2] != surfBounds[2] || - boundsMat[i][3] != surfBounds[3] || boundsMat[i][4] != surfBounds[4] || boundsMat[i][5] != surfBounds[5]) + for (int i = 0; i < 5; i++) { - passed = false; - break; + vtkSphereSource *sphereSource = vtkSphereSource::New(); + sphereSource->SetCenter(0, 0, 0); + sphereSource->SetRadius(1.0 * (i + 1.0)); + sphereSource->SetThetaResolution(10); + sphereSource->SetPhiResolution(10); + sphereSource->Update(); + sphereSource->GetOutput()->ComputeBounds(); + sphereSource->GetOutput()->GetBounds(boundsMat[i]); + m_Surface->SetVtkPolyData(sphereSource->GetOutput(), i); + sphereSource->Delete(); } + + m_Surface->UpdateOutputInformation(); + m_Surface->SetRequestedRegionToLargestPossibleRegion(); + + unsigned int numberoftimesteps = m_Surface->GetTimeSteps(); + mitk::Surface::Pointer dummy = mitk::Surface::New(); + dummy->Graft(m_Surface); + + CPPUNIT_ASSERT_MESSAGE(" Old timesteps == copy of timesteps ", dummy->GetTimeSteps() == numberoftimesteps); + } + + void DestructionOfSurface_Success() + { + m_Surface = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing destruction of surface", m_Surface.IsNull()); } - MITK_TEST_CONDITION_REQUIRED(passed, "Testing mitk::Surface::Testing 4D surface data creation!"); - - const mitk::TimeGeometry *inputTimeGeometry = surface->GetUpdatedTimeGeometry(); - - int time = 3; - int timestep = 0; - timestep = inputTimeGeometry->TimePointToTimeStep(time); - MITK_TEST_CONDITION_REQUIRED(time == timestep, - "Testing correctness of geometry for surface->GetUpdatedTimeGeometry()!"); - - sphereSource = vtkSphereSource::New(); - sphereSource->SetCenter(0, 0, 0); - sphereSource->SetRadius(100.0); - sphereSource->SetThetaResolution(10); - sphereSource->SetPhiResolution(10); - sphereSource->Update(); - surface->SetVtkPolyData(sphereSource->GetOutput(), 3); - sphereSource->Delete(); - - inputTimeGeometry = surface->GetUpdatedTimeGeometry(); - time = 3; - - timestep = inputTimeGeometry->TimePointToTimeStep(time); - MITK_TEST_CONDITION_REQUIRED( - time == timestep, - "Explicitly changing the data of timestep 3 and checking for timebounds correctness of surface's geometry again!"); - - unsigned int numberoftimesteps = surface->GetTimeSteps(); - mitk::Surface::Pointer dummy = mitk::Surface::New(); - dummy->Graft(surface); - MITK_TEST_CONDITION_REQUIRED(dummy->GetVtkPolyData() != nullptr, "Testing copying a Surface with Graft()!"); - MITK_TEST_CONDITION_REQUIRED( - dummy->GetTimeSteps() == numberoftimesteps, - "orig-numberofTimeSteps:" << numberoftimesteps << " copy-numberofTimeSteps:" << dummy->GetTimeSteps()); - - surface = nullptr; - MITK_TEST_CONDITION_REQUIRED(surface.IsNull(), "Testing destruction of surface!"); - - MITK_TEST_END(); -} +}; +MITK_TEST_SUITE_REGISTRATION(mitkSurface) diff --git a/Modules/Core/test/mitkTinyXMLTest.cpp b/Modules/Core/test/mitkTinyXMLTest.cpp index b810c8f3c9..69dd7575aa 100644 --- a/Modules/Core/test/mitkTinyXMLTest.cpp +++ b/Modules/Core/test/mitkTinyXMLTest.cpp @@ -1,158 +1,164 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include -#include "mitkTestingMacros.h" - -#include - +// std includes #include #include #include #include +// MITK includes +#include "mitkStringProperty.h" +#include + +// itksys #include -static const std::string filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt"; -static const std::string elementToStoreAttributeName = "DoubleTest"; -static const std::string attributeToStoreName = "CommaValue"; +// VTK includes +#include -static double calcPrecision(const unsigned int requiredDecimalPlaces) -{ - return pow(10.0, -1.0 * ((double)requiredDecimalPlaces)); -} - -/** - * create a simple xml document which stores the values - * @param valueToWrite value which should be stored - * @return true, if document was successfully created. - */ -static bool Setup(double valueToWrite) -{ - // 1. create simple document - TiXmlDocument document; - auto decl = new TiXmlDeclaration("1.0", "", ""); // TODO what to write here? encoding? etc.... - document.LinkEndChild(decl); - - auto version = new TiXmlElement("Version"); - version->SetAttribute("Writer", __FILE__); - version->SetAttribute("CVSRevision", "$Revision: 17055 $"); - version->SetAttribute("FileVersion", 1); - document.LinkEndChild(version); - - // 2. store one element containing a double value with potentially many after comma digits. - auto vElement = new TiXmlElement(elementToStoreAttributeName); - vElement->SetDoubleAttribute(attributeToStoreName, valueToWrite); - document.LinkEndChild(vElement); - - // 3. store in file. - return document.SaveFile(filename); -} - -static int readValueFromSetupDocument(double &readOutValue) +// vnl includes +#include + +class mitkTinyXMLTestSuite : public mitk::TestFixture { - TiXmlDocument document; + CPPUNIT_TEST_SUITE(mitkTinyXMLTestSuite); + + MITK_TEST(TestingFunctionSetupWorks_Success); + MITK_TEST(TestingReadValueFromSetupDocument_Success); + MITK_TEST(TestingReadOutValueWorks_Success); + MITK_TEST(TestDoubleValueWriteOut_Success); + MITK_TEST(TestDoubleValueWriteOutManyDecimalPlaces_Success); - if (!document.LoadFile(filename)) + CPPUNIT_TEST_SUITE_END(); + +private: + const std::string m_Filename = itksys::SystemTools::GetCurrentWorkingDirectory() + "/TinyXMLTest.txt"; + const std::string m_ElementToStoreAttributeName = "DoubleTest"; + const std::string m_AttributeToStoreName = "CommaValue"; + + TiXmlDocument m_Document; + TiXmlElement *m_DoubleTest; + + double calcPrecision(const unsigned int requiredDecimalPlaces) { - MITK_TEST_CONDITION_REQUIRED(false, "Test Setup failed, could not open " << filename); - return TIXML_NO_ATTRIBUTE; + return pow(10.0, -1.0 * ((double)requiredDecimalPlaces)); } - else + + bool Setup(double valueToWrite) { - TiXmlElement *doubleTest = document.FirstChildElement(elementToStoreAttributeName); - return doubleTest->QueryDoubleAttribute(attributeToStoreName, &readOutValue); + // 1. create simple document + auto decl = new TiXmlDeclaration("1.0", "", ""); // TODO what to write here? encoding? etc.... + m_Document.LinkEndChild(decl); + + auto version = new TiXmlElement("Version"); + version->SetAttribute("Writer", __FILE__); + version->SetAttribute("CVSRevision", "$Revision: 17055 $"); + version->SetAttribute("FileVersion", 1); + m_Document.LinkEndChild(version); + + // 2. store one element containing a double value with potentially many after comma digits. + auto vElement = new TiXmlElement(m_ElementToStoreAttributeName); + vElement->SetDoubleAttribute(m_AttributeToStoreName, valueToWrite); + m_Document.LinkEndChild(vElement); + + // 3. store in file. + return m_Document.SaveFile(m_Filename); } -} -/** - * - * @return true if TearDown was successful. - */ -static bool TearDown() -{ - return !remove(filename.c_str()); -} - -static void Test_Setup_works() -{ - MITK_TEST_CONDITION_REQUIRED( - Setup(1.0) && TearDown(), - "Test if setup and teardown correctly writes data to " << filename << " and deletes the file after the test"); -} - -/** - * this first test ensures we can correctly readout values from the - * TinyXMLDocument. - */ -static void Test_ReadOutValue_works() -{ - Setup(1.0); +public: + void setUp() {} - double readValue; + void tearDown() {} - MITK_TEST_CONDITION_REQUIRED(TIXML_SUCCESS == readValueFromSetupDocument(readValue), - "checking if readout mechanism works."); -} - -static void Test_DoubleValueWriteOut() -{ - const double valueToWrite = -1.123456; - const int validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite - const double neededPrecision = calcPrecision(validDigitsAfterComma + 1); - double readValue; + void TestingFunctionSetupWorks_Success() + { + CPPUNIT_ASSERT_MESSAGE("Test if Setup correctly writes data to file", Setup(1.0)); + } - Setup(valueToWrite); + int readValueFromSetupDocument(double &readOutValue) + { + if (!m_Document.LoadFile(m_Filename)) + { + CPPUNIT_ASSERT_MESSAGE("Test Setup failed, could not open file", false); + return TIXML_NO_ATTRIBUTE; + } + else + { + m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName); + return m_DoubleTest->QueryDoubleAttribute(m_AttributeToStoreName, &readOutValue); + } + } - readValueFromSetupDocument(readValue); + void TestingReadValueFromSetupDocument_Success() + { + if (!m_Document.LoadFile(m_Filename)) + { + CPPUNIT_ASSERT_MESSAGE("Test Setup failed, could not open file", !m_Document.LoadFile(m_Filename)); + } + else + { + m_DoubleTest = m_Document.FirstChildElement(m_ElementToStoreAttributeName); + CPPUNIT_ASSERT_MESSAGE("Test Setup could open file", m_DoubleTest != nullptr); + } + } - MITK_TEST_CONDITION_REQUIRED( - mitk::Equal(valueToWrite, readValue, neededPrecision), - std::setprecision(validDigitsAfterComma) << "Testing if value " << valueToWrite << " equals " << readValue - << " which was retrieved from TinyXML document"); + /** + * this first test ensures we can correctly readout values from the + * TinyXMLDocument. + */ + void TestingReadOutValueWorks_Success() + { + double readValue; - TearDown(); -} + CPPUNIT_ASSERT_MESSAGE("checking if readout mechanism works.", + TIXML_SUCCESS == readValueFromSetupDocument(readValue)); + } -static void Test_DoubleValueWriteOut_manyDecimalPlaces() -{ - const double valueToWrite = -1.12345678910111; - const int validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite - const double neededPrecision = calcPrecision(validDigitsAfterComma + 1); - double readValue; + void TestDoubleValueWriteOut_Success() + { + const double valueToWrite = -1.123456; + const int validDigitsAfterComma = 6; // indicates the number of valid digits after comma of valueToWrite + const double neededPrecision = calcPrecision(validDigitsAfterComma + 1); + double readValue; - Setup(valueToWrite); + Setup(valueToWrite); + readValueFromSetupDocument(readValue); - readValueFromSetupDocument(readValue); + CPPUNIT_ASSERT_MESSAGE("Testing if value valueToWrite equals readValue which was retrieved from TinyXML document", + mitk::Equal(valueToWrite, readValue, neededPrecision)); + } - MITK_TEST_CONDITION_REQUIRED( - mitk::Equal(valueToWrite, readValue, neededPrecision), - std::setprecision(validDigitsAfterComma) << "Testing if value " << valueToWrite << " equals " << readValue - << " which was retrieved from TinyXML document"); + void TestDoubleValueWriteOutManyDecimalPlaces_Success() + { + const double valueToWrite = -1.12345678910111; + const int validDigitsAfterComma = 14; // indicates the number of valid digits after comma of valueToWrite + const double neededPrecision = calcPrecision(validDigitsAfterComma + 1); + double readValue; - TearDown(); -} + Setup(valueToWrite); -int mitkTinyXMLTest(int /* argc */, char * /*argv*/ []) -{ - MITK_TEST_BEGIN("TinyXMLTest"); + readValueFromSetupDocument(readValue); - Test_Setup_works(); - Test_ReadOutValue_works(); - Test_DoubleValueWriteOut(); - Test_DoubleValueWriteOut_manyDecimalPlaces(); + CPPUNIT_ASSERT_MESSAGE("Testing if value valueToWrite equals readValue which was retrieved from TinyXML document", + mitk::Equal(valueToWrite, readValue, neededPrecision)); + } +}; - MITK_TEST_END() -} +MITK_TEST_SUITE_REGISTRATION(mitkTinyXML) diff --git a/Modules/Core/test/mitkVectorTest.cpp b/Modules/Core/test/mitkVectorTest.cpp index 3b0ec9ebbf..720c7a1d15 100644 --- a/Modules/Core/test/mitkVectorTest.cpp +++ b/Modules/Core/test/mitkVectorTest.cpp @@ -1,171 +1,441 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" +#include + +// std includes +#include +// MITK includes +#include "mitkStringProperty.h" #include -#include +// itksys +#include "itkImage.h" + +// VTK includes +#include + +// vnl includes #include -int mitkVectorTest(int /*argc*/, char * /*argv*/ []) +class mitkVectorTestSuite : public mitk::TestFixture { - MITK_TEST_BEGIN("mitkVector"); - // test itk vector equality methods - itk::Vector itkVector_1; - itkVector_1[0] = 4.6; - itkVector_1[1] = 9.76543; - itkVector_1[2] = 746.09; - - itk::Vector itkVector_2; - itk::Vector itkVector_3; - for (int i = 0; i < 3; i++) - { - itkVector_2[i] = itkVector_1[i] - mitk::eps * 1.1; - itkVector_3[i] = itkVector_1[i] - mitk::eps * 0.9; - } - - MITK_TEST_CONDITION(mitk::Equal(itkVector_1, itkVector_1), - "Test vector equality using the same vector with mitk::eps"); - MITK_TEST_CONDITION( - !mitk::Equal(itkVector_1, itkVector_2), - "Test vector equality using different vectors with an element-wise difference greater than mitk::eps"); - MITK_TEST_CONDITION(mitk::Equal(itkVector_1, itkVector_2, mitk::eps * 1.2), - "Vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )"); - MITK_TEST_CONDITION( - mitk::Equal(itkVector_1, itkVector_3), - "Test vector equality using different vectors with an element-wise difference less than mitk::eps"); - // test itk point equality methods - itk::Point itkPoint_1; - itk::Point itkPoint_2; - itk::Point itkPoint_3; - for (int i = 0; i < 3; i++) - { - itkPoint_1[i] = itkVector_1[i]; - itkPoint_2[i] = itkVector_2[i]; - itkPoint_3[i] = itkVector_3[i]; - } - MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_1), "Test point equality using the same point with mitk::eps"); - MITK_TEST_CONDITION( - !mitk::Equal(itkPoint_1, itkPoint_2), - "Test point equality using different points with an element-wise difference greater than mitk::eps"); - MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_2, mitk::eps * 1.2), - "Points are equal for higher epsilon tolerance ( 1.2 * mitk::eps )"); - MITK_TEST_CONDITION(mitk::Equal(itkPoint_1, itkPoint_3), - "Test point equality using different points with an element-wise difference less than mitk::eps"); - // test mitk vnl vector equality methods - mitk::VnlVector mitk_vnl_vector_1(3); - mitk::VnlVector mitk_vnl_vector_2(3); - mitk::VnlVector mitk_vnl_vector_3(3); - for (int i = 0; i < 3; i++) - { - mitk_vnl_vector_1.put(i, itkVector_1[i]); - mitk_vnl_vector_2.put(i, itkVector_2[i]); - mitk_vnl_vector_3.put(i, itkVector_1[i]); - } - - MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_1), - "Test mitk vnl vector equality using the same mitk vnl vector with mitk::eps"); - MITK_TEST_CONDITION(!mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_2), - "Test mitk vnl vector equality using different mitk vnl vectors with an element-wise difference " - "greater than mitk::eps"); - MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_2, mitk::eps * 1.2), - "Vnl vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )"); - MITK_TEST_CONDITION(mitk::Equal(mitk_vnl_vector_1, mitk_vnl_vector_3), - "Test mitk vnl vector equality using " - "different mitk vnl vectors with an " - "element-wise difference less than mitk::eps"); - - // test vnl_vector equality method + CPPUNIT_TEST_SUITE(mitkVectorTestSuite); + + MITK_TEST(ItkVecorEqualityUsingSameVector_Success); + MITK_TEST(ItkVecorEqualityUsingDifferentVectors_Failure); + MITK_TEST(ItkVecorEqualityForHigherEpsilonTolerance_Success); + MITK_TEST(ItkVecorEqualityUsingDifferentVectorsWithElementWise_Success); + + MITK_TEST(ItkPointEqualitySamePoint_Success); + MITK_TEST(ItkPointEqualityDifferentPoints_Failure); + MITK_TEST(ItkPointEqualitForHigherEpsilons_Success); + MITK_TEST(ItkPointEqualitDifferentPointsWithElementWise_Success); + + MITK_TEST(MitkVnlVectorEqualitySameMitkVnlVector_Success); + MITK_TEST(MitkVnlVectorEqualityDifferentMitkVnlVectors_Failure); + MITK_TEST(MitkVnlVectorEqualityHigherEpsilon_Success); + MITK_TEST(MitkVnlVectorEqualityUsingDifferentMitkVnlVectorsWithElementWise_Success); + + MITK_TEST(VnlVectorEqualitySameVnlVector_Success); + MITK_TEST(VnlVectorEqualityDifferentVnlVectors_Failure); + MITK_TEST(VnlVectorEqualityDifferentVnlVectorsWithHighEps_Success); + MITK_TEST(VnlVectorEqualityDifferentVnlVectorsWithLowEps_Success); + MITK_TEST(VnlVectorEqualityDifferentVnlVectorsWithLowEps_Failure); + + MITK_TEST(ScalarEqualitySameScalar_Successs); + MITK_TEST(ScalarEqualityDifferentScalarsDifferenceGreaterEps_Failure); + MITK_TEST(ScalarEqualityDifferentScalarsDifferenceEqualEps_Successs); + MITK_TEST(ScalarEqualityDifferentScalarsDifferenceLessEps_Successs); + + MITK_TEST(MatrixEqualitySameMatrixElementsWithEps_Success); + MITK_TEST(MatrixEqualityElementWiseDifferentMatrixElementsWithEpsilonZero_Failure); + MITK_TEST(MatrixEqualityDifferentMatrixElementsWithEpsilon_Success); + MITK_TEST(MatrixEqualityRMSDifferentMatrixElementsWithEpsilon_Failure); + MITK_TEST(MatrixEqualityRMSDifferentMatrixElementsWithEpsilonZero_Success); + + CPPUNIT_TEST_SUITE_END(); + +private: + itk::Vector m_ItkVector_1; + itk::Vector m_ItkVector_2; + itk::Vector m_ItkVector_3; + + itk::Point m_ItkPoint_1; + itk::Point m_ItkPoint_2; + itk::Point m_ItkPoint_3; + typedef mitk::ScalarType VnlValueType; - vnl_vector_fixed vnlVector_1; - vnlVector_1[3] = 56.98; - vnlVector_1[4] = 22.32; - vnlVector_1[5] = 1.00; - vnlVector_1[6] = 746.09; - vnl_vector_fixed vnlVector_2; - vnl_vector_fixed vnlVector_3; - for (int i = 0; i < 7; i++) + vnl_vector_fixed m_VnlVector_1; + vnl_vector_fixed m_VnlVector_2; + vnl_vector_fixed m_VnlVector_3; + + mitk::ScalarType m_Scalar1; + mitk::ScalarType m_Scalar2; + mitk::ScalarType m_Scalar3; + mitk::ScalarType m_Scalar4; + + vnl_matrix_fixed m_VnlMatrix3x3_1; + vnl_matrix_fixed m_VnlMatrix3x3_2; + + mitk::ScalarType m_Epsilon; + +public: + void setUp() + { + m_ItkVector_1[0] = 4.6; + m_ItkVector_1[1] = 9.76543; + m_ItkVector_1[2] = 746.09; + + m_VnlVector_1[0] = 4.6; + m_VnlVector_1[1] = 9.76543; + m_VnlVector_1[2] = 746.09; + m_VnlVector_1[3] = 56.98; + m_VnlVector_1[4] = 22.32; + m_VnlVector_1[5] = 1.00; + m_VnlVector_1[6] = 746.09; + + m_Scalar1 = 0.5689; + m_Scalar2 = m_Scalar1 + mitk::eps * 1.01; + m_Scalar3 = m_Scalar1; + m_Scalar4 = m_Scalar1 + mitk::eps * 0.95; + + m_VnlMatrix3x3_1(0, 0) = 1.1; + m_VnlMatrix3x3_1(0, 1) = 0.4; + m_VnlMatrix3x3_1(0, 2) = 5.3; + m_VnlMatrix3x3_1(1, 0) = 2.7; + m_VnlMatrix3x3_1(1, 1) = 3578.56418; + m_VnlMatrix3x3_1(1, 2) = 123.56; + m_VnlMatrix3x3_1(2, 0) = 546.89; + m_VnlMatrix3x3_1(2, 1) = 0.0001; + m_VnlMatrix3x3_1(2, 2) = 1.0; + + m_VnlMatrix3x3_2(0, 0) = 1.1000009; + m_VnlMatrix3x3_2(0, 1) = 0.4000009; + m_VnlMatrix3x3_2(0, 2) = 5.3000009; + m_VnlMatrix3x3_2(1, 0) = 2.7000009; + m_VnlMatrix3x3_2(1, 1) = 3578.5641809; + m_VnlMatrix3x3_2(1, 2) = 123.5600009; + m_VnlMatrix3x3_2(2, 0) = 546.8900009; + m_VnlMatrix3x3_2(2, 1) = 0.0001009; + m_VnlMatrix3x3_2(2, 2) = 1.0000009; + + m_Epsilon = 0.000001; + } + + void tearDown() + { + m_ItkVector_1.Fill(0); + m_ItkVector_2.Fill(0); + m_ItkVector_3.Fill(0); + m_ItkPoint_1.Fill(0); + m_ItkPoint_2.Fill(0); + m_ItkPoint_3.Fill(0); + + m_VnlVector_1.fill(0); + m_VnlVector_2.fill(0); + m_VnlVector_3.fill(0); + + m_Scalar1 = 0; + m_Scalar2 = 0; + m_Scalar3 = 0; + m_Scalar4 = 0; + + m_VnlMatrix3x3_1.fill(0); + m_VnlMatrix3x3_2.fill(0); + } + + void ItkVecorEqualityUsingSameVector_Success() + { + CPPUNIT_ASSERT_MESSAGE("Test vector equality using the same vector with mitk::eps", + mitk::Equal(m_ItkVector_1, m_ItkVector_1)); + } + + void ItkVecorEqualityUsingDifferentVectors_Failure() + { + for (int i = 0; i < 3; i++) + { + m_ItkVector_2[i] = m_ItkVector_1[i] - mitk::eps * 1.1; + } + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test vector equality using different vectors with an element-wise difference greater than mitk::eps", + mitk::Equal(m_ItkVector_1, m_ItkVector_2)); + } + + void ItkVecorEqualityForHigherEpsilonTolerance_Success() + { + for (int i = 0; i < 3; i++) + { + m_ItkVector_2[i] = m_ItkVector_1[i] - mitk::eps * 1.1; + } + CPPUNIT_ASSERT_MESSAGE("Vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )", + mitk::Equal(m_ItkVector_1, m_ItkVector_2, mitk::eps * 1.2)); + } + + void ItkVecorEqualityUsingDifferentVectorsWithElementWise_Success() + { + for (int i = 0; i < 3; i++) + { + m_ItkVector_3[i] = m_ItkVector_1[i] - mitk::eps * 0.9; + } + CPPUNIT_ASSERT_MESSAGE( + "Test vector equality using different vectors with an element-wise difference less than mitk::eps", + mitk::Equal(m_ItkVector_1, m_ItkVector_3)); + } + + void ItkPointEqualitySamePoint_Success() + { + // test itk point equality methods + for (int i = 0; i < 3; i++) + { + m_ItkPoint_1[i] = m_ItkVector_1[i]; + } + CPPUNIT_ASSERT_MESSAGE("Test point equality using the same point with mitk::eps", + mitk::Equal(m_ItkPoint_1, m_ItkPoint_1)); + } + + void ItkPointEqualityDifferentPoints_Failure() + { + for (int i = 0; i < 3; i++) + { + m_ItkPoint_1[i] = m_ItkVector_1[i]; + m_ItkPoint_2[i] = m_ItkVector_2[i]; + } + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test point equality using different points with an element-wise difference greater than mitk::eps", + mitk::Equal(m_ItkPoint_1, m_ItkPoint_2)); + } + + void ItkPointEqualitForHigherEpsilons_Success() + { + for (int i = 0; i < 3; i++) + { + m_ItkVector_2[i] = m_ItkVector_1[i] - mitk::eps * 1.1; + } + + for (int i = 0; i < 3; i++) + { + m_ItkPoint_1[i] = m_ItkVector_1[i]; + m_ItkPoint_2[i] = m_ItkVector_2[i]; + } + CPPUNIT_ASSERT_MESSAGE("Points are equal for higher epsilon tolerance ( 1.2 * mitk::eps )", + mitk::Equal(m_ItkPoint_1, m_ItkPoint_2, mitk::eps * 1.2)); + } + + void ItkPointEqualitDifferentPointsWithElementWise_Success() + { + for (int i = 0; i < 3; i++) + { + m_ItkVector_3[i] = m_ItkVector_1[i] - mitk::eps * 0.9; + } + + for (int i = 0; i < 3; i++) + { + m_ItkPoint_1[i] = m_ItkVector_1[i]; + m_ItkPoint_3[i] = m_ItkVector_3[i]; + } + CPPUNIT_ASSERT_MESSAGE( + "Test point equality using different points with an element-wise difference less than mitk::eps", + mitk::Equal(m_ItkPoint_1, m_ItkPoint_3)); + } + + void MitkVnlVectorEqualitySameMitkVnlVector_Success() + { + mitk::VnlVector mitkVnlVector_1(3); + + for (int i = 0; i < 3; i++) + { + mitkVnlVector_1.put(i, m_ItkVector_1[i]); + } + + CPPUNIT_ASSERT_MESSAGE("Test mitk vnl vector equality using the same mitk vnl vector with mitk::eps", + mitk::Equal(mitkVnlVector_1, mitkVnlVector_1)); + } + + void MitkVnlVectorEqualityDifferentMitkVnlVectors_Failure() + { + mitk::VnlVector mitkVnlVector_1(3); + mitk::VnlVector mitkVnlVector_2(3); + + for (int i = 0; i < 3; i++) + { + mitkVnlVector_1.put(i, m_ItkVector_1[i]); + mitkVnlVector_2.put(i, m_ItkVector_2[i]); + } + + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test mitk vnl vector equality using different mitk vnl vectors with an element-wise difference " + "greater than mitk::eps", + mitk::Equal(mitkVnlVector_1, mitkVnlVector_2)); + } + + void MitkVnlVectorEqualityHigherEpsilon_Success() + { + mitk::VnlVector mitkVnlVector_1(3); + mitk::VnlVector mitkVnlVector_2(3); + + for (int i = 0; i < 3; i++) + { + m_ItkVector_2[i] = m_ItkVector_1[i] - mitk::eps * 1.1; + } + + for (int i = 0; i < 3; i++) + { + mitkVnlVector_1.put(i, m_ItkVector_1[i]); + mitkVnlVector_2.put(i, m_ItkVector_2[i]); + } + + CPPUNIT_ASSERT_MESSAGE("Vnl vectors are equal for higher epsilon tolerance ( 1.2 * mitk::eps )", + mitk::Equal(mitkVnlVector_1, mitkVnlVector_2, mitk::eps * 1.2)); + } + + void MitkVnlVectorEqualityUsingDifferentMitkVnlVectorsWithElementWise_Success() { - if (i < 3) + mitk::VnlVector mitkVnlVector_1(3); + mitk::VnlVector mitkVnlVector_3(3); + + for (int i = 0; i < 3; i++) + { + m_ItkVector_3[i] = m_ItkVector_1[i] - mitk::eps * 0.9; + } + + for (int i = 0; i < 3; i++) { - vnlVector_1.put(i, itkVector_1[i]); + mitkVnlVector_1.put(i, m_ItkVector_1[i]); + mitkVnlVector_3.put(i, m_ItkVector_3[i]); } - vnlVector_2[i] = vnlVector_1[i] - mitk::eps * 1.1f; - vnlVector_3[i] = vnlVector_1[i] - mitk::eps * 0.9f; + CPPUNIT_ASSERT_MESSAGE("Test mitk vnl vector equality using " + "different mitk vnl vectors with an " + "element-wise difference less than mitk::eps", + mitk::Equal(mitkVnlVector_1, mitkVnlVector_3)); + } + + void VnlVectorEqualitySameVnlVector_Success() + { + // test vnl_vector equality method + + CPPUNIT_ASSERT_MESSAGE("vnl_fixed : v_1 == v_1 ", (mitk::Equal(m_VnlVector_1, m_VnlVector_1))); } - MITK_TEST_CONDITION((mitk::Equal(vnlVector_1, vnlVector_1)), "vnl_fixed : v_1 == v_1 "); // the v_2 is constructed so that the equality test fails for mitk::eps, the norm of the difference between the // vectors is 7 * eps/6.9 - MITK_TEST_CONDITION(!(mitk::Equal(vnlVector_1, vnlVector_2)), - "vnl_fixed : v_1 != v_2 with mitk::eps "); + void VnlVectorEqualityDifferentVnlVectors_Failure() + { + for (int i = 0; i < 7; i++) + { + m_VnlVector_2[i] = m_VnlVector_1[i] - mitk::eps * 1.1f; + } + + CPPUNIT_ASSERT_NO_THROW_MESSAGE("vnl_fixed : v_1 != v_2 with mitk::eps ", + (mitk::Equal(m_VnlVector_1, m_VnlVector_2))); + } + // increase the epsilon value used for testing equality - should now pass ( 1.2 * mitk::eps > 7 * mitk::eps/6.9 ) - MITK_TEST_CONDITION((mitk::Equal(vnlVector_1, vnlVector_2, mitk::eps * 1.2f)), - "vnl_fixed : v_1 == v_2 with eps = 1.2 * mitk::eps "); - MITK_TEST_CONDITION((mitk::Equal(vnlVector_1, vnlVector_3, mitk::eps)), - "vnl_fixed : v_1 == v_3 with eps = 0.8 * mitk::eps "); - MITK_TEST_CONDITION(!(mitk::Equal(vnlVector_1, vnlVector_3, mitk::eps * 0.8f)), - "vnl_fixed : v_1 != v_3 with eps = 0.8 * mitk::eps "); - - // test scalar equality method - mitk::ScalarType scalar1 = 0.5689; - mitk::ScalarType scalar2 = scalar1 + mitk::eps * 1.01; - mitk::ScalarType scalar3 = scalar1; - mitk::ScalarType scalar4 = scalar1 + mitk::eps * 0.95; - MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar1), "Test scalar equality using the same scalar with mitk::eps"); - MITK_TEST_CONDITION(!mitk::Equal(scalar1, scalar2), - "Test scalar equality using the different scalars with a difference greater than mitk::eps"); - MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar3), - "Test scalar equality using the different scalars with a difference equal to mitk::eps"); - MITK_TEST_CONDITION(mitk::Equal(scalar1, scalar4), - "Test scalar equality using the different scalars with a difference less than mitk::eps"); - - // test matrix equality methods - vnl_matrix_fixed vnlMatrix3x3_1; - vnlMatrix3x3_1(0, 0) = 1.1; - vnlMatrix3x3_1(0, 1) = 0.4; - vnlMatrix3x3_1(0, 2) = 5.3; - vnlMatrix3x3_1(1, 0) = 2.7; - vnlMatrix3x3_1(1, 1) = 3578.56418; - vnlMatrix3x3_1(1, 2) = 123.56; - vnlMatrix3x3_1(2, 0) = 546.89; - vnlMatrix3x3_1(2, 1) = 0.0001; - vnlMatrix3x3_1(2, 2) = 1.0; - vnl_matrix_fixed vnlMatrix3x3_2; - vnlMatrix3x3_2(0, 0) = 1.1000009; - vnlMatrix3x3_2(0, 1) = 0.4000009; - vnlMatrix3x3_2(0, 2) = 5.3000009; - vnlMatrix3x3_2(1, 0) = 2.7000009; - vnlMatrix3x3_2(1, 1) = 3578.5641809; - vnlMatrix3x3_2(1, 2) = 123.5600009; - vnlMatrix3x3_2(2, 0) = 546.8900009; - vnlMatrix3x3_2(2, 1) = 0.0001009; - vnlMatrix3x3_2(2, 2) = 1.0000009; - - mitk::ScalarType epsilon = 0.000001; - MITK_TEST_CONDITION(mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_1, mitk::eps), - "Test for matrix equality with given epsilon=mitk::eps and exactly the same matrix elements"); - MITK_TEST_CONDITION(!mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_2, 0.0), - "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements"); - MITK_TEST_CONDITION(mitk::MatrixEqualElementWise(vnlMatrix3x3_1, vnlMatrix3x3_2, epsilon), - "Test for matrix equality with given epsilon and slightly different matrix elements"); - MITK_TEST_CONDITION(!mitk::MatrixEqualRMS(vnlMatrix3x3_1, vnlMatrix3x3_2, 0.0), - "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements"); - MITK_TEST_CONDITION(mitk::MatrixEqualRMS(vnlMatrix3x3_1, vnlMatrix3x3_2, epsilon), - "Test for matrix equality with given epsilon and slightly different matrix elements"); - - MITK_TEST_END(); -} + void VnlVectorEqualityDifferentVnlVectorsWithHighEps_Success() + { + for (int i = 0; i < 7; i++) + { + m_VnlVector_2[i] = m_VnlVector_1[i] - mitk::eps * 1.1f; + } + + CPPUNIT_ASSERT_MESSAGE("vnl_fixed : v_1 == v_2 with eps = 1.2 * mitk::eps ", + (mitk::Equal(m_VnlVector_1, m_VnlVector_2, mitk::eps * 1.2f))); + } + + void VnlVectorEqualityDifferentVnlVectorsWithLowEps_Success() + { + for (int i = 0; i < 7; i++) + { + m_VnlVector_3[i] = m_VnlVector_1[i] - mitk::eps * 0.9f; + } + + CPPUNIT_ASSERT_MESSAGE("vnl_fixed : v_1 == v_3 with eps = 0.8 * mitk::eps ", + (mitk::Equal(m_VnlVector_1, m_VnlVector_3, mitk::eps))); + } + + void VnlVectorEqualityDifferentVnlVectorsWithLowEps_Failure() + { + for (int i = 0; i < 7; i++) + { + m_VnlVector_3[i] = m_VnlVector_1[i] - mitk::eps * 0.9f; + } + + CPPUNIT_ASSERT_NO_THROW_MESSAGE("vnl_fixed : v_1 != v_3 with eps = 0.8 * mitk::eps ", + (mitk::Equal(m_VnlVector_1, m_VnlVector_3, mitk::eps * 0.8f))); + } + + void ScalarEqualitySameScalar_Successs() + { + // test scalar equality method + CPPUNIT_ASSERT_MESSAGE("Test scalar equality using the same scalar with mitk::eps", + mitk::Equal(m_Scalar1, m_Scalar1)); + } + void ScalarEqualityDifferentScalarsDifferenceGreaterEps_Failure() + { + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test scalar equality using the different scalars with a difference greater than mitk::eps", + mitk::Equal(m_Scalar1, m_Scalar2)); + } + + void ScalarEqualityDifferentScalarsDifferenceEqualEps_Successs() + { + CPPUNIT_ASSERT_MESSAGE("Test scalar equality using the different scalars with a difference equal to mitk::eps", + mitk::Equal(m_Scalar1, m_Scalar3)); + } + + void ScalarEqualityDifferentScalarsDifferenceLessEps_Successs() + { + CPPUNIT_ASSERT_MESSAGE("Test scalar equality using the different scalars with a difference less than mitk::eps", + mitk::Equal(m_Scalar1, m_Scalar4)); + } + + void MatrixEqualitySameMatrixElementsWithEps_Success() + { + // test matrix equality methods + CPPUNIT_ASSERT_MESSAGE("Test for matrix equality with given epsilon=mitk::eps and exactly the same matrix elements", + mitk::MatrixEqualElementWise(m_VnlMatrix3x3_1, m_VnlMatrix3x3_1, mitk::eps)); + } + + void MatrixEqualityElementWiseDifferentMatrixElementsWithEpsilonZero_Failure() + { + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements", + mitk::MatrixEqualElementWise(m_VnlMatrix3x3_1, m_VnlMatrix3x3_2, 0.0)); + } + + void MatrixEqualityDifferentMatrixElementsWithEpsilon_Success() + { + CPPUNIT_ASSERT_MESSAGE("Test for matrix equality with given epsilon and slightly different matrix elements", + mitk::MatrixEqualElementWise(m_VnlMatrix3x3_1, m_VnlMatrix3x3_2, m_Epsilon)); + } + + void MatrixEqualityRMSDifferentMatrixElementsWithEpsilon_Failure() + { + CPPUNIT_ASSERT_NO_THROW_MESSAGE( + "Test for matrix equality with given epsilon=0.0 and slightly different matrix elements", + mitk::MatrixEqualRMS(m_VnlMatrix3x3_1, m_VnlMatrix3x3_2, 0.0)); + } + + void MatrixEqualityRMSDifferentMatrixElementsWithEpsilonZero_Success() + { + CPPUNIT_ASSERT_MESSAGE("Test for matrix equality with given epsilon and slightly different matrix elements", + mitk::MatrixEqualRMS(m_VnlMatrix3x3_1, m_VnlMatrix3x3_2, m_Epsilon)); + } +}; +MITK_TEST_SUITE_REGISTRATION(mitkVector) diff --git a/Modules/Core/test/mitkWeakPointerTest.cpp b/Modules/Core/test/mitkWeakPointerTest.cpp index 2d033c535a..5bc4633223 100644 --- a/Modules/Core/test/mitkWeakPointerTest.cpp +++ b/Modules/Core/test/mitkWeakPointerTest.cpp @@ -1,60 +1,111 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ +// Testing +#include "mitkTestFixture.h" #include "mitkTestingMacros.h" -#include +// std includes +#include + +// MITK includes #include -int mitkWeakPointerTest(int /*argc*/, char * /*argv*/ []) +// ITK includes +#include + +class mitkWeakPointerTestSuite : public mitk::TestFixture { - MITK_TEST_BEGIN("WeakPointer") + CPPUNIT_TEST_SUITE(mitkWeakPointerTestSuite); + + MITK_TEST(EqualPointers_Success); + MITK_TEST(ExpiredWeakPointerWithSmartPointerAssignment_Success); + MITK_TEST(ExpiredWeakPointerWithWeakPointerAssignment_Success); + MITK_TEST(ExpiredWeakPointerWithSmartPointerConstructor_Success); + MITK_TEST(ExpiredWeakPointerWithWeakPointerConstructor_Success); + MITK_TEST(DeleteEventCall_Success); - int deleteEventCallbackCalled = 0; + CPPUNIT_TEST_SUITE_END(); - mitk::WeakPointer weakPointer; +private: + mitk::WeakPointer m_WeakPointer; + mitk::WeakPointer m_WeakPointer2; + itk::Object::Pointer m_SmartPointer; - weakPointer.SetDeleteEventCallback([&deleteEventCallbackCalled]() +public: + void setUp() { - ++deleteEventCallbackCalled; - }); + m_SmartPointer = itk::Object::New(); + m_WeakPointer = m_SmartPointer; + m_WeakPointer2 = m_WeakPointer; + } - mitk::WeakPointer weakPointer2; + void tearDown() + { + m_SmartPointer = nullptr; + m_WeakPointer = nullptr; + m_WeakPointer2 = nullptr; + } - // Testing constructors and reference counting - itk::Object::Pointer smartPointer = itk::Object::New(); - mitk::WeakPointer weakPointer3(smartPointer); - mitk::WeakPointer weakPointer4(weakPointer); + void EqualPointers_Success() { - itk::Object::Pointer tmpSmartPointer(weakPointer.Lock()); - itk::Object::Pointer tmpSmartPointer2(weakPointer2.Lock()); - MITK_TEST_CONDITION_REQUIRED(tmpSmartPointer.GetPointer() == tmpSmartPointer2.GetPointer(), - "Testing equal pointers"); + itk::Object::Pointer tmpSmartPointer(m_WeakPointer.Lock()); + itk::Object::Pointer tmpSmartPointer2(m_WeakPointer2.Lock()); + CPPUNIT_ASSERT_MESSAGE("Testing equal pointers", tmpSmartPointer.GetPointer() == tmpSmartPointer2.GetPointer()); } - weakPointer = smartPointer; - weakPointer2 = weakPointer; + void ReferenceCountOfPointers_Success() + { + CPPUNIT_ASSERT_MESSAGE("Testing reference count", 1 == m_SmartPointer->GetReferenceCount()); + } - MITK_TEST_CONDITION_REQUIRED(1 == smartPointer->GetReferenceCount(), "Testing reference count"); - smartPointer = nullptr; - MITK_TEST_CONDITION_REQUIRED(weakPointer.IsExpired(), "Testing expired weak pointer (smart pointer assignment)"); - MITK_TEST_CONDITION_REQUIRED(weakPointer2.IsExpired(), "Testing expired weak pointer (weak pointer assignment)"); - MITK_TEST_CONDITION_REQUIRED(weakPointer3.IsExpired(), "Testing expired weak pointer (smart pointer constructor)"); - MITK_TEST_CONDITION_REQUIRED(weakPointer4.IsExpired(), "Testing expired weak pointer (copy constructor)"); - MITK_TEST_CONDITION_REQUIRED(1 == deleteEventCallbackCalled, "Testing call of delete event callback"); + void ExpiredWeakPointerWithSmartPointerAssignment_Success() + { + m_SmartPointer = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (smart pointer assignment)", m_WeakPointer.IsExpired()); + } - MITK_TEST_END() -} + void ExpiredWeakPointerWithWeakPointerAssignment_Success() + { + m_SmartPointer = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (weak pointer assignment)", m_WeakPointer2.IsExpired()); + } + + void ExpiredWeakPointerWithSmartPointerConstructor_Success() + { + mitk::WeakPointer weakPointer3(m_SmartPointer); + m_SmartPointer = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (smart pointer constructor)", weakPointer3.IsExpired()); + } + + void ExpiredWeakPointerWithWeakPointerConstructor_Success() + { + mitk::WeakPointer weakPointer4(m_WeakPointer); + m_WeakPointer = m_SmartPointer; + m_SmartPointer = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing expired weak pointer (copy constructor)", weakPointer4.IsExpired()); + } + + void DeleteEventCall_Success() + { + int deleteEventCallbackCalled = 0; + m_WeakPointer.SetDeleteEventCallback([&deleteEventCallbackCalled]() { ++deleteEventCallbackCalled; }); + m_WeakPointer = m_SmartPointer; + m_SmartPointer = nullptr; + CPPUNIT_ASSERT_MESSAGE("Testing call of delete event callback", 1 == deleteEventCallbackCalled); + } +}; +MITK_TEST_SUITE_REGISTRATION(mitkWeakPointer)