| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-timeout-factor 2 } |
| 3 | |
| 4 | // Copyright (C) 2010-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 | // 28.3 Requirements [re.req] |
| 22 | // 28.2(4) Table 127 - Regular expression traits class requirements |
| 23 | // 28.7(9) Class template regex_traits [re.traits] |
| 24 | |
| 25 | #include <regex> |
| 26 | #include <testsuite_hooks.h> |
| 27 | |
| 28 | void |
| 29 | test01() |
| 30 | { |
| 31 | typedef wchar_t CharT; |
| 32 | typedef std::regex_traits<CharT> traits; |
| 33 | |
| 34 | wchar_t n1[] = L"lower"; |
| 35 | wchar_t n2[] = L"alpha"; |
| 36 | traits t; |
| 37 | |
| 38 | #define range(s) s, s+sizeof(s)/sizeof(s[0])-1 |
| 39 | traits::char_class_type c1 = t.lookup_classname(range(n1)); |
| 40 | VERIFY( c1 != 0 ); |
| 41 | |
| 42 | traits::char_class_type c2 = t.lookup_classname(range(n1), true); |
| 43 | traits::char_class_type c3 = t.lookup_classname(range(n2), true); |
| 44 | VERIFY( c2 == c3 ); |
| 45 | #undef range |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | test04() |
| 50 | { |
| 51 | // Transform "string" into "StRiNg" |
| 52 | auto mix_case = [](std::wstring s) { |
| 53 | int i = 0; |
| 54 | for (auto& ch : s) |
| 55 | if (++i % 2) |
| 56 | ch = toupper(ch); |
| 57 | return s; |
| 58 | }; |
| 59 | |
| 60 | typedef wchar_t CharT; |
| 61 | typedef std::regex_traits<CharT> traits; |
| 62 | traits t; |
| 63 | |
| 64 | auto lookup = [&t](const std::wstring& s, bool icase) { |
| 65 | return t.lookup_classname(s.begin(), s.end(), icase); |
| 66 | }; |
| 67 | |
| 68 | VERIFY( lookup(L"", false) == 0 ); |
| 69 | VERIFY( lookup(L":::not a valid classname:::", false) == 0 ); |
| 70 | VERIFY( lookup(L":::not a valid classname:::", true) == 0 ); |
| 71 | VERIFY( lookup(L"alnu", false) == 0 ); |
| 72 | VERIFY( lookup(L"alnumb", false) == 0 ); |
| 73 | VERIFY( lookup(L"x", false) == 0 ); |
| 74 | VERIFY( lookup(L"di", false) == 0 ); |
| 75 | VERIFY( lookup(L"digi", false) == 0 ); |
| 76 | VERIFY( lookup(L"digix", false) == 0 ); |
| 77 | VERIFY( lookup(std::wstring{L"d\0i", 3}, false) == 0 ); |
| 78 | VERIFY( lookup(std::wstring{L"digit\0", 6}, false) == 0 ); |
| 79 | VERIFY( lookup(std::wstring{L"digit\0bad", 9}, false) == 0 ); |
| 80 | |
| 81 | for (std::wstring cls : { L"alnum", L"alpha", L"blank", L"cntrl", L"digit", |
| 82 | L"graph", L"lower", L"print", L"punct", L"space", |
| 83 | L"upper", L"xdigit", L"d", L"s", L"w" }) |
| 84 | { |
| 85 | traits::char_class_type val = lookup(cls, false); |
| 86 | // val should be non-zero: |
| 87 | VERIFY( val != 0 ); |
| 88 | // val is independent of the case of the name, |
| 89 | // i.e. "alpha" and "ALPHA" and "AlPhA" give same result: |
| 90 | VERIFY( lookup(mix_case(cls), false) == val ); |
| 91 | |
| 92 | // Repeat same checks for icase=true. |
| 93 | traits::char_class_type ival = lookup(cls, true); |
| 94 | VERIFY( ival != 0 ); |
| 95 | VERIFY( lookup(mix_case(cls), true) == ival ); |
| 96 | |
| 97 | if (cls == L"lower" || cls == L"upper") // icase=true should affect value |
| 98 | { |
| 99 | VERIFY( ival != val ); |
| 100 | VERIFY( ival == lookup(L"alpha", false) ); |
| 101 | } |
| 102 | else |
| 103 | VERIFY( ival == val ); |
| 104 | |
| 105 | if (cls == L"d") |
| 106 | VERIFY( val == lookup(L"digit", false) ); |
| 107 | else if (cls == L"s") |
| 108 | VERIFY( val == lookup(L"space", false) ); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | int main() |
| 113 | { |
| 114 | test01(); |
| 115 | test04(); |
| 116 | return 0; |
| 117 | } |