4 #include <cstddef> // std::byte
5 #include <testsuite_hooks.h>
6 #include <testsuite_iterators.h>
8 // PR libstdc++/88545 made std::find use memchr as an optimization.
9 // This test verifies that it didn't change any semantics.
15 const C a
[] = { (C
)'a', (C
)'b', (C
)'c', (C
)'d' };
16 const C
* end
= a
+ sizeof(a
);
17 const C
* res
= std::find(a
, end
, a
[0]);
19 res
= std::find(a
, end
, a
[2]);
21 res
= std::find(a
, end
, a
[0] + 256);
23 res
= std::find(a
, end
, a
[0] - 256);
25 res
= std::find(a
, end
, 256);
28 #ifdef __cpp_lib_ranges
29 res
= std::ranges::find(a
, a
[0]);
31 res
= std::ranges::find(a
, a
[2]);
33 res
= std::ranges::find(a
, a
[0] + 256);
35 res
= std::ranges::find(a
, a
[0] - 256);
37 res
= std::ranges::find(a
, 256);
42 // Trivial type of size 1, with custom equality.
44 bool operator==(const S
&) const { return true; };
48 // Trivial type of size 1, with custom equality.
50 #if __cplusplus >= 201103L
53 { e1
= 1, e255
= 255 };
55 bool operator==(E l
, E r
) { return (l
% 3) == (r
% 3); }
58 bool operator==(X
, char) { return false; }
59 bool operator==(char, X
) { return false; }
61 bool operator==(E
, char) { return false; }
62 bool operator==(char, E
) { return false; }
67 S s
[3] = { {'a'}, {'b'}, {'c'} };
69 S
* sres
= std::find(s
, s
+3, sx
);
70 VERIFY( sres
== s
); // memchr optimization would not find a match
72 E e
[3] = { E(1), E(2), E(3) };
73 E
* eres
= std::find(e
, e
+3, E(4));
74 VERIFY( eres
== e
); // memchr optimization would not find a match
78 char* xres
= std::find(x
, x
+1, xx
);
79 VERIFY( xres
== x
+1 ); // memchr optimization would find a match
80 xres
= std::find(x
, x
+1, E('x'));
81 VERIFY( xres
== x
+1 ); // memchr optimization would find a match
84 std::byte b
[] = { std::byte
{0}, std::byte
{1}, std::byte
{2}, std::byte
{3} };
85 std::byte
* bres
= std::find(b
, b
+4, std::byte
{4});
86 VERIFY( bres
== b
+4 );
87 bres
= std::find(b
, b
+2, std::byte
{3});
88 VERIFY( bres
== b
+2 );
89 bres
= std::find(b
, b
+3, std::byte
{3});
90 VERIFY( bres
== b
+3 );
93 #ifdef __cpp_lib_ranges
94 sres
= std::ranges::find(s
, sx
);
97 eres
= std::ranges::find(e
, e
+3, E(4));
100 // std::equality_comparable_with<X, char> is not satisfied, so can't do
101 // std::ranges::find(x, xx)
103 bres
= std::ranges::find(b
, std::byte
{4});
104 VERIFY( bres
== b
+4 );
105 bres
= std::ranges::find(b
, b
+2, std::byte
{3});
106 VERIFY( bres
== b
+2 );
107 bres
= std::ranges::find(b
, std::byte
{3});
108 VERIFY( bres
== b
+3 );
110 xres
= std::find(x
, x
+1, xx
);
111 VERIFY( xres
== std::ranges::end(x
) );
112 xres
= std::find(x
, x
+1, E('x'));
113 VERIFY( xres
== std::ranges::end(x
) );
119 test_pr115799c0(__gnu_test::test_contiguous_range
<char> r
)
121 // Non-common range with integer-class type as difference_type.
122 (void) std::ranges::find(r
, 'a');
127 test_pr115799c2(__gnu_test::input_iterator_wrapper
<char> i
)
129 // Non-contiguous range of character type.
130 (void) std::find(i
, i
, 'a');
136 test_char
<signed char>();
137 test_char
<unsigned char>();
138 test_non_characters();
140 #if __cpp_lib_constexpr_algorithms
143 return std::find(c
, c
+4, 'b') == c
+1;
145 #ifdef __cpp_lib_ranges
148 return std::ranges::find(c
, 'b') == c
+1;