Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 4 additions & 28 deletions Modules/Core/Common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -129,7 +126,6 @@ set(
itkDataObjectAndProcessObjectTest.cxx
itkOptimizerParametersTest.cxx
itkImageVectorOptimizerParametersHelperTest.cxx
itkCompensatedSummationTest.cxx
itkCompensatedSummationTest2.cxx
itkImageRegionConstIteratorWithOnlyIndexTest.cxx
itkImageRandomConstIteratorWithOnlyIndexTest.cxx
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1288,12 +1266,6 @@ itk_add_test(
ITKCommon2TestDriver
itkImageVectorOptimizerParametersHelperTest
)
itk_add_test(
NAME itkCompensatedSummationTest
COMMAND
ITKCommon2TestDriver
itkCompensatedSummationTest
)
itk_add_test(
NAME itkCompensatedSummationTest2
COMMAND
Expand Down Expand Up @@ -1525,6 +1497,7 @@ set(
itkByteSwapGTest.cxx
itkCommandObserverObjectGTest.cxx
itkCommonTypeTraitsGTest.cxx
itkCompensatedSummationGTest.cxx
itkConnectedImageNeighborhoodShapeGTest.cxx
itkConstantBoundaryImageNeighborhoodPixelAccessPolicyGTest.cxx
itkCopyGTest.cxx
Expand Down Expand Up @@ -1579,6 +1552,9 @@ set(
itkPointGTest.cxx
itkPointSetGTest.cxx
itkPrintHelperGTest.cxx
itkPriorityQueueGTest.cxx
itkRealTimeIntervalGTest.cxx
itkRealTimeStampGTest.cxx
itkRGBAPixelGTest.cxx
itkRGBPixelGTest.cxx
itkShapedImageNeighborhoodRangeGTest.cxx
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down Expand Up @@ -56,66 +50,27 @@ itkCompensatedSummationTest(int, char *[])
const FloatType accumulatorMean = accumulatorSum / static_cast<FloatType>(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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,8 +30,6 @@ itkPriorityQueueTest(int, char *[])
using MinPQType = itk::PriorityQueueContainer<MinPQElementType, MinPQElementType, double, ElementIdentifier>;
auto min_priority_queue = MinPQType::New();

std::cout << min_priority_queue->GetNameOfClass() << std::endl;

using MaxPQType = itk::PriorityQueueContainer<MaxPQElementType, MaxPQElementType, double, ElementIdentifier>;
auto max_priority_queue = MaxPQType::New();

Expand All @@ -49,46 +47,24 @@ 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())
{
break;
}
sequence.pop_back();
}
std::cout << "OK" << std::endl;

return EXIT_SUCCESS;
}
139 changes: 139 additions & 0 deletions Modules/Core/Common/test/itkRealTimeIntervalGTest.cxx
Original file line number Diff line number Diff line change
@@ -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<double>::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);
}
Loading
Loading