From 45bc2929f30b44754a480ce57e25a3c2e99bca09 Mon Sep 17 00:00:00 2001 From: Niels Dekker Date: Sat, 2 Nov 2024 12:06:16 +0100 Subject: [PATCH] STYLE: Place `LabelObjectLine` objects in LabelMap tests on the stack MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../test/itkLabelObjectLineComparatorTest.cxx | 30 ++++------------ .../LabelMap/test/itkLabelObjectLineTest.cxx | 36 +++++++------------ 2 files changed, 20 insertions(+), 46 deletions(-) diff --git a/Modules/Filtering/LabelMap/test/itkLabelObjectLineComparatorTest.cxx b/Modules/Filtering/LabelMap/test/itkLabelObjectLineComparatorTest.cxx index b7c286cdd5a..c7f57e463ee 100644 --- a/Modules/Filtering/LabelMap/test/itkLabelObjectLineComparatorTest.cxx +++ b/Modules/Filtering/LabelMap/test/itkLabelObjectLineComparatorTest.cxx @@ -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, highSetIndex(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); }