2014-04-03 19:17:14 +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.
|
|
|
|
// Copyright 2014. Los Alamos National Security
|
|
|
|
//
|
|
|
|
// 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/DynamicArrayHandle.h>
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
#include <vtkm/cont/StorageImplicit.h>
|
|
|
|
|
2014-04-03 19:17:14 +00:00
|
|
|
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2014-07-10 16:53:02 +00:00
|
|
|
#include <typeinfo>
|
2014-04-03 19:17:14 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const vtkm::Id ARRAY_SIZE = 10;
|
|
|
|
|
|
|
|
vtkm::Id TestValue(vtkm::Id index, vtkm::Id) {
|
|
|
|
return index*100;
|
|
|
|
}
|
|
|
|
|
|
|
|
vtkm::Scalar TestValue(vtkm::Id index, vtkm::Scalar) {
|
|
|
|
return static_cast<vtkm::Scalar>(index)/100;
|
|
|
|
}
|
|
|
|
|
2014-10-07 16:59:34 +00:00
|
|
|
template<typename T, vtkm::IdComponent N>
|
2014-10-07 23:17:03 +00:00
|
|
|
vtkm::Vec<T,N> TestValue(vtkm::Id index, vtkm::Vec<T,N>) {
|
|
|
|
vtkm::Vec<T,N> value;
|
2014-10-07 16:59:34 +00:00
|
|
|
for (vtkm::IdComponent i = 0; i < N; i++)
|
2014-04-03 19:17:14 +00:00
|
|
|
{
|
|
|
|
value[i] = TestValue(index, T()) + (i + 1);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TestValue(vtkm::Id index, std::string) {
|
|
|
|
std::stringstream stream;
|
|
|
|
stream << index;
|
|
|
|
return stream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TypeListTagString : vtkm::ListTagBase<std::string> { };
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct UnusualPortal
|
|
|
|
{
|
|
|
|
typedef T ValueType;
|
|
|
|
|
|
|
|
VTKM_EXEC_CONT_EXPORT
|
|
|
|
vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
|
|
|
|
|
|
|
|
VTKM_EXEC_CONT_EXPORT
|
|
|
|
ValueType Get(vtkm::Id index) const { return TestValue(index, ValueType()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2014-06-23 23:33:04 +00:00
|
|
|
class ArrayHandleWithUnusualStorage
|
|
|
|
: public vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagImplicit<UnusualPortal<T> > >
|
2014-04-03 19:17:14 +00:00
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
typedef vtkm::cont::ArrayHandle<T, vtkm::cont::StorageTagImplicit<UnusualPortal<T> > >
|
2014-04-03 19:17:14 +00:00
|
|
|
Superclass;
|
|
|
|
public:
|
|
|
|
VTKM_CONT_EXPORT
|
2014-06-23 23:33:04 +00:00
|
|
|
ArrayHandleWithUnusualStorage()
|
2014-04-03 19:17:14 +00:00
|
|
|
: Superclass(typename Superclass::PortalConstControl()) { }
|
|
|
|
};
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
struct StorageListTagUnusual :
|
2014-04-03 19:17:14 +00:00
|
|
|
vtkm::ListTagBase2<
|
2014-06-23 23:33:04 +00:00
|
|
|
ArrayHandleWithUnusualStorage<vtkm::Id>::StorageTag,
|
|
|
|
ArrayHandleWithUnusualStorage<std::string>::StorageTag>
|
2014-04-03 19:17:14 +00:00
|
|
|
{ };
|
|
|
|
|
|
|
|
bool CheckCalled;
|
|
|
|
|
|
|
|
struct CheckFunctor
|
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
template<typename T, typename Storage>
|
|
|
|
void operator()(vtkm::cont::ArrayHandle<T, Storage> array) const {
|
2014-04-03 19:17:14 +00:00
|
|
|
CheckCalled = true;
|
2014-07-10 16:53:02 +00:00
|
|
|
std::cout << " Checking for type: " << typeid(T).name() << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
|
|
|
|
VTKM_TEST_ASSERT(array.GetNumberOfValues() == ARRAY_SIZE,
|
|
|
|
"Unexpected array size.");
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
typename vtkm::cont::ArrayHandle<T,Storage>::PortalConstControl portal =
|
2014-04-03 19:17:14 +00:00
|
|
|
array.GetPortalConstControl();
|
|
|
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
|
|
|
{
|
2014-07-10 16:53:02 +00:00
|
|
|
VTKM_TEST_ASSERT(test_equal(portal.Get(index), TestValue(index, T())),
|
2014-04-03 19:17:14 +00:00
|
|
|
"Got bad value in array. Perhaps a bad cast?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
vtkm::cont::DynamicArrayHandle CreateDynamicArray(T)
|
|
|
|
{
|
|
|
|
// Declared static to prevent going out of scope.
|
|
|
|
static T buffer[ARRAY_SIZE];
|
|
|
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
|
|
|
{
|
|
|
|
buffer[index] = TestValue(index, T());
|
|
|
|
}
|
|
|
|
|
|
|
|
return vtkm::cont::DynamicArrayHandle(
|
|
|
|
vtkm::cont::make_ArrayHandle(buffer, ARRAY_SIZE));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void TryDefaultType(T)
|
|
|
|
{
|
|
|
|
CheckCalled = false;
|
|
|
|
|
|
|
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(T());
|
|
|
|
|
|
|
|
array.CastAndCall(CheckFunctor());
|
|
|
|
|
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
|
|
|
}
|
|
|
|
|
|
|
|
struct TryBasicVTKmType
|
|
|
|
{
|
|
|
|
template<typename T>
|
|
|
|
void operator()(T) {
|
|
|
|
CheckCalled = false;
|
|
|
|
|
|
|
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(T());
|
|
|
|
|
|
|
|
array.ResetTypeList(vtkm::TypeListTagAll()).CastAndCall(CheckFunctor());
|
|
|
|
|
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TryUnusualType()
|
|
|
|
{
|
|
|
|
// A string is an unlikely type to be declared elsewhere in VTK-m.
|
|
|
|
vtkm::cont::DynamicArrayHandle array = CreateDynamicArray(std::string());
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
array.CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized type.");
|
|
|
|
}
|
|
|
|
catch (vtkm::cont::ErrorControlBadValue)
|
|
|
|
{
|
|
|
|
std::cout << " Caught exception for unrecognized type." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckCalled = false;
|
|
|
|
array.ResetTypeList(TypeListTagString()).CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
|
|
|
std::cout << " Found type when type list was reset." << std:: endl;
|
|
|
|
}
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
void TryUnusualStorage()
|
2014-04-03 19:17:14 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::DynamicArrayHandle array =
|
2014-06-23 23:33:04 +00:00
|
|
|
ArrayHandleWithUnusualStorage<vtkm::Id>();
|
2014-04-03 19:17:14 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
array.CastAndCall(CheckFunctor());
|
2014-06-23 23:33:04 +00:00
|
|
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized storage.");
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
catch (vtkm::cont::ErrorControlBadValue)
|
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Caught exception for unrecognized storage." << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CheckCalled = false;
|
2014-06-23 23:33:04 +00:00
|
|
|
array.ResetStorageList(StorageListTagUnusual()).CastAndCall(CheckFunctor());
|
2014-04-03 19:17:14 +00:00
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Found instance when storage list was reset." << std:: endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
void TryUnusualTypeAndStorage()
|
2014-04-03 19:17:14 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::DynamicArrayHandle array =
|
2014-06-23 23:33:04 +00:00
|
|
|
ArrayHandleWithUnusualStorage<std::string>();
|
2014-04-03 19:17:14 +00:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
array.CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_FAIL(
|
2014-06-23 23:33:04 +00:00
|
|
|
"CastAndCall failed to error for unrecognized type/storage.");
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
catch (vtkm::cont::ErrorControlBadValue)
|
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Caught exception for unrecognized type/storage."
|
2014-04-03 19:17:14 +00:00
|
|
|
<< std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
array.ResetTypeList(TypeListTagString()).CastAndCall(CheckFunctor());
|
2014-06-23 23:33:04 +00:00
|
|
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized storage.");
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
catch (vtkm::cont::ErrorControlBadValue)
|
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Caught exception for unrecognized storage." << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
2014-06-23 23:33:04 +00:00
|
|
|
array.ResetStorageList(StorageListTagUnusual()).
|
2014-04-03 19:17:14 +00:00
|
|
|
CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_FAIL("CastAndCall failed to error for unrecognized type.");
|
|
|
|
}
|
|
|
|
catch (vtkm::cont::ErrorControlBadValue)
|
|
|
|
{
|
|
|
|
std::cout << " Caught exception for unrecognized type." << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckCalled = false;
|
|
|
|
array
|
|
|
|
.ResetTypeList(TypeListTagString())
|
2014-06-23 23:33:04 +00:00
|
|
|
.ResetStorageList(StorageListTagUnusual())
|
2014-04-03 19:17:14 +00:00
|
|
|
.CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Found instance when type and storage lists were reset." << std:: endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
|
|
|
|
CheckCalled = false;
|
|
|
|
array
|
2014-06-23 23:33:04 +00:00
|
|
|
.ResetStorageList(StorageListTagUnusual())
|
2014-04-03 19:17:14 +00:00
|
|
|
.ResetTypeList(TypeListTagString())
|
|
|
|
.CastAndCall(CheckFunctor());
|
|
|
|
VTKM_TEST_ASSERT(CheckCalled,
|
|
|
|
"The functor was never called (and apparently a bad value exception not thrown).");
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << " Found instance when storage and type lists were reset." << std:: endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TestDynamicArrayHandle()
|
|
|
|
{
|
|
|
|
std::cout << "Try common types with default type lists." << std::endl;
|
2014-07-10 16:53:02 +00:00
|
|
|
std::cout << "*** vtkm::Id ***************" << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
TryDefaultType(vtkm::Id());
|
2014-07-10 16:53:02 +00:00
|
|
|
std::cout << "*** vtkm::Scalar ***********" << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
TryDefaultType(vtkm::Scalar());
|
2014-07-10 16:53:02 +00:00
|
|
|
std::cout << "*** vtkm::Vector3 **********" << std::endl;
|
2014-04-03 19:17:14 +00:00
|
|
|
TryDefaultType(vtkm::Vector3());
|
|
|
|
|
|
|
|
std::cout << "Try all VTK-m types." << std::endl;
|
|
|
|
vtkm::testing::Testing::TryAllTypes(TryBasicVTKmType());
|
|
|
|
|
|
|
|
std::cout << "Try unusual type." << std::endl;
|
|
|
|
TryUnusualType();
|
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << "Try unusual storage." << std::endl;
|
|
|
|
TryUnusualStorage();
|
2014-04-03 19:17:14 +00:00
|
|
|
|
2014-06-23 23:33:04 +00:00
|
|
|
std::cout << "Try unusual type in unusual storage." << std::endl;
|
|
|
|
TryUnusualTypeAndStorage();
|
2014-04-03 19:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
int UnitTestDynamicArrayHandle(int, char *[])
|
|
|
|
{
|
|
|
|
return vtkm::cont::testing::Testing::Run(TestDynamicArrayHandle);
|
|
|
|
}
|