Skip to content

Commit 9e60fb9

Browse files
Port functional_placeholders_logical Thrust test to Catch2 (#4882)
1 parent 33ff0b9 commit 9e60fb9

File tree

3 files changed

+91
-77
lines changed

3 files changed

+91
-77
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <thrust/detail/allocator/allocator_traits.h>
2+
#include <thrust/functional.h>
3+
#include <thrust/transform.h>
4+
5+
#include "catch2_test_helper.h"
6+
#include <unittest/unittest.h>
7+
8+
constexpr size_t num_samples = 10000;
9+
10+
template <typename Vector, typename U>
11+
struct rebind_vector;
12+
13+
template <typename T, typename U, typename Allocator>
14+
struct rebind_vector<thrust::host_vector<T, Allocator>, U>
15+
{
16+
using type = thrust::host_vector<U, typename thrust::detail::allocator_traits<Allocator>::template rebind_alloc<U>>;
17+
};
18+
19+
template <typename T, typename U, typename Allocator>
20+
struct rebind_vector<thrust::device_vector<T, Allocator>, U>
21+
{
22+
using type = thrust::device_vector<U, typename Allocator::template rebind<U>::other>;
23+
};
24+
25+
template <typename T, typename U, typename Allocator>
26+
struct rebind_vector<thrust::universal_vector<T, Allocator>, U>
27+
{
28+
using type = thrust::universal_vector<U, typename Allocator::template rebind<U>::other>;
29+
};
30+
31+
TEMPLATE_LIST_TEST_CASE("LogicalAndOr", "[functional]", vector_list)
32+
{
33+
using Vector = TestType;
34+
using T = typename Vector::value_type;
35+
using bool_vector = typename rebind_vector<Vector, bool>::type;
36+
Vector lhs = unittest::random_samples<T>(num_samples);
37+
Vector rhs = unittest::random_samples<T>(num_samples);
38+
39+
using namespace thrust::placeholders;
40+
bool_vector reference(lhs.size());
41+
bool_vector result(lhs.size());
42+
43+
SECTION("and")
44+
{
45+
thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), reference.begin(), ::cuda::std::logical_and<T>{});
46+
thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), _1 && _2);
47+
CHECK(reference == result);
48+
}
49+
SECTION("or")
50+
{
51+
thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), reference.begin(), ::cuda::std::logical_or<T>{});
52+
thrust::transform(lhs.begin(), lhs.end(), rhs.begin(), result.begin(), _1 || _2);
53+
CHECK(reference == result);
54+
}
55+
}
56+
57+
TEMPLATE_LIST_TEST_CASE("LogicalNot", "[functional]", integral_vector_list)
58+
{
59+
using Vector = TestType;
60+
using T = typename Vector::value_type;
61+
using bool_vector = typename rebind_vector<Vector, bool>::type;
62+
Vector input = unittest::random_samples<T>(num_samples);
63+
64+
if (input.size() > 0)
65+
{
66+
// produce at least one true in the output
67+
input[0] = T(0);
68+
}
69+
70+
using namespace thrust::placeholders;
71+
bool_vector reference(input.size());
72+
bool_vector result(input.size());
73+
74+
thrust::transform(input.begin(), input.end(), reference.begin(), ::cuda::std::logical_not<T>{});
75+
thrust::transform(input.begin(), input.end(), result.begin(), !_1);
76+
ASSERT_EQUAL(reference, result);
77+
}

thrust/testing/catch2_test_helper.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ using vector_list = cuda::std::__type_list<
5252
thrust::universal_vector<int>,
5353
thrust::universal_host_pinned_vector<int>>;
5454

55+
// corresponds to DECLARE_INTEGRAL_VECTOR_UNITTEST
56+
using integral_vector_list = cuda::std::__type_list<
57+
// host
58+
thrust::host_vector<signed char>,
59+
thrust::host_vector<short>,
60+
thrust::host_vector<int>,
61+
// device
62+
thrust::device_vector<signed char>,
63+
thrust::device_vector<short>,
64+
thrust::device_vector<int>,
65+
// universal
66+
thrust::universal_vector<int>,
67+
thrust::universal_host_pinned_vector<int>>;
68+
5569
// corresponds to DECLARE_VARIABLE_UNITTEST
5670
using variable_list =
5771
cuda::std::__type_list<signed char, unsigned char, short, unsigned short, int, unsigned int, float, double>;

thrust/testing/functional_placeholders_logical.cu

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)