Extended binary search tests
MODIFY
test/CMakeLists.txt
test/CMakeLists.txt
@@ -58,7 +58,9 @@ endfunction()
add_hipthrust_test("thrust.hip.allocator" test_allocator.cpp)
add_hipthrust_test("thrust.hip.binary_search" test_binary_search.cpp)
+add_hipthrust_test("thrust.hip.binary_search_descending" test_binary_search_descending.cpp)
add_hipthrust_test("thrust.hip.binary_search_vector" test_binary_search_vector.cpp)
+add_hipthrust_test("thrust.hip.binary_search_vector_descending" test_binary_search_vector_descending.cpp)
# add_hipthrust_test("thrust.hip.complex" test_complex.cpp)
# add_hipthrust_test("thrust.hip.complex_transform" test_complex_transform.cpp)
add_hipthrust_test("thrust.hip.constant_iterator" test_constant_iterator.cpp)

ADD
test/test_binary_search_descending.cpp
+// MIT License
+//
+// Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+// Google Test
+#include <gtest/gtest.h>
+
+// Thrust
+#include <thrust/binary_search.h>
+#include <thrust/functional.h>
+
+#include <thrust/sequence.h>
+#include <thrust/sort.h>
+
+// HIP API
+#if THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+#include <hip/hip_runtime_api.h>
+
+#define HIP_CHECK(condition) ASSERT_EQ(condition, hipSuccess)
+#endif // THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+
+#include "test_utils.hpp"
+#include "test_assertions.hpp"
+
+template<
+ class InputType
+>
+struct Params
+{
+ using input_type = InputType;
+};
+
+template<class Params>
+class BinarySearchDescendingTests : public ::testing::Test
+{
+public:
+ using input_type = typename Params::input_type;
+};
+
+typedef ::testing::Types<
+ Params<thrust::host_vector<short>>,
+ Params<thrust::host_vector<int>>,
+ Params<thrust::host_vector<long long>>,
+ Params<thrust::host_vector<unsigned short>>,
+ Params<thrust::host_vector<unsigned int>>,
+ Params<thrust::host_vector<unsigned long long>>,
+ Params<thrust::host_vector<float>>,
+ Params<thrust::host_vector<double>>,
+ Params<thrust::device_vector<short>>,
+ Params<thrust::device_vector<int>>,
+ Params<thrust::device_vector<long long>>,
+ Params<thrust::device_vector<unsigned short>>,
+ Params<thrust::device_vector<unsigned int>>,
+ Params<thrust::device_vector<unsigned long long>>,
+ Params<thrust::device_vector<float>>,
+ Params<thrust::device_vector<double>>
+> BinarySearchDescendingTestsParams;
+
+TYPED_TEST_CASE(BinarySearchDescendingTests, BinarySearchDescendingTestsParams);
+
+TYPED_TEST(BinarySearchDescendingTests, TestScalarLowerBoundDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using T = typename Vector::value_type;
+ Vector vec(5);
+
+ vec[0] = 8;
+ vec[1] = 7;
+ vec[2] = 5;
+ vec[3] = 2;
+ vec[4] = 0;
+
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::lower_bound(vec.begin(), vec.end(), 0, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::lower_bound(vec.begin(), vec.end(), 1, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 2, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 3, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::lower_bound(vec.begin(), vec.end(), 4, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::lower_bound(vec.begin(), vec.end(), 5, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::lower_bound(vec.begin(), vec.end(), 6, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 1, thrust::lower_bound(vec.begin(), vec.end(), 7, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::lower_bound(vec.begin(), vec.end(), 8, thrust::greater<T>()));
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::lower_bound(vec.begin(), vec.end(), 9, thrust::greater<T>()));
+}
+
+TYPED_TEST(BinarySearchDescendingTests, TestScalarUpperBoundDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using T = typename Vector::value_type;
+ Vector vec(5);
+
+ vec[0] = 8;
+ vec[1] = 7;
+ vec[2] = 5;
+ vec[3] = 2;
+ vec[4] = 0;
+
+ ASSERT_EQ_QUIET(vec.begin() + 5, thrust::upper_bound(vec.begin(), vec.end(), 0, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::upper_bound(vec.begin(), vec.end(), 1, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::upper_bound(vec.begin(), vec.end(), 2, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 3, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 4, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::upper_bound(vec.begin(), vec.end(), 5, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::upper_bound(vec.begin(), vec.end(), 6, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::upper_bound(vec.begin(), vec.end(), 7, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 1, thrust::upper_bound(vec.begin(), vec.end(), 8, thrust::greater<int>()));
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::upper_bound(vec.begin(), vec.end(), 9, thrust::greater<int>()));
+}
+
+TYPED_TEST(BinarySearchDescendingTests, TestScalarBinarySearchDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using T = typename Vector::value_type;
+ Vector vec(5);
+
+ vec[0] = 8;
+ vec[1] = 7;
+ vec[2] = 5;
+ vec[3] = 2;
+ vec[4] = 0;
+
+ ASSERT_EQ(true, thrust::binary_search(vec.begin(), vec.end(), 0, thrust::greater<int>()));
+ ASSERT_EQ(false, thrust::binary_search(vec.begin(), vec.end(), 1, thrust::greater<int>()));
+ ASSERT_EQ(true, thrust::binary_search(vec.begin(), vec.end(), 2, thrust::greater<int>()));
+ ASSERT_EQ(false, thrust::binary_search(vec.begin(), vec.end(), 3, thrust::greater<int>()));
+ ASSERT_EQ(false, thrust::binary_search(vec.begin(), vec.end(), 4, thrust::greater<int>()));
+ ASSERT_EQ(true, thrust::binary_search(vec.begin(), vec.end(), 5, thrust::greater<int>()));
+ ASSERT_EQ(false, thrust::binary_search(vec.begin(), vec.end(), 6, thrust::greater<int>()));
+ ASSERT_EQ(true, thrust::binary_search(vec.begin(), vec.end(), 7, thrust::greater<int>()));
+ ASSERT_EQ(true, thrust::binary_search(vec.begin(), vec.end(), 8, thrust::greater<int>()));
+ ASSERT_EQ(false, thrust::binary_search(vec.begin(), vec.end(), 9, thrust::greater<int>()));
+}
+
+TYPED_TEST(BinarySearchDescendingTests, TestScalarEqualRangeDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using T = typename Vector::value_type;
+ Vector vec(5);
+
+ vec[0] = 8;
+ vec[1] = 7;
+ vec[2] = 5;
+ vec[3] = 2;
+ vec[4] = 0;
+
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 0, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 1, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 2, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 3, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 4, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 5, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 6, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 1, thrust::equal_range(vec.begin(), vec.end(), 7, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 8, thrust::greater<int>()).first);
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 9, thrust::greater<int>()).first);
+
+ ASSERT_EQ_QUIET(vec.begin() + 5, thrust::equal_range(vec.begin(), vec.end(), 0, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 1, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 4, thrust::equal_range(vec.begin(), vec.end(), 2, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 3, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 4, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 3, thrust::equal_range(vec.begin(), vec.end(), 5, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 6, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 2, thrust::equal_range(vec.begin(), vec.end(), 7, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 1, thrust::equal_range(vec.begin(), vec.end(), 8, thrust::greater<int>()).second);
+ ASSERT_EQ_QUIET(vec.begin() + 0, thrust::equal_range(vec.begin(), vec.end(), 9, thrust::greater<int>()).second);
+}

ADD
test/test_binary_search_vector_descending.cpp
+// MIT License
+//
+// Copyright (c) 2018 Advanced Micro Devices, Inc. All rights reserved.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in all
+// copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+// SOFTWARE.
+
+// Google Test
+#include <gtest/gtest.h>
+
+// Thrust
+#include <thrust/binary_search.h>
+#include <thrust/functional.h>
+
+#include <thrust/sequence.h>
+#include <thrust/sort.h>
+
+// HIP API
+#if THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+#include <hip/hip_runtime_api.h>
+
+#define HIP_CHECK(condition) ASSERT_EQ(condition, hipSuccess)
+#endif // THRUST_DEVICE_COMPILER == THRUST_DEVICE_COMPILER_HCC
+
+#include "test_utils.hpp"
+#include "test_assertions.hpp"
+
+template<
+ class InputType
+>
+struct Params
+{
+ using input_type = InputType;
+};
+
+template<class Params>
+class BinarySearchVectorDescendingTests : public ::testing::Test
+{
+public:
+ using input_type = typename Params::input_type;
+};
+
+typedef ::testing::Types<
+ Params<thrust::host_vector<short>>,
+ Params<thrust::host_vector<int>>,
+ Params<thrust::host_vector<long long>>,
+ Params<thrust::host_vector<unsigned short>>,
+ Params<thrust::host_vector<unsigned int>>,
+ Params<thrust::host_vector<unsigned long long>>,
+ Params<thrust::host_vector<float>>,
+ Params<thrust::host_vector<double>>,
+ Params<thrust::device_vector<short>>,
+ Params<thrust::device_vector<int>>,
+ Params<thrust::device_vector<long long>>,
+ Params<thrust::device_vector<unsigned short>>,
+ Params<thrust::device_vector<unsigned int>>,
+ Params<thrust::device_vector<unsigned long long>>,
+ Params<thrust::device_vector<float>>,
+ Params<thrust::device_vector<double>>
+> BinarySearchVectorDescendingTestsParams;
+
+TYPED_TEST_CASE(BinarySearchVectorDescendingTests, BinarySearchVectorDescendingTestsParams);
+
+template<class Params>
+class BinarySearchVectorDescendingIntegerTests : public ::testing::Test
+{
+public:
+ using input_type = typename Params::input_type;
+};
+
+typedef ::testing::Types<
+ Params<short>,
+ Params<int>,
+ Params<long long>
+> BinarySearchVectorDescendingIntegerTestsParams;
+
+TYPED_TEST_CASE(BinarySearchVectorDescendingIntegerTests, BinarySearchVectorDescendingIntegerTestsParams);
+
+
+// convert xxx_vector<T1> to xxx_vector<T2>
+template <class ExampleVector, typename NewType>
+struct vector_like
+{
+ typedef typename ExampleVector::allocator_type alloc;
+ typedef typename alloc::template rebind<NewType>::other new_alloc;
+ typedef thrust::detail::vector_base<NewType, new_alloc> type;
+};
+
+TYPED_TEST(BinarySearchVectorDescendingTests, TestVectorLowerBoundDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ using T = typename Vector::value_type;
+ 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, int>::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_EQ_QUIET(integral_output.end(), output_end);
+
+ ASSERT_EQ(4, integral_output[0]);
+ ASSERT_EQ(4, integral_output[1]);
+ ASSERT_EQ(3, integral_output[2]);
+ ASSERT_EQ(3, integral_output[3]);
+ ASSERT_EQ(3, integral_output[4]);
+ ASSERT_EQ(2, integral_output[5]);
+ ASSERT_EQ(2, integral_output[6]);
+ ASSERT_EQ(1, integral_output[7]);
+ ASSERT_EQ(0, integral_output[8]);
+ ASSERT_EQ(0, integral_output[9]);
+}
+
+TYPED_TEST(BinarySearchVectorDescendingTests, TestVectorUpperBoundDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ 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());
+
+ using IntVector = typename vector_like<Vector, int>::type;
+
+ // 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<int>());
+
+ ASSERT_EQ_QUIET(output_end, integral_output.end());
+
+ ASSERT_EQ(5, integral_output[0]);
+ ASSERT_EQ(4, integral_output[1]);
+ ASSERT_EQ(4, integral_output[2]);
+ ASSERT_EQ(3, integral_output[3]);
+ ASSERT_EQ(3, integral_output[4]);
+ ASSERT_EQ(3, integral_output[5]);
+ ASSERT_EQ(2, integral_output[6]);
+ ASSERT_EQ(2, integral_output[7]);
+ ASSERT_EQ(1, integral_output[8]);
+ ASSERT_EQ(0, integral_output[9]);
+}
+
+TYPED_TEST(BinarySearchVectorDescendingTests, TestVectorBinarySearchDescendingSimple)
+{
+ using Vector = typename TestFixture::input_type;
+ 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());
+
+ using BoolVector = typename vector_like<Vector, bool>::type;
+ using IntVector = typename vector_like<Vector, int>::type;
+
+ // 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<int>());
+
+ ASSERT_EQ_QUIET(bool_output_end, bool_output.end());
+
+ ASSERT_EQ(true, bool_output[0]);
+ ASSERT_EQ(false, bool_output[1]);
+ ASSERT_EQ(true, bool_output[2]);
+ ASSERT_EQ(false, bool_output[3]);
+ ASSERT_EQ(false, bool_output[4]);
+ ASSERT_EQ(true, bool_output[5]);
+ ASSERT_EQ(false, bool_output[6]);
+ ASSERT_EQ(true, bool_output[7]);
+ ASSERT_EQ(true, bool_output[8]);
+ ASSERT_EQ(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<int>());
+
+ ASSERT_EQ_QUIET(int_output_end, integral_output.end());
+
+ ASSERT_EQ(1, integral_output[0]);
+ ASSERT_EQ(0, integral_output[1]);
+ ASSERT_EQ(1, integral_output[2]);
+ ASSERT_EQ(0, integral_output[3]);
+ ASSERT_EQ(0, integral_output[4]);
+ ASSERT_EQ(1, integral_output[5]);
+ ASSERT_EQ(0, integral_output[6]);
+ ASSERT_EQ(1, integral_output[7]);
+ ASSERT_EQ(1, integral_output[8]);
+ ASSERT_EQ(0, integral_output[9]);
+}
+
+TYPED_TEST(BinarySearchVectorDescendingIntegerTests, TestVectorLowerBoundDescending)
+{
+ using T = typename TestFixture::input_type;
+ for (auto size : get_sizes())
+ {
+ SCOPED_TRACE(testing::Message() << "with size = " << size);
+
+ thrust::host_vector<T> h_vec = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ 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 = get_random_data<T>(2*size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ thrust::device_vector<T> d_input = h_input;
+
+ thrust::host_vector<int> h_output(2*size);
+ thrust::device_vector<int> d_output(2*size);
+
+ 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_EQ(h_output, d_output);
+ }
+}
+
+TYPED_TEST(BinarySearchVectorDescendingIntegerTests, TestVectorUpperBoundDescending)
+{
+ using T = typename TestFixture::input_type;
+ for (auto size : get_sizes())
+ {
+ SCOPED_TRACE(testing::Message() << "with size = " << size);
+
+ thrust::host_vector<T> h_vec = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ 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 = get_random_data<T>(2*size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ thrust::device_vector<T> d_input = h_input;
+
+ thrust::host_vector<int> h_output(2*size);
+ thrust::device_vector<int> d_output(2*size);
+
+ 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_EQ(h_output, d_output);
+ }
+}
+
+TYPED_TEST(BinarySearchVectorDescendingIntegerTests, TestVectorBinarySearchDescending)
+{
+ using T = typename TestFixture::input_type;
+ for (auto size : get_sizes())
+ {
+ SCOPED_TRACE(testing::Message() << "with size = " << size);
+
+ thrust::host_vector<T> h_vec = get_random_data<T>(size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ 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 = get_random_data<T>(2*size,
+ std::numeric_limits<T>::min(),
+ std::numeric_limits<T>::max());
+ thrust::device_vector<T> d_input = h_input;
+
+ thrust::host_vector<int> h_output(2*size);
+ thrust::device_vector<int> d_output(2*size);
+
+ 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_EQ(h_output, d_output);
+ }
+}

MODIFY
testing/binary_search_vector_descending.cu
testing/binary_search_vector_descending.cu
@@ -160,8 +160,8 @@ struct TestVectorLowerBoundDescending
thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);
- thrust::lower_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
- thrust::lower_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());
+ 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);
}
@@ -174,7 +174,7 @@ 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<int>());
+ 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);
@@ -183,8 +183,8 @@ struct TestVectorUpperBoundDescending
thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);
- thrust::upper_bound(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
- thrust::upper_bound(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());
+ 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);
}
@@ -196,7 +196,7 @@ 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<int>());
+ 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);
@@ -205,11 +205,10 @@ struct TestVectorBinarySearchDescending
thrust::host_vector<int> h_output(2*n);
thrust::device_vector<int> d_output(2*n);
- thrust::binary_search(h_vec.begin(), h_vec.end(), h_input.begin(), h_input.end(), h_output.begin(), thrust::greater<int>());
- thrust::binary_search(d_vec.begin(), d_vec.end(), d_input.begin(), d_input.end(), d_output.begin(), thrust::greater<int>());
+ 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;
-

Extended binary search tests