Skip to content
This repository was archived by the owner on Mar 21, 2024. It is now read-only.

Commit d527624

Browse files
committed
unique: test with ForwardIterator parameters
1 parent 6b3b0c1 commit d527624

File tree

2 files changed

+67
-7
lines changed

2 files changed

+67
-7
lines changed

testing/unique.cu

+15-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <unittest/unittest.h>
2+
#include <unittest/iterator_helpers.h>
23
#include <thrust/unique.h>
34
#include <thrust/functional.h>
45
#include <thrust/iterator/discard_iterator.h>
@@ -163,11 +164,13 @@ void TestUniqueSimple(void)
163164
data[8] = 31;
164165
data[9] = 37;
165166

166-
typename Vector::iterator new_last;
167+
forward_iterator_wrapper<typename Vector::iterator> new_last;
168+
const auto begin = make_forward_iterator_wrapper(data.begin());
169+
const auto end = make_forward_iterator_wrapper(data.end());
167170

168-
new_last = thrust::unique(data.begin(), data.end());
171+
new_last = thrust::unique(begin, end);
169172

170-
ASSERT_EQUAL(new_last - data.begin(), 7);
173+
ASSERT_EQUAL(thrust::distance(begin, new_last), 7);
171174
ASSERT_EQUAL(data[0], 11);
172175
ASSERT_EQUAL(data[1], 12);
173176
ASSERT_EQUAL(data[2], 20);
@@ -176,9 +179,9 @@ void TestUniqueSimple(void)
176179
ASSERT_EQUAL(data[5], 31);
177180
ASSERT_EQUAL(data[6], 37);
178181

179-
new_last = thrust::unique(data.begin(), new_last, is_equal_div_10_unique<T>());
182+
new_last = thrust::unique(begin, new_last, is_equal_div_10_unique<T>());
180183

181-
ASSERT_EQUAL(new_last - data.begin(), 3);
184+
ASSERT_EQUAL(thrust::distance(begin, new_last), 3);
182185
ASSERT_EQUAL(data[0], 11);
183186
ASSERT_EQUAL(data[1], 20);
184187
ASSERT_EQUAL(data[2], 31);
@@ -327,11 +330,16 @@ void TestUniqueCountSimple(void)
327330
data[8] = 31;
328331
data[9] = 37;
329332

330-
int count = thrust::unique_count(data.begin(), data.end());
333+
int count = thrust::unique_count(
334+
make_forward_iterator_wrapper(data.begin()),
335+
make_forward_iterator_wrapper(data.end()));
331336

332337
ASSERT_EQUAL(count, 7);
333338

334-
int div_10_count = thrust::unique_count(data.begin(), data.end(), is_equal_div_10_unique<T>());
339+
int div_10_count = thrust::unique_count(
340+
make_forward_iterator_wrapper(data.begin()),
341+
make_forward_iterator_wrapper(data.end()),
342+
is_equal_div_10_unique<T>());
335343

336344
ASSERT_EQUAL(div_10_count, 3);
337345
}

testing/unittest/iterator_helpers.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#pragma once
2+
3+
#include <iterator>
4+
5+
6+
// Wraps an existing iterator into a forward iterator,
7+
// thus removing some of its functionality
8+
template <typename Iterator>
9+
struct forward_iterator_wrapper {
10+
// LegacyIterator requirements
11+
using reference = typename Iterator::reference;
12+
using pointer = typename Iterator::pointer;
13+
using value_type = typename Iterator::value_type;
14+
using difference_type = typename Iterator::difference_type;
15+
using iterator_category = std::forward_iterator_tag;
16+
17+
__host__ __device__ reference operator*() const {
18+
return *wrapped;
19+
}
20+
21+
__host__ __device__ forward_iterator_wrapper& operator++() {
22+
++wrapped;
23+
return *this;
24+
}
25+
26+
// LegacyInputIterator
27+
__host__ __device__ bool operator==(const forward_iterator_wrapper& other) {
28+
return wrapped == other.wrapped;
29+
}
30+
31+
__host__ __device__ bool operator!=(const forward_iterator_wrapper& other) {
32+
return !(*this == other);
33+
}
34+
35+
__host__ __device__ forward_iterator_wrapper operator++(int) {
36+
auto cpy = *this;
37+
++(*this);
38+
return cpy;
39+
}
40+
41+
__host__ __device__ pointer operator->() const {
42+
return wrapped.operator->();
43+
}
44+
45+
Iterator wrapped;
46+
};
47+
48+
49+
template <typename Iterator>
50+
forward_iterator_wrapper<Iterator> make_forward_iterator_wrapper(Iterator it) {
51+
return {it};
52+
}

0 commit comments

Comments
 (0)