#include #include #include template void TestCountSimple() { Vector data{1, 1, 0, 0, 1}; ASSERT_EQUAL(thrust::count(data.begin(), data.end(), 0), 2); ASSERT_EQUAL(thrust::count(data.begin(), data.end(), 1), 3); ASSERT_EQUAL(thrust::count(data.begin(), data.end(), 2), 0); } DECLARE_VECTOR_UNITTEST(TestCountSimple); template void TestCount(const size_t n) { thrust::host_vector h_data = unittest::random_samples(n); thrust::device_vector d_data = h_data; size_t cpu_result = thrust::count(h_data.begin(), h_data.end(), T(5)); size_t gpu_result = thrust::count(d_data.begin(), d_data.end(), T(5)); ASSERT_EQUAL(cpu_result, gpu_result); } DECLARE_VARIABLE_UNITTEST(TestCount); template struct greater_than_five { _CCCL_HOST_DEVICE bool operator()(const T& x) const { return x > 5; } }; template void TestCountIfSimple() { using T = typename Vector::value_type; Vector data{1, 6, 1, 9, 2}; ASSERT_EQUAL(thrust::count_if(data.begin(), data.end(), greater_than_five()), 2); } DECLARE_VECTOR_UNITTEST(TestCountIfSimple); template void TestCountIf(const size_t n) { thrust::host_vector h_data = unittest::random_samples(n); thrust::device_vector d_data = h_data; size_t cpu_result = thrust::count_if(h_data.begin(), h_data.end(), greater_than_five()); size_t gpu_result = thrust::count_if(d_data.begin(), d_data.end(), greater_than_five()); ASSERT_EQUAL(cpu_result, gpu_result); } DECLARE_VARIABLE_UNITTEST(TestCountIf); template void TestCountFromConstIteratorSimple() { Vector data{1, 1, 0, 0, 1}; ASSERT_EQUAL(thrust::count(data.cbegin(), data.cend(), 0), 2); ASSERT_EQUAL(thrust::count(data.cbegin(), data.cend(), 1), 3); ASSERT_EQUAL(thrust::count(data.cbegin(), data.cend(), 2), 0); } DECLARE_VECTOR_UNITTEST(TestCountFromConstIteratorSimple); template int count(my_system& system, InputIterator, InputIterator, EqualityComparable x) { system.validate_dispatch(); return x; } void TestCountDispatchExplicit() { thrust::device_vector vec(1); my_system sys(0); thrust::count(sys, vec.begin(), vec.end(), 13); ASSERT_EQUAL(true, sys.is_valid()); } DECLARE_UNITTEST(TestCountDispatchExplicit); template int count(my_tag, InputIterator /*first*/, InputIterator, EqualityComparable x) { return x; } void TestCountDispatchImplicit() { thrust::device_vector vec(1); auto result = thrust::count(thrust::retag(vec.begin()), thrust::retag(vec.end()), 13); ASSERT_EQUAL(13, result); } DECLARE_UNITTEST(TestCountDispatchImplicit); void TestCountWithBigIndexesHelper(int magnitude) { thrust::counting_iterator begin(1); thrust::counting_iterator end = begin + (1ll << magnitude); ASSERT_EQUAL(::cuda::std::distance(begin, end), 1ll << magnitude); long long result = thrust::count(thrust::device, begin, end, (1ll << magnitude) - 17); ASSERT_EQUAL(result, 1); } void TestCountWithBigIndexes() { TestCountWithBigIndexesHelper(30); #ifndef THRUST_FORCE_32_BIT_OFFSET_TYPE TestCountWithBigIndexesHelper(31); TestCountWithBigIndexesHelper(32); TestCountWithBigIndexesHelper(33); #endif } DECLARE_UNITTEST(TestCountWithBigIndexes);