diff --git a/Core/Code/Common/mitkTestFixture.h b/Core/Code/Common/mitkTestFixture.h index a15c5fbb7b..6657749b10 100644 --- a/Core/Code/Common/mitkTestFixture.h +++ b/Core/Code/Common/mitkTestFixture.h @@ -1,125 +1,132 @@ /*=================================================================== 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. ===================================================================*/ #ifndef MITKTESTFIXTURE_H #define MITKTESTFIXTURE_H #include #include #include #include #include namespace mitk { /** * \brief Test fixture for parameterized tests * * This class is a drop-in replacement for CppUnit::TextFixture and * enables test methods to access individual parameters. You can also * invoke one method multiple times with different parameters. * * * The following simple example creates a single test withoud custom * parameters: * * \code * class MySimpleTestSuite : public mitk::TestFixture * { * CPPUNIT_TEST_SUITE(MySimpleTestSuite); * MITK_TEST(FivePlusFiveTest); * CPPUNIT_TEST_SUITE_END(); * * public: * void FivePlusFiveTest() * { * CPPUNIT_ASSERT(5+5 == 10); * } * }; * MITK_TEST_SUITE_REGISTRATION(MySimpleTestSuite) * \endcode * * * The following example creates a test class containing only * one test method, but the associated test suite contains three tests, * using different parameters for each call of the same method. Use * the macro MITK_PARAMETERIZED_TEST_1 only if you know what you are * doing. If you are not sure, use MITK_TEST instead. * * \code * class MyTestSuite : public mitk::TestFixture * { * CPPUNIT_TEST_SUITE(MyTestSuite); * MITK_PARAMETERIZED_TEST_1(TestSomething, "One"); * MITK_PARAMETERIZED_TEST_1(TestSomething, "Two"); * MITK_PARAMETERIZED_TEST_1(TestSomething, "Three"); * CPPUNIT_TEST_SUITE_END(); * * public: * * void TestSomething() * { * std::vector parameter = GetTestParameter(); * CPPUNIT_ASSERT(parameter.size() == 1); * std::string testParam = parameter[0]; * * MITK_INFO << "Parameter: " << testParam; * } * }; * MITK_TEST_SUITE_REGISTRATION(MyTestSuite) * \endcode * * \sa MITK_PARAMETERIZED_TEST * \sa MITK_PARAMETERIZED_TEST_1 */ class TestFixture : public CppUnit::TestFixture { protected: /** * \brief Get parameters for this test fixture * * This method can be called in tests added via the MITK_PARAMETERIZED_TEST * macro or one of its variants. * * \return The list of \c std::string parameters passed to previous calls * of the MITK_PARAMETERIZED_TEST macro or one of its variants. * */ std::vector GetTestParameter() const { return m_Parameter; } - static std::string getTestDataFilePath(const std::string& testData) + /** + * \brief Get the absolute path for test data. + * + * \param testData The realative path in the MITK test data repository. + * + * \return The absolute path for the test data. + */ + static std::string GetTestDataFilePath(const std::string& testData) { if (itksys::SystemTools::FileIsFullPath(testData.c_str())) return testData; return std::string(MITK_DATA_DIR) + "/" + testData; } private: template friend class TestCaller; std::vector m_Parameter; }; } #endif // MITKTESTFIXTURE_H diff --git a/Core/Code/Testing/mitkDICOMLocaleTest.cpp b/Core/Code/Testing/mitkDICOMLocaleTest.cpp index d22bc146aa..7c62936cab 100644 --- a/Core/Code/Testing/mitkDICOMLocaleTest.cpp +++ b/Core/Code/Testing/mitkDICOMLocaleTest.cpp @@ -1,147 +1,147 @@ /*=================================================================== 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. ===================================================================*/ /* This test is meant to reproduce the following error: - The machine or current user has a German locale. - This esp. means that stream IO expects the decimal separator as a comma: "," - DICOM files use a point "." as the decimal separator to be locale independent - The parser used by MITK (ITK's GDCM) seems to use the current locale instead of the "C" or "POSIX" locale - This leads to spacings (and probably other numbers) being trimmed/rounded, e.g. the correct spacing of 0.314 is read as 1.0 etc. */ #include "mitkDataNodeFactory.h" #include "mitkStandardFileLocations.h" #include "mitkDicomSeriesReader.h" #include "mitkTestingMacros.h" #include "mitkTestFixture.h" #include #include #include class mitkDICOMLocaleTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkDICOMLocaleTestSuite); CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS(addDICOMLocaleWithReferenceImageTests); CPPUNIT_TEST_SUITE_END(); private: // A custom method for adding a combination of filename and locale tests static void addDICOMLocaleWithReferenceImageTests(TestSuiteBuilderContextType& context) { std::vector fileArgs; fileArgs.push_back("spacing-ok-ct.dcm"); fileArgs.push_back("spacing-ok-mr.dcm"); fileArgs.push_back("spacing-ok-sc.dcm"); // load a reference DICOM file with German locales being set std::vector localeArgs; localeArgs.push_back("C"); localeArgs.push_back("de_DE"); localeArgs.push_back("de_DE.utf8"); localeArgs.push_back("de_DE.UTF8"); localeArgs.push_back("de_DE@euro"); localeArgs.push_back("German_Germany"); for (std::size_t fileIndex = 0; fileIndex < fileArgs.size(); ++fileIndex) { for (std::size_t localeIndex = 0; localeIndex < localeArgs.size(); ++localeIndex) { MITK_PARAMETERIZED_TEST_2(testLocaleWithReferenceImage, fileArgs[fileIndex], localeArgs[localeIndex]); } } } private: std::string m_FileName; std::string m_Locale; bool m_SkipImageTest; char* m_OldLocale; void SetTestParameter() { std::vector parameter = GetTestParameter(); CPPUNIT_ASSERT(parameter.size() == 2); - m_FileName = getTestDataFilePath(parameter[0]); + m_FileName = GetTestDataFilePath(parameter[0]); m_Locale = parameter[1]; } public: mitkDICOMLocaleTestSuite() : m_OldLocale(NULL) {} // Change the current locale to m_Locale void setUp() { m_SkipImageTest = false; m_OldLocale = NULL; SetTestParameter(); try { m_OldLocale = setlocale(LC_ALL, NULL); MITK_TEST_OUTPUT(<< " ** Changing locale from " << m_OldLocale << " to '" << m_Locale << "'") setlocale(LC_ALL, m_Locale.c_str()); std::cin.imbue(std::locale(m_Locale.c_str())); } catch(...) { MITK_TEST_OUTPUT(<< "Could not activate locale " << m_Locale) m_SkipImageTest = true; } } void tearDown() { if (m_OldLocale) { setlocale(LC_ALL, m_OldLocale); std::cin.imbue(std::locale(m_OldLocale)); } } void testLocaleWithReferenceImage() { if (m_SkipImageTest) return; mitk::Image::Pointer image; mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); factory->SetFileName( m_FileName ); factory->Update(); CPPUNIT_ASSERT(factory->GetNumberOfOutputs() > 0); mitk::DataNode::Pointer node = factory->GetOutput( 0 ); image = dynamic_cast(node->GetData()); CPPUNIT_ASSERT(image.IsNotNull()); // note importance of minor differences in spacings: // DICOM has order y-spacing, x-spacing, while in MITK we assume x-spacing, y-spacing (both meant for 0 and 1 index in array) CPPUNIT_ASSERT_MESSAGE("incorrect x spacing", mitk::Equal(image->GetGeometry()->GetSpacing()[0], 0.3141592)); CPPUNIT_ASSERT_MESSAGE("incorrect y spacing ", mitk::Equal(image->GetGeometry()->GetSpacing()[1], 0.3411592)); } }; MITK_TEST_SUITE_REGISTRATION(mitkDICOMLocale) diff --git a/Core/Code/Testing/mitkIOUtilTest.cpp b/Core/Code/Testing/mitkIOUtilTest.cpp index 41cd28db16..5eb29dcf85 100644 --- a/Core/Code/Testing/mitkIOUtilTest.cpp +++ b/Core/Code/Testing/mitkIOUtilTest.cpp @@ -1,174 +1,174 @@ /*=================================================================== 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. ===================================================================*/ #include "mitkTestingMacros.h" #include #include #include #include #include class mitkIOUtilTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkIOUtilTestSuite); MITK_TEST(TestTempMethods); MITK_TEST(TestLoadAndSaveImage); MITK_TEST(TestLoadAndSavePointSet); MITK_TEST(TestLoadAndSaveSurface); CPPUNIT_TEST_SUITE_END(); private: std::string m_ImagePath; std::string m_SurfacePath; std::string m_PointSetPath; public: void setUp() { - m_ImagePath = getTestDataFilePath("Pic3D.nrrd"); - m_SurfacePath = getTestDataFilePath("binary.stl"); - m_PointSetPath = getTestDataFilePath("pointSet.mps"); + m_ImagePath = GetTestDataFilePath("Pic3D.nrrd"); + m_SurfacePath = GetTestDataFilePath("binary.stl"); + m_PointSetPath = GetTestDataFilePath("pointSet.mps"); } void TestTempMethods() { std::string tmpPath = mitk::IOUtil::GetTempPath(); CPPUNIT_ASSERT(!tmpPath.empty()); std::ofstream tmpFile; std::string tmpFilePath = mitk::IOUtil::CreateTemporaryFile(tmpFile); CPPUNIT_ASSERT(tmpFile && tmpFile.is_open()); CPPUNIT_ASSERT(tmpFilePath.size() > tmpPath.size()); CPPUNIT_ASSERT(tmpFilePath.substr(0, tmpPath.size()) == tmpPath); tmpFile.close(); CPPUNIT_ASSERT(std::remove(tmpFilePath.c_str()) == 0); std::string programPath = mitk::IOUtil::GetProgramPath(); CPPUNIT_ASSERT(!programPath.empty()); std::ofstream tmpFile2; std::string tmpFilePath2 = mitk::IOUtil::CreateTemporaryFile(tmpFile2, "my-XXXXXX", programPath); CPPUNIT_ASSERT(tmpFile2 && tmpFile2.is_open()); CPPUNIT_ASSERT(tmpFilePath2.size() > programPath.size()); CPPUNIT_ASSERT(tmpFilePath2.substr(0, programPath.size()) == programPath); tmpFile2.close(); CPPUNIT_ASSERT(std::remove(tmpFilePath2.c_str()) == 0); std::ofstream tmpFile3; std::string tmpFilePath3 = mitk::IOUtil::CreateTemporaryFile(tmpFile3, std::ios_base::binary, "my-XXXXXX.TXT", programPath); CPPUNIT_ASSERT(tmpFile3 && tmpFile3.is_open()); CPPUNIT_ASSERT(tmpFilePath3.size() > programPath.size()); CPPUNIT_ASSERT(tmpFilePath3.substr(0, programPath.size()) == programPath); CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 13, 3) == "my-"); CPPUNIT_ASSERT(tmpFilePath3.substr(tmpFilePath3.size() - 4) == ".TXT"); tmpFile3.close(); //CPPUNIT_ASSERT(std::remove(tmpFilePath3.c_str()) == 0) CPPUNIT_ASSERT_THROW(mitk::IOUtil::CreateTemporaryFile(tmpFile2, "XX"), mitk::Exception); std::string tmpDir = mitk::IOUtil::CreateTemporaryDirectory(); CPPUNIT_ASSERT(tmpDir.size() > tmpPath.size()); CPPUNIT_ASSERT(tmpDir.substr(0, tmpPath.size()) == tmpPath); CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir.c_str())); std::string tmpDir2 = mitk::IOUtil::CreateTemporaryDirectory("my-XXXXXX", programPath); CPPUNIT_ASSERT(tmpDir2.size() > programPath.size()); CPPUNIT_ASSERT(tmpDir2.substr(0, programPath.size()) == programPath); CPPUNIT_ASSERT(itksys::SystemTools::RemoveADirectory(tmpDir2.c_str())); } void TestLoadAndSaveImage() { mitk::Image::Pointer img1 = mitk::IOUtil::LoadImage(m_ImagePath); CPPUNIT_ASSERT( img1.IsNotNull()); std::ofstream tmpStream; std::string imagePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nrrd"); tmpStream.close(); std::string imagePath2 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffpic3d-XXXXXX.nii.gz"); tmpStream.close(); // the cases where no exception should be thrown CPPUNIT_ASSERT(mitk::IOUtil::SaveImage(img1, imagePath)); CPPUNIT_ASSERT(mitk::IOUtil::SaveBaseData(img1.GetPointer(), imagePath2)); //load data which does not exist CPPUNIT_ASSERT_THROW(mitk::IOUtil::LoadImage("fileWhichDoesNotExist.nrrd"), mitk::Exception); //delete the files after the test is done std::remove(imagePath.c_str()); std::remove(imagePath2.c_str()); mitk::Image::Pointer relativImage = mitk::ImageGenerator::GenerateGradientImage(4,4,4,1); std::string imagePath3 = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.nrrd"); tmpStream.close(); mitk::IOUtil::SaveImage(relativImage, imagePath3); CPPUNIT_ASSERT_NO_THROW(mitk::IOUtil::LoadImage(imagePath3)); std::remove(imagePath3.c_str()); } void TestLoadAndSavePointSet() { mitk::PointSet::Pointer pointset = mitk::IOUtil::LoadPointSet(m_PointSetPath); CPPUNIT_ASSERT( pointset.IsNotNull()); std::ofstream tmpStream; std::string pointSetPath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps"); tmpStream.close(); std::string pointSetPathWithDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.mps"); tmpStream.close(); std::string pointSetPathWithoutDefaultExtension = mitk::IOUtil::CreateTemporaryFile(tmpStream, "XXXXXX.xXx"); tmpStream.close(); // the cases where no exception should be thrown CPPUNIT_ASSERT(mitk::IOUtil::SavePointSet(pointset, pointSetPathWithDefaultExtension)); // test if defaultextension is inserted if no extension is present CPPUNIT_ASSERT(mitk::IOUtil::SavePointSet(pointset, pointSetPathWithoutDefaultExtension.c_str())); //delete the files after the test is done std::remove(pointSetPath.c_str()); std::remove(pointSetPathWithDefaultExtension.c_str()); std::remove(pointSetPathWithoutDefaultExtension.c_str()); } void TestLoadAndSaveSurface() { mitk::Surface::Pointer surface = mitk::IOUtil::LoadSurface(m_SurfacePath); CPPUNIT_ASSERT( surface.IsNotNull()); std::ofstream tmpStream; std::string surfacePath = mitk::IOUtil::CreateTemporaryFile(tmpStream, "diffsurface-XXXXXX.stl"); // the cases where no exception should be thrown CPPUNIT_ASSERT(mitk::IOUtil::SaveSurface(surface, surfacePath)); // test if exception is thrown as expected on unknown extsension CPPUNIT_ASSERT_THROW(mitk::IOUtil::SaveSurface(surface,"testSurface.xXx"), mitk::Exception); //delete the files after the test is done std::remove(surfacePath.c_str()); } }; MITK_TEST_SUITE_REGISTRATION(mitkIOUtil)