| 1 | // { dg-options "-lstdc++exp" } |
| 2 | // { dg-do run { target c++26 } } |
| 3 | // { dg-require-namedlocale "en_US.ISO8859-1" } |
| 4 | // { dg-require-namedlocale "fr_FR.ISO8859-15" } |
| 5 | |
| 6 | #include <text_encoding> |
| 7 | #include <string_view> |
| 8 | #include <locale.h> |
| 9 | #include <testsuite_hooks.h> |
| 10 | |
| 11 | using namespace std::string_view_literals; |
| 12 | |
| 13 | void |
| 14 | test_literal() |
| 15 | { |
| 16 | const std::text_encoding lit = std::text_encoding::literal(); |
| 17 | VERIFY( lit.name() == std::string_view(__GNUC_EXECUTION_CHARSET_NAME) ); |
| 18 | } |
| 19 | |
| 20 | void |
| 21 | test_env() |
| 22 | { |
| 23 | const std::text_encoding env = std::text_encoding::environment(); |
| 24 | |
| 25 | if (env.mib() == std::text_encoding::UTF8) |
| 26 | VERIFY( std::text_encoding::environment_is<std::text_encoding::UTF8>() ); |
| 27 | |
| 28 | ::setlocale(LC_ALL, ISO_8859(1, en_US)); |
| 29 | const std::text_encoding env1 = std::text_encoding::environment(); |
| 30 | VERIFY( env1 == env ); |
| 31 | |
| 32 | ::setlocale(LC_ALL, ISO_8859(15, fr_FR)); |
| 33 | const std::text_encoding env2 = std::text_encoding::environment(); |
| 34 | VERIFY( env2 == env ); |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | test_every_id() |
| 39 | { |
| 40 | for (int i = 0; i <= 2260; ++i) |
| 41 | { |
| 42 | std::text_encoding::id mib{i}; |
| 43 | switch (i) |
| 44 | { |
| 45 | case 0: |
| 46 | case 33 ... 34: |
| 47 | case 107 ... 108: |
| 48 | case 120 ... 999: |
| 49 | case 1022 ... 1999: |
| 50 | case 2110 ... 2249: |
| 51 | // These do not correspond to an enumerator of std::text_encoding::id |
| 52 | // so are not valid arguments to the constructor. Without assertions |
| 53 | // enabled, we map bad IDs to id::unknown as a libstdc++ extension. |
| 54 | #ifndef _GLIBCXX_ASSERTIONS |
| 55 | { |
| 56 | std::text_encoding bad{mib}; |
| 57 | VERIFY( bad.mib() == std::text_encoding::unknown ); |
| 58 | VERIFY( bad.name() == ""sv ); |
| 59 | } |
| 60 | #endif |
| 61 | continue; |
| 62 | } |
| 63 | std::text_encoding enc{mib}; |
| 64 | auto aliases = enc.aliases(); |
| 65 | if (i == 1 || i == 2) |
| 66 | VERIFY( enc.name() == ""sv ); |
| 67 | else |
| 68 | VERIFY( enc.name() == std::string_view(aliases.front()) ); |
| 69 | auto begin = aliases.begin(); |
| 70 | auto end = aliases.end(); |
| 71 | VERIFY( (begin + std::ranges::distance(aliases)) == end ); |
| 72 | #ifndef _GLIBCXX_ASSERTIONS |
| 73 | // These ops violate preconditions, but as libstdc++ extensions they are |
| 74 | // guaranteed to either assert or have well-defined behaviour. |
| 75 | |
| 76 | // This erroneously returns ""sv: |
| 77 | VERIFY( begin[std::ranges::distance(aliases)] == ""sv ); |
| 78 | // Likewise: |
| 79 | VERIFY( begin[999999] == ""sv ); |
| 80 | |
| 81 | auto iter = begin; |
| 82 | std::ranges::advance(iter, end); |
| 83 | // Erroneously sets iter to a value-initialized state. |
| 84 | ++iter; |
| 85 | VERIFY( iter == decltype(iter){} ); |
| 86 | VERIFY( *iter == ""sv ); |
| 87 | |
| 88 | iter = begin; |
| 89 | // Erroneously sets iter to a value-initialized state. |
| 90 | --iter; |
| 91 | VERIFY( iter == decltype(iter){} ); |
| 92 | #endif |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | int main() |
| 97 | { |
| 98 | test_literal(); |
| 99 | test_env(); |
| 100 | test_every_id(); |
| 101 | } |