vtk-m/vtkm/testing/VecTraitsTests.h
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

273 lines
10 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.
//============================================================================
#ifndef vtkm_testing_VecTraitsTest_h
#define vtkm_testing_VecTraitsTest_h
//GCC 4+ when running the test code have false positive warnings
//about uninitialized vtkm::VecC<> when filled by VecTraits<T>::CopyInto.
//The testing code already verifies that CopyInto works by verifying the
//results, so we are going to suppress `-Wmaybe-uninitialized` for this
//file
//This block has to go before we include any vtkm file that brings in
//<vtkm/Types.h> otherwise the warning suppression will not work
#include <vtkm/internal/Configure.h>
#if (defined(VTKM_GCC) && __GNUC__ >= 4)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
#endif // gcc 4+
#include <vtkm/VecTraits.h>
#include <vtkm/StaticAssert.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/testing/Testing.h>
namespace vtkm
{
namespace testing
{
namespace detail
{
inline void CompareDimensionalityTags(vtkm::TypeTraitsScalarTag, vtkm::VecTraitsTagSingleComponent)
{
// If we are here, everything is fine.
}
inline void CompareDimensionalityTags(vtkm::TypeTraitsVectorTag,
vtkm::VecTraitsTagMultipleComponents)
{
// If we are here, everything is fine.
}
inline void CompareDimensionalityTags(vtkm::TypeTraitsUnknownTag, vtkm::VecTraitsTagSingleComponent)
{
// If we are here, type traits are probably not defined (and default to unknown). In this case,
// we expect VecTraits to have the default implementation, in which case it is treated as a
// single component.
}
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
inline void CheckIsStatic(const T&, vtkm::VecTraitsTagSizeStatic)
{
VTKM_TEST_ASSERT(vtkm::VecTraits<T>::NUM_COMPONENTS == NUM_COMPONENTS,
"Traits returns unexpected number of components");
}
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
inline void CheckIsStatic(const T&, vtkm::VecTraitsTagSizeVariable)
{
// If we are here, everything is fine.
}
template <typename VecType>
struct VecIsWritable
{
using type = std::true_type;
};
template <typename ComponentType>
struct VecIsWritable<vtkm::VecCConst<ComponentType>>
{
using type = std::false_type;
};
template <typename ComponentType>
struct VecIsWritable<vtkm::VecCConst<ComponentType>*>
{
using type = std::false_type;
};
// Part of TestVecTypeImpl that writes to the Vec type
template <vtkm::IdComponent NUM_COMPONENTS, typename T, typename VecCopyType>
static void TestVecTypeWritableImpl(const T& inVector,
const VecCopyType& vectorCopy,
T& outVector,
std::true_type)
{
using Traits = vtkm::VecTraits<T>;
using ComponentType = typename Traits::ComponentType;
{
const ComponentType multiplier = 4;
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
{
Traits::SetComponent(
outVector, i, ComponentType(multiplier * Traits::GetComponent(inVector, i)));
}
vtkm::Vec<ComponentType, NUM_COMPONENTS> resultCopy;
Traits::CopyInto(outVector, resultCopy);
VTKM_TEST_ASSERT(test_equal(resultCopy, multiplier * vectorCopy),
"Got bad result for scalar multiple");
}
{
const ComponentType multiplier = 7;
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
{
Traits::GetComponent(outVector, i) =
ComponentType(multiplier * Traits::GetComponent(inVector, i));
}
vtkm::Vec<ComponentType, NUM_COMPONENTS> resultCopy;
Traits::CopyInto(outVector, resultCopy);
VTKM_TEST_ASSERT(test_equal(resultCopy, multiplier * vectorCopy),
"Got bad result for scalar multiple");
}
}
template <vtkm::IdComponent NUM_COMPONENTS, typename T, typename VecCopyType>
static void TestVecTypeWritableImpl(const T& vtkmNotUsed(inVector),
const VecCopyType& vtkmNotUsed(vectorCopy),
T& vtkmNotUsed(outVector),
std::false_type)
{
// Skip writable functionality.
}
/// Compares some manual arithmetic through type traits to arithmetic with
/// the Tuple class.
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
static void TestVecTypeImpl(const typename std::remove_const<T>::type& inVector,
typename std::remove_const<T>::type& outVector)
{
using Traits = vtkm::VecTraits<T>;
using ComponentType = typename Traits::ComponentType;
using NonConstT = typename std::remove_const<T>::type;
CheckIsStatic<NUM_COMPONENTS>(inVector, typename Traits::IsSizeStatic());
VTKM_TEST_ASSERT(Traits::GetNumberOfComponents(inVector) == NUM_COMPONENTS,
"Traits returned wrong number of components.");
vtkm::Vec<ComponentType, NUM_COMPONENTS> vectorCopy;
Traits::CopyInto(inVector, vectorCopy);
VTKM_TEST_ASSERT(test_equal(vectorCopy, inVector), "CopyInto does not work.");
{
auto expected = vtkm::Dot(vectorCopy, vectorCopy);
decltype(expected) result = 0;
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
{
ComponentType component = Traits::GetComponent(inVector, i);
result = result + (component * component);
}
VTKM_TEST_ASSERT(test_equal(result, expected), "Got bad result for dot product");
}
// This will fail to compile if the tags are wrong.
detail::CompareDimensionalityTags(
typename vtkm::TypeTraits<std::remove_pointer_t<T>>::DimensionalityTag(),
typename vtkm::VecTraits<T>::HasMultipleComponents());
TestVecTypeWritableImpl<NUM_COMPONENTS, NonConstT>(
inVector, vectorCopy, outVector, typename VecIsWritable<NonConstT>::type());
// Compiler checks for base component types
using BaseComponentType = typename vtkm::VecTraits<T>::BaseComponentType;
VTKM_STATIC_ASSERT((std::is_same<typename vtkm::TypeTraits<BaseComponentType>::DimensionalityTag,
vtkm::TypeTraitsScalarTag>::value) ||
(std::is_same<typename vtkm::TypeTraits<BaseComponentType>::DimensionalityTag,
vtkm::TypeTraitsUnknownTag>::value));
VTKM_STATIC_ASSERT((std::is_same<typename vtkm::VecTraits<ComponentType>::BaseComponentType,
BaseComponentType>::value));
// Compiler checks for replacing component types
using ReplaceWithVecComponent =
typename vtkm::VecTraits<T>::template ReplaceComponentType<vtkm::Vec<char, 2>>;
VTKM_STATIC_ASSERT(
(std::is_same<typename vtkm::TypeTraits<std::remove_pointer_t<T>>::DimensionalityTag,
vtkm::TypeTraitsVectorTag>::value &&
std::is_same<typename vtkm::VecTraits<ReplaceWithVecComponent>::ComponentType,
vtkm::Vec<char, 2>>::value) ||
(!std::is_same<typename vtkm::TypeTraits<std::remove_pointer_t<T>>::DimensionalityTag,
vtkm::TypeTraitsVectorTag>::value &&
std::is_same<typename vtkm::VecTraits<ReplaceWithVecComponent>::ComponentType, char>::value));
VTKM_STATIC_ASSERT(
(std::is_same<typename vtkm::VecTraits<ReplaceWithVecComponent>::BaseComponentType,
char>::value));
using ReplaceBaseComponent =
typename vtkm::VecTraits<ReplaceWithVecComponent>::template ReplaceBaseComponentType<short>;
VTKM_STATIC_ASSERT(
(std::is_same<typename vtkm::TypeTraits<std::remove_pointer_t<T>>::DimensionalityTag,
vtkm::TypeTraitsVectorTag>::value &&
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::ComponentType,
vtkm::Vec<short, 2>>::value) ||
(!std::is_same<typename vtkm::TypeTraits<std::remove_pointer_t<T>>::DimensionalityTag,
vtkm::TypeTraitsVectorTag>::value &&
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::ComponentType, short>::value));
VTKM_STATIC_ASSERT((
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::BaseComponentType, short>::value));
}
inline void CheckVecComponentsTag(vtkm::VecTraitsTagMultipleComponents)
{
// If we are running here, everything is fine.
}
} // namespace detail
/// Checks to make sure that the HasMultipleComponents tag is actually for
/// multiple components. Should only be called for vector classes that actually
/// have multiple components.
///
template <class T>
inline void TestVecComponentsTag()
{
// This will fail to compile if the tag is wrong
// (i.e. not vtkm::VecTraitsTagMultipleComponents)
detail::CheckVecComponentsTag(typename vtkm::VecTraits<T>::HasMultipleComponents());
}
namespace detail
{
inline void CheckScalarComponentsTag(vtkm::VecTraitsTagSingleComponent)
{
// If we are running here, everything is fine.
}
} // namespace detail
/// Compares some manual arithmetic through type traits to arithmetic with
/// the Tuple class.
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
static void TestVecType(const T& inVector, T& outVector)
{
detail::TestVecTypeImpl<NUM_COMPONENTS, T>(inVector, outVector);
detail::TestVecTypeImpl<NUM_COMPONENTS, const T>(inVector, outVector);
// The local pointer variables are for some weirdness about `TestVecTypeImpl` taking references
// of its argument type.
T* inPointer = const_cast<T*>(&inVector);
T* outPointer = &outVector;
detail::TestVecTypeImpl<NUM_COMPONENTS, T*>(inPointer, outPointer);
VTKM_STATIC_ASSERT_MSG((std::is_base_of<vtkm::VecTraits<T*>, vtkm::VecTraits<const T*>>::value),
"Constant pointer should have same implementation as pointer.");
}
/// Checks to make sure that the HasMultipleComponents tag is actually for a
/// single component. Should only be called for "vector" classes that actually
/// have only a single component (that is, are really scalars).
///
template <class T>
inline void TestScalarComponentsTag()
{
// This will fail to compile if the tag is wrong
// (i.e. not vtkm::VecTraitsTagSingleComponent)
detail::CheckScalarComponentsTag(typename vtkm::VecTraits<T>::HasMultipleComponents());
}
}
} // namespace vtkm::testing
#if (defined(VTKM_GCC) && __GNUC__ > 4 && __GNUC__ < 7)
#pragma GCC diagnostic pop
#endif // gcc 5 or 6
#endif //vtkm_testing_VecTraitsTest_h