UnitTestDynamicTransform.cxx 7.05 KB
Newer Older
1 2 3 4 5 6 7 8
//============================================================================
//  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.
//
9
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
10
//  Copyright 2014 UT-Battelle, LLC.
11
//  Copyright 2014 Los Alamos National Security.
12
//
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14 15 16 17 18 19 20 21 22 23
//  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/DynamicTransform.h"

#include "vtkm/cont/ArrayHandle.h"
24
#include "vtkm/cont/DynamicCellSet.h"
25
#include "vtkm/cont/VariantArrayHandle.h"
26 27 28 29 30

#include "vtkm/internal/FunctionInterface.h"

#include "vtkm/cont/testing/Testing.h"

31 32
namespace vtkm
{
33

34
// VariantArrayHandle requires its value type to have a defined VecTraits
35 36 37 38
// class. One of the tests is to use an "unusual" array of std::string
// (which is pretty pointless but might tease out some assumptions).
// Make an implementation here. Because I am lazy, this is only a partial
// implementation.
39
template <>
40 41
struct VecTraits<std::string>
{
42
  static constexpr vtkm::IdComponent NUM_COMPONENTS = 1;
43
  using HasMultipleComponents = vtkm::VecTraitsTagSingleComponent;
44 45 46 47
};

} // namespace vtkm

48 49
namespace
{
50 51 52

static int g_FunctionCalls;

53 54 55
#define TRY_TRANSFORM(expr)                                                                        \
  g_FunctionCalls = 0;                                                                             \
  expr;                                                                                            \
56 57
  VTKM_TEST_ASSERT(g_FunctionCalls == 1, "Functor not called correctly.")

58 59 60
struct TypeListTagString : vtkm::ListTagBase<std::string>
{
};
61

62 63 64 65
struct ScalarFunctor
{
  void operator()(vtkm::FloatDefault) const
  {
66 67 68 69 70
    std::cout << "    In Scalar functor." << std::endl;
    g_FunctionCalls++;
  }
};

71 72
struct ArrayHandleScalarFunctor
{
73 74
  template <typename ArrayType>
  void operator()(const ArrayType&) const
75
  {
76 77
    VTKM_TEST_FAIL("Called wrong form of functor operator.");
  }
78 79 80 81 82
  void operator()(const vtkm::cont::ArrayHandleVirtual<vtkm::FloatDefault>&) const
  {
    std::cout << "    In ArrayHandleVirtual<Scalar> functor." << std::endl;
    g_FunctionCalls++;
  }
83 84
  void operator()(const vtkm::cont::ArrayHandle<vtkm::FloatDefault>&) const
  {
85 86 87 88 89
    std::cout << "    In ArrayHandle<Scalar> functor." << std::endl;
    g_FunctionCalls++;
  }
};

90 91
struct ArrayHandleStringFunctor
{
92
  void operator()(const vtkm::cont::ArrayHandleVirtual<std::string>&) const
93
  {
94
    std::cout << "    In ArrayHandleVirtual<string> functor." << std::endl;
95 96 97 98
    g_FunctionCalls++;
  }
};

99 100 101 102 103
struct CellSetStructuredFunctor
{
  template <typename T>
  void operator()(const T&) const
  {
104 105
    VTKM_TEST_FAIL("Called wrong form of functor operator.");
  }
106 107
  void operator()(const vtkm::cont::CellSetStructured<3>&) const
  {
108 109 110 111 112
    std::cout << "    In CellSetStructured<3> functor." << std::endl;
    g_FunctionCalls++;
  }
};

113 114 115 116 117
struct FunctionInterfaceFunctor
{
  template <typename Signature>
  void operator()(const vtkm::internal::FunctionInterface<Signature>&) const
  {
118 119 120
    VTKM_TEST_FAIL("Called wrong form of functor operator.");
  }
  void operator()(
121 122 123 124
    const vtkm::internal::FunctionInterface<void(vtkm::cont::ArrayHandle<vtkm::FloatDefault>,
                                                 vtkm::cont::ArrayHandle<vtkm::FloatDefault>,
                                                 vtkm::cont::ArrayHandle<std::string>,
                                                 vtkm::cont::CellSetStructured<3>)>&) const
125
  {
126 127 128
    std::cout << "    In FunctionInterface<...> functor." << std::endl;
    g_FunctionCalls++;
  }
129 130 131 132 133 134 135 136 137 138

  void operator()(
    const vtkm::internal::FunctionInterface<void(vtkm::cont::ArrayHandle<vtkm::FloatDefault>,
                                                 vtkm::cont::ArrayHandleVirtual<vtkm::FloatDefault>,
                                                 vtkm::cont::ArrayHandleVirtual<std::string>,
                                                 vtkm::cont::CellSetStructured<3>)>&) const
  {
    std::cout << "    In FunctionInterface<...> functor." << std::endl;
    g_FunctionCalls++;
  }
139 140 141 142 143 144 145
};

void TestBasicTransform()
{
  std::cout << "Testing basic transform." << std::endl;

  vtkm::cont::internal::DynamicTransform transform;
146
  vtkm::internal::IndexTag<1> indexTag;
147 148

  std::cout << "  Trying with simple scalar." << std::endl;
149
  TRY_TRANSFORM(transform(vtkm::FloatDefault(5), ScalarFunctor(), indexTag));
150 151

  std::cout << "  Trying with basic scalar array." << std::endl;
152
  vtkm::cont::ArrayHandle<vtkm::FloatDefault> concreteArray;
153
  TRY_TRANSFORM(transform(concreteArray, ArrayHandleScalarFunctor(), indexTag));
154 155

  std::cout << "  Trying scalar dynamic array." << std::endl;
156
  vtkm::cont::VariantArrayHandle dynamicArray = concreteArray;
157
  TRY_TRANSFORM(transform(dynamicArray, ArrayHandleScalarFunctor(), indexTag));
158 159 160

  std::cout << "  Trying with unusual (string) dynamic array." << std::endl;
  dynamicArray = vtkm::cont::ArrayHandle<std::string>();
161 162
  TRY_TRANSFORM(
    transform(dynamicArray.ResetTypes(TypeListTagString()), ArrayHandleStringFunctor(), indexTag));
163 164 165

  std::cout << "  Trying with structured cell set." << std::endl;
  vtkm::cont::CellSetStructured<3> concreteCellSet;
166
  TRY_TRANSFORM(transform(concreteCellSet, CellSetStructuredFunctor(), indexTag));
167 168 169

  std::cout << "  Trying with dynamic cell set." << std::endl;
  vtkm::cont::DynamicCellSet dynamicCellSet = concreteCellSet;
170
  TRY_TRANSFORM(transform(dynamicCellSet, CellSetStructuredFunctor(), indexTag));
171 172 173 174 175 176
}

void TestFunctionTransform()
{
  std::cout << "Testing transforms in FunctionInterface." << std::endl;

177
  vtkm::cont::ArrayHandle<vtkm::FloatDefault> scalarArray;
178
  vtkm::cont::ArrayHandle<std::string> stringArray;
179
  vtkm::cont::CellSetStructured<3> structuredCellSet;
180

181 182 183
  std::cout << "  Trying basic functor call w/o transform (make sure it works)." << std::endl;
  TRY_TRANSFORM(FunctionInterfaceFunctor()(vtkm::internal::make_FunctionInterface<void>(
    scalarArray, scalarArray, stringArray, structuredCellSet)));
184 185 186

  std::cout << "  Trying dynamic cast" << std::endl;
  TRY_TRANSFORM(
187
    vtkm::internal::make_FunctionInterface<void>(
188
      scalarArray,
189 190
      vtkm::cont::VariantArrayHandle(scalarArray),
      vtkm::cont::VariantArrayHandle(stringArray).ResetTypes(TypeListTagString()),
191 192
      vtkm::cont::DynamicCellSet(structuredCellSet))
      .DynamicTransformCont(vtkm::cont::internal::DynamicTransform(), FunctionInterfaceFunctor()));
193 194 195 196 197 198 199 200 201 202
}

void TestDynamicTransform()
{
  TestBasicTransform();
  TestFunctionTransform();
}

} // anonymous namespace

203
int UnitTestDynamicTransform(int, char* [])
204 205 206
{
  return vtkm::cont::testing::Testing::Run(TestDynamicTransform);
}