2014-02-10 19:53:03 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2014-02-10 19:53:03 +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.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/internal/ArrayPortalFromIterators.h>
|
|
|
|
|
2014-10-07 23:27:31 +00:00
|
|
|
#include <vtkm/VecTraits.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace
|
|
|
|
{
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T>
|
2014-02-10 19:53:03 +00:00
|
|
|
struct TemplatedTests
|
|
|
|
{
|
2018-02-27 14:25:25 +00:00
|
|
|
static constexpr vtkm::Id ARRAY_SIZE = 10;
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-08-10 19:26:05 +00:00
|
|
|
using ValueType = T;
|
|
|
|
using ComponentType = typename vtkm::VecTraits<ValueType>::ComponentType;
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2014-02-11 21:20:30 +00:00
|
|
|
ValueType ExpectedValue(vtkm::Id index, ComponentType value)
|
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
return ValueType(static_cast<ComponentType>(index + static_cast<vtkm::Id>(value)));
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class IteratorType>
|
2014-02-11 21:20:30 +00:00
|
|
|
void FillIterator(IteratorType begin, IteratorType end, ComponentType value)
|
|
|
|
{
|
2014-02-10 19:53:03 +00:00
|
|
|
vtkm::Id index = 0;
|
|
|
|
for (IteratorType iter = begin; iter != end; iter++)
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2014-02-10 19:53:03 +00:00
|
|
|
*iter = ExpectedValue(index, value);
|
|
|
|
index++;
|
2014-02-11 21:20:30 +00:00
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class IteratorType>
|
|
|
|
bool CheckIterator(IteratorType begin, IteratorType end, ComponentType value)
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2014-02-10 19:53:03 +00:00
|
|
|
vtkm::Id index = 0;
|
|
|
|
for (IteratorType iter = begin; iter != end; iter++)
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
if (*iter != ExpectedValue(index, value))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
index++;
|
2014-02-11 21:20:30 +00:00
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class PortalType>
|
|
|
|
bool CheckPortal(const PortalType& portal, ComponentType value)
|
2014-09-08 20:59:11 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
|
|
|
|
return CheckIterator(iterators.GetBegin(), iterators.GetEnd(), value);
|
|
|
|
}
|
|
|
|
|
2014-02-10 19:53:03 +00:00
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
ValueType array[ARRAY_SIZE];
|
|
|
|
|
2018-02-27 14:25:25 +00:00
|
|
|
constexpr ComponentType ORIGINAL_VALUE = 109;
|
2017-05-18 14:29:41 +00:00
|
|
|
FillIterator(array, array + ARRAY_SIZE, ORIGINAL_VALUE);
|
|
|
|
|
|
|
|
::vtkm::cont::internal::ArrayPortalFromIterators<ValueType*> portal(array, array + ARRAY_SIZE);
|
2017-05-26 17:53:28 +00:00
|
|
|
::vtkm::cont::internal::ArrayPortalFromIterators<const ValueType*> const_portal(
|
|
|
|
array, array + ARRAY_SIZE);
|
2017-05-18 14:29:41 +00:00
|
|
|
|
2020-05-05 17:26:59 +00:00
|
|
|
using PortalType = decltype(portal);
|
|
|
|
using PortalConstType = decltype(const_portal);
|
|
|
|
|
|
|
|
std::cout << "Check that PortalSupports* results are valid:" << std::endl;
|
|
|
|
VTKM_TEST_ASSERT(vtkm::internal::PortalSupportsSets<PortalType>::value,
|
|
|
|
"Writable portals should support Set operations");
|
|
|
|
VTKM_TEST_ASSERT(vtkm::internal::PortalSupportsGets<PortalType>::value,
|
|
|
|
"Writable portals should support Get operations");
|
|
|
|
VTKM_TEST_ASSERT(!vtkm::internal::PortalSupportsSets<PortalConstType>::value,
|
|
|
|
"Read-only portals should not allow Set operations");
|
|
|
|
VTKM_TEST_ASSERT(vtkm::internal::PortalSupportsGets<PortalConstType>::value,
|
|
|
|
"Read-only portals should support Get operations");
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
std::cout << " Check that ArrayPortalToIterators is not doing indirection." << std::endl;
|
2019-12-13 19:19:27 +00:00
|
|
|
// If you get a compile error here about mismatched types, it might be
|
|
|
|
// that ArrayPortalToIterators is not properly overloaded to return the
|
|
|
|
// original iterator.
|
2014-09-08 20:59:11 +00:00
|
|
|
VTKM_TEST_ASSERT(vtkm::cont::ArrayPortalToIteratorBegin(portal) == array,
|
|
|
|
"Begin iterator wrong.");
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(vtkm::cont::ArrayPortalToIteratorEnd(portal) == array + ARRAY_SIZE,
|
2014-09-08 20:59:11 +00:00
|
|
|
"End iterator wrong.");
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(vtkm::cont::ArrayPortalToIteratorBegin(const_portal) == array,
|
2014-09-08 20:59:11 +00:00
|
|
|
"Begin const iterator wrong.");
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(vtkm::cont::ArrayPortalToIteratorEnd(const_portal) == array + ARRAY_SIZE,
|
2014-09-08 20:59:11 +00:00
|
|
|
"End const iterator wrong.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(portal.GetNumberOfValues() == ARRAY_SIZE, "Portal array size wrong.");
|
2014-02-10 19:53:03 +00:00
|
|
|
VTKM_TEST_ASSERT(const_portal.GetNumberOfValues() == ARRAY_SIZE,
|
2014-02-11 21:20:30 +00:00
|
|
|
"Const portal array size wrong.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2018-01-30 00:24:31 +00:00
|
|
|
std::cout << " Check initial value." << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckPortal(portal, ORIGINAL_VALUE), "Portal iterator has bad value.");
|
2014-09-08 20:59:11 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckPortal(const_portal, ORIGINAL_VALUE),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Const portal iterator has bad value.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2018-02-27 14:25:25 +00:00
|
|
|
constexpr ComponentType SET_VALUE = 62;
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Check get/set methods." << std::endl;
|
|
|
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(portal.Get(index) == ExpectedValue(index, ORIGINAL_VALUE),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Bad portal value.");
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(const_portal.Get(index) == ExpectedValue(index, ORIGINAL_VALUE),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Bad const portal value.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
portal.Set(index, ExpectedValue(index, SET_VALUE));
|
2014-02-11 21:20:30 +00:00
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Make sure set has correct value." << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckPortal(portal, SET_VALUE), "Portal iterator has bad value.");
|
|
|
|
VTKM_TEST_ASSERT(CheckIterator(array, array + ARRAY_SIZE, SET_VALUE), "Array has bad value.");
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestFunctor
|
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T>
|
2014-10-08 22:56:33 +00:00
|
|
|
void operator()(T) const
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
|
|
|
TemplatedTests<T> tests;
|
|
|
|
tests();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestArrayPortalFromIterators()
|
|
|
|
{
|
2016-08-24 22:02:18 +00:00
|
|
|
vtkm::testing::Testing::TryTypes(TestFunctor());
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2019-01-01 22:19:02 +00:00
|
|
|
int UnitTestArrayPortalFromIterators(int argc, char* argv[])
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2019-01-01 22:19:02 +00:00
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayPortalFromIterators, argc, argv);
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|