| 1 |
// { dg-do compile { target c++17 } } |
| 2 |
// { dg-add-options no_pch } |
| 3 |
|
| 4 |
// Copyright (C) 2017-2025 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_string |
| 77 |
# error Feature-test macro for constexpr char_traits is missing |
| 78 |
#elif __cpp_lib_constexpr_string < (__cplusplus == 201703 ? 201611 : 201811) |
| 79 |
# error Feature-test macro for constexpr char_traits has the wrong value |
| 80 |
#endif |
| 81 |
|
| 82 |
// We also provide this non-standard macro for P0426R1 (and P1032R1 in C++20). |
| 83 |
#ifndef __cpp_lib_constexpr_char_traits |
| 84 |
# error Feature-test macro for constexpr char_traits is missing |
| 85 |
#elif __cpp_lib_constexpr_char_traits != (__cplusplus == 201703 ? 201611 : 201811) |
| 86 |
# error Feature-test macro for constexpr char_traits has the wrong value |
| 87 |
#endif |
| 88 |
|
| 89 |
static_assert( test_assign<std::char_traits<char>>() ); |
| 90 |
static_assert( test_compare<std::char_traits<char>>() ); |
| 91 |
static_assert( test_length<std::char_traits<char>>() ); |
| 92 |
static_assert( test_find<std::char_traits<char>>() ); |
| 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 |
#ifdef _GLIBCXX_USE_CHAR8_T |
| 98 |
static_assert( test_assign<std::char_traits<char8_t>>() ); |
| 99 |
static_assert( test_compare<std::char_traits<char8_t>>() ); |
| 100 |
static_assert( test_length<std::char_traits<char8_t>>() ); |
| 101 |
static_assert( test_find<std::char_traits<char8_t>>() ); |
| 102 |
#endif |
| 103 |
static_assert( test_assign<std::char_traits<char16_t>>() ); |
| 104 |
static_assert( test_compare<std::char_traits<char16_t>>() ); |
| 105 |
static_assert( test_length<std::char_traits<char16_t>>() ); |
| 106 |
static_assert( test_find<std::char_traits<char16_t>>() ); |
| 107 |
static_assert( test_assign<std::char_traits<char32_t>>() ); |
| 108 |
static_assert( test_compare<std::char_traits<char32_t>>() ); |
| 109 |
static_assert( test_length<std::char_traits<char32_t>>() ); |
| 110 |
static_assert( test_find<std::char_traits<char32_t>>() ); |
| 111 |
|
| 112 |
struct C { unsigned char c; }; |
| 113 |
constexpr bool operator==(const C& c1, const C& c2) { return c1.c == c2.c; } |
| 114 |
constexpr bool operator<(const C& c1, const C& c2) { return c1.c < c2.c; } |
| 115 |
static_assert( test_assign<std::char_traits<C>>() ); |
| 116 |
static_assert( test_compare<std::char_traits<C>>() ); |
| 117 |
static_assert( test_length<std::char_traits<C>>() ); |
| 118 |
static_assert( test_find<std::char_traits<C>>() ); |