diff --git a/Modules/Core/Common/test/CMakeLists.txt b/Modules/Core/Common/test/CMakeLists.txt index 6a7a3636bef..a40f1d241a2 100644 --- a/Modules/Core/Common/test/CMakeLists.txt +++ b/Modules/Core/Common/test/CMakeLists.txt @@ -46,10 +46,7 @@ set( itkPointSetTest.cxx itkPointSetToImageFilterTest1.cxx itkPointSetToImageFilterTest2.cxx - itkPriorityQueueTest.cxx itkRealTimeClockTest.cxx - itkRealTimeIntervalTest.cxx - itkRealTimeStampTest.cxx itkRGBPixelTest.cxx itkSimpleFilterWatcherTest.cxx itkSobelOperatorImageConvolutionTest.cxx @@ -129,7 +126,6 @@ set( itkDataObjectAndProcessObjectTest.cxx itkOptimizerParametersTest.cxx itkImageVectorOptimizerParametersHelperTest.cxx - itkCompensatedSummationTest.cxx itkCompensatedSummationTest2.cxx itkImageRegionConstIteratorWithOnlyIndexTest.cxx itkImageRandomConstIteratorWithOnlyIndexTest.cxx @@ -981,30 +977,12 @@ itk_add_test( ITKCommon1TestDriver itkPhasedArray3DSpecialCoordinatesImageTest ) -itk_add_test( - NAME itkPriorityQueueTest - COMMAND - ITKCommon1TestDriver - itkPriorityQueueTest -) itk_add_test( NAME itkRealTimeClockTest COMMAND ITKCommon1TestDriver itkRealTimeClockTest ) -itk_add_test( - NAME itkRealTimeStampTest - COMMAND - ITKCommon1TestDriver - itkRealTimeStampTest -) -itk_add_test( - NAME itkRealTimeIntervalTest - COMMAND - ITKCommon1TestDriver - itkRealTimeIntervalTest -) itk_add_test( NAME itkTimeProbeTest COMMAND @@ -1288,12 +1266,6 @@ itk_add_test( ITKCommon2TestDriver itkImageVectorOptimizerParametersHelperTest ) -itk_add_test( - NAME itkCompensatedSummationTest - COMMAND - ITKCommon2TestDriver - itkCompensatedSummationTest -) itk_add_test( NAME itkCompensatedSummationTest2 COMMAND @@ -1525,6 +1497,7 @@ set( itkByteSwapGTest.cxx itkCommandObserverObjectGTest.cxx itkCommonTypeTraitsGTest.cxx + itkCompensatedSummationGTest.cxx itkConnectedImageNeighborhoodShapeGTest.cxx itkConstantBoundaryImageNeighborhoodPixelAccessPolicyGTest.cxx itkCopyGTest.cxx @@ -1579,6 +1552,9 @@ set( itkPointGTest.cxx itkPointSetGTest.cxx itkPrintHelperGTest.cxx + itkPriorityQueueGTest.cxx + itkRealTimeIntervalGTest.cxx + itkRealTimeStampGTest.cxx itkRGBAPixelGTest.cxx itkRGBPixelGTest.cxx itkShapedImageNeighborhoodRangeGTest.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); } 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; } diff --git a/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx b/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx new file mode 100644 index 00000000000..208f9b8a255 --- /dev/null +++ b/Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx @@ -0,0 +1,139 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#include "itkRealTimeInterval.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 + +TEST(RealTimeInterval, DefaultConstructor) +{ + const itk::RealTimeInterval interval0; + + 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); + for (unsigned int i = 0; i < 1000000L; ++i) + { + intervalX += oneSecond; + } + + itk::RealTimeInterval manySeconds = intervalX - interval0; + CheckForValue(manySeconds.GetTimeInSeconds(), 1000000.0); + + itk::RealTimeInterval fiveMicroseconds; + fiveMicroseconds.Set(0, 5); + + itk::RealTimeInterval interval3 = interval0; + + for (unsigned int i = 0; i < 1000000L; ++i) + { + interval3 += fiveMicroseconds; + } + + manySeconds = interval3 - interval0; + CheckForValue(manySeconds.GetTimeInSeconds(), 5.0); + + for (unsigned int i = 0; i < 1000000L; ++i) + { + interval3 -= fiveMicroseconds; + } + + manySeconds = interval3 - interval0; + CheckForValue(manySeconds.GetTimeInSeconds(), 0.0); +} + +TEST(RealTimeInterval, SetWithNormalization) +{ + itk::RealTimeInterval timeSpan; + + timeSpan.Set(19, -5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 14.0); + + timeSpan.Set(-19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), -14.0); + + timeSpan.Set(-19, -5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), -24.0); + + timeSpan.Set(19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 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); +} + +TEST(RealTimeInterval, ComparisonOperators) +{ + // Test comparison operations + const itk::RealTimeInterval dt1(15, 13); + const itk::RealTimeInterval dt2(19, 11); + const itk::RealTimeInterval dt3(15, 25); + + 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); +} diff --git a/Modules/Core/Common/test/itkRealTimeIntervalTest.cxx b/Modules/Core/Common/test/itkRealTimeIntervalTest.cxx deleted file mode 100644 index ce16d2536f1..00000000000 --- a/Modules/Core/Common/test/itkRealTimeIntervalTest.cxx +++ /dev/null @@ -1,180 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ - -#include -#include "itkRealTimeInterval.h" -#include "itkMacro.h" -#include "itkNumericTraits.h" -#include "itkMath.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 -itkRealTimeIntervalTest(int, char *[]) -{ - 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; - itk::RealTimeInterval intervalX = interval0; - - const itk::RealTimeInterval oneSecond(1, 0); - for (unsigned int i = 0; i < 1000000L; ++i) - { - intervalX += oneSecond; - } - - std::cout << "intervalX = " << intervalX << std::endl; - - itk::RealTimeInterval manySeconds = intervalX - interval0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 1000000.0); - - itk::RealTimeInterval fiveMicroseconds; - fiveMicroseconds.Set(0, 5); - - itk::RealTimeInterval interval3 = interval0; - - for (unsigned int i = 0; i < 1000000L; ++i) - { - interval3 += fiveMicroseconds; - } - - manySeconds = interval3 - interval0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 5.0); - - for (unsigned int i = 0; i < 1000000L; ++i) - { - interval3 -= fiveMicroseconds; - } - - manySeconds = interval3 - interval0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 0.0); - - - itk::RealTimeInterval timeSpan; - - timeSpan.Set(19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 14.0); - - timeSpan.Set(-19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -14.0); - - timeSpan.Set(-19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -24.0); - - timeSpan.Set(19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 24.0); - - - const itk::RealTimeInterval timeSpan1(19, 300000L); - const itk::RealTimeInterval timeSpan2(13, 500000L); - - const itk::RealTimeInterval timeSpan3 = timeSpan1 + timeSpan2; - - timeInSeconds = timeSpan3.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 32.8); - - // 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; -} diff --git a/Modules/Core/Common/test/itkRealTimeStampGTest.cxx b/Modules/Core/Common/test/itkRealTimeStampGTest.cxx new file mode 100644 index 00000000000..c6ee76c941e --- /dev/null +++ b/Modules/Core/Common/test/itkRealTimeStampGTest.cxx @@ -0,0 +1,158 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#include "itkRealTimeStamp.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 + +TEST(RealTimeStamp, DefaultConstructor) +{ + const itk::RealTimeStamp stamp0; + + 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); + + EXPECT_THROW(stamp += minusOneSecond, itk::ExceptionObject); +} + +TEST(RealTimeStamp, AccumulationAndSubtraction) +{ + const itk::RealTimeStamp stamp0; + itk::RealTimeStamp stamp2 = stamp0; + + const itk::RealTimeInterval oneSecond(1, 0); + for (unsigned int i = 0; i < 1000000L; ++i) + { + stamp2 += oneSecond; + } + + itk::RealTimeInterval manySeconds = stamp2 - stamp0; + CheckForValue(manySeconds.GetTimeInSeconds(), 1000000.0); + + itk::RealTimeInterval fiveMicroseconds; + fiveMicroseconds.Set(0, 5); + + itk::RealTimeStamp stamp3 = stamp0; + + for (unsigned int i = 0; i < 1000000L; ++i) + { + stamp3 += fiveMicroseconds; + } + + manySeconds = stamp3 - stamp0; + CheckForValue(manySeconds.GetTimeInSeconds(), 5.0); + + for (unsigned int i = 0; i < 1000000L; ++i) + { + stamp3 -= fiveMicroseconds; + } + + manySeconds = stamp3 - stamp0; + CheckForValue(manySeconds.GetTimeInSeconds(), 0.0); + + const itk::RealTimeInterval minusOneSecond(-1, 0); + EXPECT_THROW(stamp3 += minusOneSecond, itk::ExceptionObject); +} + +TEST(RealTimeStamp, IntervalSetWithNormalization) +{ + itk::RealTimeInterval timeSpan; + + timeSpan.Set(19, -5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 14.0); + + timeSpan.Set(-19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), -14.0); + + timeSpan.Set(-19, -5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), -24.0); + + timeSpan.Set(19, 5000000L); + CheckForValue(timeSpan.GetTimeInSeconds(), 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); +} + +TEST(RealTimeStamp, ComparisonOperators) +{ + // Test comparison operations + const itk::RealTimeInterval dt1(15, 13); + const itk::RealTimeInterval dt2(19, 11); + const itk::RealTimeInterval dt3(15, 25); + + itk::RealTimeInterval t1; + t1 += dt1; + + itk::RealTimeInterval t2; + t2 += dt2; + + itk::RealTimeInterval t3; + t3 += dt3; + + 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); +} diff --git a/Modules/Core/Common/test/itkRealTimeStampTest.cxx b/Modules/Core/Common/test/itkRealTimeStampTest.cxx deleted file mode 100644 index 014a98d9c6e..00000000000 --- a/Modules/Core/Common/test/itkRealTimeStampTest.cxx +++ /dev/null @@ -1,191 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ - -#include -#include "itkRealTimeStamp.h" -#include "itkNumericTraits.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 *[]) -{ - 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(); - - 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); - - const itk::RealTimeStamp stamp1; - 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); - - itk::RealTimeInterval fiveMicroseconds; - fiveMicroseconds.Set(0, 5); - - itk::RealTimeStamp stamp3 = stamp0; - - for (unsigned int i = 0; i < 1000000L; ++i) - { - stamp3 += fiveMicroseconds; - } - - manySeconds = stamp3 - stamp0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 5.0); - - for (unsigned int i = 0; i < 1000000L; ++i) - { - stamp3 -= fiveMicroseconds; - } - - manySeconds = stamp3 - stamp0; - - timeInSeconds = manySeconds.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 0.0); - - ITK_TRY_EXPECT_EXCEPTION(stamp3 += minusOneSecond); - - itk::RealTimeInterval timeSpan; - - timeSpan.Set(19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 14.0); - - timeSpan.Set(-19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -14.0); - - timeSpan.Set(-19, -5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, -24.0); - - timeSpan.Set(19, 5000000L); - - timeInSeconds = timeSpan.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 24.0); - - - const itk::RealTimeInterval timeSpan1(19, 300000L); - const itk::RealTimeInterval timeSpan2(13, 500000L); - - const itk::RealTimeInterval timeSpan3 = timeSpan1 + timeSpan2; - - timeInSeconds = timeSpan3.GetTimeInSeconds(); - - CHECK_FOR_VALUE(timeInSeconds, 32.8); - - // Test comparison operations - const itk::RealTimeInterval dt1(15, 13); - const itk::RealTimeInterval dt2(19, 11); - const itk::RealTimeInterval dt3(15, 25); - - itk::RealTimeInterval t1; - t1 += dt1; - - itk::RealTimeInterval t2; - t2 += dt2; - - 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; -}