From ba0bf14e380a213438dbbdc0c80dd7fb9c669288 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Wed, 15 Apr 2026 12:12:18 -0500 Subject: [PATCH 1/5] ENH: Convert itkCompensatedSummationTest to itkCompensatedSummationGTest --- Modules/Core/Common/test/CMakeLists.txt | 8 +-- ...t.cxx => itkCompensatedSummationGTest.cxx} | 65 +++---------------- 2 files changed, 11 insertions(+), 62 deletions(-) rename Modules/Core/Common/test/{itkCompensatedSummationTest.cxx => itkCompensatedSummationGTest.cxx} (52%) diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 6a7a3636bef..7de0edee97d 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -129,7 +129,6 @@ set( itkDataObjectAndProcessObjectTest.cxx itkOptimizerParametersTest.cxx itkImageVectorOptimizerParametersHelperTest.cxx - itkCompensatedSummationTest.cxx itkCompensatedSummationTest2.cxx itkImageRegionConstIteratorWithOnlyIndexTest.cxx itkImageRandomConstIteratorWithOnlyIndexTest.cxx @@ -1288,12 +1287,6 @@ itk_add_test( ITKCommon2TestDriver itkImageVectorOptimizerParametersHelperTest ) -itk_add_test( - NAME itkCompensatedSummationTest - COMMAND - ITKCommon2TestDriver - itkCompensatedSummationTest -) itk_add_test( NAME itkCompensatedSummationTest2 COMMAND @@ -1525,6 +1518,7 @@ set( itkByteSwapGTest.cxx itkCommandObserverObjectGTest.cxx itkCommonTypeTraitsGTest.cxx + itkCompensatedSummationGTest.cxx itkConnectedImageNeighborhoodShapeGTest.cxx itkConstantBoundaryImageNeighborhoodPixelAccessPolicyGTest.cxx itkCopyGTest.cxx diff --git a/Modules/Core/Common/test/itkCompensatedSummationTest.cxx b/Modules/Core/Common/test/itkCompensatedSummationGTest.cxx similarity index 52% rename from Modules/Core/Common/test/itkCompensatedSummationTest.cxx rename to Modules/Core/Common/test/itkCompensatedSummationGTest.cxx index dc99b65ab6b..9059dc62707 100644 --- a/Modules/Core/Common/test/itkCompensatedSummationTest.cxx +++ b/Modules/Core/Common/test/itkCompensatedSummationGTest.cxx @@ -16,19 +16,13 @@ * *=========================================================================*/ #include "itkCompensatedSummation.h" -#include "itkStdStreamStateSave.h" +#include "itkGTest.h" #include "itkMath.h" #include "itkMersenneTwisterRandomVariateGenerator.h" -int -itkCompensatedSummationTest(int, char *[]) +TEST(CompensatedSummation, ConvertedLegacyTest) { - // Save the format stream variables for std::cout - // They will be restored when coutState goes out of scope - // scope. - const itk::StdStreamStateSave coutState(std::cout); - using FloatType = float; constexpr long seedValue{ 17 }; @@ -56,66 +50,27 @@ itkCompensatedSummationTest(int, char *[]) const FloatType accumulatorMean = accumulatorSum / static_cast(accumSize); const FloatType accumulatorError = itk::Math::Absolute(accumulatorMean - expectedMean); - std::cout << "The expected mean is: " << expectedMean << std::endl; - - std::cout << "The vanilla sum is: " << vanillaSum << std::endl; - std::cout << "The vanilla mean is: " << vanillaMean << std::endl; - std::cout << "The vanilla error is: " << vanillaError << std::endl; - - std::cout << "The accumulator sum is: " << accumulatorSum << std::endl; - std::cout << "The accumulator mean is: " << accumulatorMean << std::endl; - std::cout << "The accumulator error is: " << accumulatorError << std::endl; - - if (vanillaError <= accumulatorError || accumulatorError > 1.0e-4) - { - std::cerr << "The compensated summation did not compensate well (crazy compiler flags?)." << std::endl; - return EXIT_FAILURE; - } + EXPECT_LT(accumulatorError, vanillaError); + EXPECT_LE(accumulatorError, 1.0e-4); // exercise other methods const CompensatedSummationType floatAccumulatorCopy = floatAccumulator; - if (itk::Math::NotExactlyEquals(floatAccumulatorCopy.GetSum(), floatAccumulator.GetSum())) - { - std::cerr << "The copy constructor failed." << std::endl; - return EXIT_FAILURE; - } + EXPECT_EQ(floatAccumulatorCopy.GetSum(), floatAccumulator.GetSum()); const CompensatedSummationType floatAccumulatorCopy2 = floatAccumulator; - if (itk::Math::NotExactlyEquals(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum())) - { - std::cerr << "The assignment operator failed." << std::endl; - return EXIT_FAILURE; - } + EXPECT_EQ(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum()); floatAccumulator += randomNumber; floatAccumulator -= randomNumber; - if (itk::Math::NotAlmostEquals(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum())) - { - std::cerr << "The operator+= and operator-= are not reversible." << std::endl; - return EXIT_FAILURE; - } + EXPECT_FLOAT_EQ(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum()); floatAccumulator *= randomNumber; floatAccumulator /= randomNumber; - if (itk::Math::NotAlmostEquals(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum())) - { - std::cerr << "The operator*= and operator/= are not reversible." << std::endl; - return EXIT_FAILURE; - } + EXPECT_FLOAT_EQ(floatAccumulatorCopy2.GetSum(), floatAccumulator.GetSum()); floatAccumulator.ResetToZero(); - if (itk::Math::NotAlmostEquals(floatAccumulator.GetSum(), FloatType{})) - { - std::cerr << "GetSize() did return the correct value!" << std::endl; - return EXIT_FAILURE; - } + EXPECT_FLOAT_EQ(floatAccumulator.GetSum(), FloatType{}); floatAccumulator = 2.0; - if (floatAccumulator.GetSum() != 2.0) - { - std::cerr << "operator= did not set the value." << std::endl; - return EXIT_FAILURE; - } - - return EXIT_SUCCESS; + EXPECT_EQ(floatAccumulator.GetSum(), 2.0); } From 8a9bcc41d7c7ebb24d6b5182f5a660afb8abf9dc Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Wed, 15 Apr 2026 12:20:03 -0500 Subject: [PATCH 2/5] ENH: Convert itkPriorityQueueTest to itkPriorityQueueGTest --- Modules/Core/Common/test/CMakeLists.txt | 8 +--- ...ueueTest.cxx => itkPriorityQueueGTest.cxx} | 38 ++++--------------- 2 files changed, 8 insertions(+), 38 deletions(-) rename Modules/Core/Common/test/{itkPriorityQueueTest.cxx => itkPriorityQueueGTest.cxx} (65%) diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 7de0edee97d..2165617310e 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -46,7 +46,6 @@ set( itkPointSetTest.cxx itkPointSetToImageFilterTest1.cxx itkPointSetToImageFilterTest2.cxx - itkPriorityQueueTest.cxx itkRealTimeClockTest.cxx itkRealTimeIntervalTest.cxx itkRealTimeStampTest.cxx @@ -980,12 +979,6 @@ itk_add_test( ITKCommon1TestDriver itkPhasedArray3DSpecialCoordinatesImageTest ) -itk_add_test( - NAME itkPriorityQueueTest - COMMAND - ITKCommon1TestDriver - itkPriorityQueueTest -) itk_add_test( NAME itkRealTimeClockTest COMMAND @@ -1573,6 +1566,7 @@ set( itkPointGTest.cxx itkPointSetGTest.cxx itkPrintHelperGTest.cxx + itkPriorityQueueGTest.cxx itkRGBAPixelGTest.cxx itkRGBPixelGTest.cxx itkShapedImageNeighborhoodRangeGTest.cxx diff --git a/Modules/Core/Common/test/itkPriorityQueueTest.cxx b/Modules/Core/Common/test/itkPriorityQueueGTest.cxx similarity index 65% rename from Modules/Core/Common/test/itkPriorityQueueTest.cxx rename to Modules/Core/Common/test/itkPriorityQueueGTest.cxx index 6166bae003a..e4352fa7a24 100644 --- a/Modules/Core/Common/test/itkPriorityQueueTest.cxx +++ b/Modules/Core/Common/test/itkPriorityQueueGTest.cxx @@ -15,12 +15,12 @@ * limitations under the License. * *=========================================================================*/ +#include "itkPriorityQueueContainer.h" +#include "itkGTest.h" #include "itkMath.h" -#include "itkPriorityQueueContainer.h" -int -itkPriorityQueueTest(int, char *[]) +TEST(PriorityQueue, ConvertedLegacyTest) { using ElementIdentifier = itk::IdentifierType; @@ -30,8 +30,6 @@ itkPriorityQueueTest(int, char *[]) using MinPQType = itk::PriorityQueueContainer; auto min_priority_queue = MinPQType::New(); - std::cout << min_priority_queue->GetNameOfClass() << std::endl; - using MaxPQType = itk::PriorityQueueContainer; auto max_priority_queue = MaxPQType::New(); @@ -49,38 +47,19 @@ itkPriorityQueueTest(int, char *[]) it = sequence.begin(); i = sequence.size(); - std::cout << "Min Priority Queue "; while (!min_priority_queue->Empty()) { - if (itk::Math::NotAlmostEquals(min_priority_queue->Peek().m_Priority, *it)) - { - std::cout << min_priority_queue->Peek().m_Priority << ' ' << *it << std::endl; - return EXIT_FAILURE; - } - if (min_priority_queue->Size() != i) - { - std::cout << "Size " << min_priority_queue->Size() << ' ' << i << std::endl; - return EXIT_FAILURE; - } + EXPECT_DOUBLE_EQ(min_priority_queue->Peek().m_Priority, *it); + EXPECT_EQ(min_priority_queue->Size(), i); min_priority_queue->Pop(); ++it; --i; } - std::cout << "OK" << std::endl; - std::cout << "Max Priority Queue "; while (!max_priority_queue->Empty()) { - if (itk::Math::NotAlmostEquals(max_priority_queue->Peek().m_Priority, sequence.back())) - { - std::cout << max_priority_queue->Peek().m_Priority << ' ' << sequence.back() << std::endl; - return EXIT_FAILURE; - } - if (max_priority_queue->Size() != sequence.size()) - { - std::cout << "Size " << max_priority_queue->Size() << ' ' << sequence.size() << std::endl; - return EXIT_FAILURE; - } + EXPECT_DOUBLE_EQ(max_priority_queue->Peek().m_Priority, sequence.back()); + EXPECT_EQ(max_priority_queue->Size(), sequence.size()); max_priority_queue->Pop(); if (max_priority_queue->Empty()) { @@ -88,7 +67,4 @@ itkPriorityQueueTest(int, char *[]) } sequence.pop_back(); } - std::cout << "OK" << std::endl; - - return EXIT_SUCCESS; } From 3ac77ca6e1f7089c671e51215dcbf00bbaf16806 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Wed, 15 Apr 2026 12:49:06 -0500 Subject: [PATCH 3/5] ENH: Rename itkRealTimeInterval/StampTest to GTest --- .../{itkRealTimeIntervalTest.cxx => itkRealTimeIntervalGTest.cxx} | 0 .../test/{itkRealTimeStampTest.cxx => itkRealTimeStampGTest.cxx} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Modules/Core/Common/test/{itkRealTimeIntervalTest.cxx => itkRealTimeIntervalGTest.cxx} (100%) rename Modules/Core/Common/test/{itkRealTimeStampTest.cxx => itkRealTimeStampGTest.cxx} (100%) diff --git a/Modules/Core/Common/test/itkRealTimeIntervalTest.cxx b/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx similarity index 100% rename from Modules/Core/Common/test/itkRealTimeIntervalTest.cxx rename to Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx diff --git a/Modules/Core/Common/test/itkRealTimeStampTest.cxx b/Modules/Core/Common/test/itkRealTimeStampGTest.cxx similarity index 100% rename from Modules/Core/Common/test/itkRealTimeStampTest.cxx rename to Modules/Core/Common/test/itkRealTimeStampGTest.cxx From ae69f2bb8c881eea9c9d88d1042253e64e8ba533 Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Wed, 15 Apr 2026 12:49:59 -0500 Subject: [PATCH 4/5] ENH: Convert itkRealTimeIntervalTest to GTest format --- Modules/Core/Common/test/CMakeLists.txt | 8 +- .../Common/test/itkRealTimeIntervalGTest.cxx | 173 +++++++----------- 2 files changed, 67 insertions(+), 114 deletions(-) diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 2165617310e..0abe9cf17b8 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -47,7 +47,6 @@ set( itkPointSetToImageFilterTest1.cxx itkPointSetToImageFilterTest2.cxx itkRealTimeClockTest.cxx - itkRealTimeIntervalTest.cxx itkRealTimeStampTest.cxx itkRGBPixelTest.cxx itkSimpleFilterWatcherTest.cxx @@ -991,12 +990,6 @@ itk_add_test( ITKCommon1TestDriver itkRealTimeStampTest ) -itk_add_test( - NAME itkRealTimeIntervalTest - COMMAND - ITKCommon1TestDriver - itkRealTimeIntervalTest -) itk_add_test( NAME itkTimeProbeTest COMMAND @@ -1567,6 +1560,7 @@ set( itkPointSetGTest.cxx itkPrintHelperGTest.cxx itkPriorityQueueGTest.cxx + itkRealTimeIntervalGTest.cxx itkRGBAPixelGTest.cxx itkRGBPixelGTest.cxx itkShapedImageNeighborhoodRangeGTest.cxx diff --git a/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx b/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx index ce16d2536f1..208f9b8a255 100644 --- a/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx +++ b/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx @@ -15,59 +15,41 @@ * limitations under the License. * *=========================================================================*/ - -#include #include "itkRealTimeInterval.h" -#include "itkMacro.h" -#include "itkNumericTraits.h" +#include "itkGTest.h" + #include "itkMath.h" +#include "itkNumericTraits.h" + +namespace +{ +void +CheckForValue(double a, double b) +{ + double eps = 4.0 * itk::NumericTraits::epsilon(); + ITK_GCC_PRAGMA_PUSH + ITK_GCC_SUPPRESS_Wfloat_equal + eps = (b == 0.0) ? eps : itk::Math::Absolute(b * eps); + ITK_GCC_PRAGMA_POP + EXPECT_LE(itk::Math::Absolute(a - b), eps); +} +} // namespace -#define CHECK_FOR_VALUE(a, b) \ - { \ - double eps = 4.0 * itk::NumericTraits::epsilon(); \ - ITK_GCC_PRAGMA_PUSH \ - ITK_GCC_SUPPRESS_Wfloat_equal \ - eps = (b == 0.0) ? eps : itk::Math::Absolute(b * eps); \ - ITK_GCC_PRAGMA_POP \ - if (itk::Math::Absolute(a - b) > eps) \ - { \ - std::cerr << "Error in " #a << " expected " << b << " but got " << a << std::endl; \ - return EXIT_FAILURE; \ - } \ - } \ - ITK_MACROEND_NOOP_STATEMENT - -#define CHECK_FOR_BOOLEAN(x, expected) \ - { \ - if ((x) != expected) \ - { \ - std::cerr << "Error in " #x << std::endl; \ - return EXIT_FAILURE; \ - } \ - } \ - ITK_MACROEND_NOOP_STATEMENT - - -int -itkRealTimeIntervalTest(int, char *[]) +TEST(RealTimeInterval, DefaultConstructor) { const itk::RealTimeInterval interval0; - const double timeInMicroSeconds = interval0.GetTimeInMicroSeconds(); - const double timeInMilliSeconds = interval0.GetTimeInMilliSeconds(); - double timeInSeconds = interval0.GetTimeInSeconds(); - const double timeInMinutes = interval0.GetTimeInMinutes(); - const double timeInHours = interval0.GetTimeInHours(); - const double timeInDays = interval0.GetTimeInDays(); - - CHECK_FOR_VALUE(timeInMicroSeconds, 0.0); - CHECK_FOR_VALUE(timeInMilliSeconds, 0.0); - CHECK_FOR_VALUE(timeInSeconds, 0.0); - CHECK_FOR_VALUE(timeInMinutes, 0.0); - CHECK_FOR_VALUE(timeInHours, 0.0); - CHECK_FOR_VALUE(timeInDays, 0.0); - - const itk::RealTimeInterval interval1; + CheckForValue(interval0.GetTimeInMicroSeconds(), 0.0); + CheckForValue(interval0.GetTimeInMilliSeconds(), 0.0); + CheckForValue(interval0.GetTimeInSeconds(), 0.0); + CheckForValue(interval0.GetTimeInMinutes(), 0.0); + CheckForValue(interval0.GetTimeInHours(), 0.0); + CheckForValue(interval0.GetTimeInDays(), 0.0); +} + +TEST(RealTimeInterval, AccumulationAndSubtraction) +{ + const itk::RealTimeInterval interval0; itk::RealTimeInterval intervalX = interval0; const itk::RealTimeInterval oneSecond(1, 0); @@ -76,13 +58,8 @@ itkRealTimeIntervalTest(int, char *[]) intervalX += oneSecond; } - std::cout << "intervalX = " << intervalX << std::endl; - itk::RealTimeInterval manySeconds = intervalX - interval0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 1000000.0); + CheckForValue(manySeconds.GetTimeInSeconds(), 1000000.0); itk::RealTimeInterval fiveMicroseconds; fiveMicroseconds.Set(0, 5); @@ -95,10 +72,7 @@ itkRealTimeIntervalTest(int, char *[]) } manySeconds = interval3 - interval0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 5.0); + CheckForValue(manySeconds.GetTimeInSeconds(), 5.0); for (unsigned int i = 0; i < 1000000L; ++i) { @@ -106,75 +80,60 @@ itkRealTimeIntervalTest(int, char *[]) } manySeconds = interval3 - interval0; + CheckForValue(manySeconds.GetTimeInSeconds(), 0.0); +} - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 0.0); - - +TEST(RealTimeInterval, SetWithNormalization) +{ itk::RealTimeInterval timeSpan; timeSpan.Set(19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 14.0); + CheckForValue(timeSpan.GetTimeInSeconds(), 14.0); timeSpan.Set(-19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -14.0); + CheckForValue(timeSpan.GetTimeInSeconds(), -14.0); timeSpan.Set(-19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -24.0); + CheckForValue(timeSpan.GetTimeInSeconds(), -24.0); timeSpan.Set(19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 24.0); +} - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 24.0); - - +TEST(RealTimeInterval, Addition) +{ const itk::RealTimeInterval timeSpan1(19, 300000L); const itk::RealTimeInterval timeSpan2(13, 500000L); const itk::RealTimeInterval timeSpan3 = timeSpan1 + timeSpan2; + CheckForValue(timeSpan3.GetTimeInSeconds(), 32.8); +} - timeInSeconds = timeSpan3.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 32.8); - +TEST(RealTimeInterval, ComparisonOperators) +{ // Test comparison operations const itk::RealTimeInterval dt1(15, 13); const itk::RealTimeInterval dt2(19, 11); const itk::RealTimeInterval dt3(15, 25); - CHECK_FOR_BOOLEAN(dt1 == dt1, true); - CHECK_FOR_BOOLEAN(dt1 != dt2, true); - CHECK_FOR_BOOLEAN(dt1 != dt1, false); - CHECK_FOR_BOOLEAN(dt2 >= dt1, true); - CHECK_FOR_BOOLEAN(dt1 >= dt1, true); - CHECK_FOR_BOOLEAN(dt2 > dt1, true); - CHECK_FOR_BOOLEAN(dt1 <= dt2, true); - CHECK_FOR_BOOLEAN(dt1 <= dt1, true); - CHECK_FOR_BOOLEAN(dt1 < dt2, true); - - CHECK_FOR_BOOLEAN(dt3 == dt3, true); - CHECK_FOR_BOOLEAN(dt1 != dt3, true); - CHECK_FOR_BOOLEAN(dt3 >= dt1, true); - CHECK_FOR_BOOLEAN(dt3 > dt1, true); - CHECK_FOR_BOOLEAN(dt3 <= dt1, false); - CHECK_FOR_BOOLEAN(dt3 < dt1, false); - CHECK_FOR_BOOLEAN(dt1 <= dt3, true); - CHECK_FOR_BOOLEAN(dt1 < dt3, true); - CHECK_FOR_BOOLEAN(dt1 >= dt3, false); - CHECK_FOR_BOOLEAN(dt1 > dt3, false); - - - std::cout << "[PASSED]" << std::endl; - return EXIT_SUCCESS; + EXPECT_TRUE(dt1 == dt1); + EXPECT_TRUE(dt1 != dt2); + EXPECT_FALSE(dt1 != dt1); + EXPECT_TRUE(dt2 >= dt1); + EXPECT_TRUE(dt1 >= dt1); + EXPECT_TRUE(dt2 > dt1); + EXPECT_TRUE(dt1 <= dt2); + EXPECT_TRUE(dt1 <= dt1); + EXPECT_TRUE(dt1 < dt2); + + EXPECT_TRUE(dt3 == dt3); + EXPECT_TRUE(dt1 != dt3); + EXPECT_TRUE(dt3 >= dt1); + EXPECT_TRUE(dt3 > dt1); + EXPECT_FALSE(dt3 <= dt1); + EXPECT_FALSE(dt3 < dt1); + EXPECT_TRUE(dt1 <= dt3); + EXPECT_TRUE(dt1 < dt3); + EXPECT_FALSE(dt1 >= dt3); + EXPECT_FALSE(dt1 > dt3); } From 214508246c2b9150f4be78bc57d3dd9788d6f4af Mon Sep 17 00:00:00 2001 From: "Hans J. Johnson" Date: Wed, 15 Apr 2026 12:50:20 -0500 Subject: [PATCH 5/5] ENH: Convert itkRealTimeStampTest to GTest format --- Modules/Core/Common/test/CMakeLists.txt | 8 +- .../Common/test/itkRealTimeStampGTest.cxx | 181 +++++++----------- 2 files changed, 75 insertions(+), 114 deletions(-) diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 0abe9cf17b8..a40f1d241a2 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -47,7 +47,6 @@ set( itkPointSetToImageFilterTest1.cxx itkPointSetToImageFilterTest2.cxx itkRealTimeClockTest.cxx - itkRealTimeStampTest.cxx itkRGBPixelTest.cxx itkSimpleFilterWatcherTest.cxx itkSobelOperatorImageConvolutionTest.cxx @@ -984,12 +983,6 @@ itk_add_test( ITKCommon1TestDriver itkRealTimeClockTest ) -itk_add_test( - NAME itkRealTimeStampTest - COMMAND - ITKCommon1TestDriver - itkRealTimeStampTest -) itk_add_test( NAME itkTimeProbeTest COMMAND @@ -1561,6 +1554,7 @@ set( itkPrintHelperGTest.cxx itkPriorityQueueGTest.cxx itkRealTimeIntervalGTest.cxx + itkRealTimeStampGTest.cxx itkRGBAPixelGTest.cxx itkRGBPixelGTest.cxx itkShapedImageNeighborhoodRangeGTest.cxx diff --git a/Modules/Core/Common/test/itkRealTimeStampGTest.cxx b/Modules/Core/Common/test/itkRealTimeStampGTest.cxx index 014a98d9c6e..c6ee76c941e 100644 --- a/Modules/Core/Common/test/itkRealTimeStampGTest.cxx +++ b/Modules/Core/Common/test/itkRealTimeStampGTest.cxx @@ -15,75 +15,58 @@ * limitations under the License. * *=========================================================================*/ - -#include #include "itkRealTimeStamp.h" -#include "itkNumericTraits.h" +#include "itkGTest.h" + #include "itkMath.h" -#include "itkTestingMacros.h" - -#define CHECK_FOR_VALUE(a, b) \ - { \ - double eps = 4.0 * itk::NumericTraits::epsilon(); \ - ITK_GCC_PRAGMA_PUSH \ - ITK_GCC_SUPPRESS_Wfloat_equal \ - eps = (b == 0.0) ? eps : itk::Math::Absolute(b * eps); \ - ITK_GCC_PRAGMA_POP \ - if (itk::Math::Absolute(a - b) > eps) \ - { \ - std::cerr << "Error in " #a << " expected " << b << " but got " << a << std::endl; \ - return EXIT_FAILURE; \ - } \ - } \ - ITK_MACROEND_NOOP_STATEMENT - -#define CHECK_FOR_BOOLEAN(x, expected) \ - { \ - if ((x) != expected) \ - { \ - std::cerr << "Error in " #x << std::endl; \ - return EXIT_FAILURE; \ - } \ - } \ - ITK_MACROEND_NOOP_STATEMENT - -int -itkRealTimeStampTest(int, char *[]) +#include "itkNumericTraits.h" + +namespace +{ +void +CheckForValue(double a, double b) +{ + double eps = 4.0 * itk::NumericTraits::epsilon(); + ITK_GCC_PRAGMA_PUSH + ITK_GCC_SUPPRESS_Wfloat_equal + eps = (b == 0.0) ? eps : itk::Math::Absolute(b * eps); + ITK_GCC_PRAGMA_POP + EXPECT_LE(itk::Math::Absolute(a - b), eps); +} +} // namespace + +TEST(RealTimeStamp, DefaultConstructor) { const itk::RealTimeStamp stamp0; - const double timeInMicroSeconds = stamp0.GetTimeInMicroSeconds(); - const double timeInMilliSeconds = stamp0.GetTimeInMilliSeconds(); - double timeInSeconds = stamp0.GetTimeInSeconds(); - const double timeInHours = stamp0.GetTimeInHours(); - const double timeInDays = stamp0.GetTimeInDays(); + CheckForValue(stamp0.GetTimeInMicroSeconds(), 0.0); + CheckForValue(stamp0.GetTimeInMilliSeconds(), 0.0); + CheckForValue(stamp0.GetTimeInSeconds(), 0.0); + CheckForValue(stamp0.GetTimeInHours(), 0.0); + CheckForValue(stamp0.GetTimeInDays(), 0.0); +} + +TEST(RealTimeStamp, NegativeIntervalThrows) +{ + itk::RealTimeStamp stamp; + const itk::RealTimeInterval minusOneSecond(-1, 0); - CHECK_FOR_VALUE(timeInMicroSeconds, 0.0); - CHECK_FOR_VALUE(timeInMilliSeconds, 0.0); - CHECK_FOR_VALUE(timeInSeconds, 0.0); - CHECK_FOR_VALUE(timeInHours, 0.0); - CHECK_FOR_VALUE(timeInDays, 0.0); + EXPECT_THROW(stamp += minusOneSecond, itk::ExceptionObject); +} - const itk::RealTimeStamp stamp1; +TEST(RealTimeStamp, AccumulationAndSubtraction) +{ + const itk::RealTimeStamp stamp0; itk::RealTimeStamp stamp2 = stamp0; - const itk::RealTimeInterval minusOneSecond(-1, 0); - ITK_TRY_EXPECT_EXCEPTION(stamp2 += minusOneSecond); - const itk::RealTimeInterval oneSecond(1, 0); - for (unsigned int i = 0; i < 1000000L; ++i) { stamp2 += oneSecond; } - std::cout << "Stamp2 = " << stamp2 << std::endl; - itk::RealTimeInterval manySeconds = stamp2 - stamp0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 1000000.0); + CheckForValue(manySeconds.GetTimeInSeconds(), 1000000.0); itk::RealTimeInterval fiveMicroseconds; fiveMicroseconds.Set(0, 5); @@ -96,10 +79,7 @@ itkRealTimeStampTest(int, char *[]) } manySeconds = stamp3 - stamp0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 5.0); + CheckForValue(manySeconds.GetTimeInSeconds(), 5.0); for (unsigned int i = 0; i < 1000000L; ++i) { @@ -107,49 +87,40 @@ itkRealTimeStampTest(int, char *[]) } manySeconds = stamp3 - stamp0; + CheckForValue(manySeconds.GetTimeInSeconds(), 0.0); - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 0.0); - - ITK_TRY_EXPECT_EXCEPTION(stamp3 += minusOneSecond); + const itk::RealTimeInterval minusOneSecond(-1, 0); + EXPECT_THROW(stamp3 += minusOneSecond, itk::ExceptionObject); +} +TEST(RealTimeStamp, IntervalSetWithNormalization) +{ itk::RealTimeInterval timeSpan; timeSpan.Set(19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 14.0); + CheckForValue(timeSpan.GetTimeInSeconds(), 14.0); timeSpan.Set(-19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -14.0); + CheckForValue(timeSpan.GetTimeInSeconds(), -14.0); timeSpan.Set(-19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -24.0); + CheckForValue(timeSpan.GetTimeInSeconds(), -24.0); timeSpan.Set(19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 24.0); +} - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 24.0); - - +TEST(RealTimeStamp, IntervalAddition) +{ const itk::RealTimeInterval timeSpan1(19, 300000L); const itk::RealTimeInterval timeSpan2(13, 500000L); const itk::RealTimeInterval timeSpan3 = timeSpan1 + timeSpan2; + CheckForValue(timeSpan3.GetTimeInSeconds(), 32.8); +} - timeInSeconds = timeSpan3.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 32.8); - +TEST(RealTimeStamp, ComparisonOperators) +{ // Test comparison operations const itk::RealTimeInterval dt1(15, 13); const itk::RealTimeInterval dt2(19, 11); @@ -164,28 +135,24 @@ itkRealTimeStampTest(int, char *[]) itk::RealTimeInterval t3; t3 += dt3; - CHECK_FOR_BOOLEAN(t1 == t1, true); - CHECK_FOR_BOOLEAN(t1 != t2, true); - CHECK_FOR_BOOLEAN(t1 != t1, false); - CHECK_FOR_BOOLEAN(t2 >= t1, true); - CHECK_FOR_BOOLEAN(t1 >= t1, true); - CHECK_FOR_BOOLEAN(t2 > t1, true); - CHECK_FOR_BOOLEAN(t1 <= t2, true); - CHECK_FOR_BOOLEAN(t1 <= t1, true); - CHECK_FOR_BOOLEAN(t1 < t2, true); - - CHECK_FOR_BOOLEAN(t3 == t3, true); - CHECK_FOR_BOOLEAN(t1 != t3, true); - CHECK_FOR_BOOLEAN(t3 >= t1, true); - CHECK_FOR_BOOLEAN(t3 > t1, true); - CHECK_FOR_BOOLEAN(t3 <= t1, false); - CHECK_FOR_BOOLEAN(t3 < t1, false); - CHECK_FOR_BOOLEAN(t1 <= t3, true); - CHECK_FOR_BOOLEAN(t1 < t3, true); - CHECK_FOR_BOOLEAN(t1 >= t3, false); - CHECK_FOR_BOOLEAN(t1 > t3, false); - - - std::cout << "[PASSED]" << std::endl; - return EXIT_SUCCESS; + EXPECT_TRUE(t1 == t1); + EXPECT_TRUE(t1 != t2); + EXPECT_FALSE(t1 != t1); + EXPECT_TRUE(t2 >= t1); + EXPECT_TRUE(t1 >= t1); + EXPECT_TRUE(t2 > t1); + EXPECT_TRUE(t1 <= t2); + EXPECT_TRUE(t1 <= t1); + EXPECT_TRUE(t1 < t2); + + EXPECT_TRUE(t3 == t3); + EXPECT_TRUE(t1 != t3); + EXPECT_TRUE(t3 >= t1); + EXPECT_TRUE(t3 > t1); + EXPECT_FALSE(t3 <= t1); + EXPECT_FALSE(t3 < t1); + EXPECT_TRUE(t1 <= t3); + EXPECT_TRUE(t1 < t3); + EXPECT_FALSE(t1 >= t3); + EXPECT_FALSE(t1 > t3); }