22#include < thrust/adjacent_difference.h>
33#include < thrust/iterator/discard_iterator.h>
44#include < thrust/iterator/retag.h>
5+ #include < thrust/device_malloc.h>
6+ #include < thrust/device_free.h>
57
68template <class Vector >
79void TestAdjacentDifferenceSimple (void )
@@ -13,21 +15,21 @@ void TestAdjacentDifferenceSimple(void)
1315 input[0 ] = 1 ; input[1 ] = 4 ; input[2 ] = 6 ;
1416
1517 typename Vector::iterator result;
16-
18+
1719 result = thrust::adjacent_difference (input.begin (), input.end (), output.begin ());
1820
1921 ASSERT_EQUAL (result - output.begin (), 3 );
2022 ASSERT_EQUAL (output[0 ], T (1 ));
2123 ASSERT_EQUAL (output[1 ], T (3 ));
2224 ASSERT_EQUAL (output[2 ], T (2 ));
23-
25+
2426 result = thrust::adjacent_difference (input.begin (), input.end (), output.begin (), thrust::plus<T>());
25-
27+
2628 ASSERT_EQUAL (result - output.begin (), 3 );
2729 ASSERT_EQUAL (output[0 ], T ( 1 ));
2830 ASSERT_EQUAL (output[1 ], T ( 5 ));
2931 ASSERT_EQUAL (output[2 ], T (10 ));
30-
32+
3133 // test in-place operation, result and first are permitted to be the same
3234 result = thrust::adjacent_difference (input.begin (), input.end (), input.begin ());
3335
@@ -57,14 +59,14 @@ void TestAdjacentDifference(const size_t n)
5759 ASSERT_EQUAL (std::size_t (h_result - h_output.begin ()), n);
5860 ASSERT_EQUAL (std::size_t (d_result - d_output.begin ()), n);
5961 ASSERT_EQUAL (h_output, d_output);
60-
62+
6163 h_result = thrust::adjacent_difference (h_input.begin (), h_input.end (), h_output.begin (), thrust::plus<T>());
6264 d_result = thrust::adjacent_difference (d_input.begin (), d_input.end (), d_output.begin (), thrust::plus<T>());
6365
6466 ASSERT_EQUAL (std::size_t (h_result - h_output.begin ()), n);
6567 ASSERT_EQUAL (std::size_t (d_result - d_output.begin ()), n);
6668 ASSERT_EQUAL (h_output, d_output);
67-
69+
6870 // in-place operation
6971 h_result = thrust::adjacent_difference (h_input.begin (), h_input.end (), h_input.begin (), thrust::plus<T>());
7072 d_result = thrust::adjacent_difference (d_input.begin (), d_input.end (), d_input.begin (), thrust::plus<T>());
@@ -90,7 +92,7 @@ void TestAdjacentDifferenceInPlaceWithRelatedIteratorTypes(const size_t n)
9092
9193 h_result = thrust::adjacent_difference (h_input.begin (), h_input.end (), h_output.begin (), thrust::plus<T>());
9294 d_result = thrust::adjacent_difference (d_input.begin (), d_input.end (), d_output.begin (), thrust::plus<T>());
93-
95+
9496 // in-place operation with different iterator types
9597 h_result = thrust::adjacent_difference (h_input.cbegin (), h_input.cend (), h_input.begin (), thrust::plus<T>());
9698 d_result = thrust::adjacent_difference (d_input.cbegin (), d_input.cend (), d_input.begin (), thrust::plus<T>());
@@ -160,3 +162,51 @@ void TestAdjacentDifferenceDispatchImplicit()
160162}
161163DECLARE_UNITTEST (TestAdjacentDifferenceDispatchImplicit);
162164
165+ struct detect_wrong_difference
166+ {
167+ bool * flag;
168+
169+ __host__ __device__ detect_wrong_difference operator ++() const { return *this ; }
170+ __host__ __device__ detect_wrong_difference operator *() const { return *this ; }
171+ template <typename Difference>
172+ __host__ __device__ detect_wrong_difference operator +(Difference) const { return *this ; }
173+ template <typename Index>
174+ __host__ __device__ detect_wrong_difference operator [](Index) const { return *this ; }
175+
176+ __device__
177+ void operator =(long long difference) const
178+ {
179+ if (difference != 1 )
180+ {
181+ *flag = false ;
182+ }
183+ }
184+ };
185+
186+ void TestAdjacentDifferenceWithBigIndexesHelper (int magnitude)
187+ {
188+ thrust::counting_iterator<long long > begin (1 );
189+ thrust::counting_iterator<long long > end = begin + (1ll << magnitude);
190+ ASSERT_EQUAL (thrust::distance (begin, end), 1ll << magnitude);
191+
192+ thrust::device_ptr<bool > all_differences_correct = thrust::device_malloc<bool >(1 );
193+ *all_differences_correct = true ;
194+
195+ detect_wrong_difference out = { thrust::raw_pointer_cast (all_differences_correct) };
196+
197+ thrust::adjacent_difference (thrust::device, begin, end, out);
198+
199+ bool all_differences_correct_h = *all_differences_correct;
200+ thrust::device_free (all_differences_correct);
201+
202+ ASSERT_EQUAL (all_differences_correct_h, true );
203+ }
204+
205+ void TestAdjacentDifferenceWithBigIndexes ()
206+ {
207+ TestAdjacentDifferenceWithBigIndexesHelper (30 );
208+ TestAdjacentDifferenceWithBigIndexesHelper (31 );
209+ TestAdjacentDifferenceWithBigIndexesHelper (32 );
210+ TestAdjacentDifferenceWithBigIndexesHelper (33 );
211+ }
212+ DECLARE_UNITTEST (TestAdjacentDifferenceWithBigIndexes);
0 commit comments