1 // -*- C++ -*- 2 //===-- adjacent_find.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 #include <execution> 13 #include <algorithm> 14 15 #include "support/utils.h" 16 17 using namespace TestUtils; 18 19 struct test_adjacent_find 20 { 21 template <typename Policy, typename Iterator, typename Pred> 22 void 23 operator()(Policy&& exec, Iterator first, Iterator last, Pred pred) 24 { 25 using namespace std; 26 27 auto k = std::adjacent_find(first, last, pred); 28 auto i = adjacent_find(exec, first, last, pred); 29 EXPECT_TRUE(i == k, "wrong return value from adjacent_find with predicate"); 30 31 i = adjacent_find(exec, first, last); 32 EXPECT_TRUE(i == k, "wrong return value from adjacent_find without predicate"); 33 } 34 }; 35 36 template <typename T> 37 void 38 test_adjacent_find_by_type() 39 { 40 41 size_t counts[] = {2, 3, 500}; 42 for (int32_t c = 0; c < const_size(counts); ++c) 43 { 44 45 for (int32_t e = 0; e < (counts[c] >= 64 ? 64 : (counts[c] == 2 ? 1 : 2)); ++e) 46 { 47 Sequence<T> in(counts[c], [](int32_t v) -> T { return T(v); }); //fill 0...n 48 in[e] = in[e + 1] = -1; //make an adjacent pair 49 50 auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to<T>()); 51 EXPECT_TRUE(i == in.cbegin() + e, "std::adjacent_find returned wrong result"); 52 53 invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to<T>()); 54 invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to<T>()); 55 } 56 } 57 58 //special cases: size=0, size=1; 59 for (int32_t expect = 0; expect < 1; ++expect) 60 { 61 Sequence<T> in(expect, [](int32_t v) -> T { return T(v); }); //fill 0...n 62 auto i = std::adjacent_find(in.cbegin(), in.cend(), std::equal_to<T>()); 63 EXPECT_TRUE(i == in.cbegin() + expect, "std::adjacent_find returned wrong result"); 64 65 invoke_on_all_policies(test_adjacent_find(), in.begin(), in.end(), std::equal_to<T>()); 66 invoke_on_all_policies(test_adjacent_find(), in.cbegin(), in.cend(), std::equal_to<T>()); 67 } 68 69 //special cases: 70 Sequence<T> a1 = {5, 5, 5, 6, 7, 8, 9}; 71 invoke_on_all_policies(test_adjacent_find(), a1.begin(), a1.end(), std::equal_to<T>()); 72 invoke_on_all_policies(test_adjacent_find(), a1.begin() + 1, a1.end(), std::equal_to<T>()); 73 74 invoke_on_all_policies(test_adjacent_find(), a1.cbegin(), a1.cend(), std::equal_to<T>()); 75 invoke_on_all_policies(test_adjacent_find(), a1.cbegin() + 1, a1.cend(), std::equal_to<T>()); 76 77 Sequence<T> a2 = {5, 6, 7, 8, 9, 9}; 78 invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end(), std::equal_to<T>()); 79 invoke_on_all_policies(test_adjacent_find(), a2.begin(), a2.end() - 1, std::equal_to<T>()); 80 81 invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend(), std::equal_to<T>()); 82 invoke_on_all_policies(test_adjacent_find(), a2.cbegin(), a2.cend() - 1, std::equal_to<T>()); 83 84 Sequence<T> a3 = {5, 6, 6, 6, 7, 9, 9, 9, 9}; 85 invoke_on_all_policies(test_adjacent_find(), a3.begin(), a3.end(), std::equal_to<T>()); 86 87 invoke_on_all_policies(test_adjacent_find(), a3.cbegin(), a3.cend(), std::equal_to<T>()); 88 } 89 90 template <typename T> 91 struct test_non_const 92 { 93 template <typename Policy, typename Iterator> 94 void 95 operator()(Policy&& exec, Iterator iter) 96 { 97 adjacent_find(exec, iter, iter, non_const(std::equal_to<T>())); 98 } 99 }; 100 101 int32_t 102 main() 103 { 104 105 test_adjacent_find_by_type<int32_t>(); 106 test_adjacent_find_by_type<float64_t>(); 107 108 test_algo_basic_single<int32_t>(run_for_rnd_bi<test_non_const<int32_t>>()); 109 110 std::cout << done() << std::endl; 111 return 0; 112 } 113