1 #include <unittest/unittest.h>
2 #include <thrust/iterator/constant_iterator.h>
3 #include <thrust/copy.h>
4 #include <thrust/transform.h>
5 #include <thrust/reduce.h>
6 
TestConstantIteratorConstructFromConvertibleSystem(void)7 void TestConstantIteratorConstructFromConvertibleSystem(void)
8 {
9   using namespace thrust;
10 
11   constant_iterator<int> default_system(13);
12 
13   constant_iterator<int, use_default, host_system_tag> host_system = default_system;
14   ASSERT_EQUAL(*default_system, *host_system);
15 
16   constant_iterator<int, use_default, device_system_tag> device_system = default_system;
17   ASSERT_EQUAL(*default_system, *device_system);
18 }
19 DECLARE_UNITTEST(TestConstantIteratorConstructFromConvertibleSystem);
20 
TestConstantIteratorIncrement(void)21 void TestConstantIteratorIncrement(void)
22 {
23     using namespace thrust;
24 
25     constant_iterator<int> lhs(0,0);
26     constant_iterator<int> rhs(0,0);
27 
28     ASSERT_EQUAL(0, lhs - rhs);
29 
30     lhs++;
31 
32     ASSERT_EQUAL(1, lhs - rhs);
33 
34     lhs++;
35     lhs++;
36 
37     ASSERT_EQUAL(3, lhs - rhs);
38 
39     lhs += 5;
40 
41     ASSERT_EQUAL(8, lhs - rhs);
42 
43     lhs -= 10;
44 
45     ASSERT_EQUAL(-2, lhs - rhs);
46 }
47 DECLARE_UNITTEST(TestConstantIteratorIncrement);
48 
TestConstantIteratorComparison(void)49 void TestConstantIteratorComparison(void)
50 {
51     using namespace thrust;
52 
53     constant_iterator<int> iter1(0);
54     constant_iterator<int> iter2(0);
55 
56     ASSERT_EQUAL(0, iter1 - iter2);
57     ASSERT_EQUAL(true, iter1 == iter2);
58 
59     iter1++;
60 
61     ASSERT_EQUAL(1, iter1 - iter2);
62     ASSERT_EQUAL(false, iter1 == iter2);
63 
64     iter2++;
65 
66     ASSERT_EQUAL(0, iter1 - iter2);
67     ASSERT_EQUAL(true, iter1 == iter2);
68 
69     iter1 += 100;
70     iter2 += 100;
71 
72     ASSERT_EQUAL(0, iter1 - iter2);
73     ASSERT_EQUAL(true, iter1 == iter2);
74 }
75 DECLARE_UNITTEST(TestConstantIteratorComparison);
76 
77 
TestMakeConstantIterator(void)78 void TestMakeConstantIterator(void)
79 {
80     using namespace thrust;
81 
82     // test one argument version
83     constant_iterator<int> iter0 = make_constant_iterator<int>(13);
84 
85     ASSERT_EQUAL(13, *iter0);
86 
87     // test two argument version
88     constant_iterator<int,int> iter1 = make_constant_iterator<int,int>(13, 7);
89 
90     ASSERT_EQUAL(13, *iter1);
91     ASSERT_EQUAL(7, iter1 - iter0);
92 }
93 DECLARE_UNITTEST(TestMakeConstantIterator);
94 
95 
96 template<typename Vector>
TestConstantIteratorCopy(void)97 void TestConstantIteratorCopy(void)
98 {
99   using namespace thrust;
100 
101   typedef constant_iterator<int> ConstIter;
102 
103   Vector result(4);
104 
105   ConstIter first = make_constant_iterator<int>(7);
106   ConstIter last  = first + result.size();
107   thrust::copy(first, last, result.begin());
108 
109   ASSERT_EQUAL(7, result[0]);
110   ASSERT_EQUAL(7, result[1]);
111   ASSERT_EQUAL(7, result[2]);
112   ASSERT_EQUAL(7, result[3]);
113 };
114 DECLARE_VECTOR_UNITTEST(TestConstantIteratorCopy);
115 
116 
117 template<typename Vector>
TestConstantIteratorTransform(void)118 void TestConstantIteratorTransform(void)
119 {
120   using namespace thrust;
121 
122   typedef typename Vector::value_type T;
123   typedef constant_iterator<T> ConstIter;
124 
125   Vector result(4);
126 
127   ConstIter first1 = make_constant_iterator<T>(7);
128   ConstIter last1  = first1 + result.size();
129   ConstIter first2 = make_constant_iterator<T>(3);
130 
131   thrust::transform(first1, last1, result.begin(), thrust::negate<T>());
132 
133   ASSERT_EQUAL(-7, result[0]);
134   ASSERT_EQUAL(-7, result[1]);
135   ASSERT_EQUAL(-7, result[2]);
136   ASSERT_EQUAL(-7, result[3]);
137 
138   thrust::transform(first1, last1, first2, result.begin(), thrust::plus<T>());
139 
140   ASSERT_EQUAL(10, result[0]);
141   ASSERT_EQUAL(10, result[1]);
142   ASSERT_EQUAL(10, result[2]);
143   ASSERT_EQUAL(10, result[3]);
144 };
145 DECLARE_VECTOR_UNITTEST(TestConstantIteratorTransform);
146 
147 
TestConstantIteratorReduce(void)148 void TestConstantIteratorReduce(void)
149 {
150   using namespace thrust;
151 
152   typedef int T;
153   typedef constant_iterator<T> ConstIter;
154 
155   ConstIter first = make_constant_iterator<T>(7);
156   ConstIter last  = first + 4;
157 
158   T sum = thrust::reduce(first, last);
159 
160   ASSERT_EQUAL(sum, 4 * 7);
161 };
162 DECLARE_UNITTEST(TestConstantIteratorReduce);
163 
164