1 // -*- C++ -*- 2 //===-- count.pass.cpp ----------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 // Tests for count and count_if 11 #include "support/pstl_test_config.h" 12 13 #include <execution> 14 #include <algorithm> 15 16 #include "support/utils.h" 17 18 using namespace TestUtils; 19 20 struct test_count 21 { 22 template <typename Policy, typename Iterator, typename T> 23 void 24 operator()(Policy&& exec, Iterator first, Iterator last, T needle) 25 { 26 auto expected = std::count(first, last, needle); 27 auto result = std::count(exec, first, last, needle); 28 EXPECT_EQ(expected, result, "wrong count result"); 29 } 30 }; 31 32 struct test_count_if 33 { 34 template <typename Policy, typename Iterator, typename Predicate> 35 void 36 operator()(Policy&& exec, Iterator first, Iterator last, Predicate pred) 37 { 38 auto expected = std::count_if(first, last, pred); 39 auto result = std::count_if(exec, first, last, pred); 40 EXPECT_EQ(expected, result, "wrong count_if result"); 41 } 42 }; 43 44 template <typename T> 45 class IsEqual 46 { 47 T value; 48 49 public: 50 IsEqual(T value_, OddTag) : value(value_) {} 51 bool 52 operator()(const T& x) const 53 { 54 return x == value; 55 } 56 }; 57 58 template <typename In, typename T, typename Predicate, typename Convert> 59 void 60 test(T needle, Predicate pred, Convert convert) 61 { 62 // Try sequences of various lengths. 63 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 64 { 65 Sequence<In> in(n, [=](size_t k) -> In { 66 // Sprinkle "42" and "50" early, so that short sequences have non-zero count. 67 return convert((n - k - 1) % 3 == 0 ? 42 : (n - k - 2) % 5 == 0 ? 50 : 3 * (int(k) % 1000 - 500)); 68 }); 69 invoke_on_all_policies(test_count(), in.begin(), in.end(), needle); 70 invoke_on_all_policies(test_count_if(), in.begin(), in.end(), pred); 71 72 invoke_on_all_policies(test_count(), in.cbegin(), in.cend(), needle); 73 invoke_on_all_policies(test_count_if(), in.cbegin(), in.cend(), pred); 74 } 75 } 76 77 struct test_non_const 78 { 79 template <typename Policy, typename Iterator> 80 void 81 operator()(Policy&& exec, Iterator iter) 82 { 83 auto is_even = [&](float64_t v) { 84 uint32_t i = (uint32_t)v; 85 return i % 2 == 0; 86 }; 87 count_if(exec, iter, iter, non_const(is_even)); 88 } 89 }; 90 91 int32_t 92 main() 93 { 94 test<int32_t>(42, IsEqual<int32_t>(50, OddTag()), [](int32_t j) { return j; }); 95 #if !_PSTL_ICC_16_17_TEST_REDUCTION_RELEASE_BROKEN 96 test<int32_t>(42, [](const int32_t&) { return true; }, [](int32_t j) { return j; }); 97 #endif 98 test<float64_t>(42, IsEqual<float64_t>(50, OddTag()), [](int32_t j) { return float64_t(j); }); 99 test<Number>(Number(42, OddTag()), IsEqual<Number>(Number(50, OddTag()), OddTag()), 100 [](int32_t j) { return Number(j, OddTag()); }); 101 102 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); 103 104 std::cout << done() << std::endl; 105 return 0; 106 } 107