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