vtk-m/vtkm/cont/testing/UnitTestArrayHandleIndex.cxx
Kenneth Moreland 9b992dcdde Add GetNumberOfComponentsFlat method to ArrayHandle
Getting the number of components (or the number of flattened components)
from an `ArrayHandle` is usually trivial. However, if the `ArrayHandle` is
special in that the number of components is specified at runtime, then it
becomes much more difficult to determine.

Getting the number of components is most important when extracting
component arrays (or reconstructions using component arrays) with
`UnknownArrayHandle`. Previously, `UnknownArrayHandle` used a hack to get
the number of components, which mostly worked but broke down when wrapping
a runtime array inside another array such as `ArrayHandleView`.

To prevent this issue, the ability to get the number of components has been
added to `ArrayHandle` proper. All `Storage` objects for `ArrayHandle`s now
need a method named `GetNumberOfComponentsFlat`. The implementation of this
method is usually trivial. The `ArrayHandle` template now also provides a
`GetNumberOfComponentsFlat` method that gets this information from the
`Storage`. This provides an easy access point for the `UnknownArrayHandle`
to pull this information.
2023-10-03 10:31:38 -04:00

39 lines
1.2 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/ArrayHandleIndex.h>
#include <vtkm/cont/testing/Testing.h>
namespace UnitTestArrayHandleIndexNamespace
{
const vtkm::Id ARRAY_SIZE = 10;
void TestArrayHandleIndex()
{
vtkm::cont::ArrayHandleIndex array(ARRAY_SIZE);
VTKM_TEST_ASSERT(array.GetNumberOfValues() == ARRAY_SIZE, "Bad size.");
VTKM_TEST_ASSERT(array.GetNumberOfComponentsFlat() == 1);
auto portal = array.ReadPortal();
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
VTKM_TEST_ASSERT(portal.Get(index) == index, "Index array has unexpected value.");
}
}
} // namespace UnitTestArrayHandleIndexNamespace
int UnitTestArrayHandleIndex(int argc, char* argv[])
{
using namespace UnitTestArrayHandleIndexNamespace;
return vtkm::cont::testing::Testing::Run(TestArrayHandleIndex, argc, argv);
}