]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/char_traits/requirements/constexpr_functions_c++17.cc
libstdc++: Remove redundant -std=gnu++17 option from strings tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / char_traits / requirements / constexpr_functions_c++17.cc
1 // { dg-do compile { target c++17 } }
2
3 // Copyright (C) 2017-2021 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 #include <string>
21
22 template<typename CT>
23 constexpr bool
24 test_assign()
25 {
26 using char_type = typename CT::char_type;
27 char_type s1[2] = {};
28 const char_type s2[2] = {1, 0};
29 CT::assign(s1[0], s2[0]);
30 return s1[0] == char_type{1};
31 }
32
33 template<typename CT>
34 constexpr bool
35 test_compare()
36 {
37 using char_type = typename CT::char_type;
38 const char_type s1[3] = {1, 2, 3};
39 const char_type s2[3] = {1, 2, 3};
40 if (CT::compare(s1, s2, 3) != 0)
41 return false;
42 if (CT::compare(s2, s1, 3) != 0)
43 return false;
44 if (CT::compare(s1+1, s2, 2) <= 0)
45 return false;
46 return true;
47 }
48
49 template<typename CT>
50 constexpr bool
51 test_length()
52 {
53 using char_type = typename CT::char_type;
54 const char_type s1[4] = {1, 2, 3, 0};
55 if (CT::length(s1) != 3)
56 return false;
57 if (CT::length(s1+1) != 2)
58 return false;
59 return true;
60 }
61
62 template<typename CT>
63 constexpr bool
64 test_find()
65 {
66 using char_type = typename CT::char_type;
67 const char_type s1[3] = {1, 2, 3};
68 if (CT::find(s1, 3, char_type{2}) != s1+1)
69 return false;
70 if (CT::find(s1, 3, char_type{4}) != nullptr)
71 return false;
72 return true;
73 }
74
75 #ifndef __cpp_lib_constexpr_string
76 # error Feature-test macro for constexpr char_traits is missing
77 #elif __cpp_lib_constexpr_string < (__cplusplus == 201703 ? 201611 : 201811)
78 # error Feature-test macro for constexpr char_traits has the wrong value
79 #endif
80
81 // We also provide this non-standard macro for P0426R1 (and P1032R1 in C++20).
82 #ifndef __cpp_lib_constexpr_char_traits
83 # error Feature-test macro for constexpr char_traits is missing
84 #elif __cpp_lib_constexpr_char_traits != (__cplusplus == 201703 ? 201611 : 201811)
85 # error Feature-test macro for constexpr char_traits has the wrong value
86 #endif
87
88 static_assert( test_assign<std::char_traits<char>>() );
89 static_assert( test_compare<std::char_traits<char>>() );
90 static_assert( test_length<std::char_traits<char>>() );
91 static_assert( test_find<std::char_traits<char>>() );
92 #ifdef _GLIBCXX_USE_WCHAR_T
93 static_assert( test_assign<std::char_traits<wchar_t>>() );
94 static_assert( test_compare<std::char_traits<wchar_t>>() );
95 static_assert( test_length<std::char_traits<wchar_t>>() );
96 static_assert( test_find<std::char_traits<wchar_t>>() );
97 #endif
98 #ifdef _GLIBCXX_USE_CHAR8_T
99 static_assert( test_assign<std::char_traits<char8_t>>() );
100 static_assert( test_compare<std::char_traits<char8_t>>() );
101 static_assert( test_length<std::char_traits<char8_t>>() );
102 static_assert( test_find<std::char_traits<char8_t>>() );
103 #endif
104 static_assert( test_assign<std::char_traits<char16_t>>() );
105 static_assert( test_compare<std::char_traits<char16_t>>() );
106 static_assert( test_length<std::char_traits<char16_t>>() );
107 static_assert( test_find<std::char_traits<char16_t>>() );
108 static_assert( test_assign<std::char_traits<char32_t>>() );
109 static_assert( test_compare<std::char_traits<char32_t>>() );
110 static_assert( test_length<std::char_traits<char32_t>>() );
111 static_assert( test_find<std::char_traits<char32_t>>() );
112
113 struct C { unsigned char c; };
114 constexpr bool operator==(const C& c1, const C& c2) { return c1.c == c2.c; }
115 constexpr bool operator<(const C& c1, const C& c2) { return c1.c < c2.c; }
116 static_assert( test_assign<std::char_traits<C>>() );
117 static_assert( test_compare<std::char_traits<C>>() );
118 static_assert( test_length<std::char_traits<C>>() );
119 static_assert( test_find<std::char_traits<C>>() );