vtk-m/vtkm/cont/testing/UnitTestArrayHandleOffsetsToNumComponents.cxx
Kenneth Moreland b1343474c1 Consolidate count-to-offset algorithms
For no particularly good reason, there were two functions that converted
and array of counts to an array of offsets: `ConvertNumComponentsToOffsets`
and `ConvertNumIndicesToOffsets`. These functions were identical, except
one was defined in `ArrayHandleGroupVecVariable.h` and the other was
defined in `CellSetExplicit.h`.

These two functions have been consolidated into one (which is now called
`ConvertNumComponentsToOffsets`). The consolidated function has also been
put in its own header file: `ConvertNumComponentsToOffsets.h`.

Normally, backward compatibility would be established using deprecated
features. However, one of the things being worked on is the removal of
device-specific code (e.g. `vtkm::cont::Algorithm`) from core classes like
`CellSetExplicit` so that less code needs to use the device compiler
(especially downstream code).

Part of this change removed unnecessary includes of `Algorithm.h` in
`ArrayHandleGroupVecVariable.h` and `CellSetExplicit.h`. This header had to
be added to some classes that were not including it themselves.
2021-09-16 14:24:41 -06:00

74 lines
2.3 KiB
C++

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#include <vtkm/cont/ArrayHandleOffsetsToNumComponents.h>
#include <vtkm/cont/ArrayHandleConstant.h>
#include <vtkm/cont/ArrayHandleCounting.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/ConvertNumComponentsToOffsets.h>
#include <vtkm/cont/testing/Testing.h>
namespace
{
constexpr vtkm::Id ARRAY_SIZE = 20;
template <typename OffsetsArray, typename ExpectedNumComponents>
void TestOffsetsToNumComponents(const OffsetsArray& offsetsArray,
const ExpectedNumComponents& expectedNumComponents)
{
VTKM_TEST_ASSERT(offsetsArray.GetNumberOfValues() ==
expectedNumComponents.GetNumberOfValues() + 1);
auto numComponents = vtkm::cont::make_ArrayHandleOffsetsToNumComponents(offsetsArray);
VTKM_TEST_ASSERT(numComponents.GetNumberOfValues() == expectedNumComponents.GetNumberOfValues());
VTKM_TEST_ASSERT(
test_equal_portals(numComponents.ReadPortal(), expectedNumComponents.ReadPortal()));
}
void TryNormalOffsets()
{
std::cout << "Normal offset array." << std::endl;
vtkm::cont::ArrayHandle<vtkm::IdComponent> numComponents;
numComponents.Allocate(ARRAY_SIZE);
auto numComponentsPortal = numComponents.WritePortal();
for (vtkm::IdComponent i = 0; i < ARRAY_SIZE; ++i)
{
numComponentsPortal.Set(i, i % 5);
}
auto offsets = vtkm::cont::ConvertNumComponentsToOffsets(numComponents);
TestOffsetsToNumComponents(offsets, numComponents);
}
void TryFancyOffsets()
{
std::cout << "Fancy offset array." << std::endl;
vtkm::cont::ArrayHandleCounting<vtkm::Id> offsets(0, 3, ARRAY_SIZE + 1);
TestOffsetsToNumComponents(offsets,
vtkm::cont::ArrayHandleConstant<vtkm::IdComponent>(3, ARRAY_SIZE));
}
void Run()
{
TryNormalOffsets();
TryFancyOffsets();
}
} // anonymous namespace
int UnitTestArrayHandleOffsetsToNumComponents(int argc, char* argv[])
{
return vtkm::cont::testing::Testing::Run(Run, argc, argv);
}