d: Merge dmd, druntime c7902293d7, phobos 03aeafd20
[gcc.git] / libstdc++-v3 / testsuite / 25_algorithms / find / bytes.cc
blob03dada0fec753be3d2650eaebf5c2b085012f700
1 // { dg-do run }
3 #include <algorithm>
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.
11 template<typename C>
12 void
13 test_char()
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]);
18 VERIFY( res == a );
19 res = std::find(a, end, a[2]);
20 VERIFY( res == a+2 );
21 res = std::find(a, end, a[0] + 256);
22 VERIFY( res == end );
23 res = std::find(a, end, a[0] - 256);
24 VERIFY( res == end );
25 res = std::find(a, end, 256);
26 VERIFY( res == end );
28 #ifdef __cpp_lib_ranges
29 res = std::ranges::find(a, a[0]);
30 VERIFY( res == a );
31 res = std::ranges::find(a, a[2]);
32 VERIFY( res == a+2 );
33 res = std::ranges::find(a, a[0] + 256);
34 VERIFY( res == end );
35 res = std::ranges::find(a, a[0] - 256);
36 VERIFY( res == end );
37 res = std::ranges::find(a, 256);
38 VERIFY( res == end );
39 #endif
42 // Trivial type of size 1, with custom equality.
43 struct S {
44 bool operator==(const S&) const { return true; };
45 char c;
48 // Trivial type of size 1, with custom equality.
49 enum E
50 #if __cplusplus >= 201103L
51 : unsigned char
52 #endif
53 { e1 = 1, e255 = 255 };
55 bool operator==(E l, E r) { return (l % 3) == (r % 3); }
57 struct X { char c; };
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; }
64 void
65 test_non_characters()
67 S s[3] = { {'a'}, {'b'}, {'c'} };
68 S sx = {'x'};
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
76 char x[1] = { 'x' };
77 X xx = { 'x' };
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
83 #ifdef __cpp_lib_byte
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 );
91 #endif
93 #ifdef __cpp_lib_ranges
94 sres = std::ranges::find(s, sx);
95 VERIFY( sres == s );
97 eres = std::ranges::find(e, e+3, E(4));
98 VERIFY( eres == e );
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) );
114 #endif
117 #if __cpp_lib_ranges
118 void
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');
124 #endif
126 void
127 test_pr115799c2(__gnu_test::input_iterator_wrapper<char> i)
129 // Non-contiguous range of character type.
130 (void) std::find(i, i, 'a');
133 int main()
135 test_char<char>();
136 test_char<signed char>();
137 test_char<unsigned char>();
138 test_non_characters();
140 #if __cpp_lib_constexpr_algorithms
141 static_assert( [] {
142 char c[] = "abcd";
143 return std::find(c, c+4, 'b') == c+1;
144 }() );
145 #ifdef __cpp_lib_ranges
146 static_assert( [] {
147 char c[] = "abcd";
148 return std::ranges::find(c, 'b') == c+1;
149 }() );
150 #endif
151 #endif