Skip to content

Commit

Permalink
STYLE: Place LabelObjectLine objects in LabelMap tests on the stack
Browse files Browse the repository at this point in the history
Following C++ Core Guidelines, Oct 3, 2024, "Prefer scoped objects, don’t
heap-allocate unnecessarily",
https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#r5-prefer-scoped-objects-dont-heap-allocate-unnecessarily
  • Loading branch information
N-Dekker committed Nov 2, 2024
1 parent 1ce96d2 commit 45bc292
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,49 +37,33 @@ itkLabelObjectLineComparatorTest(int, char *[])
highIndex[0] = 14;
highIndex[1] = 7;

auto * low = new LabelObjectLineType(lowIndex, 11);
auto * high = new LabelObjectLineType(highIndex, 11);
auto * lowlong = new LabelObjectLineType(lowIndex, 15);
LabelObjectLineType low(lowIndex, 11);
LabelObjectLineType high(highIndex, 11);
LabelObjectLineType lowlong(lowIndex, 15);

if (lessThan(*high, *low))
if (lessThan(high, low))
{
std::cerr << "Failed, high<low returned true." << std::endl;
delete low;
delete high;
delete lowlong;
return (EXIT_FAILURE);
}

if (!lessThan(*low, *high))
if (!lessThan(low, high))
{
std::cerr << "Failed, low<high returned false." << std::endl;
delete low;
delete high;
delete lowlong;
return (EXIT_FAILURE);
}

if (lessThan(*low, *low))
if (lessThan(low, low))
{
std::cerr << "Failed, low<low returned true." << std::endl;
delete low;
delete high;
delete lowlong;
return (EXIT_FAILURE);
}

if (!lessThan(*low, *lowlong))
if (!lessThan(low, lowlong))
{
std::cerr << "Failed, low<lowlong returned false." << std::endl;
delete low;
delete high;
delete lowlong;
return (EXIT_FAILURE);
}

delete low;
delete high;
delete lowlong;

return (EXIT_SUCCESS);
}
36 changes: 13 additions & 23 deletions Modules/Filtering/LabelMap/test/itkLabelObjectLineTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -33,77 +33,67 @@ itkLabelObjectLineTest(int, char *[])
nextIndex[0] = 14;
nextIndex[1] = 7;

auto * labelLine = new LabelObjectLineType;
labelLine->SetIndex(currentIndex);
labelLine->SetLength(11);
LabelObjectLineType labelLine;
labelLine.SetIndex(currentIndex);
labelLine.SetLength(11);

IndexType indexBack;
indexBack = labelLine->GetIndex();
indexBack = labelLine.GetIndex();

if ((indexBack[0] != 3) || (indexBack[1] != 7))
{
std::cerr << "Set/Get Index failed on null constructor. " << indexBack << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

LabelObjectLineType::LengthType length;
length = labelLine->GetLength();
length = labelLine.GetLength();
if (length != 11)
{
std::cerr << "Set/Get length failed on null constructor." << length << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}
delete labelLine;

labelLine = new LabelObjectLineType(currentIndex, 11);
indexBack = labelLine->GetIndex();
labelLine = LabelObjectLineType(currentIndex, 11);
indexBack = labelLine.GetIndex();

if ((indexBack[0] != 3) || (indexBack[1] != 7))
{
std::cerr << "Set/Get Index failed on arg constructor. " << indexBack << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

if (labelLine->GetLength() != 11)
if (labelLine.GetLength() != 11)
{
std::cerr << "Set/Get length failed on arg constructor." << length << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

if (!labelLine->HasIndex(currentIndex))
if (!labelLine.HasIndex(currentIndex))
{
std::cerr << "Has Index failed." << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

if (labelLine->HasIndex(nextIndex))
if (labelLine.HasIndex(nextIndex))
{
std::cerr << "Has Index failed." << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

if (labelLine->IsNextIndex(currentIndex))
if (labelLine.IsNextIndex(currentIndex))
{
std::cerr << "Is Next Index failed." << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

if (!labelLine->IsNextIndex(nextIndex))
if (!labelLine.IsNextIndex(nextIndex))
{
std::cerr << "Is Next Index failed." << std::endl;
delete labelLine;
return (EXIT_FAILURE);
}

labelLine->Print(std::cout);
delete labelLine;
labelLine.Print(std::cout);

return (EXIT_SUCCESS);
}

0 comments on commit 45bc292

Please sign in to comment.