1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10
11 #include <vtkm/cont/internal/IteratorFromArrayPortal.h>
12
13 #include <vtkm/VecTraits.h>
14 #include <vtkm/cont/internal/ArrayPortalFromIterators.h>
15
16 #include <vtkm/cont/testing/Testing.h>
17
18 namespace
19 {
20
21 template <typename T>
22 struct TemplatedTests
23 {
24 static constexpr vtkm::Id ARRAY_SIZE = 10;
25
26 using ValueType = T;
27 using ComponentType = typename vtkm::VecTraits<ValueType>::ComponentType;
28
ExpectedValue__anon979376530111::TemplatedTests29 ValueType ExpectedValue(vtkm::Id index, ComponentType value)
30 {
31 return ValueType(static_cast<ComponentType>(index + static_cast<vtkm::Id>(value)));
32 }
33
34 template <class IteratorType>
FillIterator__anon979376530111::TemplatedTests35 void FillIterator(IteratorType begin, IteratorType end, ComponentType value)
36 {
37 vtkm::Id index = 0;
38 for (IteratorType iter = begin; iter != end; iter++)
39 {
40 *iter = ExpectedValue(index, value);
41 index++;
42 }
43 }
44
45 template <class IteratorType>
CheckIterator__anon979376530111::TemplatedTests46 bool CheckIterator(IteratorType begin, IteratorType end, ComponentType value)
47 {
48 vtkm::Id index = 0;
49 for (IteratorType iter = begin; iter != end; iter++)
50 {
51 if (ValueType(*iter) != ExpectedValue(index, value))
52 {
53 return false;
54 }
55 index++;
56 }
57 return true;
58 }
59
60 template <class PortalType>
CheckPortal__anon979376530111::TemplatedTests61 bool CheckPortal(const PortalType& portal, const ComponentType& value)
62 {
63 vtkm::cont::ArrayPortalToIterators<PortalType> iterators(portal);
64 return CheckIterator(iterators.GetBegin(), iterators.GetEnd(), value);
65 }
66
ORIGINAL_VALUE__anon979376530111::TemplatedTests67 ComponentType ORIGINAL_VALUE() { return 39; }
68
69 template <class ArrayPortalType>
TestIteratorRead__anon979376530111::TemplatedTests70 void TestIteratorRead(ArrayPortalType portal)
71 {
72 using IteratorType = vtkm::cont::internal::IteratorFromArrayPortal<ArrayPortalType>;
73
74 IteratorType begin = vtkm::cont::internal::make_IteratorBegin(portal);
75 IteratorType end = vtkm::cont::internal::make_IteratorEnd(portal);
76 VTKM_TEST_ASSERT(std::distance(begin, end) == ARRAY_SIZE,
77 "Distance between begin and end incorrect.");
78 VTKM_TEST_ASSERT(std::distance(end, begin) == -ARRAY_SIZE,
79 "Distance between begin and end incorrect.");
80
81 std::cout << " Check forward iteration." << std::endl;
82 VTKM_TEST_ASSERT(CheckIterator(begin, end, ORIGINAL_VALUE()), "Forward iteration wrong");
83
84 std::cout << " Check backward iteration." << std::endl;
85 IteratorType middle = end;
86 for (vtkm::Id index = portal.GetNumberOfValues() - 1; index >= 0; index--)
87 {
88 middle--;
89 ValueType value = *middle;
90 VTKM_TEST_ASSERT(value == ExpectedValue(index, ORIGINAL_VALUE()), "Backward iteration wrong");
91 }
92
93 std::cout << " Check advance" << std::endl;
94 middle = begin + ARRAY_SIZE / 2;
95 VTKM_TEST_ASSERT(std::distance(begin, middle) == ARRAY_SIZE / 2, "Bad distance to middle.");
96 VTKM_TEST_ASSERT(ValueType(*middle) == ExpectedValue(ARRAY_SIZE / 2, ORIGINAL_VALUE()),
97 "Bad value at middle.");
98 }
99
100 template <class ArrayPortalType>
TestIteratorWrite__anon979376530111::TemplatedTests101 void TestIteratorWrite(ArrayPortalType portal)
102 {
103 using IteratorType = vtkm::cont::internal::IteratorFromArrayPortal<ArrayPortalType>;
104
105 IteratorType begin = vtkm::cont::internal::make_IteratorBegin(portal);
106 IteratorType end = vtkm::cont::internal::make_IteratorEnd(portal);
107
108 static const ComponentType WRITE_VALUE = 73;
109
110 std::cout << " Write values to iterator." << std::endl;
111 FillIterator(begin, end, WRITE_VALUE);
112
113 std::cout << " Check values in portal." << std::endl;
114 VTKM_TEST_ASSERT(CheckPortal(portal, WRITE_VALUE),
115 "Did not get correct values when writing to iterator.");
116 }
117
operator ()__anon979376530111::TemplatedTests118 void operator()()
119 {
120 ValueType array[ARRAY_SIZE];
121
122 FillIterator(array, array + ARRAY_SIZE, ORIGINAL_VALUE());
123
124 ::vtkm::cont::internal::ArrayPortalFromIterators<ValueType*> portal(array, array + ARRAY_SIZE);
125 ::vtkm::cont::internal::ArrayPortalFromIterators<const ValueType*> const_portal(
126 array, array + ARRAY_SIZE);
127
128 std::cout << " Test read from iterator." << std::endl;
129 TestIteratorRead(portal);
130
131 std::cout << " Test read from const iterator." << std::endl;
132 TestIteratorRead(const_portal);
133
134 std::cout << " Test write to iterator." << std::endl;
135 TestIteratorWrite(portal);
136 }
137 };
138
139 struct TestFunctor
140 {
141 template <typename T>
operator ()__anon979376530111::TestFunctor142 void operator()(T) const
143 {
144 TemplatedTests<T> tests;
145 tests();
146 }
147 };
148
TestArrayIteratorFromArrayPortal()149 void TestArrayIteratorFromArrayPortal()
150 {
151 vtkm::testing::Testing::TryTypes(TestFunctor());
152 }
153
154 } // Anonymous namespace
155
UnitTestIteratorFromArrayPortal(int argc,char * argv[])156 int UnitTestIteratorFromArrayPortal(int argc, char* argv[])
157 {
158 return vtkm::cont::testing::Testing::Run(TestArrayIteratorFromArrayPortal, argc, argv);
159 }
160