From 2a577476dddc2ea673a4a793a52060cbf0210ea5 Mon Sep 17 00:00:00 2001 From: Dave Pugmire Date: Wed, 10 Aug 2022 11:57:18 -0400 Subject: [PATCH] Add function to reduce code in tests. --- .../testing/UnitTestCellLocatorGeneral.cxx | 67 +++++++++++-------- .../testing/UnitTestCellLocatorTwoLevel.cxx | 65 ++++++++++-------- .../CellLocatorBoundingIntervalHierarchy.h | 1 - 3 files changed, 77 insertions(+), 56 deletions(-) diff --git a/vtkm/cont/testing/UnitTestCellLocatorGeneral.cxx b/vtkm/cont/testing/UnitTestCellLocatorGeneral.cxx index f1342ebe1..737770105 100644 --- a/vtkm/cont/testing/UnitTestCellLocatorGeneral.cxx +++ b/vtkm/cont/testing/UnitTestCellLocatorGeneral.cxx @@ -195,6 +195,33 @@ public: } }; +void TestLastCell(vtkm::cont::CellLocatorGeneral& locator, + vtkm::Id numPoints, + vtkm::cont::ArrayHandle& lastCell, + const vtkm::cont::ArrayHandle& points, + const vtkm::cont::ArrayHandle& expCellIds, + const vtkm::cont::ArrayHandle& expPCoords) + +{ + vtkm::cont::ArrayHandle cellIds; + vtkm::cont::ArrayHandle pcoords; + + vtkm::cont::Invoker invoker; + invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCell); + + auto cellIdPortal = cellIds.ReadPortal(); + auto expCellIdsPortal = expCellIds.ReadPortal(); + auto pcoordsPortal = pcoords.ReadPortal(); + auto expPCoordsPortal = expPCoords.ReadPortal(); + + for (vtkm::Id i = 0; i < numPoints; ++i) + { + VTKM_TEST_ASSERT(cellIdPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); + VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), + "Incorrect parameteric coordinates"); + } +} + void TestWithDataSet(vtkm::cont::CellLocatorGeneral& locator, const vtkm::cont::DataSet& dataset) { locator.SetCellSet(dataset.GetCellSet()); @@ -223,40 +250,24 @@ void TestWithDataSet(vtkm::cont::CellLocatorGeneral& locator, const vtkm::cont:: "Incorrect parameteric coordinates"); } - //Test the last cell option. - //Call the locator to fill in the lastCell array. - std::vector lastCell(64); - auto lastCellArray = vtkm::cont::make_ArrayHandle(lastCell, vtkm::CopyFlag::On); + //Test locator using lastCell - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCellArray); + //Test it with initialized. + vtkm::cont::ArrayHandle lastCell; + lastCell.AllocateAndFill(64, vtkm::cont::CellLocatorGeneral::LastCell{}); + TestLastCell(locator, 64, lastCell, points, expCellIds, pcoords); - //Call it again so that it uses the last-cell array. We should get the same results. - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCellArray); - cellIdPortal = cellIds.ReadPortal(); - pcoordsPortal = pcoords.ReadPortal(); - for (vtkm::Id i = 0; i < 64; ++i) - { - if (cellIdPortal.Get(i) != expCellIdsPortal.Get(i)) - std::cout << "Error at: " << i << " " << cellIdPortal.Get(i) << " " << expCellIdsPortal.Get(i) - << std::endl; - VTKM_TEST_ASSERT(cellIdPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); - VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), - "Incorrect parameteric coordinates"); - } + //Call it again using the lastCell just computed to validate. + TestLastCell(locator, 64, lastCell, points, expCellIds, pcoords); - //Test with uninitialized lastCell objects. + + //Test it with uninitialized array. vtkm::cont::ArrayHandle lastCell2; lastCell2.Allocate(64); + TestLastCell(locator, 64, lastCell2, points, expCellIds, pcoords); - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCell2); - cellIdPortal = cellIds.ReadPortal(); - pcoordsPortal = pcoords.ReadPortal(); - for (vtkm::Id i = 0; i < 64; ++i) - { - VTKM_TEST_ASSERT(cellIdPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); - VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), - "Incorrect parameteric coordinates"); - } + //Call it again using the lastCell just computed to validate. + TestLastCell(locator, 64, lastCell2, points, expCellIds, pcoords); } void TestCellLocatorGeneral() diff --git a/vtkm/cont/testing/UnitTestCellLocatorTwoLevel.cxx b/vtkm/cont/testing/UnitTestCellLocatorTwoLevel.cxx index 1f228750a..8b8089567 100644 --- a/vtkm/cont/testing/UnitTestCellLocatorTwoLevel.cxx +++ b/vtkm/cont/testing/UnitTestCellLocatorTwoLevel.cxx @@ -204,6 +204,33 @@ public: } }; +void TestLastCell(vtkm::cont::CellLocatorTwoLevel& locator, + vtkm::Id numPoints, + vtkm::cont::ArrayHandle& lastCell, + const vtkm::cont::ArrayHandle& points, + const vtkm::cont::ArrayHandle& expCellIds, + const vtkm::cont::ArrayHandle& expPCoords) + +{ + vtkm::cont::ArrayHandle cellIds; + vtkm::cont::ArrayHandle pcoords; + + vtkm::cont::Invoker invoker; + invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCell); + + auto cellIdPortal = cellIds.ReadPortal(); + auto expCellIdsPortal = expCellIds.ReadPortal(); + auto pcoordsPortal = pcoords.ReadPortal(); + auto expPCoordsPortal = expPCoords.ReadPortal(); + + for (vtkm::Id i = 0; i < numPoints; ++i) + { + VTKM_TEST_ASSERT(cellIdPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); + VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), + "Incorrect parameteric coordinates"); + } +} + template void TestCellLocator(const vtkm::Vec& dim, vtkm::Id numberOfPoints) { @@ -241,39 +268,23 @@ void TestCellLocator(const vtkm::Vec& dim, vtkm::Id number "Incorrect parameteric coordinates"); } - //Test the last cell option. - //Call the locator to fill in the lastCell array. - std::vector lastCell(numberOfPoints); - auto lastCellArray = vtkm::cont::make_ArrayHandle(lastCell, vtkm::CopyFlag::Off); + //Test locator using lastCell - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCellArray); + //Test it with initialized. + vtkm::cont::ArrayHandle lastCell; + lastCell.AllocateAndFill(numberOfPoints, vtkm::cont::CellLocatorTwoLevel::LastCell{}); + TestLastCell(locator, numberOfPoints, lastCell, points, expCellIds, pcoords); - //Call it again so that it uses the last-cell array. We should get the same results. - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCellArray); + //Call it again using the lastCell just computed to validate. + TestLastCell(locator, numberOfPoints, lastCell, points, expCellIds, pcoords); - cellIdsPortal = cellIds.ReadPortal(); - pcoordsPortal = pcoords.ReadPortal(); - for (vtkm::Id i = 0; i < numberOfPoints; ++i) - { - VTKM_TEST_ASSERT(cellIdsPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); - VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), - "Incorrect parameteric coordinates"); - } - - //Test with uninitialized lastCell objects. + //Test it with uninitialized array. vtkm::cont::ArrayHandle lastCell2; lastCell2.Allocate(numberOfPoints); + TestLastCell(locator, numberOfPoints, lastCell2, points, expCellIds, pcoords); - invoker(FindCellWorkletWithLastCell{}, points, locator, cellIds, pcoords, lastCell2); - - cellIdsPortal = cellIds.ReadPortal(); - pcoordsPortal = pcoords.ReadPortal(); - for (vtkm::Id i = 0; i < numberOfPoints; ++i) - { - VTKM_TEST_ASSERT(cellIdsPortal.Get(i) == expCellIdsPortal.Get(i), "Incorrect cell ids"); - VTKM_TEST_ASSERT(test_equal(pcoordsPortal.Get(i), expPCoordsPortal.Get(i), 1e-3), - "Incorrect parameteric coordinates"); - } + //Call it again using the lastCell2 just computed to validate. + TestLastCell(locator, numberOfPoints, lastCell2, points, expCellIds, pcoords); } void TestingCellLocatorTwoLevel() diff --git a/vtkm/exec/CellLocatorBoundingIntervalHierarchy.h b/vtkm/exec/CellLocatorBoundingIntervalHierarchy.h index 620c7754d..d80306982 100644 --- a/vtkm/exec/CellLocatorBoundingIntervalHierarchy.h +++ b/vtkm/exec/CellLocatorBoundingIntervalHierarchy.h @@ -120,7 +120,6 @@ public: if ((lastCell.NodeIdx >= 0) && (lastCell.NodeIdx < this->Nodes.GetNumberOfValues())) { const auto& node = this->Nodes.Get(lastCell.NodeIdx); - VTKM_ASSERT(node.ChildIndex < 0); //should be a leaf node. if (node.ChildIndex < 0) {