2014-02-11 17:34:56 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2014-02-11 17:34:56 +00:00
|
|
|
// 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.
|
|
|
|
//============================================================================
|
2014-10-07 23:27:31 +00:00
|
|
|
#ifndef vtkm_testing_VecTraitsTest_h
|
|
|
|
#define vtkm_testing_VecTraitsTest_h
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2018-05-04 19:28:44 +00:00
|
|
|
//GCC 4+ when running the test code have false positive warnings
|
2018-05-02 19:52:33 +00:00
|
|
|
//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
|
2018-05-04 19:28:44 +00:00
|
|
|
//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)
|
2018-05-02 19:52:33 +00:00
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
|
2018-05-04 19:28:44 +00:00
|
|
|
#endif // gcc 4+
|
|
|
|
|
|
|
|
#include <vtkm/VecTraits.h>
|
|
|
|
|
2019-09-03 03:52:56 +00:00
|
|
|
#include <vtkm/StaticAssert.h>
|
2018-05-04 19:28:44 +00:00
|
|
|
#include <vtkm/TypeTraits.h>
|
|
|
|
|
|
|
|
#include <vtkm/testing/Testing.h>
|
2018-05-02 19:52:33 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace testing
|
|
|
|
{
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
inline void CompareDimensionalityTags(vtkm::TypeTraitsScalarTag, vtkm::VecTraitsTagSingleComponent)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// If we are here, everything is fine.
|
|
|
|
}
|
|
|
|
inline void CompareDimensionalityTags(vtkm::TypeTraitsVectorTag,
|
2014-10-07 23:27:31 +00:00
|
|
|
vtkm::VecTraitsTagMultipleComponents)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// If we are here, everything is fine.
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
|
|
|
|
inline void CheckIsStatic(const T&, vtkm::VecTraitsTagSizeStatic)
|
2015-08-07 23:58:52 +00:00
|
|
|
{
|
|
|
|
VTKM_TEST_ASSERT(vtkm::VecTraits<T>::NUM_COMPONENTS == NUM_COMPONENTS,
|
|
|
|
"Traits returns unexpected number of components");
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
|
|
|
|
inline void CheckIsStatic(const T&, vtkm::VecTraitsTagSizeVariable)
|
2015-08-07 23:58:52 +00:00
|
|
|
{
|
|
|
|
// If we are here, everything is fine.
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename VecType>
|
2016-11-30 22:03:30 +00:00
|
|
|
struct VecIsWritable
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2016-11-30 22:03:30 +00:00
|
|
|
using type = std::true_type;
|
|
|
|
};
|
2015-08-07 23:58:52 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename ComponentType>
|
|
|
|
struct VecIsWritable<vtkm::VecCConst<ComponentType>>
|
2016-11-30 22:03:30 +00:00
|
|
|
{
|
|
|
|
using type = std::false_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Part of TestVecTypeImpl that writes to the Vec type
|
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T, typename VecCopyType>
|
2017-05-26 17:53:28 +00:00
|
|
|
static void TestVecTypeWritableImpl(const T& inVector,
|
|
|
|
const VecCopyType& vectorCopy,
|
|
|
|
T& outVector,
|
2016-11-30 22:03:30 +00:00
|
|
|
std::true_type)
|
|
|
|
{
|
|
|
|
using Traits = vtkm::VecTraits<T>;
|
|
|
|
using ComponentType = typename Traits::ComponentType;
|
2015-08-07 23:58:52 +00:00
|
|
|
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2014-02-11 21:20:30 +00:00
|
|
|
const ComponentType multiplier = 4;
|
2015-05-26 14:49:52 +00:00
|
|
|
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2017-05-26 17:53:28 +00:00
|
|
|
Traits::SetComponent(
|
|
|
|
outVector, i, ComponentType(multiplier * Traits::GetComponent(inVector, i)));
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Vec<ComponentType, NUM_COMPONENTS> resultCopy;
|
2016-11-30 22:03:30 +00:00
|
|
|
Traits::CopyInto(outVector, resultCopy);
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(resultCopy, multiplier * vectorCopy),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Got bad result for scalar multiple");
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-02-11 21:20:30 +00:00
|
|
|
const ComponentType multiplier = 7;
|
2015-05-26 14:49:52 +00:00
|
|
|
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
Traits::GetComponent(outVector, i) =
|
|
|
|
ComponentType(multiplier * Traits::GetComponent(inVector, i));
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Vec<ComponentType, NUM_COMPONENTS> resultCopy;
|
2016-11-30 22:03:30 +00:00
|
|
|
Traits::CopyInto(outVector, resultCopy);
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(resultCopy, multiplier * vectorCopy),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Got bad result for scalar multiple");
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
2016-11-30 22:03:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T, typename VecCopyType>
|
2017-05-18 14:29:41 +00:00
|
|
|
static void TestVecTypeWritableImpl(const T& vtkmNotUsed(inVector),
|
|
|
|
const VecCopyType& vtkmNotUsed(vectorCopy),
|
2017-05-26 17:53:28 +00:00
|
|
|
T& vtkmNotUsed(outVector),
|
|
|
|
std::false_type)
|
2016-11-30 22:03:30 +00:00
|
|
|
{
|
|
|
|
// Skip writable functionality.
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Compares some manual arithmetic through type traits to arithmetic with
|
|
|
|
/// the Tuple class.
|
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
|
2017-05-18 14:29:41 +00:00
|
|
|
static void TestVecTypeImpl(const typename std::remove_const<T>::type& inVector,
|
|
|
|
typename std::remove_const<T>::type& outVector)
|
2016-11-30 22:03:30 +00:00
|
|
|
{
|
|
|
|
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.");
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Vec<ComponentType, NUM_COMPONENTS> vectorCopy;
|
2016-11-30 22:03:30 +00:00
|
|
|
Traits::CopyInto(inVector, vectorCopy);
|
|
|
|
VTKM_TEST_ASSERT(test_equal(vectorCopy, inVector), "CopyInto does not work.");
|
2014-02-11 17:34:56 +00:00
|
|
|
|
|
|
|
{
|
2018-05-14 18:25:46 +00:00
|
|
|
auto expected = vtkm::Dot(vectorCopy, vectorCopy);
|
2018-01-03 15:24:17 +00:00
|
|
|
decltype(expected) result = 0;
|
2015-05-26 14:49:52 +00:00
|
|
|
for (vtkm::IdComponent i = 0; i < NUM_COMPONENTS; i++)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
ComponentType component = Traits::GetComponent(inVector, i);
|
2018-01-03 15:24:17 +00:00
|
|
|
result = result + (component * component);
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
2018-01-03 15:24:17 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(result, expected), "Got bad result for dot product");
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This will fail to compile if the tags are wrong.
|
2017-05-18 14:29:41 +00:00
|
|
|
detail::CompareDimensionalityTags(typename vtkm::TypeTraits<T>::DimensionalityTag(),
|
|
|
|
typename vtkm::VecTraits<T>::HasMultipleComponents());
|
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
TestVecTypeWritableImpl<NUM_COMPONENTS, NonConstT>(
|
|
|
|
inVector, vectorCopy, outVector, typename VecIsWritable<NonConstT>::type());
|
2019-09-03 03:52:56 +00:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
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<T>::DimensionalityTag,
|
|
|
|
vtkm::TypeTraitsVectorTag>::value &&
|
|
|
|
std::is_same<typename vtkm::VecTraits<ReplaceWithVecComponent>::ComponentType,
|
|
|
|
vtkm::Vec<char, 2>>::value) ||
|
|
|
|
(std::is_same<typename vtkm::TypeTraits<T>::DimensionalityTag,
|
|
|
|
vtkm::TypeTraitsScalarTag>::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<T>::DimensionalityTag,
|
|
|
|
vtkm::TypeTraitsVectorTag>::value &&
|
|
|
|
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::ComponentType,
|
|
|
|
vtkm::Vec<short, 2>>::value) ||
|
|
|
|
(std::is_same<typename vtkm::TypeTraits<T>::DimensionalityTag,
|
|
|
|
vtkm::TypeTraitsScalarTag>::value &&
|
|
|
|
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::ComponentType, short>::value));
|
|
|
|
VTKM_STATIC_ASSERT((
|
|
|
|
std::is_same<typename vtkm::VecTraits<ReplaceBaseComponent>::BaseComponentType, short>::value));
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2014-10-07 23:27:31 +00:00
|
|
|
inline void CheckVecComponentsTag(vtkm::VecTraitsTagMultipleComponents)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// 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.
|
|
|
|
///
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class T>
|
2014-10-07 23:27:31 +00:00
|
|
|
inline void TestVecComponentsTag()
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// This will fail to compile if the tag is wrong
|
2014-10-07 23:27:31 +00:00
|
|
|
// (i.e. not vtkm::VecTraitsTagMultipleComponents)
|
2017-05-18 14:29:41 +00:00
|
|
|
detail::CheckVecComponentsTag(typename vtkm::VecTraits<T>::HasMultipleComponents());
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2014-10-07 23:27:31 +00:00
|
|
|
inline void CheckScalarComponentsTag(vtkm::VecTraitsTagSingleComponent)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// If we are running here, everything is fine.
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
/// Compares some manual arithmetic through type traits to arithmetic with
|
|
|
|
/// the Tuple class.
|
2015-08-07 23:58:52 +00:00
|
|
|
template <vtkm::IdComponent NUM_COMPONENTS, typename T>
|
2017-05-18 14:29:41 +00:00
|
|
|
static void TestVecType(const T& inVector, T& outVector)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2016-11-30 22:03:30 +00:00
|
|
|
detail::TestVecTypeImpl<NUM_COMPONENTS, T>(inVector, outVector);
|
|
|
|
detail::TestVecTypeImpl<NUM_COMPONENTS, const T>(inVector, outVector);
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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).
|
|
|
|
///
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class T>
|
2014-02-11 17:34:56 +00:00
|
|
|
inline void TestScalarComponentsTag()
|
|
|
|
{
|
|
|
|
// This will fail to compile if the tag is wrong
|
2014-10-07 23:27:31 +00:00
|
|
|
// (i.e. not vtkm::VecTraitsTagSingleComponent)
|
2017-05-18 14:29:41 +00:00
|
|
|
detail::CheckScalarComponentsTag(typename vtkm::VecTraits<T>::HasMultipleComponents());
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace vtkm::testing
|
|
|
|
|
2018-05-02 19:52:33 +00:00
|
|
|
#if (defined(VTKM_GCC) && __GNUC__ > 4 && __GNUC__ < 7)
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif // gcc 5 or 6
|
|
|
|
|
2014-10-07 23:27:31 +00:00
|
|
|
#endif //vtkm_testing_VecTraitsTest_h
|