vtk-m/vtkm/testing/UnitTestVecTraits.cxx
Kenneth Moreland ac889b5004 Implement VecTraits class for all types
The `VecTraits` class allows templated functions, methods, and classes to
treat type arguments uniformly as `Vec` types or to otherwise differentiate
between scalar and vector types. This only works for types that `VecTraits`
is defined for.

The `VecTraits` templated class now has a default implementation that will
be used for any type that does not have a `VecTraits` specialization. This
removes many surprise compiler errors when using a template that, unknown
to you, has `VecTraits` in its implementation.

One potential issue is that if `VecTraits` gets defined for a new type, the
behavior of `VecTraits` could change for that type in backward-incompatible
ways. If `VecTraits` is used in a purely generic way, this should not be an
issue. However, if assumptions were made about the components and length,
this could cause problems.

Fixes #589
2023-03-16 12:59:38 -06:00

84 lines
2.8 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/testing/VecTraitsTests.h>
#include <vtkm/testing/Testing.h>
namespace
{
static constexpr vtkm::Id MAX_VECTOR_SIZE = 5;
static constexpr vtkm::Id VecInit[MAX_VECTOR_SIZE] = { 42, 54, 67, 12, 78 };
struct TypeWithoutVecTraits
{
vtkm::Id Value = -1;
TypeWithoutVecTraits() = default;
TypeWithoutVecTraits(vtkm::Id value)
: Value(value)
{
}
operator vtkm::Id() const { return this->Value; }
};
struct TestVecTypeFunctor
{
template <typename T>
void operator()(const T&) const
{
using Traits = vtkm::VecTraits<T>;
using ComponentType = typename Traits::ComponentType;
VTKM_TEST_ASSERT(Traits::NUM_COMPONENTS <= MAX_VECTOR_SIZE,
"Need to update test for larger vectors.");
T inVector;
for (vtkm::IdComponent index = 0; index < Traits::NUM_COMPONENTS; index++)
{
Traits::SetComponent(inVector, index, ComponentType(VecInit[index]));
}
T outVector;
vtkm::testing::TestVecType<Traits::NUM_COMPONENTS>(inVector, outVector);
vtkm::VecC<ComponentType> outVecC(outVector);
vtkm::testing::TestVecType<Traits::NUM_COMPONENTS>(vtkm::VecC<ComponentType>(inVector),
outVecC);
vtkm::VecCConst<ComponentType> outVecCConst(outVector);
vtkm::testing::TestVecType<Traits::NUM_COMPONENTS>(vtkm::VecCConst<ComponentType>(inVector),
outVecCConst);
}
};
void TestVecTraits()
{
TestVecTypeFunctor test;
vtkm::testing::Testing::TryTypes(test);
std::cout << "vtkm::Vec<vtkm::FloatDefault, 5>" << std::endl;
test(vtkm::Vec<vtkm::FloatDefault, 5>());
std::cout << "TypeWithoutVecTraits" << std::endl;
test(TypeWithoutVecTraits{});
vtkm::testing::TestVecComponentsTag<vtkm::Id3>();
vtkm::testing::TestVecComponentsTag<vtkm::Vec3f>();
vtkm::testing::TestVecComponentsTag<vtkm::Vec4f>();
vtkm::testing::TestVecComponentsTag<vtkm::VecC<vtkm::FloatDefault>>();
vtkm::testing::TestVecComponentsTag<vtkm::VecCConst<vtkm::Id>>();
vtkm::testing::TestScalarComponentsTag<vtkm::Id>();
vtkm::testing::TestScalarComponentsTag<vtkm::FloatDefault>();
vtkm::testing::TestScalarComponentsTag<TypeWithoutVecTraits>();
}
} // anonymous namespace
int UnitTestVecTraits(int argc, char* argv[])
{
return vtkm::testing::Testing::Run(TestVecTraits, argc, argv);
}