From bc3ac223323ac6ccdad816bb2d8d591bbf0627e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Fri, 10 Dec 2021 20:57:32 -0500 Subject: [PATCH] STYLE: Improve the `JPEG` module tests' style Improve the `JPEG` module tests' style: - Use the `ITK_TRY_EXPECT_NO_EXCEPTION` macro to avoid boilerplate code. - Use the `ITK_TEST_EXPECT_TRUE` macro to avoid boilerplate code. - Improve the style to make them more consistent (e.g. input argument checking message, endline characters in input argument checking, define the image dimensionality constant). --- .../IO/JPEG/test/itkJPEGImageIOCMYKTest.cxx | 12 ++-------- Modules/IO/JPEG/test/itkJPEGImageIOTest.cxx | 23 ++++++++----------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Modules/IO/JPEG/test/itkJPEGImageIOCMYKTest.cxx b/Modules/IO/JPEG/test/itkJPEGImageIOCMYKTest.cxx index ac6d3e7107a..b46a1206c3f 100644 --- a/Modules/IO/JPEG/test/itkJPEGImageIOCMYKTest.cxx +++ b/Modules/IO/JPEG/test/itkJPEGImageIOCMYKTest.cxx @@ -46,11 +46,7 @@ itkJPEGImageIOCMYKTest(int argc, char * argv[]) ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update()); - if (!(io->GetPixelType() == itk::CommonEnums::IOPixel::RGB)) - { - std::cout << "Expected RGB. Test failed" << std::endl; - return EXIT_FAILURE; - } + ITK_TEST_EXPECT_TRUE(io->GetPixelType() == itk::CommonEnums::IOPixel::RGB); } { @@ -66,11 +62,7 @@ itkJPEGImageIOCMYKTest(int argc, char * argv[]) ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update()); - if (!(io->GetPixelType() == itk::CommonEnums::IOPixel::VECTOR)) - { - std::cout << "Expected VECTOR. Test failed" << std::endl; - return EXIT_FAILURE; - } + ITK_TEST_EXPECT_TRUE(io->GetPixelType() == itk::CommonEnums::IOPixel::VECTOR); } std::cout << "Test finished." << std::endl; diff --git a/Modules/IO/JPEG/test/itkJPEGImageIOTest.cxx b/Modules/IO/JPEG/test/itkJPEGImageIOTest.cxx index b82f5247856..c8881482b0d 100644 --- a/Modules/IO/JPEG/test/itkJPEGImageIOTest.cxx +++ b/Modules/IO/JPEG/test/itkJPEGImageIOTest.cxx @@ -30,30 +30,25 @@ itkJPEGImageIOTest(int argc, char * argv[]) if (argc < 3) { - std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " Input Output\n"; + std::cerr << "Missing parameters." << std::endl; + std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv); + std::cerr << " inputFilename outputFilename" << std::endl; return EXIT_FAILURE; } // ATTENTION THIS IS THE PIXEL TYPE FOR // THE RESULTING IMAGE + constexpr unsigned int Dimension = 2; using PixelType = unsigned char; - using myImage = itk::Image; + using myImage = itk::Image; itk::ImageFileReader::Pointer reader = itk::ImageFileReader::New(); reader->SetFileName(argv[1]); - try - { - reader->Update(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << "exception in file reader " << std::endl; - std::cerr << e << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update()); + myImage::Pointer image = reader->GetOutput(); @@ -67,7 +62,9 @@ itkJPEGImageIOTest(int argc, char * argv[]) writer = itk::ImageFileWriter::New(); writer->SetInput(reader->GetOutput()); writer->SetFileName(argv[2]); - writer->Update(); + + ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update()); + return EXIT_SUCCESS; }