Spaces:
Runtime error
Runtime error
File size: 7,940 Bytes
28958dc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
#include <unittest/unittest.h>
#include <thrust/binary_search.h>
#include <thrust/functional.h>
#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/sequence.h>
#include <thrust/sort.h>
//////////////////////
// Vector Functions //
//////////////////////
// convert xxx_vector<T1> to xxx_vector<T2>
template <class ExampleVector, typename NewType>
struct vector_like
{
typedef typename ExampleVector::allocator_type alloc;
typedef typename thrust::detail::allocator_traits<alloc> alloc_traits;
typedef typename alloc_traits::template rebind_alloc<NewType> new_alloc;
typedef thrust::detail::vector_base<NewType, new_alloc> type;
};
template <class Vector>
void TestVectorLowerBoundDescendingSimple(void)
{
typedef typename Vector::value_type T;
Vector vec(5);
vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;
Vector input(10);
thrust::sequence(input.begin(), input.end());
typedef typename Vector::difference_type int_type;
typedef typename vector_like<Vector, int_type>::type IntVector;
// test with integral output type
IntVector integral_output(10);
typename IntVector::iterator output_end = thrust::lower_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<T>());
ASSERT_EQUAL_QUIET(integral_output.end(), output_end);
ASSERT_EQUAL(4, integral_output[0]);
ASSERT_EQUAL(4, integral_output[1]);
ASSERT_EQUAL(3, integral_output[2]);
ASSERT_EQUAL(3, integral_output[3]);
ASSERT_EQUAL(3, integral_output[4]);
ASSERT_EQUAL(2, integral_output[5]);
ASSERT_EQUAL(2, integral_output[6]);
ASSERT_EQUAL(1, integral_output[7]);
ASSERT_EQUAL(0, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorLowerBoundDescendingSimple);
template <class Vector>
void TestVectorUpperBoundDescendingSimple(void)
{
Vector vec(5);
vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;
Vector input(10);
thrust::sequence(input.begin(), input.end());
typedef typename Vector::difference_type int_type;
typedef typename Vector::value_type T;
typedef typename vector_like<Vector, int_type>::type IntVector;
// test with integral output type
IntVector integral_output(10);
typename IntVector::iterator output_end = thrust::upper_bound(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<T>());
ASSERT_EQUAL_QUIET(output_end, integral_output.end());
ASSERT_EQUAL(5, integral_output[0]);
ASSERT_EQUAL(4, integral_output[1]);
ASSERT_EQUAL(4, integral_output[2]);
ASSERT_EQUAL(3, integral_output[3]);
ASSERT_EQUAL(3, integral_output[4]);
ASSERT_EQUAL(3, integral_output[5]);
ASSERT_EQUAL(2, integral_output[6]);
ASSERT_EQUAL(2, integral_output[7]);
ASSERT_EQUAL(1, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorUpperBoundDescendingSimple);
template <class Vector>
void TestVectorBinarySearchDescendingSimple(void)
{
Vector vec(5);
vec[0] = 8;
vec[1] = 7;
vec[2] = 5;
vec[3] = 2;
vec[4] = 0;
Vector input(10);
thrust::sequence(input.begin(), input.end());
typedef typename vector_like<Vector, bool>::type BoolVector;
typedef typename Vector::difference_type int_type;
typedef typename Vector::value_type T;
typedef typename vector_like<Vector, int_type>::type IntVector;
// test with boolean output type
BoolVector bool_output(10);
typename BoolVector::iterator bool_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), bool_output.begin(), thrust::greater<T>());
ASSERT_EQUAL_QUIET(bool_output_end, bool_output.end());
ASSERT_EQUAL(true, bool_output[0]);
ASSERT_EQUAL(false, bool_output[1]);
ASSERT_EQUAL(true, bool_output[2]);
ASSERT_EQUAL(false, bool_output[3]);
ASSERT_EQUAL(false, bool_output[4]);
ASSERT_EQUAL(true, bool_output[5]);
ASSERT_EQUAL(false, bool_output[6]);
ASSERT_EQUAL(true, bool_output[7]);
ASSERT_EQUAL(true, bool_output[8]);
ASSERT_EQUAL(false, bool_output[9]);
// test with integral output type
IntVector integral_output(10, 2);
typename IntVector::iterator int_output_end = thrust::binary_search(vec.begin(), vec.end(), input.begin(), input.end(), integral_output.begin(), thrust::greater<T>());
ASSERT_EQUAL_QUIET(int_output_end, integral_output.end());
ASSERT_EQUAL(1, integral_output[0]);
ASSERT_EQUAL(0, integral_output[1]);
ASSERT_EQUAL(1, integral_output[2]);
ASSERT_EQUAL(0, integral_output[3]);
ASSERT_EQUAL(0, integral_output[4]);
ASSERT_EQUAL(1, integral_output[5]);
ASSERT_EQUAL(0, integral_output[6]);
ASSERT_EQUAL(1, integral_output[7]);
ASSERT_EQUAL(1, integral_output[8]);
ASSERT_EQUAL(0, integral_output[9]);
}
DECLARE_VECTOR_UNITTEST(TestVectorBinarySearchDescendingSimple);
template <typename T>
struct TestVectorLowerBoundDescending
{
void operator()(const size_t n)
{
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<T>());
thrust::device_vector<T> d_vec = h_vec;
thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;
typedef typename thrust::host_vector<T>::difference_type int_type;
thrust::host_vector<int_type> h_output(2*n);
thrust::device_vector<int_type> d_output(2*n);
thrust::lower_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<T>());
thrust::lower_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<T>());
ASSERT_EQUAL(h_output, d_output);
}
};
VariableUnitTest<TestVectorLowerBoundDescending, SignedIntegralTypes> TestVectorLowerBoundDescendingInstance;
template <typename T>
struct TestVectorUpperBoundDescending
{
void operator()(const size_t n)
{
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<T>());
thrust::device_vector<T> d_vec = h_vec;
thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;
typedef typename thrust::host_vector<T>::difference_type int_type;
thrust::host_vector<int_type> h_output(2*n);
thrust::device_vector<int_type> d_output(2*n);
thrust::upper_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<T>());
thrust::upper_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<T>());
ASSERT_EQUAL(h_output, d_output);
}
};
VariableUnitTest<TestVectorUpperBoundDescending, SignedIntegralTypes> TestVectorUpperBoundDescendingInstance;
template <typename T>
struct TestVectorBinarySearchDescending
{
void operator()(const size_t n)
{
thrust::host_vector<T> h_vec = unittest::random_integers<T>(n); thrust::sort(h_vec.begin(), h_vec.end(), thrust::greater<T>());
thrust::device_vector<T> d_vec = h_vec;
thrust::host_vector<T> h_input = unittest::random_integers<T>(2*n);
thrust::device_vector<T> d_input = h_input;
typedef typename thrust::host_vector<T>::difference_type int_type;
thrust::host_vector<int_type> h_output(2*n);
thrust::device_vector<int_type> d_output(2*n);
thrust::binary_search(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<T>());
thrust::binary_search(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<T>());
ASSERT_EQUAL(h_output, d_output);
}
};
VariableUnitTest<TestVectorBinarySearchDescending, SignedIntegralTypes> TestVectorBinarySearchDescendingInstance;
|