void mitkImageStatisticsCalculatorTestSuite::TestUninitializedImage() { /***************************** * loading uninitialized image to datastorage ******************************/ MITK_TEST_FOR_EXCEPTION_BEGIN(mitk::Exception) mitk::Image::Pointer image = mitk::Image::New(); mitk::DataNode::Pointer node = mitk::DataNode::New(); node->SetData(image); mitk::ImageStatisticsCalculator::Pointer is = mitk::ImageStatisticsCalculator::New(); is->ComputeStatistics(); MITK_TEST_FOR_EXCEPTION_END(mitk::Exception) }
int mitkToFImageDownsamplingFilterTest(int argc , char* argv[]) { //Defining constants const int XDIM = 127; const int YDIM = 96; const int ZDIM = 19; // always start with this MITK_TEST_BEGIN("mitkToFImageDownSamplingFilterFilter"); // create a new instance of filter and new image mitk::ToFImageDownsamplingFilter::Pointer testDownSampler = mitk::ToFImageDownsamplingFilter::New(); // make sure new filter ins't null MITK_TEST_CONDITION_REQUIRED(testDownSampler.IsNotNull(), "Testing instantiation!"); // Load ToF image MITK_INFO<<"Loading test image file: " << argv[1] << "\n"; // update with proper path and figure out how iti s passed from the test driver mitk::PicFileReader::Pointer reader = mitk::PicFileReader::New(); std::string filename = MITK_TOF_DATA_DIR; filename.append("/"); filename.append(argv[1]); reader->SetFileName(filename); reader->Update(); mitk::Image::Pointer image = reader->GetOutput(); MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(), "Testing image reading"); MITK_INFO << "Original image dimensions " << image->GetDimension (0)<<" " << image->GetDimension(1)<< " " << image->GetDimension(2) ; //call filter testDownSampler->SetInput(image); testDownSampler->SetResampledX(XDIM); testDownSampler->SetResampledY(YDIM); testDownSampler->SetResampledZ(ZDIM); if(image->GetDimension(0) >= XDIM && image->GetDimension(1)>=YDIM && image->GetDimension(2)>=ZDIM && (image->GetDimension()==2 || image->GetDimension()==3)) { testDownSampler->Update(); mitk::Image::Pointer resultImage = testDownSampler->GetOutput(); MITK_TEST_CONDITION_REQUIRED(resultImage->GetDimension(0) == XDIM && resultImage->GetDimension(1)==YDIM &&resultImage->GetDimension(2)==ZDIM, "Test result image dimensions with 3D image"); MITK_INFO << "new image dimensions " << resultImage->GetDimension (0)<<" " << resultImage->GetDimension(1)<<" " << resultImage->GetDimension(2) ; } else { MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject); testDownSampler->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject); } // Mean for debugging purposes if you want to write the resutling image to a file //mitk::PicFileWriter::Pointer writer = mitk::PicFileWriter::New(); //writer->SetInputImage( resultImage); //writer->SetFileName( "tofResult1.pic" ); //writer->Update(); // always end with this! MITK_TEST_END(); }
/** * test for "ImageWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkImageWriterTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("ImageWriter") // let's create an object of our class mitk::ImageWriter::Pointer myImageWriter = mitk::ImageWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myImageWriter.IsNotNull(),"Testing instantiation") // write your own tests here and use the macros from mitkTestingMacros.h !!! // do not write to std::cout and do not return from this function yourself! // load image MITK_TEST_CONDITION_REQUIRED(argc > 1, "File to load has been specified"); mitk::Image::Pointer image = NULL; try { MITK_TEST_OUTPUT(<< "Loading file: " << argv[1]); image = mitk::IOUtil::LoadImage( argv[1] ); } catch (itk::ExceptionObject & ex) { MITK_TEST_FAILED_MSG(<< "Exception during file loading: " << ex.GetDescription()); } MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL") std::stringstream filename_stream; #ifdef WIN32 filename_stream << "test" << _getpid(); #else filename_stream << "test" << getpid(); #endif std::string filename = filename_stream.str(); std::cout << filename << std::endl; // test set/get methods myImageWriter->SetInput(image); MITK_TEST_CONDITION_REQUIRED(myImageWriter->GetInput()==image,"test Set/GetInput()"); myImageWriter->SetFileName(filename); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFileName(),filename.c_str()),"test Set/GetFileName()"); myImageWriter->SetFilePrefix("pref"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePrefix(),"pref"),"test Set/GetFilePrefix()"); myImageWriter->SetFilePattern("pattern"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePattern(),"pattern"),"test Set/GetFilePattern()"); // write ITK .mhd image (2D and 3D only) if( image->GetDimension() <= 3 ) { try { myImageWriter->SetExtension(".mhd"); myImageWriter->Update(); mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage( AppendExtension(filename, ".mhd").c_str() ); MITK_TEST_CONDITION_REQUIRED( compareImage.IsNotNull(), "Image stored in MHD format was succesfully loaded again! "); std::string rawExtension = ".raw"; std::fstream rawPartIn; rawPartIn.open(AppendExtension(filename, ".raw").c_str()); if( !rawPartIn.is_open() ) { rawExtension = ".zraw"; rawPartIn.open(AppendExtension(filename, ".zraw").c_str()); } MITK_TEST_CONDITION_REQUIRED(rawPartIn.is_open(),"Write .raw file"); rawPartIn.close(); // delete remove(AppendExtension(filename, ".mhd").c_str()); remove(AppendExtension(filename, rawExtension.c_str()).c_str()); } catch (...) { MITK_TEST_FAILED_MSG(<< "Exception during .mhd file writing"); } } //testing more component image writing as nrrd files try { myImageWriter->SetExtension(".nrrd"); myImageWriter->Update(); std::fstream fin; mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage(AppendExtension(filename, ".nrrd").c_str()); MITK_TEST_CONDITION_REQUIRED(compareImage.IsNotNull(), "Image stored in NRRD format was succesfully loaded again"); fin.close(); remove(AppendExtension(filename, ".nrrd").c_str()); } catch(...) { MITK_TEST_FAILED_MSG(<< "Exception during .nrrd file writing"); } mitk::Image::Pointer singleSliceImage = NULL; if( image->GetDimension() == 3 ) { mitk::ExtractSliceFilter::Pointer extractFilter = mitk::ExtractSliceFilter::New(); extractFilter->SetInput( image ); extractFilter->SetWorldGeometry( image->GetSlicedGeometry()->GetGeometry2D(0) ); extractFilter->Update(); singleSliceImage = extractFilter->GetOutput(); // test 3D writing in format supporting only 2D myImageWriter->SetExtension(".png"); myImageWriter->Update(); // test images unsigned int foundImagesCount = 0; for( unsigned int i=0; i< image->GetDimension(2); i++) { std::stringstream series_filenames; series_filenames << filename << "." << i+1 << ".png"; mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage( series_filenames.str() ); if( compareImage.IsNotNull() ) { foundImagesCount++; MITK_TEST_CONDITION(CompareImageMetaData( singleSliceImage, compareImage ), "Image meta data unchanged after writing and loading again. "); } remove( series_filenames.str().c_str() ); } MITK_TEST_CONDITION( foundImagesCount == image->GetDimension(2), "All 2D-Slices of a 3D image were stored correctly as PNGs."); } else if( image->GetDimension() == 2 ) { singleSliceImage = image; } // test 2D writing if( singleSliceImage.IsNotNull() ) { try { myImageWriter->SetInput( singleSliceImage ); myImageWriter->SetExtension(".png"); myImageWriter->Update(); mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage( AppendExtension(filename, ".png").c_str()); MITK_TEST_CONDITION_REQUIRED( compareImage.IsNotNull(), "Image stored in PNG format was succesfully loaded again"); MITK_TEST_CONDITION_REQUIRED( CompareImageMetaData(singleSliceImage, compareImage ), "Image meta data unchanged after writing and loading again. "); remove(AppendExtension(filename, ".png").c_str()); } catch(itk::ExceptionObject &e) { MITK_TEST_FAILED_MSG(<< "Exception during .png file writing: " << e.what() ); } } // test for exception handling try { MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myImageWriter->SetInput(image); myImageWriter->SetFileName("/usr/bin"); myImageWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown MITK_TEST_FAILED_MSG(<< "Wrong exception (i.e. no itk:Exception) caught during write"); } // always end with this! MITK_TEST_END(); }
/** * Simple example for a test for the (non-existent) class "PicFileWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkPicFileWriterTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("PicFileWriter") // let's create an object of our class mitk::PicFileWriter::Pointer myPicFileWriter = mitk::PicFileWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myPicFileWriter.IsNotNull(),"Testing instantiation") // load image std::cout << "Loading file: " << std::flush; if(argc==0) { std::cout<<"no file specified [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::Image::Pointer image = NULL; try{ // test for exception handling of NULL image std::cout << "Testing handling of NULL image " << std::flush; MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myPicFileWriter->SetInputImage(image); myPicFileWriter->SetFileName("/usr/bin"); myPicFileWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { std::cout << "Success: Writer warns on NULL image." << std::endl; } mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); try { std::cout<<argv[1]<<std::endl; factory->SetFileName( argv[1] ); factory->Update(); if(factory->GetNumberOfOutputs()<1) { std::cout<<"file could not be loaded [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::DataNode::Pointer node = factory->GetOutput( 0 ); image = dynamic_cast<mitk::Image*>(node->GetData()); if(image.IsNull()) { std::cout<<"file "<< argv[1]<< "is not an image - test will not be applied [PASSED]"<<std::endl; std::cout<<"[TEST DONE]"<<std::endl; return EXIT_SUCCESS; } } catch ( itk::ExceptionObject & ex ) { std::cout << "Exception: " << ex << "[FAILED]" << std::endl; return EXIT_FAILURE; } MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL") try{ // test for exception handling MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myPicFileWriter->SetInputImage(image); myPicFileWriter->SetFileName("/usr/bin"); myPicFileWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown std::cout << "Wrong exception (i.e. no itk:Exception) caught during write [FAILED]" << std::endl; return EXIT_FAILURE; } // always end with this! MITK_TEST_END() }
/** * test for "ImageWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkImageWriterTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("ImageWriter") // let's create an object of our class mitk::ImageWriter::Pointer myImageWriter = mitk::ImageWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myImageWriter.IsNotNull(),"Testing instantiation") // write your own tests here and use the macros from mitkTestingMacros.h !!! // do not write to std::cout and do not return from this function yourself! // load image MITK_TEST_CONDITION_REQUIRED(argc != 0, "File to load has been specified"); mitk::Image::Pointer image = NULL; mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); try { MITK_TEST_OUTPUT(<< "Loading file: " << argv[1]); factory->SetFileName( argv[1] ); factory->Update(); MITK_TEST_CONDITION_REQUIRED(factory->GetNumberOfOutputs() > 0, "file loaded"); mitk::DataNode::Pointer node = factory->GetOutput( 0 ); image = dynamic_cast<mitk::Image*>(node->GetData()); if(image.IsNull()) { std::cout<<"file "<< argv[1]<< "is not an image - test will not be applied."<<std::endl; std::cout<<"[TEST DONE]"<<std::endl; return EXIT_SUCCESS; } } catch (itk::ExceptionObject & ex) { MITK_TEST_FAILED_MSG(<< "Exception during file loading: " << ex.GetDescription()); } MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL") std::stringstream filename_stream; #ifdef WIN32 filename_stream << "test" << _getpid(); #else filename_stream << "test" << getpid(); #endif std::string filename = filename_stream.str(); // test set/get methods myImageWriter->SetInput(image); MITK_TEST_CONDITION_REQUIRED(myImageWriter->GetInput()==image,"test Set/GetInput()"); myImageWriter->SetFileName(filename); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFileName(),filename.c_str()),"test Set/GetFileName()"); myImageWriter->SetFilePrefix("pref"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePrefix(),"pref"),"test Set/GetFilePrefix()"); myImageWriter->SetFilePattern("pattern"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePattern(),"pattern"),"test Set/GetFilePattern()"); // write ITK .mhd image (2D and 3D only) if( image->GetDimension() <= 3 ) { try { myImageWriter->SetExtension(".mhd"); myImageWriter->Update(); std::fstream fin, fin2; fin.open(AppendExtension(filename, ".mhd").c_str(),std::ios::in); std::string rawExtension = ".raw"; fin2.open(AppendExtension(filename, ".raw").c_str(),std::ios::in); if( !fin2.is_open() ) { rawExtension = ".zraw"; fin2.open(AppendExtension(filename, ".zraw").c_str(),std::ios::in); } MITK_TEST_CONDITION_REQUIRED(fin.is_open(),"Write .mhd file"); MITK_TEST_CONDITION_REQUIRED(fin2.is_open(),"Write .raw file"); fin.close(); fin2.close(); remove(AppendExtension(filename, ".mhd").c_str()); remove(AppendExtension(filename, rawExtension.c_str()).c_str()); } catch (...) { MITK_TEST_FAILED_MSG(<< "Exception during .mhd file writing"); } } //testing more component image writing as nrrd files try { myImageWriter->SetExtension(".nrrd"); myImageWriter->Update(); std::fstream fin; fin.open(AppendExtension(filename, ".nrrd").c_str(),std::ios::in); MITK_TEST_CONDITION_REQUIRED(fin.is_open(),"Write .nrrd file"); fin.close(); remove(AppendExtension(filename, ".nrrd").c_str()); } catch(...) { MITK_TEST_FAILED_MSG(<< "Exception during .nrrd file writing"); } // test for exception handling try { MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myImageWriter->SetInput(image); myImageWriter->SetFileName("/usr/bin"); myImageWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown MITK_TEST_FAILED_MSG(<< "Wrong exception (i.e. no itk:Exception) caught during write"); } // always end with this! MITK_TEST_END(); }
/** * Test for the class "mitkPointSetFileWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkPointSetWriterTest(int /* argc */, char* /*argv*/[]) { // always start with this! MITK_TEST_BEGIN("PointSetWriter") // let's create an object of our class mitk::PointSetWriter::Pointer myPointSetWriter = mitk::PointSetWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myPointSetWriter.IsNotNull(),"Testing instantiation") // write your own tests here and use the macros from mitkTestingMacros.h !!! // do not write to std::cout and do not return from this function yourself! // create pointSet srand(time(NULL)); mitk::PointSet::Pointer pointSet = mitk::PointSet::New(); int numberOfPoints = rand()%100; for (int i=0; i<=numberOfPoints+1;i++) { mitk::Point3D point; point[0] = rand()%1000; point[1] = rand()%1000; point[2] = rand()%1000; pointSet->SetPoint(i,point); } MITK_TEST_CONDITION_REQUIRED(pointSet.IsNotNull(),"PointSet creation") try{ // test for exception handling MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myPointSetWriter->SetInput(pointSet); myPointSetWriter->SetFileName("/usr/bin"); myPointSetWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown std::cout << "Wrong exception (i.e. no itk:Exception) caught during write [FAILED]" << std::endl; return EXIT_FAILURE; } /* MITK_TEST_OUTPUT( << "Check if filename can be set correctly: "); myPointSetWriter->SetFileName("filename"); const char * filename = myPointSetWriter->GetFileName(); MITK_TEST_CONDITION_REQUIRED(std::string("filename") == "filename", "Filename set correctly?"); MITK_TEST_OUTPUT( << "Check if prefix can be set correctly: "); myPointSetWriter->SetFilePrefix("pre"); const char * prefix = myPointSetWriter->GetFilePrefix(); MITK_TEST_CONDITION_REQUIRED(std::string("pre") == prefix, "Prefix set correctly?"); MITK_TEST_OUTPUT( << "Check if pattern can be set correctly: "); myPointSetWriter->SetFilePattern("pattern"); const char * pattern = myPointSetWriter->GetFilePattern(); MITK_TEST_CONDITION_REQUIRED(std::string("pattern") == prefix, "Pattern set correctly?"); */ MITK_TEST_OUTPUT( << "Check if input can be set correctly: "); myPointSetWriter->SetInput(pointSet); mitk::PointSet::Pointer pointSet2 = mitk::PointSet::New(); pointSet2 = myPointSetWriter->GetInput(); MITK_TEST_CONDITION_REQUIRED( pointSet->GetSize() == pointSet2->GetSize(), "Pointsets have unequal size" ); for(int i=0; i<pointSet->GetSize(); i++) { mitk::Point3D p1 = pointSet->GetPoint(i); mitk::Point3D p2 = pointSet2->GetPoint(i); MITK_TEST_CONDITION_REQUIRED( p1[0] == p2[0] && p1[0] == p2[0] && p1[0] == p2[0], "Pointsets aren't equal" ); } std::vector< std::string > extensions = myPointSetWriter->GetPossibleFileExtensions(); // always end with this! MITK_TEST_END() }
/** * Simple example for a test for the (non-existent) class "PicFileWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkPicFileWriterTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("PicFileWriter") // let's create an object of our class mitk::PicFileWriter::Pointer myPicFileWriter = mitk::PicFileWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myPicFileWriter.IsNotNull(),"Testing instantiation") // load image std::cout << "Loading file: " << std::flush; if(argc==0) { std::cout<<"no file specified [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::Image::Pointer image = NULL; try{ // test for exception handling of NULL image std::cout << "Testing handling of NULL image " << std::flush; MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myPicFileWriter->SetInputImage(image); myPicFileWriter->SetFileName("/usr/bin"); myPicFileWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { std::cout << "Success: Writer warns on NULL image." << std::endl; } mitk::DataNodeFactory::Pointer factory = mitk::DataNodeFactory::New(); try { std::cout<<argv[1]<<std::endl; factory->SetFileName( argv[1] ); factory->Update(); if(factory->GetNumberOfOutputs()<1) { std::cout<<"file could not be loaded [FAILED]"<<std::endl; return EXIT_FAILURE; } mitk::DataNode::Pointer node = factory->GetOutput( 0 ); image = dynamic_cast<mitk::Image*>(node->GetData()); if(image.IsNull()) { std::cout<<"file "<< argv[1]<< "is not an image - test will not be applied [PASSED]"<<std::endl; std::cout<<"[TEST DONE]"<<std::endl; return EXIT_SUCCESS; } } catch ( itk::ExceptionObject & ex ) { std::cout << "Exception: " << ex << "[FAILED]" << std::endl; return EXIT_FAILURE; } MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL") try{ // test for exception handling MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myPicFileWriter->SetInputImage(image); myPicFileWriter->SetFileName("/usr/bin"); myPicFileWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown std::cout << "Wrong exception (i.e. no itk:Exception) caught during write [FAILED]" << std::endl; return EXIT_FAILURE; } unsigned char data_channel1[8] = {0,1,2,3,4,5,6,7}; unsigned char data_channel2[8] = {10,11,12,13,14,15,16,17}; unsigned int dims[2] = {4,2}; // create a two-channel test image mitk::Image::Pointer mcImage = mitk::Image::New(); mcImage->Initialize( mitk::MakeScalarPixelType<unsigned char>(), 2, dims, 2); mcImage->SetChannel( data_channel1, 0); mcImage->SetChannel( data_channel2, 1); // create a file name that can be saved so that the writer does not throw exception because of unwriteable location std::string filename = std::string( MITK_TEST_OUTPUT_DIR ) + "MultiChannel.pic"; MITK_TEST_CONDITION_REQUIRED(mcImage.IsNotNull(),"The multi-channel image for testing is not NULL") try{ // test for exception handling MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) mitk::PicFileWriter::Pointer myPicFileWriter2 = mitk::PicFileWriter::New(); myPicFileWriter2->SetInputImage(mcImage); myPicFileWriter2->SetFileName(filename); myPicFileWriter2->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown std::cout << "Wrong exception (i.e. no itk:Exception) caught during write [FAILED]" << std::endl; return EXIT_FAILURE; } // always end with this! MITK_TEST_END() }
int mitkImageAccessorTest(int argc, char *argv[]) { MITK_TEST_BEGIN("mitkImageAccessorTest"); std::cout << "Loading file: "; if (argc == 0) { std::cout << "no file specified [FAILED]" << std::endl; return EXIT_FAILURE; } mitk::Image::Pointer image = NULL; try { image = mitk::IOUtil::LoadImage(std::string(argv[1])); if (image.IsNull()) { MITK_TEST_FAILED_MSG(<< "file could not be loaded [FAILED]") } } catch (itk::ExceptionObject &ex) { MITK_TEST_FAILED_MSG(<< "Exception: " << ex << "[FAILED]") } // CHECK INAPPROPRIATE AND SPECIAL USAGE // recursive mutex lock MITK_TEST_OUTPUT(<< "Testing a recursive mutex lock attempt, should end in an exception ..."); MITK_TEST_FOR_EXCEPTION_BEGIN(mitk::Exception) mitk::ImageWriteAccessor first(image); mitk::ImageReadAccessor second(image); MITK_TEST_FOR_EXCEPTION_END(mitk::Exception) // ignore lock mechanism in read accessor try { mitk::ImageWriteAccessor first(image); mitk::ImageReadAccessor second(image, NULL, mitk::ImageAccessorBase::IgnoreLock); MITK_TEST_CONDITION_REQUIRED(true, "Testing the option flag \"IgnoreLock\" in ReadAccessor"); } catch (const mitk::Exception & /*e*/) { MITK_TEST_CONDITION_REQUIRED(false, "Ignoring the lock mechanism leads to exception."); } // CREATE THREADS image->GetGeometry()->Initialize(); itk::MultiThreader::Pointer threader = itk::MultiThreader::New(); unsigned int noOfThreads = 100; // initialize barrier itk::Barrier::Pointer barrier = itk::Barrier::New(); barrier->Initialize(noOfThreads + 1); // add one for we stop the base thread when the worker threads are processing ThreadData *threadData = new ThreadData; threadData->m_Barrier = barrier; threadData->m_NoOfThreads = noOfThreads; threadData->data = image; threadData->m_Successful = true; // spawn threads for (unsigned int i = 0; i < noOfThreads; ++i) { threader->SpawnThread(ThreadMethod, threadData); } // stop the base thread during worker thread execution barrier->Wait(); // terminate threads for (unsigned int j = 0; j < noOfThreads; ++j) { threader->TerminateThread(j); } bool TestSuccessful = threadData->m_Successful; delete threadData; MITK_TEST_CONDITION_REQUIRED(TestSuccessful, "Testing image access from multiple threads"); MITK_TEST_END(); }
/** * test for "ImageWriter". * * argc and argv are the command line parameters which were passed to * the ADD_TEST command in the CMakeLists.txt file. For the automatic * tests, argv is either empty for the simple tests or contains the filename * of a test image for the image tests (see CMakeLists.txt). */ int mitkImageWriterTest(int argc , char* argv[]) { // always start with this! MITK_TEST_BEGIN("ImageWriter") // let's create an object of our class mitk::ImageWriter::Pointer myImageWriter = mitk::ImageWriter::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myImageWriter.IsNotNull(),"Testing instantiation") // write your own tests here and use the macros from mitkTestingMacros.h !!! // do not write to std::cout and do not return from this function yourself! // load image MITK_TEST_CONDITION_REQUIRED(argc > 1, "File to load has been specified"); mitk::Image::Pointer image = NULL; try { MITK_TEST_OUTPUT(<< "Loading file: " << argv[1]); image = mitk::IOUtil::LoadImage( argv[1] ); } catch (itk::ExceptionObject & ex) { MITK_TEST_FAILED_MSG(<< "Exception during file loading: " << ex.GetDescription()); } MITK_TEST_CONDITION_REQUIRED(image.IsNotNull(),"loaded image not NULL") std::stringstream filename_stream; #ifdef WIN32 filename_stream << "test" << _getpid(); #else filename_stream << "test" << getpid(); #endif std::string filename = filename_stream.str(); std::cout << filename << std::endl; // test set/get methods myImageWriter->SetInput(image); MITK_TEST_CONDITION_REQUIRED(myImageWriter->GetInput()==image,"test Set/GetInput()"); myImageWriter->SetFileName(filename); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFileName(),filename.c_str()),"test Set/GetFileName()"); myImageWriter->SetFilePrefix("pref"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePrefix(),"pref"),"test Set/GetFilePrefix()"); myImageWriter->SetFilePattern("pattern"); MITK_TEST_CONDITION_REQUIRED(!strcmp(myImageWriter->GetFilePattern(),"pattern"),"test Set/GetFilePattern()"); // write ITK .mhd image (2D and 3D only) if( image->GetDimension() <= 3 ) { try { myImageWriter->SetExtension(".mhd"); myImageWriter->Update(); mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage( AppendExtension(filename, ".mhd").c_str() ); MITK_TEST_CONDITION_REQUIRED( compareImage.IsNotNull(), "Image stored in MHD format was succesfully loaded again! "); std::string rawExtension = ".raw"; std::fstream rawPartIn; rawPartIn.open(AppendExtension(filename, ".raw").c_str()); if( !rawPartIn.is_open() ) { rawExtension = ".zraw"; rawPartIn.open(AppendExtension(filename, ".zraw").c_str()); } MITK_TEST_CONDITION_REQUIRED(rawPartIn.is_open(),"Write .raw file"); rawPartIn.close(); // delete remove(AppendExtension(filename, ".mhd").c_str()); remove(AppendExtension(filename, rawExtension.c_str()).c_str()); } catch (...) { MITK_TEST_FAILED_MSG(<< "Exception during .mhd file writing"); } } //testing more component image writing as nrrd files try { myImageWriter->SetExtension(".nrrd"); myImageWriter->Update(); std::fstream fin; mitk::Image::Pointer compareImage = mitk::IOUtil::LoadImage(AppendExtension(filename, ".nrrd").c_str()); MITK_TEST_CONDITION_REQUIRED(compareImage.IsNotNull(), "Image stored in NRRD format was succesfully loaded again"); fin.close(); remove(AppendExtension(filename, ".nrrd").c_str()); } catch(...) { MITK_TEST_FAILED_MSG(<< "Exception during .nrrd file writing"); } TestPictureWriting(image, filename, ".png"); TestPictureWriting(image, filename, ".jpg"); TestPictureWriting(image, filename, ".tiff"); TestPictureWriting(image, filename, ".bmp"); // test for exception handling try { MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myImageWriter->SetInput(image); myImageWriter->SetFileName("/usr/bin"); myImageWriter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) } catch(...) { //this means that a wrong exception (i.e. no itk:Exception) has been thrown MITK_TEST_FAILED_MSG(<< "Wrong exception (i.e. no itk:Exception) caught during write"); } // always end with this! MITK_TEST_END(); }
/**Documentation * test for the class "NavigationDataVisualizationByBaseDataTransformFilter". */ int mitkCameraVisualizationTest(int /* argc */, char* /*argv*/[]) { MITK_TEST_BEGIN("CameraVisualization") // let's create an object of our class mitk::CameraVisualization::Pointer myFilter = mitk::CameraVisualization::New(); // first test: did this work? // using MITK_TEST_CONDITION_REQUIRED makes the test stop after failure, since // it makes no sense to continue without an object. MITK_TEST_CONDITION_REQUIRED(myFilter.IsNotNull(),"Testing instantiation"); /* create helper objects: navigation data with position as origin, zero quaternion, zero error and data valid */ srand(time(NULL)); // generate a random position for the navigation data mitk::NavigationData::PositionType position; position[0] = rand()%1000; position[1] = rand()%1000; position[2] = rand()%1000; // generate a random orientation for the navigation data mitk::NavigationData::OrientationType orientation; orientation[0] = (rand()%1000)/1000.0; orientation[1] = (rand()%1000)/1000.0; orientation[2] = (rand()%1000)/1000.0; orientation[3] = (rand()%1000)/1000.0; // generate a random error for the navigation data mitk::ScalarType error = rand()%10; // data valid flag of navigation data int val = rand()%2; bool valid(0); // this was uninitialized. how was this test ever meant to work?? if (val==0) { valid=false; } else if (val==1) { valid=true; } // set parameters of navigation data mitk::NavigationData::Pointer nd1 = mitk::NavigationData::New(); nd1->SetPosition(position); nd1->SetOrientation(orientation); nd1->SetPositionAccuracy(error); nd1->SetDataValid(valid); // create renderer vtkRenderWindow* renderWindow = vtkRenderWindow::New(); mitk::VtkPropRenderer::Pointer renderer = mitk::VtkPropRenderer::New( "TestRenderer",renderWindow, mitk::RenderingManager::GetInstance() ); myFilter->SetInput(nd1); MITK_TEST_CONDITION(myFilter->GetInput() == nd1, "Testing Set-/GetInput() input 1"); // test for exception if renderer not set MITK_TEST_FOR_EXCEPTION_BEGIN(itk::ExceptionObject) myFilter->Update(); MITK_TEST_FOR_EXCEPTION_END(itk::ExceptionObject) // set renderer myFilter->SetRenderer(renderer); //Update filter myFilter->Update(); //Delete renderWindow correctly renderWindow->Delete(); // always end with this! MITK_TEST_END(); }