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