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