UnitTestStorageImplicit.cxx 4.2 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.
//
Kenneth Moreland's avatar
Kenneth Moreland committed
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
//
Kenneth Moreland's avatar
Kenneth Moreland committed
13
//  Under the terms of Contract DE-NA0003525 with NTESS,
14
15
16
17
18
19
20
//  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.
//============================================================================

21
#define VTKM_STORAGE VTKM_STORAGE_ERROR
22
23

#include <vtkm/Types.h>
24
#include <vtkm/VecTraits.h>
25

26
#include <vtkm/cont/ArrayHandle.h>
27
28
#include <vtkm/cont/StorageImplicit.h>

29
30
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>

31
#include <vtkm/cont/testing/Testing.h>
32

33
34
namespace
{
35

36
37
const vtkm::Id ARRAY_SIZE = 10;

38
template <typename T>
39
struct TestImplicitStorage
40
{
41
  using ValueType = T;
42
43
  ValueType Temp;

44
  VTKM_EXEC_CONT
45
46
  TestImplicitStorage()
    : Temp(1)
47
48
49
  {
  }

50
  VTKM_EXEC_CONT
51
  vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
52

53
54
  VTKM_EXEC_CONT
  ValueType Get(vtkm::Id vtkmNotUsed(index)) const { return Temp; }
55
56
57
58
59
};

template <typename T>
struct TemplatedTests
{
60
  using StorageTagType = vtkm::cont::StorageTagImplicit<TestImplicitStorage<T>>;
61
  using StorageType = vtkm::cont::internal::Storage<T, StorageTagType>;
62

63
64
65
  using ValueType = typename StorageType::ValueType;
  using PortalType = typename StorageType::PortalType;
  using IteratorType = typename PortalType::IteratorType;
66
67
68

  void BasicAllocation()
  {
69
    StorageType arrayStorage;
70

71
    // The implicit portal defined for this test always returns ARRAY_SIZE for the
72
    // number of values. We should get that.
73
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
74
                     "Implicit Storage GetNumberOfValues returned wrong size.");
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
    // Make sure you can allocate and shrink to any value <= the reported portal size.
    arrayStorage.Allocate(ARRAY_SIZE / 2);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE / 2,
                     "Cannot re-Allocate array to half size.");

    arrayStorage.Allocate(0);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 0, "Cannot re-Allocate array to zero.");

    arrayStorage.Allocate(ARRAY_SIZE);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
                     "Cannot re-Allocate array to original size.");

    arrayStorage.Shrink(ARRAY_SIZE / 2);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE / 2,
                     "Cannot Shrink array to half size.");

    arrayStorage.Shrink(0);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 0, "Cannot Shrink array to zero.");

    arrayStorage.Shrink(ARRAY_SIZE);
    VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
                     "Cannot Shrink array to original size.");
98

99
100
    //verify that calling ReleaseResources doesn't throw an exception
    arrayStorage.ReleaseResources();
101
102
103

    //verify that you can allocate after releasing resources.
    arrayStorage.Allocate(ARRAY_SIZE);
104
105
106
  }

  void BasicAccess()
107
  {
108
    TestImplicitStorage<T> portal;
109
    vtkm::cont::ArrayHandle<T, StorageTagType> implictHandle(portal);
110
    VTKM_TEST_ASSERT(implictHandle.GetNumberOfValues() == ARRAY_SIZE, "handle has wrong size");
111
    VTKM_TEST_ASSERT(implictHandle.GetPortalConstControl().Get(0) == T(1),
112
113
                     "portals first values should be 1");
  }
114
115
116
117
118
119
120
121
122
123
124

  void operator()()
  {
    BasicAllocation();
    BasicAccess();
  }
};

struct TestFunctor
{
  template <typename T>
125
  void operator()(T) const
126
127
128
129
130
131
  {
    TemplatedTests<T> tests;
    tests();
  }
};

132
void TestStorageBasic()
133
{
134
  vtkm::testing::Testing::TryTypes(TestFunctor());
135
136
137
138
}

} // Anonymous namespace

139
int UnitTestStorageImplicit(int, char* [])
140
{
141
  return vtkm::cont::testing::Testing::Run(TestStorageBasic);
142
}