1 // -*- C++ -*- 2 //===-- all_of.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 #include "support/pstl_test_config.h" 11 12 #ifdef PSTL_STANDALONE_TESTS 13 #include "pstl/execution" 14 #include "pstl/algorithm" 15 #else 16 #include <execution> 17 #include <algorithm> 18 #endif // PSTL_STANDALONE_TESTS 19 20 #include "support/utils.h" 21 22 /* 23 TODO: consider implementing the following tests for a better code coverage 24 - correctness 25 - bad input argument (if applicable) 26 - data corruption around/of input and output 27 - correctly work with nested parallelism 28 - check that algorithm does not require anything more than is described in its requirements section 29 */ 30 31 using namespace TestUtils; 32 33 struct test_all_of 34 { 35 template <typename ExecutionPolicy, typename Iterator, typename Predicate> 36 void 37 operator()(ExecutionPolicy&& exec, Iterator begin, Iterator end, Predicate pred, bool expected) 38 { 39 40 auto actualr = std::all_of(exec, begin, end, pred); 41 EXPECT_EQ(expected, actualr, "result for all_of"); 42 } 43 }; 44 45 template <typename T> 46 struct Parity 47 { 48 bool parity; 49 50 public: 51 Parity(bool parity_) : parity(parity_) {} 52 bool 53 operator()(T value) const 54 { 55 return (size_t(value) ^ parity) % 2 == 0; 56 } 57 }; 58 59 template <typename T> 60 void 61 test(size_t bits) 62 { 63 for (size_t n = 0; n <= 100000; n = n <= 16 ? n + 1 : size_t(3.1415 * n)) 64 { 65 66 // Sequence of odd values 67 Sequence<T> in(n, [n, bits](size_t k) { return T(2 * HashBits(n, bits - 1) ^ 1); }); 68 69 // Even value, or false when T is bool. 70 T spike(2 * HashBits(n, bits - 1)); 71 Sequence<T> inCopy(in); 72 73 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), true); 74 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), true); 75 EXPECT_EQ(in, inCopy, "all_of modified input sequence"); 76 if (n > 0) 77 { 78 // Sprinkle in a miss 79 in[2 * n / 3] = spike; 80 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false); 81 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false); 82 83 // Sprinkle in a few more misses 84 in[n / 2] = spike; 85 in[n / 3] = spike; 86 invoke_on_all_policies(test_all_of(), in.begin(), in.end(), Parity<T>(1), false); 87 invoke_on_all_policies(test_all_of(), in.cbegin(), in.cend(), Parity<T>(1), false); 88 } 89 } 90 } 91 92 struct test_non_const 93 { 94 template <typename Policy, typename Iterator> 95 void 96 operator()(Policy&& exec, Iterator iter) 97 { 98 auto is_even = [&](float64_t v) { 99 uint32_t i = (uint32_t)v; 100 return i % 2 == 0; 101 }; 102 all_of(exec, iter, iter, non_const(is_even)); 103 } 104 }; 105 106 int32_t 107 main() 108 { 109 test<int32_t>(8 * sizeof(int32_t)); 110 test<uint16_t>(8 * sizeof(uint16_t)); 111 test<float64_t>(53); 112 #if !__PSTL_ICC_16_17_TEST_REDUCTION_BOOL_TYPE_RELEASE_64_BROKEN 113 test<bool>(1); 114 #endif 115 116 test_algo_basic_single<int32_t>(run_for_rnd_fw<test_non_const>()); 117 118 std::cout << done() << std::endl; 119 return 0; 120 } 121