2 //===-- adjacent_find.pass.cpp --------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 // UNSUPPORTED: c++03, c++11, c++14
12 #include "support/pstl_test_config.h"
17 #include "support/utils.h"
19 using namespace TestUtils
;
21 struct test_adjacent_find
23 template <typename Policy
, typename Iterator
, typename Pred
>
25 operator()(Policy
&& exec
, Iterator first
, Iterator last
, Pred pred
)
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");
33 i
= adjacent_find(exec
, first
, last
);
34 EXPECT_TRUE(i
== k
, "wrong return value from adjacent_find without predicate");
40 test_adjacent_find_by_type()
43 size_t counts
[] = {2, 3, 500};
44 for (size_t c
= 0; c
< const_size(counts
); ++c
)
47 for (size_t e
= 0; e
< (counts
[c
] >= 64 ? 64 : (counts
[c
] == 2 ? 1 : 2)); ++e
)
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
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");
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
>());
60 //special cases: size=0, size=1;
61 for (size_t expect
= 0; expect
< 1; ++expect
)
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");
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
>());
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
>());
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
>());
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
>());
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
>());
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
>());
89 invoke_on_all_policies(test_adjacent_find(), a3
.cbegin(), a3
.cend(), std::equal_to
<T
>());
95 template <typename Policy
, typename Iterator
>
97 operator()(Policy
&& exec
, Iterator iter
)
99 adjacent_find(exec
, iter
, iter
, non_const(std::equal_to
<T
>()));
107 test_adjacent_find_by_type
<int32_t>();
108 test_adjacent_find_by_type
<float64_t
>();
110 test_algo_basic_single
<int32_t>(run_for_rnd_bi
<test_non_const
<int32_t>>());
112 std::cout
<< done() << std::endl
;