|
| 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 | +} |
0 commit comments