2014-02-10 19:53:03 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Copyright 2014 Sandia Corporation.
|
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
2015-05-21 12:09:22 +00:00
|
|
|
// Copyright 2014 Los Alamos National Security.
|
2014-02-10 19:53:03 +00:00
|
|
|
//
|
|
|
|
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
|
|
|
// the U.S. Government retains certain rights in this software.
|
|
|
|
//
|
|
|
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
|
|
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
|
|
|
// this software.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>
|
|
|
|
|
2014-10-07 23:27:31 +00:00
|
|
|
#include <vtkm/VecTraits.h>
|
2014-02-10 19:53:03 +00:00
|
|
|
#include <vtkm/cont/internal/ArrayPortalFromIterators.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
static const vtkm::Id ARRAY_SIZE = 10;
|
|
|
|
|
|
|
|
typedef T ValueType;
|
2014-10-07 23:27:31 +00:00
|
|
|
typedef typename vtkm::VecTraits<ValueType>::ComponentType 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 (ValueType(*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, const ComponentType& value)
|
2014-09-08 20:59:11 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
|
|
|
|
return CheckIterator(iterators.GetBegin(), iterators.GetEnd(), value);
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:52:24 +00:00
|
|
|
ComponentType ORIGINAL_VALUE() { return 39; }
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class ArrayPortalType>
|
2014-02-10 19:53:03 +00:00
|
|
|
void TestIteratorRead(ArrayPortalType portal)
|
|
|
|
{
|
|
|
|
typedef vtkm::cont::internal::IteratorFromArrayPortal<ArrayPortalType> IteratorType;
|
|
|
|
|
|
|
|
IteratorType begin = vtkm::cont::internal::make_IteratorBegin(portal);
|
|
|
|
IteratorType end = vtkm::cont::internal::make_IteratorEnd(portal);
|
|
|
|
VTKM_TEST_ASSERT(std::distance(begin, end) == ARRAY_SIZE,
|
2014-02-11 21:20:30 +00:00
|
|
|
"Distance between begin and end incorrect.");
|
2016-10-18 19:35:06 +00:00
|
|
|
VTKM_TEST_ASSERT(std::distance(end, begin) == -ARRAY_SIZE,
|
|
|
|
"Distance between begin and end incorrect.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Check forward iteration." << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckIterator(begin, end, ORIGINAL_VALUE()), "Forward iteration wrong");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Check backward iteration." << std::endl;
|
|
|
|
IteratorType middle = end;
|
2017-05-18 14:29:41 +00:00
|
|
|
for (vtkm::Id index = portal.GetNumberOfValues() - 1; index >= 0; index--)
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2014-02-10 19:53:03 +00:00
|
|
|
middle--;
|
|
|
|
ValueType value = *middle;
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(value == ExpectedValue(index, ORIGINAL_VALUE()), "Backward iteration wrong");
|
2014-02-11 21:20:30 +00:00
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Check advance" << std::endl;
|
2017-05-18 14:29:41 +00:00
|
|
|
middle = begin + ARRAY_SIZE / 2;
|
|
|
|
VTKM_TEST_ASSERT(std::distance(begin, middle) == ARRAY_SIZE / 2, "Bad distance to middle.");
|
|
|
|
VTKM_TEST_ASSERT(ValueType(*middle) == ExpectedValue(ARRAY_SIZE / 2, ORIGINAL_VALUE()),
|
|
|
|
"Bad value at middle.");
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <class ArrayPortalType>
|
2014-02-10 19:53:03 +00:00
|
|
|
void TestIteratorWrite(ArrayPortalType portal)
|
|
|
|
{
|
|
|
|
typedef vtkm::cont::internal::IteratorFromArrayPortal<ArrayPortalType> IteratorType;
|
|
|
|
|
|
|
|
IteratorType begin = vtkm::cont::internal::make_IteratorBegin(portal);
|
|
|
|
IteratorType end = vtkm::cont::internal::make_IteratorEnd(portal);
|
|
|
|
|
2014-10-08 22:56:33 +00:00
|
|
|
static const ComponentType WRITE_VALUE = 73;
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Write values to iterator." << std::endl;
|
|
|
|
FillIterator(begin, end, WRITE_VALUE);
|
|
|
|
|
|
|
|
std::cout << " Check values in portal." << std::endl;
|
2014-09-08 20:59:11 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckPortal(portal, WRITE_VALUE),
|
2014-02-11 21:20:30 +00:00
|
|
|
"Did not get correct values when writing to iterator.");
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
ValueType array[ARRAY_SIZE];
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
FillIterator(array, array + ARRAY_SIZE, ORIGINAL_VALUE());
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
::vtkm::cont::internal::ArrayPortalFromIterators<ValueType*> portal(array, array + ARRAY_SIZE);
|
|
|
|
::vtkm::cont::internal::ArrayPortalFromIterators<const ValueType*> const_portal(array, array +
|
|
|
|
ARRAY_SIZE);
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
std::cout << " Test read from iterator." << std::endl;
|
|
|
|
TestIteratorRead(portal);
|
|
|
|
|
|
|
|
std::cout << " Test read from const iterator." << std::endl;
|
|
|
|
TestIteratorRead(const_portal);
|
|
|
|
|
|
|
|
std::cout << " Test write to iterator." << std::endl;
|
|
|
|
TestIteratorWrite(portal);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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 TestArrayIteratorFromArrayPortal()
|
|
|
|
{
|
2016-08-24 22:02:18 +00:00
|
|
|
vtkm::testing::Testing::TryTypes(TestFunctor());
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Anonymous namespace
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
int UnitTestIteratorFromArrayPortal(int, char* [])
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayIteratorFromArrayPortal);
|
|
|
|
}
|