Skip to content

Commit

Permalink
remove testing.h dependency (#954)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicole Marsaglia <[email protected]>
  • Loading branch information
nicolemarsaglia and Nicole Marsaglia authored Jun 15, 2022
1 parent e6de535 commit 1641fc7
Showing 1 changed file with 137 additions and 2 deletions.
139 changes: 137 additions & 2 deletions src/tests/vtkh/t_vtkm_test_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

#include <assert.h>
#include <random>
#include <vtkm/Matrix.h>
#include <vtkm/VectorAnalysis.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/DataSetBuilderRectilinear.h>
#include <vtkm/cont/DataSetBuilderExplicit.h>
#include <vtkm/cont/DataSetBuilderUniform.h>
#include <vtkm/cont/testing/Testing.h>
//#include <vtkm/cont/testing/Testing.h>

#define BASE_SIZE 32
typedef vtkm::cont::ArrayHandleUniformPointCoordinates UniformCoords;
Expand Down Expand Up @@ -556,6 +557,140 @@ inline vtkm::cont::DataSet Make3DExplicitDataSet2()
return dataSet;
}

namespace detail
{

template <typename T>
struct TestValueImpl;
} //namespace detail

// Many tests involve getting and setting values in some index-based structure
// (like an array). These tests also often involve trying many types. The
// overloaded TestValue function returns some unique value for an index for a
// given type. Different types might give different values.
//
template <typename T>
static inline T TestValue(vtkm::Id index, T)
{
return detail::TestValueImpl<T>()(index);
}

namespace detail
{

template <typename T>
struct TestValueImpl
{
T DoIt(vtkm::Id index, vtkm::TypeTraitsIntegerTag) const
{
constexpr bool larger_than_2bytes = sizeof(T) > 2;
if (larger_than_2bytes)
{
return T(index * 100);
}
else
{
return T(index + 100);
}
}

T DoIt(vtkm::Id index, vtkm::TypeTraitsRealTag) const
{
return T(0.01f * static_cast<float>(index) + 1.001f);
}

T operator()(vtkm::Id index) const
{
return this->DoIt(index, typename vtkm::TypeTraits<T>::NumericTag());
}
};

template <typename T, vtkm::IdComponent N>
struct TestValueImpl<vtkm::Vec<T, N>>
{
vtkm::Vec<T, N> operator()(vtkm::Id index) const
{
vtkm::Vec<T, N> value;
for (vtkm::IdComponent i = 0; i < N; i++)
{
value[i] = TestValue(index * N + i, T());
}
return value;
}
};

template <typename U, typename V>
struct TestValueImpl<vtkm::Pair<U, V>>
{
vtkm::Pair<U, V> operator()(vtkm::Id index) const
{
return vtkm::Pair<U, V>(TestValue(2 * index, U()), TestValue(2 * index + 1, V()));
}
};

template <typename T, vtkm::IdComponent NumRow, vtkm::IdComponent NumCol>
struct TestValueImpl<vtkm::Matrix<T, NumRow, NumCol>>
{
vtkm::Matrix<T, NumRow, NumCol> operator()(vtkm::Id index) const
{
vtkm::Matrix<T, NumRow, NumCol> value;
vtkm::Id runningIndex = index * NumRow * NumCol;
for (vtkm::IdComponent row = 0; row < NumRow; ++row)
{
for (vtkm::IdComponent col = 0; col < NumCol; ++col)
{
value(row, col) = TestValue(runningIndex, T());
++runningIndex;
}
}
return value;
}
};

template <>
struct TestValueImpl<std::string>
{
std::string operator()(vtkm::Id index) const
{
std::stringstream stream;
stream << index;
return stream.str();
}
};

} //namespace detail

// Verifies that the contents of the given array portal match the values
// returned by vtkm::testing::TestValue.
template <typename PortalType>
static inline void CheckPortal(const PortalType& portal)
{
using ValueType = typename PortalType::ValueType;
for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
{
ValueType expectedValue = TestValue(index, ValueType());
ValueType foundValue = portal.Get(index);
if (!test_equal(expectedValue, foundValue))
{
ASCENT_ERROR("Got unexpected value in array. Expected: " << expectedValue
<< ", Found: " << foundValue << "\n");
}
}
}

/// Sets all the values in a given array portal to be the values returned
/// by vtkm::testing::TestValue. The ArrayPortal must be allocated first.
template <typename PortalType>
static inline void SetPortal(const PortalType& portal)
{
using ValueType = typename PortalType::ValueType;

for (vtkm::Id index = 0; index < portal.GetNumberOfValues(); index++)
{
portal.Set(index, TestValue(index, ValueType()));
}
}

inline vtkm::cont::DataSet Make3DExplicitDataSetCowNose()
{
// prepare data array
Expand Down Expand Up @@ -645,6 +780,6 @@ inline vtkm::cont::DataSet Make3DRectilinearDataSet0()

return dataSet;
}


#endif

0 comments on commit 1641fc7

Please sign in to comment.